|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * missing full-anchored regular expressions, as well as extension |
| 4 | + * points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 9 | +private import codeql.ruby.frameworks.core.Gem::Gem as Gem |
| 10 | +private import codeql.ruby.Regexp as Regexp |
| 11 | +private import codeql.ruby.security.regexp.HostnameRegexp |
| 12 | +private import codeql.ruby.Concepts |
| 13 | + |
| 14 | +private class RegExpTerm = Regexp::RegExpTerm; |
| 15 | + |
| 16 | +/** |
| 17 | + * Provides default sources, sinks and sanitizers for detecting |
| 18 | + * missing full-anchored regular expressions, as well as extension |
| 19 | + * points for adding your own. |
| 20 | + */ |
| 21 | +module MissingFullAnchor { |
| 22 | + /** A data flow source for missing full-anchored regular expressions. */ |
| 23 | + abstract class Source extends DataFlow::Node { |
| 24 | + /** Gets a description of the source. */ |
| 25 | + string describe() { result = "user-provided value" } |
| 26 | + } |
| 27 | + |
| 28 | + /** A data flow sink for missing full-anchored regular expressions. */ |
| 29 | + abstract class Sink extends DataFlow::Node { |
| 30 | + /** Gets the node where the regexp computation happens. */ |
| 31 | + abstract DataFlow::Node getCallNode(); |
| 32 | + |
| 33 | + /** Gets the regular expression term. */ |
| 34 | + abstract RegExpTerm getRegex(); |
| 35 | + } |
| 36 | + |
| 37 | + /** A sanitizer for missing full-anchored regular expressions. */ |
| 38 | + abstract class Sanitizer extends DataFlow::Node { } |
| 39 | + |
| 40 | + private class RemoteFlowAsSource extends Source instanceof RemoteFlowSource { } |
| 41 | + |
| 42 | + private class LibrayInputAsSource extends Source { |
| 43 | + LibrayInputAsSource() { this = Gem::getALibraryInput() } |
| 44 | + |
| 45 | + override string describe() { result = "library input" } |
| 46 | + } |
| 47 | + |
| 48 | + private RegExpTerm getABadlyAnchoredTerm() { |
| 49 | + exists(RegExpTerm left | left.getRootTerm() = result | |
| 50 | + left.(Regexp::RegExpAnchor).getChar() = "^" and |
| 51 | + isLeftArmTerm(left) |
| 52 | + ) and |
| 53 | + exists(RegExpTerm right | right.getRootTerm() = result | |
| 54 | + right.(Regexp::RegExpAnchor).getChar() = "$" and |
| 55 | + isRightArmTerm(right) |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + private class DefaultSink extends Sink { |
| 60 | + RegexExecution exec; |
| 61 | + RegExpTerm term; |
| 62 | + |
| 63 | + DefaultSink() { |
| 64 | + exec.getString() = this and |
| 65 | + term = Regexp::getTermForExecution(exec) and |
| 66 | + term = getABadlyAnchoredTerm() and |
| 67 | + // looks like a sanitizer, not just input transformation |
| 68 | + exists(Ast::ConditionalExpr ifExpr | |
| 69 | + [ifExpr.getCondition(), ifExpr.getCondition().(Ast::UnaryLogicalOperation).getOperand()] = |
| 70 | + exec.asExpr().getExpr() and |
| 71 | + ifExpr.getBranch(_).(Ast::MethodCall).getMethodName() = "raise" |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + override DataFlow::Node getCallNode() { result = exec } |
| 76 | + |
| 77 | + override RegExpTerm getRegex() { result = term } |
| 78 | + } |
| 79 | +} |
0 commit comments