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