Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

CVE-2022-22931 JAMES-3646 Rely on strong typing for file paths operat… #877

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}