Skip to content

Commit 4097338

Browse files
committed
Java: Add a flow step for Path::toFile in ZipSlip
1 parent 383e82a commit 4097338

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

change-notes/1.20/analysis-java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
| **Query** | **Expected impact** | **Change** |
1616
|----------------------------|------------------------|------------------------------------------------------------------|
17+
| Arbitrary file write during archive extraction ("Zip Slip") (`java/zipslip`) | Fewer false positive results | Results involving a sanitization step that converts a destination `Path` to a `File` are no longer reported. |
1718
| Double-checked locking is not thread-safe (`java/unsafe-double-checked-locking`) | Fewer false positive results and more true positive results | Results that use safe publication through a `final` field are no longer reported. Results that initialize immutable types like `String` incorrectly are now reported. |
1819
| Result of multiplication cast to wider type (`java/integer-multiplication-cast-to-long`) | Fewer results | Results involving conversions to `float` or `double` are no longer reported, as they were almost exclusively false positives. |
1920

java/ql/src/Security/CWE/CWE-022/ZipSlip.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ predicate filePathStep(ExprNode n1, ExprNode n2) {
7979
m.getDeclaringType() instanceof TypeFile and m.hasName("toPath")
8080
or
8181
m.getDeclaringType() instanceof TypePath and m.hasName("toAbsolutePath")
82+
or
83+
m.getDeclaringType() instanceof TypePath and m.hasName("toFile")
8284
)
8385
}
8486

java/ql/test/query-tests/security/CWE-022/semmle/tests/ZipTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,13 @@ public void m5(ZipEntry entry, File dir) {
5151
throw new Exception();
5252
FileOutputStream os = new FileOutputStream(file); // OK
5353
}
54+
55+
public void m6(ZipEntry entry, Path dir) {
56+
String canonicalDest = dir.toFile().getCanonicalPath();
57+
Path target = dir.resolve(entry.getName());
58+
String canonicalTarget = target.toFile().getCanonicalPath();
59+
if (!canonicalTarget.startsWith(canonicalDest + File.separator))
60+
throw new Exception();
61+
OutputStream os = Files.newOutputStream(target); // OK
62+
}
5463
}

0 commit comments

Comments
 (0)