|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * code constructed from library input vulnerabilities, as |
| 4 | + * well as extension points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.ApiGraphs |
| 9 | +private import codeql.ruby.frameworks.core.Gem::Gem as Gem |
| 10 | +private import codeql.ruby.Concepts as Concepts |
| 11 | + |
| 12 | +/** |
| 13 | + * Module containing sources, sinks, and sanitizers for code constructed from library input. |
| 14 | + */ |
| 15 | +module UnsafeCodeConstruction { |
| 16 | + /** A source for code constructed from library input vulnerabilities. */ |
| 17 | + abstract class Source extends DataFlow::Node { } |
| 18 | + |
| 19 | + /** An input parameter to a gem seen as a source. */ |
| 20 | + private class LibraryInputAsSource extends Source instanceof DataFlow::ParameterNode { |
| 21 | + LibraryInputAsSource() { |
| 22 | + this = Gem::getALibraryInput() and |
| 23 | + not this.getName() = "code" |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** A sink for code constructed from library input vulnerabilities. */ |
| 28 | + abstract class Sink extends DataFlow::Node { |
| 29 | + /** |
| 30 | + * Gets the node where the unsafe code is executed. |
| 31 | + */ |
| 32 | + abstract DataFlow::Node getCodeSink(); |
| 33 | + |
| 34 | + /** |
| 35 | + * Gets the type of sink. |
| 36 | + */ |
| 37 | + string getSinkType() { result = "code construction" } |
| 38 | + } |
| 39 | + |
| 40 | + /** Gets a node that is eventually executed as code at `codeExec`. */ |
| 41 | + DataFlow::Node getANodeExecutedAsCode(Concepts::CodeExecution codeExec) { |
| 42 | + result = getANodeExecutedAsCode(TypeTracker::TypeBackTracker::end(), codeExec) |
| 43 | + } |
| 44 | + |
| 45 | + import codeql.ruby.typetracking.TypeTracker as TypeTracker |
| 46 | + |
| 47 | + /** Gets a node that is eventually executed as code at `codeExec`, type-tracked with `t`. */ |
| 48 | + private DataFlow::LocalSourceNode getANodeExecutedAsCode( |
| 49 | + TypeTracker::TypeBackTracker t, Concepts::CodeExecution codeExec |
| 50 | + ) { |
| 51 | + t.start() and |
| 52 | + result = codeExec.getCode().getALocalSource() and |
| 53 | + codeExec.runsArbitraryCode() // methods like `Object.send` is benign here, because of the string-construction the attacker cannot control the entire method name |
| 54 | + or |
| 55 | + exists(TypeTracker::TypeBackTracker t2 | |
| 56 | + result = getANodeExecutedAsCode(t2, codeExec).backtrack(t2, t) |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * A string constructed using a `.join(...)` call, where the resulting string ends up being executed as code. |
| 62 | + */ |
| 63 | + class ArrayJoin extends Sink { |
| 64 | + Concepts::CodeExecution s; |
| 65 | + |
| 66 | + ArrayJoin() { |
| 67 | + exists(DataFlow::CallNode call | |
| 68 | + call.getMethodName() = "join" and |
| 69 | + call.getNumberOfArguments() = 1 and // any string. E.g. ";" or "\n". |
| 70 | + call = getANodeExecutedAsCode(s) and |
| 71 | + this = call.getReceiver() |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + override DataFlow::Node getCodeSink() { result = s } |
| 76 | + |
| 77 | + override string getSinkType() { result = "array" } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * A string constructed from a string-literal (e.g. `"foo #{sink}"`), |
| 82 | + * where the resulting string ends up being executed as a code. |
| 83 | + */ |
| 84 | + class StringInterpolationAsSink extends Sink { |
| 85 | + Concepts::CodeExecution s; |
| 86 | + |
| 87 | + StringInterpolationAsSink() { |
| 88 | + exists(Ast::StringlikeLiteral lit | |
| 89 | + any(DataFlow::Node n | n.asExpr().getExpr() = lit) = getANodeExecutedAsCode(s) and |
| 90 | + this.asExpr().getExpr() = lit.getComponent(_) |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + override DataFlow::Node getCodeSink() { result = s } |
| 95 | + |
| 96 | + override string getSinkType() { result = "string interpolation" } |
| 97 | + } |
| 98 | + |
| 99 | + import codeql.ruby.security.TaintedFormatStringSpecific as TaintedFormat |
| 100 | + |
| 101 | + /** |
| 102 | + * A string constructed from a printf-style call, |
| 103 | + * where the resulting string ends up being executed as a code. |
| 104 | + */ |
| 105 | + class TaintedFormatStringAsSink extends Sink { |
| 106 | + Concepts::CodeExecution s; |
| 107 | + |
| 108 | + TaintedFormatStringAsSink() { |
| 109 | + exists(TaintedFormat::PrintfStyleCall call | |
| 110 | + call = getANodeExecutedAsCode(s) and |
| 111 | + this = [call.getFormatArgument(_), call.getFormatString()] |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + override DataFlow::Node getCodeSink() { result = s } |
| 116 | + |
| 117 | + override string getSinkType() { result = "string format" } |
| 118 | + } |
| 119 | +} |
0 commit comments