File size: 7,435 Bytes
281d6f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | diff --git a/src/main/java/org/codehaus/plexus/util/Expand.java b/src/main/java/org/codehaus/plexus/util/Expand.java
index 8abcdd95..6ac71dc1 100644
--- a/src/main/java/org/codehaus/plexus/util/Expand.java
+++ b/src/main/java/org/codehaus/plexus/util/Expand.java
@@ -111,8 +111,19 @@ public class Expand {
throws Exception {
File f = FileUtils.resolveFile(dir, entryName);
- if (!f.getAbsolutePath().startsWith(dir.getAbsolutePath())) {
- throw new IOException("Entry '" + entryName + "' outside the target directory.");
+ try {
+ String canonicalDirPath = dir.getCanonicalPath();
+ String canonicalFilePath = f.getCanonicalPath();
+
+ // Ensure the file is within the target directory
+ // We need to check that the canonical file path starts with the canonical directory path
+ // followed by a file separator to prevent path traversal attacks
+ if (!canonicalFilePath.startsWith(canonicalDirPath + File.separator)
+ && !canonicalFilePath.equals(canonicalDirPath)) {
+ throw new IOException("Entry '" + entryName + "' outside the target directory.");
+ }
+ } catch (IOException e) {
+ throw new IOException("Failed to verify entry path for '" + entryName + "'", e);
}
try {
diff --git a/src/test/java/org/codehaus/plexus/util/ExpandTest.java b/src/test/java/org/codehaus/plexus/util/ExpandTest.java
new file mode 100644
index 00000000..46e3d0e3
--- /dev/null
+++ b/src/test/java/org/codehaus/plexus/util/ExpandTest.java
@@ -0,0 +1,148 @@
+package org.codehaus.plexus.util;
+
+/*
+ * Copyright The Codehaus Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test for {@link Expand}.
+ */
+class ExpandTest extends FileBasedTestCase {
+
+ @Test
+ void testZipSlipVulnerabilityWithParentDirectory() throws Exception {
+ File tempDir = getTestDirectory();
+ File zipFile = new File(tempDir, "malicious.zip");
+ File targetDir = new File(tempDir, "extract");
+ targetDir.mkdirs();
+
+ // Create a malicious zip with path traversal
+ try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
+ ZipEntry entry = new ZipEntry("../../evil.txt");
+ zos.putNextEntry(entry);
+ zos.write("malicious content".getBytes());
+ zos.closeEntry();
+ }
+
+ Expand expand = new Expand();
+ expand.setSrc(zipFile);
+ expand.setDest(targetDir);
+
+ // This should throw an exception, not extract the file
+ assertThrows(Exception.class, () -> expand.execute());
+
+ // Verify the file was not created outside the target directory
+ File evilFile = new File(tempDir, "evil.txt");
+ assertFalse(evilFile.exists(), "File should not be extracted outside target directory");
+ }
+
+ @Test
+ void testZipSlipVulnerabilityWithAbsolutePath() throws Exception {
+ File tempDir = getTestDirectory();
+ File zipFile = new File(tempDir, "malicious-absolute.zip");
+ File targetDir = new File(tempDir, "extract-abs");
+ targetDir.mkdirs();
+
+ // Create a malicious zip with absolute path
+ File evilTarget = new File("/tmp/evil-absolute.txt");
+ try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
+ ZipEntry entry = new ZipEntry(evilTarget.getAbsolutePath());
+ zos.putNextEntry(entry);
+ zos.write("malicious content".getBytes());
+ zos.closeEntry();
+ }
+
+ Expand expand = new Expand();
+ expand.setSrc(zipFile);
+ expand.setDest(targetDir);
+
+ // This should throw an exception, not extract the file
+ assertThrows(Exception.class, () -> expand.execute());
+
+ // Verify the file was not created at the absolute path
+ assertFalse(evilTarget.exists(), "File should not be extracted to absolute path");
+ }
+
+ @Test
+ void testZipSlipVulnerabilityWithSimilarDirectoryName() throws Exception {
+ File tempDir = getTestDirectory();
+ File zipFile = new File(tempDir, "malicious-similar.zip");
+ File targetDir = new File(tempDir, "extract");
+ targetDir.mkdirs();
+
+ // Create a directory with a similar name to test prefix matching vulnerability
+ File similarDir = new File(tempDir, "extract-evil");
+ similarDir.mkdirs();
+
+ // Create a malicious zip that tries to exploit prefix matching
+ // If targetDir is /tmp/extract, this tries to write to /tmp/extract-evil/file.txt
+ String maliciousPath = "../extract-evil/evil.txt";
+ try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
+ ZipEntry entry = new ZipEntry(maliciousPath);
+ zos.putNextEntry(entry);
+ zos.write("malicious content".getBytes());
+ zos.closeEntry();
+ }
+
+ Expand expand = new Expand();
+ expand.setSrc(zipFile);
+ expand.setDest(targetDir);
+
+ // This should throw an exception, not extract the file
+ assertThrows(Exception.class, () -> expand.execute());
+
+ // Verify the file was not created in the similar directory
+ File evilFile = new File(similarDir, "evil.txt");
+ assertFalse(evilFile.exists(), "File should not be extracted to directory with similar name");
+ }
+
+ @Test
+ void testNormalZipExtraction() throws Exception {
+ File tempDir = getTestDirectory();
+ File zipFile = new File(tempDir, "normal.zip");
+ File targetDir = new File(tempDir, "extract-normal");
+ targetDir.mkdirs();
+
+ // Create a normal zip
+ try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()))) {
+ ZipEntry entry = new ZipEntry("subdir/normal.txt");
+ zos.putNextEntry(entry);
+ zos.write("normal content".getBytes());
+ zos.closeEntry();
+ }
+
+ Expand expand = new Expand();
+ expand.setSrc(zipFile);
+ expand.setDest(targetDir);
+
+ // This should succeed
+ expand.execute();
+
+ // Verify the file was created in the correct location
+ File normalFile = new File(targetDir, "subdir/normal.txt");
+ assertTrue(normalFile.exists(), "File should be extracted to correct location");
+ }
+}
|