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 #149

Merged
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
2 changes: 1 addition & 1 deletion src/main/java/org/zeroturnaround/zip/ZipUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ private static File checkDestinationFileForTraversal(File outputDir, String name
* that the outputdir + name doesn't leave the outputdir. See
* DirectoryTraversalMaliciousTest for details.
*/
if (name.indexOf("..") != -1 && !destFile.getCanonicalPath().startsWith(outputDir.getCanonicalPath())) {
if (name.indexOf("..") != -1 && !destFile.getCanonicalFile().toPath().startsWith(outputDir.getCanonicalFile().toPath())) {
throw new MaliciousZipException(outputDir, name);
}
return destFile;
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/example/UnpackExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public static void usual() throws IOException {
OutputStream out = null;
try {
in = zf.getInputStream(e);
out = new FileOutputStream(new File("demo", e.getName()));
final File zipEntryFile = new File("demo", e.getName());
if (!zipEntryFile.toPath().normalize().startsWith("demo")) {
throw new IOException("Bad zip entry");
}
out = new FileOutputStream(zipEntryFile);
IOUtils.copy(in, out);
}
finally {
Expand Down