|
| 1 | +/** |
| 2 | + * Provides classes for working with [async](https://www.npmjs.com/package/async). |
| 3 | + */ |
| 4 | +import javascript |
| 5 | + |
| 6 | +module AsyncPackage { |
| 7 | + /** |
| 8 | + * Gets a reference the given member of the `async` or `async-es` package. |
| 9 | + */ |
| 10 | + pragma[noopt] |
| 11 | + DataFlow::SourceNode member(string name) { |
| 12 | + result = DataFlow::moduleMember("async", name) or |
| 13 | + result = DataFlow::moduleMember("async-es", name) |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Gets a reference to the given member or one of its `Limit` or `Series` variants. |
| 18 | + * |
| 19 | + * For example, `memberVariant("map")` finds references to `map`, `mapLimit`, and `mapSeries`. |
| 20 | + */ |
| 21 | + DataFlow::SourceNode memberVariant(string name) { |
| 22 | + result = member(name) or |
| 23 | + result = member(name + "Limit") or |
| 24 | + result = member(name + "Series") |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * A call to `async.waterfall`. |
| 29 | + */ |
| 30 | + class Waterfall extends DataFlow::InvokeNode { |
| 31 | + Waterfall() { |
| 32 | + this = member("waterfall").getACall() |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets the array of tasks, if it can be found. |
| 37 | + */ |
| 38 | + DataFlow::ArrayCreationNode getTaskArray() { |
| 39 | + result.flowsTo(getArgument(0)) |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the callback to invoke after the last task in the array completes. |
| 44 | + */ |
| 45 | + DataFlow::FunctionNode getFinalCallback() { |
| 46 | + result.flowsTo(getArgument(1)) |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets the `n`th task, if it can be found. |
| 51 | + */ |
| 52 | + DataFlow::FunctionNode getTask(int n) { |
| 53 | + result.flowsTo(getTaskArray().getElement(n)) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets the number of tasks. |
| 58 | + */ |
| 59 | + int getNumTasks() { |
| 60 | + result = strictcount(getTaskArray().getAnElement()) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets the last parameter declared by the given function, unless that's a rest parameter. |
| 66 | + */ |
| 67 | + private DataFlow::ParameterNode getLastParameter(DataFlow::FunctionNode function) { |
| 68 | + not function.hasRestParameter() and |
| 69 | + result = function.getParameter(function.getNumParameter() - 1) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * An invocation of the callback in a waterfall task, passing arguments to the next waterfall task. |
| 74 | + * |
| 75 | + * Such a callback has the form `callback(err, result1, result2, ...)`. The error is propagated |
| 76 | + * to the first parameter of the final callback, while `result1, result2, ...` are propagated to |
| 77 | + * the parameters of the following task. |
| 78 | + */ |
| 79 | + private class WaterfallNextTaskCall extends DataFlow::AdditionalPartialInvokeNode { |
| 80 | + Waterfall waterfall; |
| 81 | + int n; |
| 82 | + |
| 83 | + WaterfallNextTaskCall() { |
| 84 | + this = getLastParameter(waterfall.getTask(n)).getACall() |
| 85 | + } |
| 86 | + |
| 87 | + override predicate isPartialArgument(DataFlow::Node callback, DataFlow::Node argument, int index) { |
| 88 | + // Pass results to next task |
| 89 | + index >= 0 and |
| 90 | + argument = getArgument(index + 1) and |
| 91 | + callback = waterfall.getTask(n + 1) |
| 92 | + or |
| 93 | + // For the last task, pass results to the final callback |
| 94 | + index >= 1 and |
| 95 | + n = waterfall.getNumTasks() - 1 and |
| 96 | + argument = getArgument(index) and |
| 97 | + callback = waterfall.getFinalCallback() |
| 98 | + or |
| 99 | + // Always pass error to the final callback |
| 100 | + index = 0 and |
| 101 | + argument = getArgument(0) and |
| 102 | + callback = waterfall.getFinalCallback() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * A call that iterates over a collection with asynchronous operations. |
| 108 | + */ |
| 109 | + class IterationCall extends DataFlow::InvokeNode { |
| 110 | + string name; |
| 111 | + |
| 112 | + IterationCall() { |
| 113 | + this = memberVariant(name).getACall() and |
| 114 | + ( |
| 115 | + name = "concat" or |
| 116 | + name = "detect" or |
| 117 | + name = "each" or |
| 118 | + name = "eachOf" or |
| 119 | + name = "forEach" or |
| 120 | + name = "forEachOf" or |
| 121 | + name = "every" or |
| 122 | + name = "filter" or |
| 123 | + name = "groupBy" or |
| 124 | + name = "map" or |
| 125 | + name = "mapValues" or |
| 126 | + name = "reduce" or |
| 127 | + name = "reduceRight" or |
| 128 | + name = "reject" or |
| 129 | + name = "some" or |
| 130 | + name = "sortBy" or |
| 131 | + name = "transform" |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Gets the name of the iteration call, without the `Limit` or `Series` suffix. |
| 137 | + */ |
| 138 | + string getName() { result = name } |
| 139 | + |
| 140 | + /** |
| 141 | + * Gets the node holding the collection being iterated over. |
| 142 | + */ |
| 143 | + DataFlow::Node getCollection() { |
| 144 | + result = getArgument(0) |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Gets the node holding the function being called for each element in the collection. |
| 149 | + */ |
| 150 | + DataFlow::Node getIteratorCallback() { |
| 151 | + result = getArgument(getNumArgument() - 2) |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Gets the node holding the function being invoked after iteration is complete. |
| 156 | + */ |
| 157 | + DataFlow::Node getFinalCallback() { |
| 158 | + result = getArgument(getNumArgument() - 1) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * A taint step from the collection into the iterator callback of an iteration call. |
| 164 | + * |
| 165 | + * For example: `data -> item` in `async.each(data, (item, cb) => {})`. |
| 166 | + */ |
| 167 | + private class IterationInputTaintStep extends TaintTracking::AdditionalTaintStep, IterationCall { |
| 168 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 169 | + exists (DataFlow::FunctionNode iteratee | |
| 170 | + iteratee = getIteratorCallback() and // Require a closure to avoid spurious call/return mismatch. |
| 171 | + pred = getCollection() and |
| 172 | + succ = iteratee.getParameter(0)) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * A taint step from the return value of an iterator callback to the result of the iteration |
| 178 | + * call. |
| 179 | + * |
| 180 | + * For example: `item + taint()` -> result` in `async.map(data, (item, cb) => cb(null, item + taint()), (err, result) => {})`. |
| 181 | + */ |
| 182 | + private class IterationOutputTaintStep extends TaintTracking::AdditionalTaintStep, IterationCall { |
| 183 | + IterationOutputTaintStep() { |
| 184 | + name = "concat" or |
| 185 | + name = "map" or |
| 186 | + name = "reduce" or |
| 187 | + name = "reduceRight" |
| 188 | + } |
| 189 | + |
| 190 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 191 | + exists (DataFlow::FunctionNode iteratee, DataFlow::FunctionNode final, int i | |
| 192 | + iteratee = getIteratorCallback().getALocalSource() and |
| 193 | + final = getFinalCallback() and // Require a closure to avoid spurious call/return mismatch. |
| 194 | + pred = getLastParameter(iteratee).getACall().getArgument(i) and |
| 195 | + succ = final.getParameter(i)) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * A taint step from the input of an iteration call, directly to its output. |
| 201 | + * |
| 202 | + * For example: `data -> result` in `async.sortBy(data, orderingFn, (err, result) => {})`. |
| 203 | + */ |
| 204 | + private class IterationPreserveTaintStep extends TaintTracking::AdditionalTaintStep, IterationCall { |
| 205 | + IterationPreserveTaintStep() { |
| 206 | + name = "sortBy" |
| 207 | + |
| 208 | + // We don't currently include `filter` and `reject` as they could act as sanitizers. |
| 209 | + } |
| 210 | + |
| 211 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 212 | + exists (DataFlow::FunctionNode final | |
| 213 | + final = getFinalCallback() and // Require a closure to avoid spurious call/return mismatch. |
| 214 | + pred = getCollection() and |
| 215 | + succ = final.getParameter(1)) |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments