|
| 1 | +import javascript |
| 2 | +private import semmle.javascript.dataflow.InferredTypes |
| 3 | + |
| 4 | +/** |
| 5 | + * Classes and predicates for modelling TaintTracking steps for arrays. |
| 6 | + */ |
| 7 | +module ArrayTaintTracking { |
| 8 | + /** |
| 9 | + * A taint propagating data flow edge caused by the builtin array functions. |
| 10 | + */ |
| 11 | + private class ArrayFunctionTaintStep extends TaintTracking::AdditionalTaintStep { |
| 12 | + DataFlow::CallNode call; |
| 13 | + |
| 14 | + ArrayFunctionTaintStep() { this = call } |
| 15 | + |
| 16 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 17 | + arrayFunctionTaintStep(pred, succ, call) |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * A taint propagating data flow edge from `pred` to `succ` caused by a call `call` to a builtin array functions. |
| 23 | + */ |
| 24 | + predicate arrayFunctionTaintStep(DataFlow::Node pred, DataFlow::Node succ, DataFlow::CallNode call) { |
| 25 | + // `array.map(function (elt, i, ary) { ... })`: if `array` is tainted, then so are |
| 26 | + // `elt` and `ary`; similar for `forEach` |
| 27 | + exists(string name, Function f, int i | |
| 28 | + (name = "map" or name = "forEach") and |
| 29 | + (i = 0 or i = 2) and |
| 30 | + call.getArgument(0).analyze().getAValue().(AbstractFunction).getFunction() = f and |
| 31 | + call.(DataFlow::MethodCallNode).getMethodName() = name and |
| 32 | + pred = call.getReceiver() and |
| 33 | + succ = DataFlow::parameterNode(f.getParameter(i)) |
| 34 | + ) |
| 35 | + or |
| 36 | + // `array.map` with tainted return value in callback |
| 37 | + exists(DataFlow::FunctionNode f | |
| 38 | + call.(DataFlow::MethodCallNode).getMethodName() = "map" and |
| 39 | + call.getArgument(0) = f and // Require the argument to be a closure to avoid spurious call/return flow |
| 40 | + pred = f.getAReturn() and |
| 41 | + succ = call |
| 42 | + ) |
| 43 | + or |
| 44 | + // `array.push(e)`, `array.unshift(e)`: if `e` is tainted, then so is `array`. |
| 45 | + exists(string name | |
| 46 | + name = "push" or |
| 47 | + name = "unshift" |
| 48 | + | |
| 49 | + pred = call.getAnArgument() and |
| 50 | + succ.(DataFlow::SourceNode).getAMethodCall(name) = call |
| 51 | + ) |
| 52 | + or |
| 53 | + // `array.push(...e)`, `array.unshift(...e)`: if `e` is tainted, then so is `array`. |
| 54 | + exists(string name | |
| 55 | + name = "push" or |
| 56 | + name = "unshift" |
| 57 | + | |
| 58 | + pred = call.getASpreadArgument() and |
| 59 | + // Make sure we handle reflective calls |
| 60 | + succ = call.getReceiver().getALocalSource() and |
| 61 | + call.getCalleeName() = name |
| 62 | + ) |
| 63 | + or |
| 64 | + // `array.splice(i, del, e)`: if `e` is tainted, then so is `array`. |
| 65 | + exists(string name | name = "splice" | |
| 66 | + pred = call.getArgument(2) and |
| 67 | + succ.(DataFlow::SourceNode).getAMethodCall(name) = call |
| 68 | + ) |
| 69 | + or |
| 70 | + // `e = array.pop()`, `e = array.shift()`, or similar: if `array` is tainted, then so is `e`. |
| 71 | + exists(string name | |
| 72 | + name = "pop" or |
| 73 | + name = "shift" or |
| 74 | + name = "slice" or |
| 75 | + name = "splice" |
| 76 | + | |
| 77 | + call.(DataFlow::MethodCallNode).calls(pred, name) and |
| 78 | + succ = call |
| 79 | + ) |
| 80 | + or |
| 81 | + // `e = Array.from(x)`: if `x` is tainted, then so is `e`. |
| 82 | + call = DataFlow::globalVarRef("Array").getAPropertyRead("from").getACall() and |
| 83 | + pred = call.getAnArgument() and |
| 84 | + succ = call |
| 85 | + or |
| 86 | + // `e = arr1.concat(arr2, arr3)`: if any of the `arr` is tainted, then so is `e`. |
| 87 | + call.(DataFlow::MethodCallNode).calls(pred, "concat") and |
| 88 | + succ = call |
| 89 | + or |
| 90 | + call.(DataFlow::MethodCallNode).getMethodName() = "concat" and |
| 91 | + succ = call and |
| 92 | + pred = call.getAnArgument() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Classes and predicates for modelling data-flow for arrays. |
| 98 | + */ |
| 99 | +private module ArrayDataFlow { |
| 100 | + /** |
| 101 | + * Gets a pseudo-field representing an element inside an array. |
| 102 | + */ |
| 103 | + private string arrayElement() { result = "$arrayElement$" } |
| 104 | + |
| 105 | + /** |
| 106 | + * A step for storing an element on an array using `arr.push(e)` or `arr.unshift(e)`. |
| 107 | + */ |
| 108 | + private class ArrayAppendStep extends DataFlow::AdditionalFlowStep, DataFlow::MethodCallNode { |
| 109 | + ArrayAppendStep() { |
| 110 | + this.getMethodName() = "push" or |
| 111 | + this.getMethodName() = "unshift" |
| 112 | + } |
| 113 | + |
| 114 | + override predicate storeStep(DataFlow::Node element, DataFlow::Node obj, string prop) { |
| 115 | + prop = arrayElement() and |
| 116 | + (element = this.getAnArgument() or element = this.getASpreadArgument()) and |
| 117 | + obj = this.getReceiver().getALocalSource() |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * A step for reading/writing an element from an array inside a for-loop. |
| 123 | + * E.g. a read from `foo[i]` to `bar` in `for(var i = 0; i < arr.length; i++) {bar = foo[i]}`. |
| 124 | + */ |
| 125 | + private class ArrayIndexingStep extends DataFlow::AdditionalFlowStep, DataFlow::Node { |
| 126 | + DataFlow::PropRef read; |
| 127 | + |
| 128 | + ArrayIndexingStep() { |
| 129 | + read = this and |
| 130 | + forex(InferredType type | type = read.getPropertyNameExpr().flow().analyze().getAType() | |
| 131 | + type = TTNumber() |
| 132 | + ) and |
| 133 | + exists(VarAccess i, ExprOrVarDecl init | |
| 134 | + i = read.getPropertyNameExpr() and init = any(ForStmt f).getInit() |
| 135 | + | |
| 136 | + i.getVariable().getADefinition() = init or |
| 137 | + i.getVariable().getADefinition().(VariableDeclarator).getDeclStmt() = init |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + override predicate loadStep(DataFlow::Node obj, DataFlow::Node element, string prop) { |
| 142 | + prop = arrayElement() and |
| 143 | + obj = this.(DataFlow::PropRead).getBase() and |
| 144 | + element = this |
| 145 | + } |
| 146 | + |
| 147 | + override predicate storeStep(DataFlow::Node element, DataFlow::Node obj, string prop) { |
| 148 | + prop = arrayElement() and |
| 149 | + element = this.(DataFlow::PropWrite).getRhs() and |
| 150 | + this = obj.(DataFlow::SourceNode).getAPropertyWrite() |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * A step for retrieving an element from an array using `.pop()` or `.shift()`. |
| 156 | + * E.g. `array.pop()`. |
| 157 | + */ |
| 158 | + private class ArrayPopStep extends DataFlow::AdditionalFlowStep, DataFlow::MethodCallNode { |
| 159 | + ArrayPopStep() { |
| 160 | + getMethodName() = "pop" or |
| 161 | + getMethodName() = "shift" |
| 162 | + } |
| 163 | + |
| 164 | + override predicate loadStep(DataFlow::Node obj, DataFlow::Node element, string prop) { |
| 165 | + prop = arrayElement() and |
| 166 | + obj = this.getReceiver() and |
| 167 | + element = this |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * A step for iterating an array using `map` or `forEach`. |
| 173 | + * |
| 174 | + * Array elements can be loaded from the array `arr` to `e` in e.g: `arr.forEach(e => ...)`. |
| 175 | + * |
| 176 | + * And array elements can be stored into a resulting array using `map(...)`. |
| 177 | + * E.g. in `arr.map(e => foo)`, the resulting array (`arr.map(e => foo)`) will contain the element `foo`. |
| 178 | + * |
| 179 | + * And the second parameter in the callback is the array ifself, so there is a `loadStoreStep` from the array to that second parameter. |
| 180 | + */ |
| 181 | + private class ArrayIteration extends DataFlow::AdditionalFlowStep, DataFlow::MethodCallNode { |
| 182 | + ArrayIteration() { |
| 183 | + this.getMethodName() = "map" or |
| 184 | + this.getMethodName() = "forEach" |
| 185 | + } |
| 186 | + |
| 187 | + override predicate loadStep(DataFlow::Node obj, DataFlow::Node element, string prop) { |
| 188 | + prop = arrayElement() and |
| 189 | + obj = this.getReceiver() and |
| 190 | + element = getCallback(0).getParameter(0) |
| 191 | + } |
| 192 | + |
| 193 | + override predicate storeStep(DataFlow::Node element, DataFlow::Node obj, string prop) { |
| 194 | + this.getMethodName() = "map" and |
| 195 | + prop = arrayElement() and |
| 196 | + element = this.getCallback(0).getAReturn() and |
| 197 | + obj = this |
| 198 | + } |
| 199 | + |
| 200 | + override predicate loadStoreStep(DataFlow::Node pred, DataFlow::Node succ, string prop) { |
| 201 | + prop = arrayElement() and |
| 202 | + pred = this.getReceiver() and |
| 203 | + succ = getCallback(0).getParameter(2) |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * A step for creating an array and storing the elements in the array. |
| 209 | + */ |
| 210 | + private class ArrayCreationStep extends DataFlow::AdditionalFlowStep, DataFlow::Node { |
| 211 | + ArrayCreationStep() { this instanceof DataFlow::ArrayCreationNode } |
| 212 | + |
| 213 | + override predicate storeStep(DataFlow::Node element, DataFlow::Node obj, string prop) { |
| 214 | + prop = arrayElement() and |
| 215 | + element = this.(DataFlow::ArrayCreationNode).getAnElement() and |
| 216 | + obj = this |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * A step modelling that `splice` can insert elements into an array. |
| 222 | + * For example in `array.splice(i, del, e)`: if `e` is tainted, then so is `array |
| 223 | + */ |
| 224 | + private class ArraySpliceStep extends DataFlow::AdditionalFlowStep, DataFlow::MethodCallNode { |
| 225 | + ArraySpliceStep() { this.getMethodName() = "splice" } |
| 226 | + |
| 227 | + override predicate storeStep(DataFlow::Node element, DataFlow::Node obj, string prop) { |
| 228 | + prop = arrayElement() and |
| 229 | + element = getArgument(2) and |
| 230 | + obj = this.getReceiver().getALocalSource() |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * A step for modelling `concat`. |
| 236 | + * For example in `e = arr1.concat(arr2, arr3)`: if any of the `arr` is tainted, then so is `e`. |
| 237 | + */ |
| 238 | + private class ArrayConcatStep extends DataFlow::AdditionalFlowStep, DataFlow::MethodCallNode { |
| 239 | + ArrayConcatStep() { this.getMethodName() = "concat" } |
| 240 | + |
| 241 | + override predicate loadStoreStep(DataFlow::Node pred, DataFlow::Node succ, string prop) { |
| 242 | + prop = arrayElement() and |
| 243 | + (pred = this.getReceiver() or pred = this.getAnArgument()) and |
| 244 | + succ = this |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * A step for modelling that elements from an array `arr` also appear in the result from calling `slice`/`splice`/`filter`. |
| 250 | + */ |
| 251 | + private class ArraySliceStep extends DataFlow::AdditionalFlowStep, DataFlow::MethodCallNode { |
| 252 | + ArraySliceStep() { |
| 253 | + this.getMethodName() = "slice" or |
| 254 | + this.getMethodName() = "splice" or |
| 255 | + this.getMethodName() = "filter" |
| 256 | + } |
| 257 | + |
| 258 | + override predicate loadStoreStep(DataFlow::Node pred, DataFlow::Node succ, string prop) { |
| 259 | + prop = arrayElement() and |
| 260 | + pred = this.getReceiver() and |
| 261 | + succ = this |
| 262 | + } |
| 263 | + } |
| 264 | +} |
0 commit comments