Skip to content

Commit

Permalink
CVE-2022-22931 JAMES-3646 Rely on strong typing for file paths operat…
Browse files Browse the repository at this point in the history
…ions (#877)
  • Loading branch information
chibenwa authored Feb 9, 2022
1 parent c4a775e commit 7d22c6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,8 @@ protected File getUserDirectory(Username username) throws StorageException {
}

private void enforceRoot(File file) throws StorageException {
try {
if (!file.getCanonicalPath().startsWith(root.getCanonicalPath())) {
throw new StorageException(new IllegalStateException("Path traversal attempted"));
}
} catch (IOException e) {
throw new StorageException(e);
if (!file.toPath().normalize().startsWith(root.toPath().normalize())) {
throw new StorageException(new IllegalStateException("Path traversal attempted"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ void getScriptShouldNotAllowToReadScriptsOfOtherUsers() throws Exception {
new ScriptName("../other/script")))
.isInstanceOf(StorageException.class);
}

@Test
void getScriptShouldNotAllowToReadScriptsOfOtherUsersWhenPrefix() throws Exception {
sieveRepository().putScript(Username.of("testa"), new ScriptName("script"), new ScriptContent("PWND!!!"));

assertThatThrownBy(() -> sieveRepository().getScript(Username.of("test"),
new ScriptName("../other/script")))
.isInstanceOf(StorageException.class);
}
}

0 comments on commit 7d22c6f

Please # to comment.