Skip to content

Commit

Permalink
Improve validation of storage location when using FileStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed May 5, 2020
1 parent 563f85a commit bb33048
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
19 changes: 17 additions & 2 deletions java/org/apache/catalina/session/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.catalina.Globals;
import org.apache.catalina.Session;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;

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

private static final Log log = LogFactory.getLog(FileStore.class);
private static final StringManager sm = StringManager.getManager(FileStore.class);


// ----------------------------------------------------- Constants

/**
Expand Down Expand Up @@ -336,11 +342,20 @@ private File directory() throws IOException {
* used in the file naming.
*/
private File file(String id) throws IOException {
if (this.directory == null) {
File storageDir = directory();
if (storageDir == null) {
return null;
}

String filename = id + FILE_EXT;
File file = new File(directory(), filename);
File file = new File(storageDir, filename);

// Check the file is within the storage directory
if (!file.getCanonicalPath().startsWith(storageDir.getCanonicalPath())) {
log.warn(sm.getString("fileStore.invalid", file.getPath(), id));
return null;
}

return file;
}
}
1 change: 1 addition & 0 deletions java/org/apache/catalina/session/LocalStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ JDBCStore.wrongDataSource=Cannot open JNDI DataSource [{0}]
fileStore.createFailed=Unable to create directory [{0}] for the storage of session data
fileStore.deleteFailed=Unable to delete file [{0}] which is preventing the creation of the session storage location
fileStore.deleteSessionFailed=Unable to delete file [{0}] which is no longer required
fileStore.invalid=Invalid persistence file [{0}] for session ID [{1}]
fileStore.loading=Loading Session [{0}] from file [{1}]
fileStore.removing=Removing Session [{0}] at file [{1}]
fileStore.saving=Saving Session [{0}] to file [{1}]
Expand Down
3 changes: 3 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
replacement to <code>:-</code> due to possible conflicts. The
syntax is now <code>${name:-default}</code>. (remm)
</fix>
<add>
Improve validation of storage location when using FileStore. (markt)
</add>
</changelog>
</subsection>
<subsection name="Coyote">
Expand Down

0 comments on commit bb33048

Please sign in to comment.