Skip to content

Commit bb33048

Browse files
committed
Improve validation of storage location when using FileStore.
1 parent 563f85a commit bb33048

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.apache.catalina.Globals;
3434
import org.apache.catalina.Session;
3535
import org.apache.juli.logging.Log;
36+
import org.apache.juli.logging.LogFactory;
37+
import org.apache.tomcat.util.res.StringManager;
3638

3739
/**
3840
* Concrete implementation of the <b>Store</b> interface that utilizes
@@ -43,6 +45,10 @@
4345
*/
4446
public final class FileStore extends StoreBase {
4547

48+
private static final Log log = LogFactory.getLog(FileStore.class);
49+
private static final StringManager sm = StringManager.getManager(FileStore.class);
50+
51+
4652
// ----------------------------------------------------- Constants
4753

4854
/**
@@ -336,11 +342,20 @@ private File directory() throws IOException {
336342
* used in the file naming.
337343
*/
338344
private File file(String id) throws IOException {
339-
if (this.directory == null) {
345+
File storageDir = directory();
346+
if (storageDir == null) {
340347
return null;
341348
}
349+
342350
String filename = id + FILE_EXT;
343-
File file = new File(directory(), filename);
351+
File file = new File(storageDir, filename);
352+
353+
// Check the file is within the storage directory
354+
if (!file.getCanonicalPath().startsWith(storageDir.getCanonicalPath())) {
355+
log.warn(sm.getString("fileStore.invalid", file.getPath(), id));
356+
return null;
357+
}
358+
344359
return file;
345360
}
346361
}

java/org/apache/catalina/session/LocalStrings.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ JDBCStore.wrongDataSource=Cannot open JNDI DataSource [{0}]
2929
fileStore.createFailed=Unable to create directory [{0}] for the storage of session data
3030
fileStore.deleteFailed=Unable to delete file [{0}] which is preventing the creation of the session storage location
3131
fileStore.deleteSessionFailed=Unable to delete file [{0}] which is no longer required
32+
fileStore.invalid=Invalid persistence file [{0}] for session ID [{1}]
3233
fileStore.loading=Loading Session [{0}] from file [{1}]
3334
fileStore.removing=Removing Session [{0}] at file [{1}]
3435
fileStore.saving=Saving Session [{0}] to file [{1}]

webapps/docs/changelog.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
replacement to <code>:-</code> due to possible conflicts. The
115115
syntax is now <code>${name:-default}</code>. (remm)
116116
</fix>
117+
<add>
118+
Improve validation of storage location when using FileStore. (markt)
119+
</add>
117120
</changelog>
118121
</subsection>
119122
<subsection name="Coyote">

0 commit comments

Comments
 (0)