Skip to content

Commit 367042b

Browse files
committed
Move ZipSlip configurations to Query.qll library
1 parent ce2cab0 commit 367042b

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/** Provides dataflow configurations to be used in ZipSlip queries. */
2+
3+
import java
4+
import semmle.code.java.controlflow.Guards
5+
import semmle.code.java.dataflow.SSA
6+
import semmle.code.java.dataflow.TaintTracking
7+
import semmle.code.java.security.PathSanitizer
8+
private import semmle.code.java.dataflow.ExternalFlow
9+
10+
/**
11+
* A method that returns the name of an archive entry.
12+
*/
13+
class ArchiveEntryNameMethod extends Method {
14+
ArchiveEntryNameMethod() {
15+
exists(RefType archiveEntry |
16+
archiveEntry.hasQualifiedName("java.util.zip", "ZipEntry") or
17+
archiveEntry.hasQualifiedName("org.apache.commons.compress.archivers", "ArchiveEntry")
18+
|
19+
this.getDeclaringType().getAnAncestor() = archiveEntry and
20+
this.hasName("getName")
21+
)
22+
}
23+
}
24+
25+
/**
26+
* A taint-tracking configuration for reasoning about unsafe zip file extraction.
27+
*/
28+
module ZipSlipConfig implements DataFlow::ConfigSig {
29+
predicate isSource(DataFlow::Node source) {
30+
source.asExpr().(MethodAccess).getMethod() instanceof ArchiveEntryNameMethod
31+
}
32+
33+
predicate isSink(DataFlow::Node sink) { sink instanceof FileCreationSink }
34+
35+
predicate isBarrier(DataFlow::Node node) { node instanceof PathInjectionSanitizer }
36+
}
37+
38+
/** Tracks flow from archive entries to file creation. */
39+
module ZipSlipFlow = TaintTracking::Global<ZipSlipConfig>;
40+
41+
/**
42+
* A sink that represents a file creation, such as a file write, copy or move operation.
43+
*/
44+
private class FileCreationSink extends DataFlow::Node {
45+
FileCreationSink() { sinkNode(this, "create-file") }
46+
}

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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,9 @@
1313
*/
1414

1515
import java
16-
import semmle.code.java.controlflow.Guards
17-
import semmle.code.java.dataflow.SSA
18-
import semmle.code.java.dataflow.TaintTracking
19-
import semmle.code.java.security.PathSanitizer
20-
private import semmle.code.java.dataflow.ExternalFlow
21-
22-
/**
23-
* A method that returns the name of an archive entry.
24-
*/
25-
class ArchiveEntryNameMethod extends Method {
26-
ArchiveEntryNameMethod() {
27-
exists(RefType archiveEntry |
28-
archiveEntry.hasQualifiedName("java.util.zip", "ZipEntry") or
29-
archiveEntry.hasQualifiedName("org.apache.commons.compress.archivers", "ArchiveEntry")
30-
|
31-
this.getDeclaringType().getAnAncestor() = archiveEntry and
32-
this.hasName("getName")
33-
)
34-
}
35-
}
36-
37-
module ZipSlipConfig implements DataFlow::ConfigSig {
38-
predicate isSource(DataFlow::Node source) {
39-
source.asExpr().(MethodAccess).getMethod() instanceof ArchiveEntryNameMethod
40-
}
41-
42-
predicate isSink(DataFlow::Node sink) { sink instanceof FileCreationSink }
43-
44-
predicate isBarrier(DataFlow::Node node) { node instanceof PathInjectionSanitizer }
45-
}
46-
47-
module ZipSlipFlow = TaintTracking::Global<ZipSlipConfig>;
48-
16+
import semmle.code.java.security.ZipSlipQuery
4917
import ZipSlipFlow::PathGraph
5018

51-
/**
52-
* A sink that represents a file creation, such as a file write, copy or move operation.
53-
*/
54-
private class FileCreationSink extends DataFlow::Node {
55-
FileCreationSink() { sinkNode(this, "create-file") }
56-
}
57-
5819
from ZipSlipFlow::PathNode source, ZipSlipFlow::PathNode sink
5920
where ZipSlipFlow::flowPath(source, sink)
6021
select source.getNode(), source, sink,

0 commit comments

Comments
 (0)