From 1b42c2e6f81e30d47e2668550dd45e9327668c3c Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 2 Nov 2022 18:47:59 +0900 Subject: [PATCH] vuln-fix: Zip Slip Vulnerability (#13) 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 Co-authored-by: Moderne --- .../src/main/java/com/ccoe/build/utils/CompressUtils.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build-service/src/main/java/com/ccoe/build/utils/CompressUtils.java b/build-service/src/main/java/com/ccoe/build/utils/CompressUtils.java index d6742aa8..da6f017f 100644 --- a/build-service/src/main/java/com/ccoe/build/utils/CompressUtils.java +++ b/build-service/src/main/java/com/ccoe/build/utils/CompressUtils.java @@ -114,8 +114,10 @@ public static List unCompress(File zip, String unzipdir) throws IOExceptio System.out.println("Extracting: " + entry); int count; byte data[] = new byte[BUFFER]; - File unzipfile = new File(unzipdir + File.separator - + entry.getName()); + File unzipfile = new File(unzipdir, entry.getName()); + if (!unzipfile.toPath().normalize().startsWith(unzipdir)) { + throw new IOException("Bad zip entry"); + } FileOutputStream fos = new FileOutputStream(unzipfile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);