CVE-2022-36007: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: Venice is a Clojure inspired sandboxed Lisp dialect with excellent Java interoperability. A partial path traversal issue exists within the functions `load-file` and `load-resource`. These functions can be limited to load files from a list of load paths. Assuming Venice has been configured with the load paths: `[ "/Users/foo/resources" ]` When passing **relative** paths to these two vulnerable functions everything is fine: `(load-resource "test.png")` => loads the file "/Users/foo/resources/test.png" `(load-resource "../resources-alt/test.png")` => rejected, outside the load path When passing **absolute** paths to these two vulnerable functions Venice may return files outside the configured load paths: `(load-resource "/Users/foo/resources/test.png")` => loads the file "/Users/foo/resources/test.png" `(load-resource "/Users/foo/resources-alt/test.png")` => loads the file "/Users/foo/resources-alt/test.png" !!! The latter call suffers from the _Partial Path Traversal_ vulnerability. This issue’s scope is limited to absolute paths whose name prefix matches a load path. E.g. for a load-path `"/Users/foo/resources"`, the actor can cause loading a resource also from `"/Users/foo/resources-alt"`, but not from `"/Users/foo/images"`. Versions of Venice before and including v1.10.17 are affected by this issue. Upgrade to Venice >= 1.10.18, if you are on a version < 1.10.18. There are currently no known workarounds.
CWE description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
CVSS v3 base score: 3.3
Published: 2022-08-15T11:21Z
Repository: https://github.com/jlangch/venice
Language: Java
Files changed: src/main/java/com/github/jlangch/venice/impl/util/io/LoadPaths.java
Commit date: 2022-07-29 13:38:50 +0000
Commit message: 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 <Jonathan.Leitschuh@gmail.com>
Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>

Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/13

Co-authored-by: Moderne <team@moderne.io>
Diff: diff --git a/src/main/java/com/github/jlangch/venice/impl/util/io/LoadPaths.java b/src/main/java/com/github/jlangch/venice/impl/util/io/LoadPaths.java
index f6887163c..57c8a27f4 100644
--- a/src/main/java/com/github/jlangch/venice/impl/util/io/LoadPaths.java
+++ b/src/main/java/com/github/jlangch/venice/impl/util/io/LoadPaths.java
@@ -256,11 +256,11 @@ public class LoadPaths implements ILoadPaths {
     ) throws IOException {
         final File dir_ = dir.getAbsoluteFile();
         if (dir_.isDirectory()) {
             final File fl = new File(dir_, file.getPath());
             if (fl.isFile()) {
-                if (fl.getCanonicalPath().startsWith(dir_.getCanonicalPath())) {
+                if (fl.getCanonicalFile().toPath().startsWith(dir_.getCanonicalFile().toPath())) {
                     // Prevent accessing files outside the load-path.
                     // E.g.: ../../coffee
                     return true;
                 }
             }

Vulnerable code:                 if (fl.getCanonicalPath().startsWith(dir_.getCanonicalPath())) {
Fixed code:                 if (fl.getCanonicalFile().toPath().startsWith(dir_.getCanonicalFile().toPath())) {