diff --git a/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java b/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java index b19e2b4913b..f0f55cce095 100644 --- a/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java +++ b/src/test/java/org/apache/commons/compress/archivers/JarTestCase.java @@ -61,6 +61,9 @@ public void testJarUnarchive() throws Exception { ZipArchiveEntry entry = (ZipArchiveEntry)in.getNextEntry(); File o = new File(dir, entry.getName()); + if (!o.toPath().normalize().startsWith(dir.toPath())) { + throw new RuntimeException("Bad zip entry"); + } o.getParentFile().mkdirs(); OutputStream out = Files.newOutputStream(o.toPath()); IOUtils.copy(in, out); diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java index e4f3ead3e1d..6bec99fd4f8 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java +++ b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java @@ -99,7 +99,10 @@ public void testZipArchiveCreation() throws Exception { fileInputStream)) { ZipArchiveEntry entry = null; while ((entry = (ZipArchiveEntry) archiveInputStream.getNextEntry()) != null) { - final File outfile = new File(resultDir.getCanonicalPath() + "/result/" + entry.getName()); + final File outfile = new File(resultDir.getCanonicalPath() + "/result/", entry.getName()); + if (!outfile.toPath().normalize().startsWith(resultDir.getCanonicalPath() + "/result/")) { + throw new RuntimeException("Bad zip entry"); + } outfile.getParentFile().mkdirs(); try (OutputStream o = Files.newOutputStream(outfile.toPath())) { IOUtils.copy(archiveInputStream, o); @@ -168,7 +171,11 @@ public void testZipUnarchive() throws Exception { try (final InputStream is = Files.newInputStream(input.toPath()); final ArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", is)) { final ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry(); - try (final OutputStream out = Files.newOutputStream(new File(dir, entry.getName()).toPath())) { + final File zipEntryFile = new File(dir, entry.getName()); + if (!zipEntryFile.toPath().normalize().startsWith(dir.toPath())) { + throw new RuntimeException("Bad zip entry"); + } + try (final OutputStream out = Files.newOutputStream(zipEntryFile.toPath())) { IOUtils.copy(in, out); } } @@ -828,7 +835,11 @@ private File getFilesToZip() throws IOException { if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } - outputFile = new File(dir, zipEntry.getName()); + final File zipEntryFile = new File(dir, zipEntry.getName()); + if (!zipEntryFile.toPath().normalize().startsWith(dir.toPath())) { + throw new RuntimeException("Bad zip entry"); + } + outputFile =zipEntryFile; try (InputStream inputStream = zipFile.getInputStream(zipEntry); OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java b/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java index 6fd2901268b..3f29cd09fd4 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java @@ -91,6 +91,9 @@ private static void list(final ZipArchiveEntry entry) { private static void extract(final String dir, final ZipArchiveEntry entry, final InputStream is) throws IOException { final File f = new File(dir, entry.getName()); + if (!f.toPath().normalize().startsWith(dir)) { + throw new RuntimeException("Bad zip entry"); + } if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } @@ -142,4 +145,4 @@ private static void usage() { + " [+storeddd] [-extract dir] archive"); System.exit(1); } -} \ No newline at end of file +} diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java index 91056d6134a..b646124a9b7 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileIgnoringLocalFileHeaderTest.java @@ -55,7 +55,11 @@ public void testZipUnarchive() throws Exception { try (final ZipFile zf = openZipWithoutLFH("bla.zip")) { for (final Enumeration e = zf.getEntries(); e.hasMoreElements(); ) { final ZipArchiveEntry entry = e.nextElement(); - try (final OutputStream out = Files.newOutputStream(new File(dir, entry.getName()).toPath())) { + final File zipEntryFile = new File(dir, entry.getName()); + if (!zipEntryFile.toPath().normalize().startsWith(dir.toPath())) { + throw new RuntimeException("Bad zip entry"); + } + try (final OutputStream out = Files.newOutputStream(zipEntryFile.toPath())) { IOUtils.copy(zf.getInputStream(entry), out); } }