You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by GitBox <gi...@apache.org> on 2022/11/29 22:02:01 UTC

[GitHub] [brooklyn-server] geomacy commented on a diff in pull request #1372: Prevent zip slip

geomacy commented on code in PR #1372:
URL: https://github.com/apache/brooklyn-server/pull/1372#discussion_r1035313830


##########
core/src/test/java/org/apache/brooklyn/util/core/file/ArchiveUtilsTest.java:
##########
@@ -110,7 +113,20 @@ public void testDeployExplicitDestFile() throws Exception {
         ArchiveUtils.deploy(origJar.getAbsolutePath(), machine, destDir.getAbsolutePath(), destFile);
         assertFilesEqual(new File(destDir, destFile), origJar);
     }
-    
+    @Test(groups="Integration")
+    public void testUnzipFileAccessingPathOutsideTargetFolderEvilWinFormat() throws Exception{

Review Comment:
   This and the tests below don't actually `throws Exception`



##########
core/src/main/java/org/apache/brooklyn/util/core/file/ArchiveUtils.java:
##########
@@ -363,21 +363,38 @@ public static String readFullyString(File sourceFile) {
     }
 
     public static void extractZip(final ZipFile zip, final String targetFolder) {
-        new File(targetFolder).mkdir();
+        File targetPath = new File(targetFolder);
+        targetPath.mkdir();
         Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
         while (zipFileEntries.hasMoreElements()) {
-            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
-            File destFile = new File(targetFolder, entry.getName());
+            ZipEntry entry = zipFileEntries.nextElement();
+            String originalName = entry.getName();
+            File destFile = new File(targetPath, originalName);
+            // validate input path
+            try {
+                String canonicalDestinationDirPath = null;
+                // enforce the file uses the appropriate file separator for the controller SO
+                String sanitizedName = originalName
+                        .replace("\\", File.separator)
+                        .replace("/", File.separator);
+                File sanitizedDestFile = new File(targetPath, sanitizedName);
+                canonicalDestinationDirPath = targetPath.getCanonicalPath();

Review Comment:
   This doesn't need to be within the `for` loop. Perhaps do it above as
   ```java
   File targetPath = new File(targetFolder).getCanonicalPath();
   ```



##########
core/src/test/java/org/apache/brooklyn/util/core/file/ArchiveUtilsTest.java:
##########
@@ -133,4 +149,19 @@ private void assertSubFilesEqual(File parentDir, Map<String, String> files) thro
             assertEquals(Joiner.on("\n").join(Files.readLines(subFile, Charsets.UTF_8)), entry.getValue());
         }
     }
+
+    private void doTestUnzip(String url)  {
+        File tempZipFile = null;
+        InputStream evilZip = ResourceUtils.create(this).getResourceFromUrl(url);
+        try {
+            tempZipFile = File.createTempFile("test-zip", null);

Review Comment:
   ```suggestion
               tempZipFile = File.createTempFile("test", "zip");
   ```



##########
core/src/test/java/org/apache/brooklyn/util/core/file/ArchiveUtilsTest.java:
##########
@@ -110,7 +113,20 @@ public void testDeployExplicitDestFile() throws Exception {
         ArchiveUtils.deploy(origJar.getAbsolutePath(), machine, destDir.getAbsolutePath(), destFile);
         assertFilesEqual(new File(destDir, destFile), origJar);
     }
-    
+    @Test(groups="Integration")
+    public void testUnzipFileAccessingPathOutsideTargetFolderEvilWinFormat() throws Exception{
+        InputStream evilZip = ResourceUtils.create(this).getResourceFromUrl("classpath://brooklyn/util/file.core/evilWin.zip");

Review Comment:
   `evilZip` isn't used now 



##########
core/src/test/java/org/apache/brooklyn/util/core/file/ArchiveUtilsTest.java:
##########
@@ -133,4 +149,19 @@ private void assertSubFilesEqual(File parentDir, Map<String, String> files) thro
             assertEquals(Joiner.on("\n").join(Files.readLines(subFile, Charsets.UTF_8)), entry.getValue());
         }
     }
+
+    private void doTestUnzip(String url)  {
+        File tempZipFile = null;
+        InputStream evilZip = ResourceUtils.create(this).getResourceFromUrl(url);

Review Comment:
   It's not necessarily `evil`, as far as this method knows it's just a file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brooklyn.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org