|
| 1 | +/** |
| 2 | + * @name Incorrect suffix check |
| 3 | + * @description Using indexOf to implement endsWith functionality is error-prone if the -1 case is not explicitly handled. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id js/incorrect-suffix-check |
| 8 | + * @tags security |
| 9 | + * correctness |
| 10 | + * external/cwe/cwe-020 |
| 11 | + */ |
| 12 | +import javascript |
| 13 | + |
| 14 | +/** |
| 15 | + * A call to `indexOf` or `lastIndexOf`. |
| 16 | + */ |
| 17 | +class IndexOfCall extends DataFlow::MethodCallNode { |
| 18 | + IndexOfCall() { |
| 19 | + exists (string name | name = getMethodName() | |
| 20 | + name = "indexOf" or |
| 21 | + name = "lastIndexOf") and |
| 22 | + getNumArgument() = 1 |
| 23 | + } |
| 24 | + |
| 25 | + /** Gets the receiver or argument of this call. */ |
| 26 | + DataFlow::Node getAnOperand() { |
| 27 | + result = getReceiver() or |
| 28 | + result = getArgument(0) |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets an `indexOf` call with the same receiver, argument, and method name, including this call itself. |
| 33 | + */ |
| 34 | + IndexOfCall getAnEquivalentIndexOfCall() { |
| 35 | + result.getReceiver().getALocalSource() = this.getReceiver().getALocalSource() and |
| 36 | + result.getArgument(0).getALocalSource() = this.getArgument(0).getALocalSource() and |
| 37 | + result.getMethodName() = this.getMethodName() |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets an expression that refers to the return value of this call. |
| 42 | + */ |
| 43 | + Expr getAUse() { |
| 44 | + this.flowsToExpr(result) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Gets a source of the given string value, or one of its operands if it is a concatenation. |
| 50 | + */ |
| 51 | +DataFlow::SourceNode getStringSource(DataFlow::Node node) { |
| 52 | + result = node.getALocalSource() |
| 53 | + or |
| 54 | + result = StringConcatenation::getAnOperand(node).getALocalSource() |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * An expression that takes the length of a string literal. |
| 59 | + */ |
| 60 | +class LiteralLengthExpr extends DotExpr { |
| 61 | + LiteralLengthExpr() { |
| 62 | + getPropertyName() = "length" and |
| 63 | + getBase() instanceof StringLiteral |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Gets the value of the string literal whose length is taken. |
| 68 | + */ |
| 69 | + string getBaseValue() { |
| 70 | + result = getBase().getStringValue() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Holds if `length` is derived from the length of the given `indexOf`-operand. |
| 76 | + */ |
| 77 | +predicate isDerivedFromLength(DataFlow::Node length, DataFlow::Node operand) { |
| 78 | + exists (IndexOfCall call | operand = call.getAnOperand() | |
| 79 | + length = getStringSource(operand).getAPropertyRead("length") |
| 80 | + or |
| 81 | + // Find a literal length with the same string constant |
| 82 | + exists (LiteralLengthExpr lengthExpr | |
| 83 | + lengthExpr.getContainer() = call.getContainer() and |
| 84 | + lengthExpr.getBaseValue() = operand.asExpr().getStringValue() and |
| 85 | + length = lengthExpr.flow()) |
| 86 | + or |
| 87 | + // Find an integer constants that equals the length of string constant |
| 88 | + exists (Expr lengthExpr | |
| 89 | + lengthExpr.getContainer() = call.getContainer() and |
| 90 | + lengthExpr.getIntValue() = operand.asExpr().getStringValue().length() and |
| 91 | + length = lengthExpr.flow()) |
| 92 | + ) |
| 93 | + or |
| 94 | + isDerivedFromLength(length.getAPredecessor(), operand) |
| 95 | + or |
| 96 | + exists (SubExpr sub | |
| 97 | + isDerivedFromLength(sub.getAnOperand().flow(), operand) and |
| 98 | + length = sub.flow()) |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * An equality comparison of form `A.indexOf(B) === A.length - B.length` or similar. |
| 103 | + * |
| 104 | + * We assume A and B are strings, even A and/or B could be also be arrays. |
| 105 | + * The comparison with the length rarely occurs for arrays, however. |
| 106 | + */ |
| 107 | +class UnsafeIndexOfComparison extends EqualityTest { |
| 108 | + IndexOfCall indexOf; |
| 109 | + DataFlow::Node testedValue; |
| 110 | + |
| 111 | + UnsafeIndexOfComparison() { |
| 112 | + hasOperands(indexOf.getAUse(), testedValue.asExpr()) and |
| 113 | + isDerivedFromLength(testedValue, indexOf.getReceiver()) and |
| 114 | + isDerivedFromLength(testedValue, indexOf.getArgument(0)) and |
| 115 | + |
| 116 | + // Ignore cases like `x.indexOf("/") === x.length - 1` that can only be bypassed if `x` is the empty string. |
| 117 | + // Sometimes strings are just known to be non-empty from the context, and it is unlikely to be a security issue, |
| 118 | + // since it's obviously not a domain name check. |
| 119 | + not indexOf.getArgument(0).mayHaveStringValue(any(string s | s.length() = 1)) and |
| 120 | + |
| 121 | + // Relative string length comparison, such as A.length > B.length, or (A.length - B.length) > 0 |
| 122 | + not exists (RelationalComparison compare | |
| 123 | + isDerivedFromLength(compare.getAnOperand().flow(), indexOf.getReceiver()) and |
| 124 | + isDerivedFromLength(compare.getAnOperand().flow(), indexOf.getArgument(0)) |
| 125 | + ) and |
| 126 | + |
| 127 | + // Check for indexOf being -1 |
| 128 | + not exists (EqualityTest test, Expr minusOne | |
| 129 | + test.hasOperands(indexOf.getAnEquivalentIndexOfCall().getAUse(), minusOne) and |
| 130 | + minusOne.getIntValue() = -1 |
| 131 | + ) and |
| 132 | + |
| 133 | + // Check for indexOf being >1, or >=0, etc |
| 134 | + not exists (RelationalComparison test | |
| 135 | + test.getGreaterOperand() = indexOf.getAnEquivalentIndexOfCall().getAUse() and |
| 136 | + exists (int value | value = test.getLesserOperand().getIntValue() | |
| 137 | + value >= 0 |
| 138 | + or |
| 139 | + not test.isInclusive() and |
| 140 | + value = -1) |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + IndexOfCall getIndexOf() { |
| 145 | + result = indexOf |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +from UnsafeIndexOfComparison comparison |
| 150 | +select comparison, "This suffix check is missing a length comparison to correctly handle " + comparison.getIndexOf().getMethodName() + " returning -1." |
0 commit comments