@@ -11,60 +11,71 @@ private import codeql.rust.dataflow.FlowSink
1111private import codeql.rust.Concepts
1212
1313/**
14- * A data flow sink for regular expression injection vulnerabilities.
14+ * Provides default sources, sinks and barriers for detecting regular expression
15+ * injection vulnerabilities, as well as extension points for adding your own.
1516 */
16- abstract class RegexInjectionSink extends QuerySink:: Range {
17- override string getSinkType ( ) { result = "RegexInjection" }
18- }
17+ module RegexInjection {
18+ /**
19+ * A data flow sink for regular expression injection vulnerabilities.
20+ */
21+ abstract class Sink extends QuerySink:: Range {
22+ override string getSinkType ( ) { result = "RegexInjection" }
23+ }
1924
20- /**
21- * A barrier for regular expression injection vulnerabilities.
22- */
23- abstract class RegexInjectionBarrier extends DataFlow:: Node { }
25+ /**
26+ * A barrier for regular expression injection vulnerabilities.
27+ */
28+ abstract class Barrier extends DataFlow:: Node { }
2429
25- /** A sink for `a` in `Regex::new(a)` when `a` is not a literal. */
26- private class NewRegexInjectionSink extends RegexInjectionSink {
27- NewRegexInjectionSink ( ) {
28- exists ( CallExprCfgNode call , PathExpr path |
29- path = call .getFunction ( ) .getExpr ( ) and
30- path .getResolvedCrateOrigin ( ) = "repo:https://github.com/rust-lang/regex:regex" and
31- path .getResolvedPath ( ) = "<crate::regex::string::Regex>::new" and
32- this .asExpr ( ) = call .getArgument ( 0 ) and
33- not this .asExpr ( ) instanceof LiteralExprCfgNode
34- )
30+ /**
31+ * A sink for `a` in `Regex::new(a)` when `a` is not a literal.
32+ */
33+ private class NewSink extends Sink {
34+ NewSink ( ) {
35+ exists ( CallExprCfgNode call , PathExpr path |
36+ path = call .getFunction ( ) .getExpr ( ) and
37+ path .getResolvedCrateOrigin ( ) = "repo:https://github.com/rust-lang/regex:regex" and
38+ path .getResolvedPath ( ) = "<crate::regex::string::Regex>::new" and
39+ this .asExpr ( ) = call .getArgument ( 0 ) and
40+ not this .asExpr ( ) instanceof LiteralExprCfgNode
41+ )
42+ }
3543 }
36- }
3744
38- private class MadRegexInjectionSink extends RegexInjectionSink {
39- MadRegexInjectionSink ( ) { sinkNode ( this , "regex-use" ) }
40- }
45+ /**
46+ * A sink for regular expression injection from model data.
47+ */
48+ private class ModelsAsDataSink extends Sink {
49+ ModelsAsDataSink ( ) { sinkNode ( this , "regex-use" ) }
50+ }
4151
42- /**
43- * A unit class for adding additional flow steps.
44- */
45- class RegexInjectionAdditionalFlowStep extends Unit {
4652 /**
47- * Holds if the step from `node1` to `node2` should be considered a flow
48- * step for paths related to regular expression injection vulnerabilities.
53+ * A unit class for adding additional flow steps.
4954 */
50- abstract predicate step ( DataFlow:: Node node1 , DataFlow:: Node node2 ) ;
51- }
55+ class AdditionalFlowStep extends Unit {
56+ /**
57+ * Holds if the step from `node1` to `node2` should be considered a flow
58+ * step for paths related to regular expression injection vulnerabilities.
59+ */
60+ abstract predicate step ( DataFlow:: Node node1 , DataFlow:: Node node2 ) ;
61+ }
5262
53- /**
54- * An escape barrier for regular expression injection vulnerabilities.
55- */
56- private class RegexInjectionDefaultBarrier extends RegexInjectionBarrier {
57- RegexInjectionDefaultBarrier ( ) {
58- // A barrier is any call to a function named `escape`, in particular this
59- // makes calls to `regex::escape` a barrier.
60- this .asExpr ( )
61- .getExpr ( )
62- .( CallExpr )
63- .getFunction ( )
64- .( PathExpr )
65- .getPath ( )
66- .getSegment ( )
67- .getIdentifier ( )
68- .getText ( ) = "escape"
63+ /**
64+ * An escape barrier for regular expression injection vulnerabilities.
65+ */
66+ private class DefaultBarrier extends Barrier {
67+ DefaultBarrier ( ) {
68+ // A barrier is any call to a function named `escape`, in particular this
69+ // makes calls to `regex::escape` a barrier.
70+ this .asExpr ( )
71+ .getExpr ( )
72+ .( CallExpr )
73+ .getFunction ( )
74+ .( PathExpr )
75+ .getPath ( )
76+ .getSegment ( )
77+ .getIdentifier ( )
78+ .getText ( ) = "escape"
79+ }
6980 }
7081}
0 commit comments