Skip to content

Commit

Permalink
UnzipFile is vulnerable to Zip Slip eugenp#5497
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-martin committed Oct 23, 2018
1 parent f83798f commit 917c643
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions core-java-io/src/main/java/com/baeldung/unzip/UnzipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

public class UnzipFile {
public static void main(final String[] args) throws IOException {
final String fileZip = "src/main/resources/compressed.zip";
final String fileZip = "src/main/resources/unzipTest/compressed.zip";
final File destDir = new File("src/main/resources/unzipTest");
final byte[] buffer = new byte[1024];
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
final String fileName = zipEntry.getName();
final File newFile = new File("src/main/resources/unzipTest/" + fileName);
final File newFile = newFile(destDir, zipEntry);
final FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
Expand All @@ -27,4 +27,20 @@ public static void main(final String[] args) throws IOException {
zis.closeEntry();
zis.close();
}

/**
* @see https://snyk.io/research/zip-slip-vulnerability
*/
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName());

String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = destFile.getCanonicalPath();

if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
}

return destFile;
}
}
Binary file not shown.

0 comments on commit 917c643

Please sign in to comment.