File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99
1010public class UnzipFile {
1111 public static void main (final String [] args ) throws IOException {
12- final String fileZip = "src/main/resources/compressed.zip" ;
12+ final String fileZip = "src/main/resources/unzipTest/compressed.zip" ;
13+ final File destDir = new File ("src/main/resources/unzipTest" );
1314 final byte [] buffer = new byte [1024 ];
1415 final ZipInputStream zis = new ZipInputStream (new FileInputStream (fileZip ));
1516 ZipEntry zipEntry = zis .getNextEntry ();
1617 while (zipEntry != null ) {
17- final String fileName = zipEntry .getName ();
18- final File newFile = new File ("src/main/resources/unzipTest/" + fileName );
18+ final File newFile = newFile (destDir , zipEntry );
1919 final FileOutputStream fos = new FileOutputStream (newFile );
2020 int len ;
2121 while ((len = zis .read (buffer )) > 0 ) {
@@ -27,4 +27,20 @@ public static void main(final String[] args) throws IOException {
2727 zis .closeEntry ();
2828 zis .close ();
2929 }
30+
31+ /**
32+ * @see https://snyk.io/research/zip-slip-vulnerability
33+ */
34+ public static File newFile (File destinationDir , ZipEntry zipEntry ) throws IOException {
35+ File destFile = new File (destinationDir , zipEntry .getName ());
36+
37+ String destDirPath = destinationDir .getCanonicalPath ();
38+ String destFilePath = destFile .getCanonicalPath ();
39+
40+ if (!destFilePath .startsWith (destDirPath + File .separator )) {
41+ throw new IOException ("Entry is outside of the target dir: " + zipEntry .getName ());
42+ }
43+
44+ return destFile ;
45+ }
3046}
You can’t perform that action at this time.
0 commit comments