Skip to content

Commit c968e94

Browse files
committed
Mitigate Zip Slip exlpoit
1 parent 5624f3f commit c968e94

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*/
3636
public final class ZipUtils {
3737

38+
// TODO: Maybe migrate to org.apache.commons.compress.archivers.examples.Expander?
3839
/**
3940
* Unzip files to path.
4041
*
@@ -67,6 +68,11 @@ public static void unzipFilesToPath(String jarPath, String destinationDir) throw
6768
String fileName = destinationDir + File.separator + entry.getName();
6869
File f = new File(fileName);
6970

71+
if (!f.getCanonicalPath().startsWith(destinationDir)) {
72+
System.out.println("Zip Slip exploit detected. Skipping entry " + entry.getName());
73+
continue;
74+
}
75+
7076
File parent = f.getParentFile();
7177
if (!parent.exists()) {
7278
parent.mkdirs();
@@ -106,15 +112,15 @@ public static void zipFile(File inputFile, File outputZip) {
106112

107113
public static void zipFolder(String srcFolder, String destZipFile, String ignore) throws Exception {
108114
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
109-
ZipOutputStream zip = new ZipOutputStream(fileWriter)){
115+
ZipOutputStream zip = new ZipOutputStream(fileWriter)) {
110116
addFolderToZip("", srcFolder, zip, ignore);
111117
zip.flush();
112118
}
113119
}
114120

115121
public static void zipFolderAPKTool(String srcFolder, String destZipFile) throws Exception {
116122
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
117-
ZipOutputStream zip = new ZipOutputStream(fileWriter)){
123+
ZipOutputStream zip = new ZipOutputStream(fileWriter)) {
118124
addFolderToZipAPKTool("", srcFolder, zip);
119125
zip.flush();
120126
}
@@ -199,4 +205,4 @@ public static void addFolderToZipAPKTool(String path, String srcFolder, ZipOutpu
199205
}
200206
}
201207
}
202-
}
208+
}

0 commit comments

Comments
 (0)