|
| 1 | +/** |
| 2 | + * @name Python Regex DoS |
| 3 | + * @description Python Regular Expression Denial of Service |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @id python/regex-dos |
| 7 | + * @tags experimental |
| 8 | + * security |
| 9 | + * external/cwe/cwe-400 |
| 10 | + */ |
| 11 | + |
| 12 | +import python |
| 13 | +import semmle.python.dataflow.new.RemoteFlowSources |
| 14 | +import semmle.python.dataflow.new.DataFlow |
| 15 | +import semmle.python.dataflow.new.TaintTracking |
| 16 | +import semmle.python.dataflow.new.internal.TaintTrackingPublic |
| 17 | +import DataFlow::PathGraph |
| 18 | + |
| 19 | +class ReMethods extends string { |
| 20 | + ReMethods() { |
| 21 | + this = "match" or |
| 22 | + this = "fullmatch" or |
| 23 | + this = "search" or |
| 24 | + this = "split" or |
| 25 | + this = "findall" or |
| 26 | + this = "finditer" |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class DirectRegex extends DataFlow::Node { |
| 31 | + DirectRegex() { |
| 32 | + exists(string reMethod, CallNode reCall | |
| 33 | + reMethod instanceof ReMethods and |
| 34 | + reCall = Value::named("re." + reMethod).getACall() and |
| 35 | + this.asExpr() = reCall.getArg(0).getNode() |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class CompiledRegex extends DataFlow::Node { |
| 41 | + CompiledRegex() { |
| 42 | + exists(CallNode patternCall, SsaVariable patternVar, CallNode reMethodCall | |
| 43 | + patternCall = Value::named("re.compile").getACall() and |
| 44 | + patternVar.getDefinition().getImmediateDominator() = patternCall and |
| 45 | + patternVar.getAUse().getNode() = reMethodCall.getNode().getFunc().(Attribute).getObject() and |
| 46 | + reMethodCall.getNode().getFunc().(Attribute).getName() instanceof ReMethods and |
| 47 | + this.asExpr() = patternCall.getArg(0).getNode() |
| 48 | + ) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class RegexDoSSink extends DataFlow::Node { |
| 53 | + RegexDoSSink() { this instanceof DirectRegex or this instanceof CompiledRegex } |
| 54 | +} |
| 55 | + |
| 56 | +class EscapeSanitizer extends DataFlow::Node { |
| 57 | + EscapeSanitizer() { |
| 58 | + exists(Call c | |
| 59 | + ( |
| 60 | + // avoid flow through any %escape% function |
| 61 | + c.getFunc().(Attribute).getName().matches("%escape%") or // something.%escape%() |
| 62 | + c.getFunc().(Name).getId().matches("%escape%") // %escape%() |
| 63 | + ) and |
| 64 | + this.asExpr() = c |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class RegexDoSFlowConfig extends TaintTracking::Configuration { |
| 70 | + RegexDoSFlowConfig() { this = "RegexDoSFlowConfig" } |
| 71 | + |
| 72 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 73 | + |
| 74 | + override predicate isSink(DataFlow::Node sink) { sink instanceof RegexDoSSink } |
| 75 | + |
| 76 | + override predicate isSanitizer(DataFlow::Node sanitizer) { sanitizer instanceof EscapeSanitizer } |
| 77 | +} |
| 78 | + |
| 79 | +from RegexDoSFlowConfig config, DataFlow::PathNode source, DataFlow::PathNode sink |
| 80 | +where config.hasFlowPath(source, sink) |
| 81 | +select sink.getNode(), source, sink, "$@ regex operation includes $@.", sink.getNode(), "This", |
| 82 | + source.getNode(), "a user-provided value" |
0 commit comments