|
| 1 | +/** |
| 2 | + * Provides classes for reasoning about composed functions. |
| 3 | + */ |
| 4 | +import javascript |
| 5 | + |
| 6 | +/** |
| 7 | + * A function composed from a collection of functions. |
| 8 | + */ |
| 9 | +private class ComposedFunction extends DataFlow::CallNode { |
| 10 | + |
| 11 | + ComposedFunction() { |
| 12 | + exists (string name | |
| 13 | + name = "just-compose" or |
| 14 | + name = "compose-function" | |
| 15 | + this = DataFlow::moduleImport(name).getACall() |
| 16 | + ) or |
| 17 | + this = LodashUnderscore::member("flow").getACall() |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Gets the ith function in this composition. |
| 22 | + */ |
| 23 | + DataFlow::FunctionNode getFunction(int i) { |
| 24 | + result.flowsTo(getArgument(i)) |
| 25 | + } |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A taint step for a composed function. |
| 31 | + */ |
| 32 | +private class ComposedFunctionTaintStep extends TaintTracking::AdditionalTaintStep { |
| 33 | + |
| 34 | + ComposedFunction composed; |
| 35 | + |
| 36 | + DataFlow::CallNode call; |
| 37 | + |
| 38 | + ComposedFunctionTaintStep() { |
| 39 | + call = composed.getACall() and |
| 40 | + this = call |
| 41 | + } |
| 42 | + |
| 43 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 44 | + exists (int fnIndex, DataFlow::FunctionNode fn | |
| 45 | + fn = composed.getFunction(fnIndex) | |
| 46 | + // flow out of the composed call |
| 47 | + fnIndex = composed.getNumArgument() - 1 and |
| 48 | + pred = fn.getAReturn() and |
| 49 | + succ = this |
| 50 | + or |
| 51 | + if fnIndex = 0 then |
| 52 | + // flow into the first composed function |
| 53 | + exists (int callArgIndex | |
| 54 | + pred = call.getArgument(callArgIndex) and |
| 55 | + succ = fn.getParameter(callArgIndex) |
| 56 | + ) |
| 57 | + else |
| 58 | + // flow through the composed functions |
| 59 | + exists (DataFlow::FunctionNode predFn | |
| 60 | + predFn = composed.getFunction(fnIndex - 1) | |
| 61 | + pred = predFn.getAReturn() and |
| 62 | + succ = fn.getParameter(0) |
| 63 | + ) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments