From 40a18af4d09812ed6a0a35f56832b9e7549dad99 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 8 Sep 2022 21:36:49 +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 --- .../wso2/maven/p2/generate/utils/FileManagementUtil.java | 3 +++ .../wso2/maven/car/artifact/utils/FileManagementUtil.java | 3 +++ .../eclipse/utils/archive/ArchiveManipulator.java | 7 +++++-- .../wso2/developerstudio/eclipse/utils/file/FileUtils.java | 7 +++++-- 4 files changed, 16 insertions(+), 4 deletions(-) mode change 100755 => 100644 carbon-p2-plugin/src/main/java/org/wso2/maven/p2/generate/utils/FileManagementUtil.java mode change 100755 => 100644 maven-car-plugin/src/main/java/org/wso2/maven/car/artifact/utils/FileManagementUtil.java diff --git a/carbon-p2-plugin/src/main/java/org/wso2/maven/p2/generate/utils/FileManagementUtil.java b/carbon-p2-plugin/src/main/java/org/wso2/maven/p2/generate/utils/FileManagementUtil.java old mode 100755 new mode 100644 index 9bfcfe08..77fa4d93 --- a/carbon-p2-plugin/src/main/java/org/wso2/maven/p2/generate/utils/FileManagementUtil.java +++ b/carbon-p2-plugin/src/main/java/org/wso2/maven/p2/generate/utils/FileManagementUtil.java @@ -425,6 +425,9 @@ public static void unzip(File archiveFile,File destination) throws Exception{ int count; byte data[] = new byte[BUFFER]; File file = new File(base,entry.getName()); + if (!file.toPath().normalize().startsWith(base.toPath().normalize())) { + throw new IOException("Bad zip entry"); + } if (entry.getName().endsWith("/")){ file.mkdirs(); continue; diff --git a/maven-car-plugin/src/main/java/org/wso2/maven/car/artifact/utils/FileManagementUtil.java b/maven-car-plugin/src/main/java/org/wso2/maven/car/artifact/utils/FileManagementUtil.java old mode 100755 new mode 100644 index e935fc55..9aa98bd4 --- a/maven-car-plugin/src/main/java/org/wso2/maven/car/artifact/utils/FileManagementUtil.java +++ b/maven-car-plugin/src/main/java/org/wso2/maven/car/artifact/utils/FileManagementUtil.java @@ -383,6 +383,9 @@ public static void unzip(File archiveFile,File destination) throws Exception{ int count; byte data[] = new byte[BUFFER]; File file = new File(base,entry.getName()); + if (!file.toPath().normalize().startsWith(base.toPath().normalize())) { + throw new IOException("Bad zip entry"); + } if (entry.getName().endsWith("/")){ file.mkdirs(); continue; diff --git a/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/archive/ArchiveManipulator.java b/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/archive/ArchiveManipulator.java index 8944d13e..920ca295 100644 --- a/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/archive/ArchiveManipulator.java +++ b/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/archive/ArchiveManipulator.java @@ -158,7 +158,10 @@ public void extractFromStream(InputStream inputStream, String extractDir) throws ZipEntry entry; while ((entry = zin.getNextEntry()) != null) { String entryName = entry.getName(); - File f = new File(extractDir + File.separator + entryName); + File f = new File(extractDir, entryName); + if (!f.toPath().normalize().startsWith(extractDir)) { + throw new IOException("Bad zip entry"); + } if (entryName.endsWith("/") && !f.exists()) { // this is a // directory @@ -171,7 +174,7 @@ public void extractFromStream(InputStream inputStream, String extractDir) throws String dirPath = ""; if (lastIndexOfSlash != -1) { dirPath = entryName.substring(0, lastIndexOfSlash); - File dir = new File(extractDir + File.separator + dirPath); + File dir = new File(extractDir, dirPath); if (!dir.exists()) { dir.mkdirs(); } diff --git a/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/file/FileUtils.java b/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/file/FileUtils.java index 0eb0bf28..21fb1ef4 100644 --- a/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/file/FileUtils.java +++ b/org.wso2.maven.utils/src/main/java/org/wso2/developerstudio/eclipse/utils/file/FileUtils.java @@ -661,7 +661,10 @@ public static void extractFromStream(InputStream inputStream, String extractDir) ZipEntry entry; while ((entry = zin.getNextEntry()) != null) { String entryName = entry.getName(); - File f = new File(extractDir + File.separator + entryName); + File f = new File(extractDir, entryName); + if (!f.toPath().normalize().startsWith(extractDir)) { + throw new IOException("Bad zip entry"); + } if (entryName.endsWith("/") && !f.exists()) { // this is a // directory @@ -674,7 +677,7 @@ public static void extractFromStream(InputStream inputStream, String extractDir) String dirPath = ""; if (lastIndexOfSlash != -1) { dirPath = entryName.substring(0, lastIndexOfSlash); - File dir = new File(extractDir + File.separator + dirPath); + File dir = new File(extractDir, dirPath); if (!dir.exists()) { dir.mkdirs(); }