|
| 1 | +/** |
| 2 | + * Contains classes for recognizing array and string inclusion tests. |
| 3 | + */ |
| 4 | + |
| 5 | +private import ruby |
| 6 | +private import codeql.ruby.DataFlow |
| 7 | +private import codeql.ruby.controlflow.CfgNodes |
| 8 | + |
| 9 | +/** |
| 10 | + * An expression that checks if an element is contained in an array |
| 11 | + * or is a substring of another string. |
| 12 | + * |
| 13 | + * Examples: |
| 14 | + * ``` |
| 15 | + * A.include?(B) |
| 16 | + * A.index(B) != nil |
| 17 | + * A.index(B) >= 0 |
| 18 | + * ``` |
| 19 | + */ |
| 20 | +class InclusionTest extends DataFlow::Node instanceof InclusionTest::Range { |
| 21 | + /** Gets the `A` in `A.include?(B)`. */ |
| 22 | + final DataFlow::Node getContainerNode() { result = super.getContainerNode() } |
| 23 | + |
| 24 | + /** Gets the `B` in `A.include?(B)`. */ |
| 25 | + final DataFlow::Node getContainedNode() { result = super.getContainedNode() } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets the polarity of the check. |
| 29 | + * |
| 30 | + * If the polarity is `false` the check returns `true` if the container does not contain |
| 31 | + * the given element. |
| 32 | + */ |
| 33 | + final boolean getPolarity() { result = super.getPolarity() } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Contains classes for recognizing array and string inclusion tests. |
| 38 | + */ |
| 39 | +module InclusionTest { |
| 40 | + /** |
| 41 | + * An expression that is equivalent to `A.include?(B)` or `!A.include?(B)`. |
| 42 | + * |
| 43 | + * Note that this also includes calls to the array method named `include?`. |
| 44 | + */ |
| 45 | + abstract class Range extends DataFlow::Node { |
| 46 | + /** Gets the `A` in `A.include?(B)`. */ |
| 47 | + abstract DataFlow::Node getContainerNode(); |
| 48 | + |
| 49 | + /** Gets the `B` in `A.include?(B)`. */ |
| 50 | + abstract DataFlow::Node getContainedNode(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Gets the polarity of the check. |
| 54 | + * |
| 55 | + * If the polarity is `false` the check returns `true` if the container does not contain |
| 56 | + * the given element. |
| 57 | + */ |
| 58 | + boolean getPolarity() { result = true } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A call to a method named `include?`, assumed to refer to `String.include?` |
| 63 | + * or `Array.include?`. |
| 64 | + */ |
| 65 | + private class Includes_Native extends Range, DataFlow::CallNode { |
| 66 | + Includes_Native() { |
| 67 | + this.getMethodName() = "include?" and |
| 68 | + strictcount(this.getArgument(_)) = 1 |
| 69 | + } |
| 70 | + |
| 71 | + override DataFlow::Node getContainerNode() { result = this.getReceiver() } |
| 72 | + |
| 73 | + override DataFlow::Node getContainedNode() { result = this.getArgument(0) } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * A check of form `A.index(B) != nil`, `A.index(B) >= 0`, or similar. |
| 78 | + */ |
| 79 | + private class Includes_IndexOfComparison extends Range, DataFlow::Node { |
| 80 | + private DataFlow::CallNode indexOf; |
| 81 | + private boolean polarity; |
| 82 | + |
| 83 | + Includes_IndexOfComparison() { |
| 84 | + exists(ExprCfgNode index, ExprNodes::ComparisonOperationCfgNode comparison, int value | |
| 85 | + indexOf.asExpr() = comparison.getAnOperand() and |
| 86 | + index = comparison.getAnOperand() and |
| 87 | + this.asExpr() = comparison and |
| 88 | + // one operand is of the form `whitelist.index(x)` |
| 89 | + indexOf.getMethodName() = "index" and |
| 90 | + // and the other one is 0 or -1 |
| 91 | + ( |
| 92 | + value = index.getConstantValue().getInt() and value = 0 |
| 93 | + or |
| 94 | + index.getConstantValue().isNil() and value = -1 |
| 95 | + ) |
| 96 | + | |
| 97 | + value = -1 and polarity = false and comparison.getExpr() instanceof CaseEqExpr |
| 98 | + or |
| 99 | + value = -1 and polarity = false and comparison.getExpr() instanceof EqExpr |
| 100 | + or |
| 101 | + value = -1 and polarity = true and comparison.getExpr() instanceof NEExpr |
| 102 | + or |
| 103 | + exists(RelationalOperation op | op = comparison.getExpr() | |
| 104 | + exists(Expr lesser, Expr greater | |
| 105 | + op.getLesserOperand() = lesser and |
| 106 | + op.getGreaterOperand() = greater |
| 107 | + | |
| 108 | + polarity = true and |
| 109 | + greater = indexOf.asExpr().getExpr() and |
| 110 | + ( |
| 111 | + value = 0 and op.isInclusive() |
| 112 | + or |
| 113 | + value = -1 and not op.isInclusive() |
| 114 | + ) |
| 115 | + or |
| 116 | + polarity = false and |
| 117 | + lesser = indexOf.asExpr().getExpr() and |
| 118 | + ( |
| 119 | + value = -1 and op.isInclusive() |
| 120 | + or |
| 121 | + value = 0 and not op.isInclusive() |
| 122 | + ) |
| 123 | + ) |
| 124 | + ) |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + override DataFlow::Node getContainerNode() { result = indexOf.getReceiver() } |
| 129 | + |
| 130 | + override DataFlow::Node getContainedNode() { result = indexOf.getArgument(0) } |
| 131 | + |
| 132 | + override boolean getPolarity() { result = polarity } |
| 133 | + } |
| 134 | +} |
0 commit comments