Skip to content

Commit

Permalink
vuln-fix: Zip Slip Vulnerability
Browse files Browse the repository at this point in the history
This fixes a Zip-Slip vulnerability.

This change does one of two things. This change either

1. Inserts a guard to protect against Zip Slip.
OR
2. Replaces `dir.getCanonicalPath().startsWith(parent.getCanonicalPath())`, which is vulnerable to partial path traversal attacks, with the more secure `dir.getCanonicalFile().toPath().startsWith(parent.getCanonicalFile().toPath())`.

For number 2, consider `"/usr/outnot".startsWith("/usr/out")`.
The check is bypassed although `/outnot` is not under the `/out` directory.
It's important to understand that the terminating slash may be removed when using various `String` representations of the `File` object.
For example, on Linux, `println(new File("/var"))` will print `/var`, but `println(new File("/var", "/")` will print `/var/`;
however, `println(new File("/var", "/").getCanonicalPath())` will print `/var`.

Weakness: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Severity: High
CVSSS: 7.4
Detection: CodeQL (https://codeql.github.com/codeql-query-help/java/java-zipslip/) & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.ZipSlip)

Reported-by: Jonathan Leitschuh <[email protected]>
Signed-off-by: Jonathan Leitschuh <[email protected]>

Bug-tracker: JLLeitschuh/security-research#16

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
JLLeitschuh and TeamModerne committed Sep 23, 2022
1 parent 9cdb6ea commit 950266f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ public static boolean jsFunction_unZip(Context cx, Scriptable thisObj, Object[]
while ((entry = zin.getNextEntry()) != null) {
name = entry.getName();
String canonicalDirPath = outdir.getCanonicalPath();
String canonicalEntryPath = new File(canonicalDirPath + entry.getName()).getCanonicalPath();
if(!canonicalEntryPath.startsWith(canonicalDirPath)){
if(!new File(canonicalDirPath + entry.getName()).getCanonicalFile().toPath().startsWith(canonicalDirPath)){
log.error("Invalid entry found in the Zip file: " + name);
return false;
}
Expand Down Expand Up @@ -605,4 +604,4 @@ private static boolean mkdirs(File parentDirectory, String path) {
File dir = new File(parentDirectory, path);
return dir.exists() || dir.mkdirs();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static void unZip(InputStream is, String destDir) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) {
File entryDir = new File(unzipDestinationDirectory.getAbsolutePath() + File.separator + entry.getName());
File entryDir = new File(unzipDestinationDirectory.getAbsolutePath(), entry.getName());
boolean created = entryDir.mkdir();
if (!created) {
log.error("Could not create DIR : " + unzipDestinationDirectory.getAbsolutePath() +
Expand All @@ -56,7 +56,11 @@ static void unZip(InputStream is, String destDir) {
// write the files to the disk
FileOutputStream fos = null;
try {
fos = new FileOutputStream(unzipDestinationDirectory.getAbsolutePath() + File.separator + entry.getName());
final File zipEntryFile = new File(unzipDestinationDirectory.getAbsolutePath(), entry.getName());
if (!zipEntryFile.toPath().normalize().startsWith(unzipDestinationDirectory.getAbsolutePath())) {
throw new IOException("Bad zip entry");
}
fos = new FileOutputStream(zipEntryFile);
dest = new BufferedOutputStream(fos, BYTE_BUFFER_SIZE);
while ((count = zis.read(data, 0, BYTE_BUFFER_SIZE))
!= -1) {
Expand Down

0 comments on commit 950266f

Please sign in to comment.