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 Jul 29, 2022
1 parent 46a7059 commit 311bb39
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void unZip(String apkFile) throws Exception {
// Create Zip Entry Folder
File zeFile = extractFolder;
if (zeFolder != null) {
zeFile = new File(extractFolder.getPath() + File.separator + zeFolder);
zeFile = new File(extractFolder.getPath(), zeFolder);
if (!zeFile.exists())
zeFile.mkdirs();
}
Expand All @@ -112,7 +112,13 @@ public void unZip(String apkFile) throws Exception {
int count;
byte data[] = new byte[BUFFER];

FileOutputStream fos = new FileOutputStream(zeFile.getPath() + File.separator + zeName);
final File zipEntryFile = new File(zeFile.getPath(), zeName);

if (!zipEntryFile.toPath().normalize().startsWith(zeFile.getPath())) {
throw new RuntimeException("Bad zip entry");
}

FileOutputStream fos = new FileOutputStream(zipEntryFile);
dest = new BufferedOutputStream(fos, BUFFER);

while ((count = zin.read(data, 0, BUFFER)) != -1) {
Expand Down

0 comments on commit 311bb39

Please sign in to comment.