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 <Jonathan.Leitschuh@gmail.com>
Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>

Bug-tracker: JLLeitschuh/security-research#16


Co-authored-by: Moderne <team@moderne.io>
  • Loading branch information
JLLeitschuh and TeamModerne committed Nov 4, 2022
1 parent 523b7cd commit 2ba039f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -210,6 +211,9 @@ private static void unzip(InputStream wf, Path dir) throws Exception {
while (ze != null) {
String fileName = ze.getName();
Path newFile = dir.resolve(fileName);
if (!newFile.normalize().startsWith(dir.normalize())) {
throw new IOException("Bad zip entry");
}
if (ze.isDirectory()) {
Files.createDirectories(newFile);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ private File extractArchive(File archive) throws IOException {
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
JarEntry entry = entries.nextElement();
File file = new File(tempDir, entry.getName());
if (!file.toPath().normalize().startsWith(tempDir.toPath().normalize())) {
throw new IOException("Bad zip entry");
}
if (entry.isDirectory()) {
file.mkdir();
continue;
Expand Down
2 changes: 1 addition & 1 deletion patching/src/main/java/org/jboss/as/patching/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static void unzip(final ZipFile zip, final File patchDir) throws IOExcep
final String name = entry.getName();
final File current = new File(patchDir, name);
final String canonicalDestinationPath = current.getCanonicalPath();
if (! canonicalDestinationPath.startsWith(patchDir.getCanonicalPath())) {
if (! current.getCanonicalFile().toPath().startsWith(patchDir.getCanonicalFile().toPath())) {
throw PatchLogger.ROOT_LOGGER.entryOutsideOfPatchDirectory(canonicalDestinationPath);
}
if (entry.isDirectory()) {
Expand Down

0 comments on commit 2ba039f

Please sign in to comment.