File tree Expand file tree Collapse file tree 2 files changed +36
-9
lines changed
swift/ql/lib/codeql/swift/regex Expand file tree Collapse file tree 2 files changed +36
-9
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,39 @@ private class ParsedStringRegex extends RegExp, StringLiteralExpr {
28
28
RegexEval getEval ( ) { result = eval }
29
29
}
30
30
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
+
31
64
/**
32
65
* A call that evaluates a regular expression. For example, the call to `firstMatch` in:
33
66
* ```
Original file line number Diff line number Diff line change @@ -21,15 +21,9 @@ private module StringLiteralUseConfig implements DataFlow::ConfigSig {
21
21
22
22
predicate isAdditionalFlowStep ( DataFlow:: Node nodeFrom , DataFlow:: Node nodeTo ) {
23
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
24
+ exists ( RegexCreation regexCreation |
25
+ nodeFrom = regexCreation .getStringInput ( ) and
26
+ nodeTo = regexCreation
33
27
)
34
28
}
35
29
}
You can’t perform that action at this time.
0 commit comments