Skip to content

Commit c76d85d

Browse files
committed
Swift: Create a model for RegexCreation.
1 parent 734a00d commit c76d85d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ private class ParsedStringRegex extends RegExp, StringLiteralExpr {
2828
RegexEval getEval() { result = eval }
2929
}
3030

31+
/**
32+
* A data-flow node where a regular expression object is created.
33+
*/
34+
abstract class RegexCreation extends DataFlow::Node {
35+
/**
36+
* Gets a dataflow node for the string that the regular expression object is
37+
* created from.
38+
*/
39+
abstract DataFlow::Node getStringInput();
40+
}
41+
42+
/**
43+
* A data-flow node where a `Regex` or `NSRegularExpression` object is created.
44+
*/
45+
private class StandardRegexCreation extends RegexCreation {
46+
DataFlow::Node input;
47+
48+
StandardRegexCreation() {
49+
exists(CallExpr call |
50+
(
51+
call.getStaticTarget().(Method).hasQualifiedName("Regex", ["init(_:)", "init(_:as:)"]) or
52+
call.getStaticTarget()
53+
.(Method)
54+
.hasQualifiedName("NSRegularExpression", "init(pattern:options:)")
55+
) and
56+
input.asExpr() = call.getArgument(0).getExpr() and
57+
this.asExpr() = call
58+
)
59+
}
60+
61+
override DataFlow::Node getStringInput() { result = input }
62+
}
63+
3164
/**
3265
* A call that evaluates a regular expression. For example, the call to `firstMatch` in:
3366
* ```

swift/ql/lib/codeql/swift/regex/internal/RegexTracking.qll

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,9 @@ private module StringLiteralUseConfig implements DataFlow::ConfigSig {
2121

2222
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
2323
// 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
24+
exists(RegexCreation regexCreation |
25+
nodeFrom = regexCreation.getStringInput() and
26+
nodeTo = regexCreation
3327
)
3428
}
3529
}

0 commit comments

Comments
 (0)