35
35
*/
36
36
public final class ZipUtils {
37
37
38
+ // TODO: Maybe migrate to org.apache.commons.compress.archivers.examples.Expander?
38
39
/**
39
40
* Unzip files to path.
40
41
*
@@ -67,6 +68,11 @@ public static void unzipFilesToPath(String jarPath, String destinationDir) throw
67
68
String fileName = destinationDir + File .separator + entry .getName ();
68
69
File f = new File (fileName );
69
70
71
+ if (!f .getCanonicalPath ().startsWith (destinationDir )) {
72
+ System .out .println ("Zip Slip exploit detected. Skipping entry " + entry .getName ());
73
+ continue ;
74
+ }
75
+
70
76
File parent = f .getParentFile ();
71
77
if (!parent .exists ()) {
72
78
parent .mkdirs ();
@@ -106,15 +112,15 @@ public static void zipFile(File inputFile, File outputZip) {
106
112
107
113
public static void zipFolder (String srcFolder , String destZipFile , String ignore ) throws Exception {
108
114
try (FileOutputStream fileWriter = new FileOutputStream (destZipFile );
109
- ZipOutputStream zip = new ZipOutputStream (fileWriter )){
115
+ ZipOutputStream zip = new ZipOutputStream (fileWriter )) {
110
116
addFolderToZip ("" , srcFolder , zip , ignore );
111
117
zip .flush ();
112
118
}
113
119
}
114
120
115
121
public static void zipFolderAPKTool (String srcFolder , String destZipFile ) throws Exception {
116
122
try (FileOutputStream fileWriter = new FileOutputStream (destZipFile );
117
- ZipOutputStream zip = new ZipOutputStream (fileWriter )){
123
+ ZipOutputStream zip = new ZipOutputStream (fileWriter )) {
118
124
addFolderToZipAPKTool ("" , srcFolder , zip );
119
125
zip .flush ();
120
126
}
@@ -199,4 +205,4 @@ public static void addFolderToZipAPKTool(String path, String srcFolder, ZipOutpu
199
205
}
200
206
}
201
207
}
202
- }
208
+ }
0 commit comments