Skip to content

Commit f57800d

Browse files
vuln-fix: Zip Slip Vulnerability
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]>
1 parent 4571c9f commit f57800d

File tree

1 file changed

+5
-1
lines changed
  • pippo-controller-parent/pippo-controller/src/test/java/ro/pippo

1 file changed

+5
-1
lines changed

pippo-controller-parent/pippo-controller/src/test/java/ro/pippo/DynamicJar.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ private void extract(Path jarPath, Path destDir) throws IOException {
233233
Enumeration<JarEntry> enumEntries = jar.entries();
234234
while (enumEntries.hasMoreElements()) {
235235
JarEntry entry = enumEntries.nextElement();
236-
File f = destDir.resolve(entry.getName()).toFile();
236+
final Path zipEntryPath = destDir.resolve(entry.getName());
237+
if (!zipEntryPath.normalize().startsWith(destDir.normalize())) {
238+
throw new IOException("Bad zip entry");
239+
}
240+
File f = zipEntryPath.toFile();
237241
f.getParentFile().mkdirs();
238242
InputStream is = jar.getInputStream(entry);
239243
FileOutputStream fos = new FileOutputStream(f);

0 commit comments

Comments
 (0)