|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about regexp |
| 3 | + * injection vulnerabilities, as well as extension points for adding your own. |
| 4 | + */ |
| 5 | + |
| 6 | +private import ruby |
| 7 | +private import codeql.ruby.DataFlow |
| 8 | +private import codeql.ruby.Concepts |
| 9 | +private import codeql.ruby.Frameworks |
| 10 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 11 | +private import codeql.ruby.dataflow.BarrierGuards |
| 12 | +private import codeql.ruby.ApiGraphs |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides default sources, sinks and sanitizers for detecting |
| 16 | + * regexp injection vulnerabilities, as well as extension points for |
| 17 | + * adding your own. |
| 18 | + */ |
| 19 | +module RegExpInjection { |
| 20 | + /** |
| 21 | + * A data flow source for regexp injection vulnerabilities. |
| 22 | + */ |
| 23 | + abstract class Source extends DataFlow::Node { } |
| 24 | + |
| 25 | + /** |
| 26 | + * A data flow sink for regexp injection vulnerabilities. |
| 27 | + */ |
| 28 | + abstract class Sink extends DataFlow::Node { } |
| 29 | + |
| 30 | + /** |
| 31 | + * A sanitizer guard for regexp injection vulnerabilities. |
| 32 | + */ |
| 33 | + abstract class SanitizerGuard extends DataFlow::BarrierGuard { } |
| 34 | + |
| 35 | + /** |
| 36 | + * A data flow sanitized for regexp injection vulnerabilities. |
| 37 | + */ |
| 38 | + abstract class Sanitizer extends DataFlow::Node { } |
| 39 | + |
| 40 | + /** |
| 41 | + * A source of remote user input, considered as a flow source. |
| 42 | + */ |
| 43 | + class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { } |
| 44 | + |
| 45 | + /** A regexp literal, considered as a flow sink. */ |
| 46 | + class RegExpLiteralAsSink extends Sink { |
| 47 | + RegExpLiteralAsSink() { this.asExpr().getExpr() instanceof RegExpLiteral } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * The first argument of a call to `Regexp.new` or `Regexp.compile`, |
| 52 | + * considered as a flow sink. |
| 53 | + */ |
| 54 | + class ConstructedRegExpAsSink extends Sink { |
| 55 | + ConstructedRegExpAsSink() { |
| 56 | + exists(API::Node regexp, DataFlow::CallNode callNode | |
| 57 | + regexp = API::getTopLevelMember("Regexp") and |
| 58 | + (callNode = regexp.getAnInstantiation() or callNode = regexp.getAMethodCall("compile")) and |
| 59 | + this = callNode.getArgument(0) |
| 60 | + ) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * A comparison with a constant string, considered as a sanitizer-guard. |
| 66 | + */ |
| 67 | + class StringConstCompareAsSanitizerGuard extends SanitizerGuard, StringConstCompare { } |
| 68 | + |
| 69 | + /** |
| 70 | + * An inclusion check against an array of constant strings, considered as a |
| 71 | + * sanitizer-guard. |
| 72 | + */ |
| 73 | + class StringConstArrayInclusionCallAsSanitizerGuard extends SanitizerGuard, |
| 74 | + StringConstArrayInclusionCall { } |
| 75 | + |
| 76 | + /** |
| 77 | + * A call to `Regexp.escape` (or its alias, `Regexp.quote`), considered as a |
| 78 | + * sanitizer. |
| 79 | + */ |
| 80 | + class RegexpEscapeSanitization extends Sanitizer { |
| 81 | + RegexpEscapeSanitization() { |
| 82 | + this = API::getTopLevelMember("Regexp").getAMethodCall(["escape", "quote"]) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments