From 3e5718f75090d3a672175b153113f69cd6cc14c9 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Mon, 3 Oct 2022 21:26:44 +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/reka/modules/builtins/UnzipOperation.java | 4 ++++ reka-core/src/main/java/reka/util/Util.java | 3 +++ 2 files changed, 7 insertions(+) diff --git a/reka-core/src/main/java/reka/modules/builtins/UnzipOperation.java b/reka-core/src/main/java/reka/modules/builtins/UnzipOperation.java index 03d93374..00c4a3e8 100644 --- a/reka-core/src/main/java/reka/modules/builtins/UnzipOperation.java +++ b/reka-core/src/main/java/reka/modules/builtins/UnzipOperation.java @@ -6,6 +6,7 @@ import java.io.ByteArrayInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.function.Function; @@ -45,6 +46,9 @@ public void call(MutableData data, OperationContext ctx) { ZipEntry e; while ((e = zip.getNextEntry()) != null) { java.nio.file.Path filepath = outputDir.resolve(e.getName()); + if (!filepath.normalize().startsWith(outputDir.normalize())) { + throw new IOException("Bad zip entry"); + } Files.createDirectories(filepath.getParent()); FileOutputStream out = new FileOutputStream(filepath.toFile()); try { diff --git a/reka-core/src/main/java/reka/util/Util.java b/reka-core/src/main/java/reka/util/Util.java index b79eab6c..d5b2ea90 100644 --- a/reka-core/src/main/java/reka/util/Util.java +++ b/reka-core/src/main/java/reka/util/Util.java @@ -256,6 +256,9 @@ public static void unzip(byte[] bytes, java.nio.file.Path dest) { ZipEntry e; while ((e = zip.getNextEntry()) != null) { java.nio.file.Path filepath = dest.resolve(e.getName()); + if (!filepath.normalize().startsWith(dest.normalize())) { + throw new IOException("Bad zip entry"); + } Files.createDirectories(filepath.getParent()); FileOutputStream out = new FileOutputStream(filepath.toFile()); try {