Skip to content

Commit c942c73

Browse files
vuln-fix: Partial Path Traversal Vulnerability
This fixes a partial path traversal vulnerability. Replaces `dir.getCanonicalPath().startsWith(parent.getCanonicalPath())`, which is vulnerable to partial path traversal attacks, with the more secure `dir.getCanonicalFile().toPath().startsWith(parent.getCanonicalFile().toPath())`. To demonstrate this vulnerability, consider `"/usr/outnot".startsWith("/usr/out")`. The check is bypassed although `/outnot` is not under the `/out` directory. It's important to understand that the terminating slash may be removed when using various `String` representations of the `File` object. For example, on Linux, `println(new File("/var"))` will print `/var`, but `println(new File("/var", "/")` will print `/var/`; however, `println(new File("/var", "/").getCanonicalPath())` will print `/var`. Weakness: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') Severity: Medium CVSSS: 6.1 Detection: CodeQL & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.PartialPathTraversalVulnerability) Reported-by: Jonathan Leitschuh <[email protected]> Signed-off-by: Jonathan Leitschuh <[email protected]> Bug-tracker: JLLeitschuh/security-research#13 Co-authored-by: Moderne <[email protected]>
1 parent c109778 commit c942c73

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/main/java/com/github/jlangch/venice/impl/util/io/LoadPaths.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private boolean isFileWithinDirectory(
258258
if (dir_.isDirectory()) {
259259
final File fl = new File(dir_, file.getPath());
260260
if (fl.isFile()) {
261-
if (fl.getCanonicalPath().startsWith(dir_.getCanonicalPath())) {
261+
if (fl.getCanonicalFile().toPath().startsWith(dir_.getCanonicalFile().toPath())) {
262262
// Prevent accessing files outside the load-path.
263263
// E.g.: ../../coffee
264264
return true;

0 commit comments

Comments
 (0)