From 950266f6de7aa062b7dd3dbe5c21659572f2ed17 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 23 Sep 2022 03:45:28 +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 --- .../org/jaggeryjs/hostobjects/file/FileHostObject.java | 5 ++--- .../jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java | 8 ++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java b/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java index e32eba43..5603ef15 100644 --- a/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java +++ b/components/hostobjects/org.jaggeryjs.hostobjects.file/src/main/java/org/jaggeryjs/hostobjects/file/FileHostObject.java @@ -411,8 +411,7 @@ public static boolean jsFunction_unZip(Context cx, Scriptable thisObj, Object[] while ((entry = zin.getNextEntry()) != null) { name = entry.getName(); String canonicalDirPath = outdir.getCanonicalPath(); - String canonicalEntryPath = new File(canonicalDirPath + entry.getName()).getCanonicalPath(); - if(!canonicalEntryPath.startsWith(canonicalDirPath)){ + if(!new File(canonicalDirPath + entry.getName()).getCanonicalFile().toPath().startsWith(canonicalDirPath)){ log.error("Invalid entry found in the Zip file: " + name); return false; } @@ -605,4 +604,4 @@ private static boolean mkdirs(File parentDirectory, String path) { File dir = new File(parentDirectory, path); return dir.exists() || dir.mkdirs(); } -} \ No newline at end of file +} diff --git a/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java b/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java index ef739a09..00120517 100644 --- a/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java +++ b/components/jaggery-core/org.jaggeryjs.jaggery.app.mgt/src/main/java/org/jaggeryjs/jaggery/app/mgt/JaggeryDeploymentUtil.java @@ -44,7 +44,7 @@ static void unZip(InputStream is, String destDir) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { - File entryDir = new File(unzipDestinationDirectory.getAbsolutePath() + File.separator + entry.getName()); + File entryDir = new File(unzipDestinationDirectory.getAbsolutePath(), entry.getName()); boolean created = entryDir.mkdir(); if (!created) { log.error("Could not create DIR : " + unzipDestinationDirectory.getAbsolutePath() + @@ -56,7 +56,11 @@ static void unZip(InputStream is, String destDir) { // write the files to the disk FileOutputStream fos = null; try { - fos = new FileOutputStream(unzipDestinationDirectory.getAbsolutePath() + File.separator + entry.getName()); + final File zipEntryFile = new File(unzipDestinationDirectory.getAbsolutePath(), entry.getName()); + if (!zipEntryFile.toPath().normalize().startsWith(unzipDestinationDirectory.getAbsolutePath())) { + throw new IOException("Bad zip entry"); + } + fos = new FileOutputStream(zipEntryFile); dest = new BufferedOutputStream(fos, BYTE_BUFFER_SIZE); while ((count = zis.read(data, 0, BYTE_BUFFER_SIZE)) != -1) {