Skip to content

Commit 9e2832a

Browse files
committed
Java: Convert zipslip sinks to CSV format
1 parent b9ce1ae commit 9e2832a

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

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

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import semmle.code.java.dataflow.SSA
1717
import semmle.code.java.dataflow.TaintTracking
1818
import DataFlow
1919
import PathGraph
20+
private import semmle.code.java.dataflow.ExternalFlow
2021

2122
/**
2223
* A method that returns the name of an archive entry.
@@ -33,34 +34,6 @@ class ArchiveEntryNameMethod extends Method {
3334
}
3435
}
3536

36-
/**
37-
* An expression that will be treated as the destination of a write.
38-
*/
39-
class WrittenFileName extends Expr {
40-
WrittenFileName() {
41-
// Constructors that write to their first argument.
42-
exists(ConstructorCall ctr | this = ctr.getArgument(0) |
43-
exists(Class c | ctr.getConstructor() = c.getAConstructor() |
44-
c.hasQualifiedName("java.io", "FileOutputStream") or
45-
c.hasQualifiedName("java.io", "RandomAccessFile") or
46-
c.hasQualifiedName("java.io", "FileWriter")
47-
)
48-
)
49-
or
50-
// Methods that write to their n'th argument
51-
exists(MethodAccess call, int n | this = call.getArgument(n) |
52-
call.getMethod().getDeclaringType().hasQualifiedName("java.nio.file", "Files") and
53-
(
54-
call.getMethod().getName().regexpMatch("new.*Reader|newOutputStream|create.*") and n = 0
55-
or
56-
call.getMethod().hasName("copy") and n = 1
57-
or
58-
call.getMethod().hasName("move") and n = 1
59-
)
60-
)
61-
}
62-
}
63-
6437
/**
6538
* Holds if `n1` to `n2` is a dataflow step that converts between `String`,
6639
* `File`, and `Path`.
@@ -151,7 +124,7 @@ class ZipSlipConfiguration extends TaintTracking::Configuration {
151124
source.asExpr().(MethodAccess).getMethod() instanceof ArchiveEntryNameMethod
152125
}
153126

154-
override predicate isSink(Node sink) { sink.asExpr() instanceof WrittenFileName }
127+
override predicate isSink(Node sink) { sinkNode(sink, "create-file") }
155128

156129
override predicate isAdditionalTaintStep(Node n1, Node n2) {
157130
filePathStep(n1, n2) or fileTaintStep(n1, n2)

java/ql/src/semmle/code/java/dataflow/ExternalFlow.qll

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,21 @@ private predicate sinkModelCsv(string row) {
189189
[
190190
// Open URL
191191
"java.net;URL;false;openConnection;;;Argument[-1];open-url",
192-
"java.net;URL;false;openStream;;;Argument[-1];open-url"
192+
"java.net;URL;false;openStream;;;Argument[-1];open-url",
193+
// Create file
194+
"java.io;FileOutputStream;false;FileOutputStream;;;Argument[0];create-file",
195+
"java.io;RandomAccessFile;false;RandomAccessFile;;;Argument[0];create-file",
196+
"java.io;FileWriter;false;FileWriter;;;Argument[0];create-file",
197+
"java.nio.file;Files;false;move;;;Argument[1];create-file",
198+
"java.nio.file;Files;false;copy;;;Argument[1];create-file",
199+
"java.nio.file;Files;false;newOutputStream;;;Argument[0];create-file",
200+
"java.nio.file;Files;false;newBufferedReader;;;Argument[0];create-file",
201+
"java.nio.file;Files;false;createDirectory;;;Argument[0];create-file",
202+
"java.nio.file;Files;false;createFile;;;Argument[0];create-file",
203+
"java.nio.file;Files;false;createLink;;;Argument[0];create-file",
204+
"java.nio.file;Files;false;createSymbolicLink;;;Argument[0];create-file",
205+
"java.nio.file;Files;false;createTempDirectory;;;Argument[0];create-file",
206+
"java.nio.file;Files;false;createTempFile;;;Argument[0];create-file"
193207
]
194208
}
195209

0 commit comments

Comments
 (0)