From 43e2d593479647e5ca384befbb8f8613c46759ea Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Mon, 8 Aug 2022 20:34:20 +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 --- src/main/java/org/zeroturnaround/zip/ZipUtil.java | 2 +- src/test/java/example/UnpackExample.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/zeroturnaround/zip/ZipUtil.java b/src/main/java/org/zeroturnaround/zip/ZipUtil.java index 111ac85..3fda627 100644 --- a/src/main/java/org/zeroturnaround/zip/ZipUtil.java +++ b/src/main/java/org/zeroturnaround/zip/ZipUtil.java @@ -1140,7 +1140,7 @@ private static File checkDestinationFileForTraversal(File outputDir, String name * that the outputdir + name doesn't leave the outputdir. See * DirectoryTraversalMaliciousTest for details. */ - if (name.indexOf("..") != -1 && !destFile.getCanonicalPath().startsWith(outputDir.getCanonicalPath())) { + if (name.indexOf("..") != -1 && !destFile.getCanonicalFile().toPath().startsWith(outputDir.getCanonicalFile().toPath())) { throw new MaliciousZipException(outputDir, name); } return destFile; diff --git a/src/test/java/example/UnpackExample.java b/src/test/java/example/UnpackExample.java index 32ed58a..1cd29ed 100644 --- a/src/test/java/example/UnpackExample.java +++ b/src/test/java/example/UnpackExample.java @@ -26,7 +26,11 @@ public static void usual() throws IOException { OutputStream out = null; try { in = zf.getInputStream(e); - out = new FileOutputStream(new File("demo", e.getName())); + final File zipEntryFile = new File("demo", e.getName()); + if (!zipEntryFile.toPath().normalize().startsWith("demo")) { + throw new RuntimeException("Bad zip entry"); + } + out = new FileOutputStream(zipEntryFile); IOUtils.copy(in, out); } finally {