aboutsummaryrefslogtreecommitdiffstats
path: root/pf4j/src/main
diff options
context:
space:
mode:
authorDecebal Suiu <decebal.suiu@gmail.com>2023-08-16 20:55:25 +0300
committerDecebal Suiu <decebal.suiu@gmail.com>2023-08-16 20:55:25 +0300
commit1c8b8aeec7a4901f63f106dff1a6dd9f94076def (patch)
tree812ed2249bffb03551c5e46b1fa28f9e4845543d /pf4j/src/main
parentc1c9c982dc372d1027468e166c3e0675d6890e2a (diff)
downloadpf4j-#526.tar.gz
pf4j-#526.zip
Add security checks to prevent directory traversal when decompressing#526
Diffstat (limited to 'pf4j/src/main')
-rw-r--r--pf4j/src/main/java/org/pf4j/util/Unzip.java14
1 files changed, 10 insertions, 4 deletions
diff --git a/pf4j/src/main/java/org/pf4j/util/Unzip.java b/pf4j/src/main/java/org/pf4j/util/Unzip.java
index 198cf7d..a78cf83 100644
--- a/pf4j/src/main/java/org/pf4j/util/Unzip.java
+++ b/pf4j/src/main/java/org/pf4j/util/Unzip.java
@@ -15,17 +15,17 @@
*/
package org.pf4j.util;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
/**
* This class extracts the content of the plugin zip into a directory.
* It's a class for only the internal use.
@@ -75,11 +75,17 @@ public class Unzip {
FileUtils.delete(destination.toPath());
}
+ String destinationCanonicalPath = destination.getCanonicalPath();
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
File file = new File(destination, zipEntry.getName());
+ String fileCanonicalPath = file.getCanonicalPath();
+ if (!fileCanonicalPath.startsWith(destinationCanonicalPath)) {
+ throw new ZipException("The file "+ zipEntry.getName() + " is trying to leave the target output directory of "+ destination);
+ }
+
// create intermediary directories - sometimes zip don't add them
File dir = new File(file.getParent());