Skip to content

Commit 6d66e99

Browse files
committed
Use java.nio.file.Path for consistent sub-directory checking
1 parent 3c3fabe commit 6d66e99

File tree

6 files changed

+16
-19
lines changed

6 files changed

+16
-19
lines changed

java/org/apache/catalina/servlets/DefaultServlet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,7 @@ private File validateGlobalXsltFile(File base) {
20702070

20712071
// First check that the resulting path is under the provided base
20722072
try {
2073-
if (!candidate.getCanonicalPath().startsWith(base.getCanonicalPath())) {
2073+
if (!candidate.getCanonicalFile().toPath().startsWith(base.getCanonicalFile().toPath())) {
20742074
return null;
20752075
}
20762076
} catch (IOException ioe) {

java/org/apache/catalina/session/FileStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private File file(String id) throws IOException {
351351
File file = new File(storageDir, filename);
352352

353353
// Check the file is within the storage directory
354-
if (!file.getCanonicalPath().startsWith(storageDir.getCanonicalPath())) {
354+
if (!file.getCanonicalFile().toPath().startsWith(storageDir.getCanonicalFile().toPath())) {
355355
log.warn(sm.getString("fileStore.invalid", file.getPath(), id));
356356
return null;
357357
}

java/org/apache/catalina/startup/ContextConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ protected void fixDocBase() throws IOException {
858858
String docBaseCanonical = docBaseAbsoluteFile.getCanonicalPath();
859859

860860
// Re-calculate now docBase is a canonical path
861-
boolean docBaseCanonicalInAppBase = docBaseCanonical.startsWith(appBase.getPath() + File.separatorChar);
861+
boolean docBaseCanonicalInAppBase =
862+
docBaseAbsoluteFile.getCanonicalFile().toPath().startsWith(appBase.toPath());
862863
String docBase;
863864
if (docBaseCanonicalInAppBase) {
864865
docBase = docBaseCanonical.substring(appBase.getPath().length());

java/org/apache/catalina/startup/ExpandWar.java

+7-14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.net.URL;
2727
import java.net.URLConnection;
2828
import java.nio.channels.FileChannel;
29+
import java.nio.file.Path;
2930
import java.util.Enumeration;
3031
import java.util.jar.JarEntry;
3132
import java.util.jar.JarFile;
@@ -116,10 +117,7 @@ public static String expand(Host host, URL war, String pathname)
116117
}
117118

118119
// 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();
123121

124122
// Creating war tracker parent (normally META-INF)
125123
File warTrackerParent = warTracker.getParentFile();
@@ -134,14 +132,13 @@ public static String expand(Host host, URL war, String pathname)
134132
JarEntry jarEntry = jarEntries.nextElement();
135133
String name = jarEntry.getName();
136134
File expandedFile = new File(docBase, name);
137-
if (!expandedFile.getCanonicalPath().startsWith(
138-
canonicalDocBasePrefix)) {
135+
if (!expandedFile.getCanonicalFile().toPath().startsWith(canonicalDocBasePath)) {
139136
// Trying to expand outside the docBase
140137
// Throw an exception to stop the deployment
141138
throw new IllegalArgumentException(
142139
sm.getString("expandWar.illegalPath",war, name,
143140
expandedFile.getCanonicalPath(),
144-
canonicalDocBasePrefix));
141+
canonicalDocBasePath));
145142
}
146143
int last = name.lastIndexOf('/');
147144
if (last >= 0) {
@@ -217,10 +214,7 @@ public static void validate(Host host, URL war, String pathname) throws IOExcept
217214
File docBase = new File(host.getAppBaseFile(), pathname);
218215

219216
// 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();
224218
JarURLConnection juc = (JarURLConnection) war.openConnection();
225219
juc.setUseCaches(false);
226220
try (JarFile jarFile = juc.getJarFile()) {
@@ -229,14 +223,13 @@ public static void validate(Host host, URL war, String pathname) throws IOExcept
229223
JarEntry jarEntry = jarEntries.nextElement();
230224
String name = jarEntry.getName();
231225
File expandedFile = new File(docBase, name);
232-
if (!expandedFile.getCanonicalPath().startsWith(
233-
canonicalDocBasePrefix)) {
226+
if (!expandedFile.getCanonicalFile().toPath().startsWith(canonicalDocBasePath)) {
234227
// Entry located outside the docBase
235228
// Throw an exception to stop the deployment
236229
throw new IllegalArgumentException(
237230
sm.getString("expandWar.illegalPath",war, name,
238231
expandedFile.getCanonicalPath(),
239-
canonicalDocBasePrefix));
232+
canonicalDocBasePath));
240233
}
241234
}
242235
} catch (IOException e) {

java/org/apache/catalina/startup/HostConfig.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,7 @@ protected void deployDescriptor(ContextName cn, File contextXml) {
598598
docBase = new File(host.getAppBaseFile(), context.getDocBase());
599599
}
600600
// If external docBase, register .xml as redeploy first
601-
if (!docBase.getCanonicalPath().startsWith(
602-
host.getAppBaseFile().getAbsolutePath() + File.separator)) {
601+
if (!docBase.getCanonicalFile().toPath().startsWith(host.getAppBaseFile().toPath())) {
603602
isExternal = true;
604603
deployedApp.redeployResources.put(
605604
contextXml.getAbsolutePath(),

webapps/docs/changelog.xml

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@
225225
<update>
226226
Migrate to new code signing service. (markt)
227227
</update>
228+
<scode>
229+
Use <code>java.nio.file.Path</code> to test for one directory being a
230+
sub-directory of another in a consistent way. (markt)
231+
</scode>
228232
</changelog>
229233
</subsection>
230234
</section>

0 commit comments

Comments
 (0)