Skip to content

Commit 579447c

Browse files
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 <Jonathan.Leitschuh@gmail.com> Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com> Bug-tracker: JLLeitschuh/security-research#16 Co-authored-by: Moderne <team@moderne.io>
1 parent f396165 commit 579447c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

compress-example/src/main/java/io/github/biezhi/compress/jar/JAR.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public static void decompress(String in, File destination) throws IOException {
3636
continue;
3737
}
3838
File curfile = new File(destination, entry.getName());
39+
if (!curfile.toPath().normalize().startsWith(destination.toPath().normalize())) {
40+
throw new IOException("Bad zip entry");
41+
}
3942
File parent = curfile.getParentFile();
4043
if (!parent.exists()) {
4144
if (!parent.mkdirs()) {
@@ -66,4 +69,4 @@ private static void addToArchiveCompression(JarArchiveOutputStream out, File fil
6669
System.out.println(file.getName() + " is not supported");
6770
}
6871
}
69-
}
72+
}

compress-example/src/main/java/io/github/biezhi/compress/zip/ZIP.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static void decompress(String in, File destination) throws IOException {
3434
continue;
3535
}
3636
File curfile = new File(destination, entry.getName());
37+
if (!curfile.toPath().normalize().startsWith(destination.toPath().normalize())) {
38+
throw new IOException("Bad zip entry");
39+
}
3740
File parent = curfile.getParentFile();
3841
if (!parent.exists()) {
3942
if (!parent.mkdirs()) {

0 commit comments

Comments
 (0)