UnzipFile is vulnerable to Zip Slip #5497
This commit is contained in:
parent
f83798f80c
commit
917c64307a
@ -9,13 +9,13 @@ import java.util.zip.ZipInputStream;
|
|||||||
|
|
||||||
public class UnzipFile {
|
public class UnzipFile {
|
||||||
public static void main(final String[] args) throws IOException {
|
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 byte[] buffer = new byte[1024];
|
||||||
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
final ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
|
||||||
ZipEntry zipEntry = zis.getNextEntry();
|
ZipEntry zipEntry = zis.getNextEntry();
|
||||||
while (zipEntry != null) {
|
while (zipEntry != null) {
|
||||||
final String fileName = zipEntry.getName();
|
final File newFile = newFile(destDir, zipEntry);
|
||||||
final File newFile = new File("src/main/resources/unzipTest/" + fileName);
|
|
||||||
final FileOutputStream fos = new FileOutputStream(newFile);
|
final FileOutputStream fos = new FileOutputStream(newFile);
|
||||||
int len;
|
int len;
|
||||||
while ((len = zis.read(buffer)) > 0) {
|
while ((len = zis.read(buffer)) > 0) {
|
||||||
@ -27,4 +27,20 @@ public class UnzipFile {
|
|||||||
zis.closeEntry();
|
zis.closeEntry();
|
||||||
zis.close();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
BIN
core-java-io/src/main/resources/unzipTest/compressed.zip
Normal file
BIN
core-java-io/src/main/resources/unzipTest/compressed.zip
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user