|
| 1 | +/** |
| 2 | + * Provides classes and predicates to reason about regular expression injection |
| 3 | + * vulnerabilities. |
| 4 | + */ |
| 5 | + |
| 6 | +private import codeql.util.Unit |
| 7 | +private import rust |
| 8 | +private import codeql.rust.dataflow.DataFlow |
| 9 | +private import codeql.rust.controlflow.CfgNodes |
| 10 | +private import codeql.rust.dataflow.FlowSink |
| 11 | + |
| 12 | +/** |
| 13 | + * A data flow sink for regular expression injection vulnerabilities. |
| 14 | + */ |
| 15 | +abstract class RegexInjectionSink extends DataFlow::Node { } |
| 16 | + |
| 17 | +/** |
| 18 | + * A barrier for regular expression injection vulnerabilities. |
| 19 | + */ |
| 20 | +abstract class RegexInjectionBarrier extends DataFlow::Node { } |
| 21 | + |
| 22 | +/** A sink for `a` in `Regex::new(a)` when `a` is not a literal. */ |
| 23 | +private class NewRegexInjectionSink extends RegexInjectionSink { |
| 24 | + NewRegexInjectionSink() { |
| 25 | + exists(CallExprCfgNode call, PathExpr path | |
| 26 | + path = call.getFunction().getExpr() and |
| 27 | + path.getResolvedCrateOrigin() = "repo:https://github.com/rust-lang/regex:regex" and |
| 28 | + path.getResolvedPath() = "<crate::regex::string::Regex>::new" and |
| 29 | + this.asExpr() = call.getArgument(0) and |
| 30 | + not this.asExpr() instanceof LiteralExprCfgNode |
| 31 | + ) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +private class MadRegexInjectionSink extends RegexInjectionSink { |
| 36 | + MadRegexInjectionSink() { sinkNode(this, "regex-use") } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A unit class for adding additional flow steps. |
| 41 | + */ |
| 42 | +class RegexInjectionAdditionalFlowStep extends Unit { |
| 43 | + /** |
| 44 | + * Holds if the step from `node1` to `node2` should be considered a flow |
| 45 | + * step for paths related to regular expression injection vulnerabilities. |
| 46 | + */ |
| 47 | + abstract predicate step(DataFlow::Node node1, DataFlow::Node node2); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * An escape barrier for regular expression injection vulnerabilities. |
| 52 | + */ |
| 53 | +private class RegexInjectionDefaultBarrier extends RegexInjectionBarrier { |
| 54 | + RegexInjectionDefaultBarrier() { |
| 55 | + // A barrier is any call to a function named `escape`, in particular this |
| 56 | + // makes calls to `regex::escape` a barrier. |
| 57 | + this.asExpr() |
| 58 | + .getExpr() |
| 59 | + .(CallExpr) |
| 60 | + .getFunction() |
| 61 | + .(PathExpr) |
| 62 | + .getPath() |
| 63 | + .getPart() |
| 64 | + .getNameRef() |
| 65 | + .getText() = "escape" |
| 66 | + } |
| 67 | +} |
0 commit comments