Skip to content

Commit f243e85

Browse files
committed
Swift: Move regex dataflow code into a RegexTracking library (similar to the layout in Ruby and Python).
1 parent b5a8a8d commit f243e85

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

swift/ql/lib/codeql/swift/regex/Regex.qll

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,7 @@ import swift
66
import codeql.swift.regex.RegexTreeView
77
private import codeql.swift.dataflow.DataFlow
88
private import internal.ParseRegex
9-
10-
/**
11-
* A data flow configuration for tracking string literals that are used as
12-
* regular expressions.
13-
*/
14-
private module RegexUseConfig implements DataFlow::ConfigSig {
15-
predicate isSource(DataFlow::Node node) { node.asExpr() instanceof StringLiteralExpr }
16-
17-
predicate isSink(DataFlow::Node node) { node.asExpr() = any(RegexEval eval).getRegexInput() }
18-
19-
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
20-
// flow through `Regex` initializer, i.e. from a string to a `Regex` object.
21-
exists(CallExpr call |
22-
(
23-
call.getStaticTarget().(Method).hasQualifiedName("Regex", ["init(_:)", "init(_:as:)"]) or
24-
call.getStaticTarget()
25-
.(Method)
26-
.hasQualifiedName("NSRegularExpression", "init(pattern:options:)")
27-
) and
28-
nodeFrom.asExpr() = call.getArgument(0).getExpr() and
29-
nodeTo.asExpr() = call
30-
)
31-
}
32-
}
33-
34-
private module RegexUseFlow = DataFlow::Global<RegexUseConfig>;
9+
private import internal.RegexTracking
3510

3611
/**
3712
* A string literal that is used as a regular expression in a regular
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Provides classes and predicates that track strings and regular expressions
3+
* to where they are used, along with properties of the regex such as parse
4+
* mode flags that have been set.
5+
*/
6+
7+
import swift
8+
import codeql.swift.regex.RegexTreeView
9+
private import codeql.swift.dataflow.DataFlow
10+
private import ParseRegex
11+
private import codeql.swift.regex.Regex
12+
13+
/**
14+
* A data flow configuration for tracking string literals that are used as
15+
* regular expressions.
16+
*/
17+
private module RegexUseConfig implements DataFlow::ConfigSig {
18+
predicate isSource(DataFlow::Node node) { node.asExpr() instanceof StringLiteralExpr }
19+
20+
predicate isSink(DataFlow::Node node) { node.asExpr() = any(RegexEval eval).getRegexInput() }
21+
22+
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
23+
// flow through `Regex` initializer, i.e. from a string to a `Regex` object.
24+
exists(CallExpr call |
25+
(
26+
call.getStaticTarget().(Method).hasQualifiedName("Regex", ["init(_:)", "init(_:as:)"]) or
27+
call.getStaticTarget()
28+
.(Method)
29+
.hasQualifiedName("NSRegularExpression", "init(pattern:options:)")
30+
) and
31+
nodeFrom.asExpr() = call.getArgument(0).getExpr() and
32+
nodeTo.asExpr() = call
33+
)
34+
}
35+
}
36+
37+
module RegexUseFlow = DataFlow::Global<RegexUseConfig>;

0 commit comments

Comments
 (0)