Skip to content

Commit 5d19880

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 e19175c commit 5d19880

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/main/java/org/yzr/utils/file/ZipUtil.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.File;
66
import java.io.FileOutputStream;
7+
import java.io.IOException;
78
import java.io.InputStream;
89
import java.util.Enumeration;
910
import java.util.zip.ZipEntry;
@@ -24,7 +25,10 @@ public static String unzip(String path) {
2425
File dir = new File(dirPath);
2526
dir.mkdirs();
2627
} else {
27-
File targetFile = new File(destDirPath + File.separator + entry.getName());
28+
File targetFile = new File(destDirPath, entry.getName());
29+
if (!targetFile.toPath().normalize().startsWith(destDirPath)) {
30+
throw new IOException("Bad zip entry");
31+
}
2832
if (!targetFile.getParentFile().exists()) {
2933
targetFile.getParentFile().mkdirs();
3034
}

0 commit comments

Comments
 (0)