11/**
2- * @name Python Regex DoS
3- * @description Python Regular Expression Denial of Service
2+ * @name Regular expression injection
3+ * @description User input should not be used in regular expressions without first being escaped,
4+ * otherwise a malicious user may be able to inject an expression that could require
5+ * exponential time on certain inputs.
46 * @kind path-problem
57 * @problem.severity error
6- * @id python/regex-dos
7- * @tags experimental
8- * security
8+ * @id python/regex-injection
9+ * @tags security
10+ * external/cwe/cwe-730
911 * external/cwe/cwe-400
1012 */
1113
14+ // determine precision above
1215import python
1316import semmle.python.dataflow.new.RemoteFlowSources
1417import semmle.python.dataflow.new.DataFlow
1518import semmle.python.dataflow.new.TaintTracking
1619import semmle.python.dataflow.new.internal.TaintTrackingPublic
1720import DataFlow:: PathGraph
1821
22+ // Should this be moved to a different structure? (For other queries to be able to use it)
1923class ReMethods extends string {
2024 ReMethods ( ) {
2125 this = "match" or
@@ -49,8 +53,8 @@ class CompiledRegex extends DataFlow::Node {
4953 }
5054}
5155
52- class RegexDoSSink extends DataFlow:: Node {
53- RegexDoSSink ( ) { this instanceof DirectRegex or this instanceof CompiledRegex }
56+ class RegexInjectionSink extends DataFlow:: Node {
57+ RegexInjectionSink ( ) { this instanceof DirectRegex or this instanceof CompiledRegex }
5458}
5559
5660class EscapeSanitizer extends DataFlow:: Node {
@@ -66,17 +70,17 @@ class EscapeSanitizer extends DataFlow::Node {
6670 }
6771}
6872
69- class RegexDoSFlowConfig extends TaintTracking:: Configuration {
70- RegexDoSFlowConfig ( ) { this = "RegexDoSFlowConfig " }
73+ class RegexInjectionFlowConfig extends TaintTracking:: Configuration {
74+ RegexInjectionFlowConfig ( ) { this = "RegexInjectionFlowConfig " }
7175
7276 override predicate isSource ( DataFlow:: Node source ) { source instanceof RemoteFlowSource }
7377
74- override predicate isSink ( DataFlow:: Node sink ) { sink instanceof RegexDoSSink }
78+ override predicate isSink ( DataFlow:: Node sink ) { sink instanceof RegexInjectionSink }
7579
7680 override predicate isSanitizer ( DataFlow:: Node sanitizer ) { sanitizer instanceof EscapeSanitizer }
7781}
7882
79- from RegexDoSFlowConfig config , DataFlow:: PathNode source , DataFlow:: PathNode sink
83+ from RegexInjectionFlowConfig config , DataFlow:: PathNode source , DataFlow:: PathNode sink
8084where config .hasFlowPath ( source , sink )
81- select sink .getNode ( ) , source , sink , "$@ regex operation includes $@." , sink . getNode ( ) , "This ",
82- source .getNode ( ) , "a user-provided value"
85+ select sink .getNode ( ) , source , sink , "$@ regular expression is constructed from a $@." ,
86+ sink .getNode ( ) , "This" , source . getNode ( ) , " user-provided value"
0 commit comments