26
26
import java .net .URL ;
27
27
import java .net .URLConnection ;
28
28
import java .nio .channels .FileChannel ;
29
+ import java .nio .file .Path ;
29
30
import java .util .Enumeration ;
30
31
import java .util .jar .JarEntry ;
31
32
import java .util .jar .JarFile ;
@@ -116,10 +117,7 @@ public static String expand(Host host, URL war, String pathname)
116
117
}
117
118
118
119
// Expand the WAR into the new document base directory
119
- String canonicalDocBasePrefix = docBase .getCanonicalPath ();
120
- if (!canonicalDocBasePrefix .endsWith (File .separator )) {
121
- canonicalDocBasePrefix += File .separator ;
122
- }
120
+ Path canonicalDocBasePath = docBase .getCanonicalFile ().toPath ();
123
121
124
122
// Creating war tracker parent (normally META-INF)
125
123
File warTrackerParent = warTracker .getParentFile ();
@@ -134,14 +132,13 @@ public static String expand(Host host, URL war, String pathname)
134
132
JarEntry jarEntry = jarEntries .nextElement ();
135
133
String name = jarEntry .getName ();
136
134
File expandedFile = new File (docBase , name );
137
- if (!expandedFile .getCanonicalPath ().startsWith (
138
- canonicalDocBasePrefix )) {
135
+ if (!expandedFile .getCanonicalFile ().toPath ().startsWith (canonicalDocBasePath )) {
139
136
// Trying to expand outside the docBase
140
137
// Throw an exception to stop the deployment
141
138
throw new IllegalArgumentException (
142
139
sm .getString ("expandWar.illegalPath" ,war , name ,
143
140
expandedFile .getCanonicalPath (),
144
- canonicalDocBasePrefix ));
141
+ canonicalDocBasePath ));
145
142
}
146
143
int last = name .lastIndexOf ('/' );
147
144
if (last >= 0 ) {
@@ -217,10 +214,7 @@ public static void validate(Host host, URL war, String pathname) throws IOExcept
217
214
File docBase = new File (host .getAppBaseFile (), pathname );
218
215
219
216
// Calculate the document base directory
220
- String canonicalDocBasePrefix = docBase .getCanonicalPath ();
221
- if (!canonicalDocBasePrefix .endsWith (File .separator )) {
222
- canonicalDocBasePrefix += File .separator ;
223
- }
217
+ Path canonicalDocBasePath = docBase .getCanonicalFile ().toPath ();
224
218
JarURLConnection juc = (JarURLConnection ) war .openConnection ();
225
219
juc .setUseCaches (false );
226
220
try (JarFile jarFile = juc .getJarFile ()) {
@@ -229,14 +223,13 @@ public static void validate(Host host, URL war, String pathname) throws IOExcept
229
223
JarEntry jarEntry = jarEntries .nextElement ();
230
224
String name = jarEntry .getName ();
231
225
File expandedFile = new File (docBase , name );
232
- if (!expandedFile .getCanonicalPath ().startsWith (
233
- canonicalDocBasePrefix )) {
226
+ if (!expandedFile .getCanonicalFile ().toPath ().startsWith (canonicalDocBasePath )) {
234
227
// Entry located outside the docBase
235
228
// Throw an exception to stop the deployment
236
229
throw new IllegalArgumentException (
237
230
sm .getString ("expandWar.illegalPath" ,war , name ,
238
231
expandedFile .getCanonicalPath (),
239
- canonicalDocBasePrefix ));
232
+ canonicalDocBasePath ));
240
233
}
241
234
}
242
235
} catch (IOException e ) {
0 commit comments