From 9714f3e3304dba5edd74d738d497ccd9bb6982fd Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Sat, 30 Jul 2022 01:39:45 +0000 Subject: [PATCH] vuln-fix: Zip Slip Vulnerability This fixes a Zip-Slip vulnerability. This change does one of two things. This change either 1. Inserts a guard to protect against Zip Slip. OR 2. 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())`. For number 2, 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: High CVSSS: 7.4 Detection: CodeQL (https://codeql.github.com/codeql-query-help/java/java-zipslip/) & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.ZipSlip) Reported-by: Jonathan Leitschuh Signed-off-by: Jonathan Leitschuh Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/16 Co-authored-by: Moderne --- external/pluto | 1 - .../java/com/msopentech/thali/toronionproxy/FileUtilities.java | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) delete mode 160000 external/pluto diff --git a/external/pluto b/external/pluto deleted file mode 160000 index 64faf224..00000000 --- a/external/pluto +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 64faf224a90ec3ef8a806f9ec45c1caffafea768 diff --git a/universal/src/main/java/com/msopentech/thali/toronionproxy/FileUtilities.java b/universal/src/main/java/com/msopentech/thali/toronionproxy/FileUtilities.java index b01f6e18..866173f5 100644 --- a/universal/src/main/java/com/msopentech/thali/toronionproxy/FileUtilities.java +++ b/universal/src/main/java/com/msopentech/thali/toronionproxy/FileUtilities.java @@ -200,6 +200,9 @@ public static void extractContentFromZip(File destinationDirectory, InputStream ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { File file = new File(destinationDirectory, zipEntry.getName()); + if (!file.toPath().normalize().startsWith(destinationDirectory.toPath().normalize())) { + throw new RuntimeException("Bad zip entry"); + } if (zipEntry.isDirectory()) { if (file.exists() == false && !file.mkdirs()) { throw new RuntimeException("Could not create directory " + file);