Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SECURITY] Fix Zip Slip Vulnerability #303

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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().normalize())) {
throw new RuntimeException("Bad zip entry");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't throw RuntimeException, it's an anti-pattern; one of ISE or IAE is likely better depending on what's at fault.

}
o.getParentFile().mkdirs();
OutputStream out = Files.newOutputStream(o.toPath());
IOUtils.copy(in, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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().normalize())) {
throw new RuntimeException("Bad zip entry");
}
try (final OutputStream out = Files.newOutputStream(zipEntryFile.toPath())) {
IOUtils.copy(in, out);
}
}
Expand Down Expand Up @@ -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().normalize())) {
throw new RuntimeException("Bad zip entry");
}
outputFile =zipEntryFile;

try (InputStream inputStream = zipFile.getInputStream(zipEntry);
OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -142,4 +145,4 @@ private static void usage() {
+ " [+storeddd] [-extract dir] archive");
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public void testZipUnarchive() throws Exception {
try (final ZipFile zf = openZipWithoutLFH("bla.zip")) {
for (final Enumeration<ZipArchiveEntry> 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().normalize())) {
throw new RuntimeException("Bad zip entry");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing a RE is an anti pattern AFIK, IllegalStateException or IllegalArgumentException would be better IMO.

}
try (final OutputStream out = Files.newOutputStream(zipEntryFile.toPath())) {
IOUtils.copy(zf.getInputStream(entry), out);
}
}
Expand Down