|
| 1 | +private import javascript |
| 2 | + |
| 3 | +private import FlowSteps as FlowSteps |
| 4 | +private import semmle.javascript.internal.CachedStages |
| 5 | + |
| 6 | +cached |
| 7 | +private module Cached { |
| 8 | + private predicate forwardsParameter( |
| 9 | + DataFlow::FunctionNode function, int i, DataFlow::CallNode call |
| 10 | + ) { |
| 11 | + exists(DataFlow::ParameterNode parameter | parameter = function.getParameter(i) | |
| 12 | + not parameter.isRestParameter() and |
| 13 | + parameter.flowsTo(call.getArgument(i)) |
| 14 | + or |
| 15 | + parameter.isRestParameter() and |
| 16 | + parameter.flowsTo(call.getASpreadArgument()) |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + cached |
| 21 | + private module Stage { |
| 22 | + // Forces the module to be computed as part of the type-tracking stage. |
| 23 | + cached |
| 24 | + predicate forceStage() { |
| 25 | + Stages::TypeTracking::ref() |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Holds if the function in `succ` forwards all its arguments to a call to `pred` and returns |
| 31 | + * its result. This can thus be seen as a step `pred -> succ` used for tracking function values |
| 32 | + * through "wrapper functions", since the `succ` function partially replicates behavior of `pred`. |
| 33 | + * |
| 34 | + * Examples: |
| 35 | + * ```js |
| 36 | + * function f(x) { |
| 37 | + * return g(x); // step: g -> f |
| 38 | + * } |
| 39 | + * |
| 40 | + * function doExec(x) { |
| 41 | + * console.log(x); |
| 42 | + * return exec(x); // step: exec -> doExec |
| 43 | + * } |
| 44 | + * |
| 45 | + * function doEither(x, y) { |
| 46 | + * if (x > y) { |
| 47 | + * return foo(x, y); // step: foo -> doEither |
| 48 | + * } else { |
| 49 | + * return bar(x, y); // step: bar -> doEither |
| 50 | + * } |
| 51 | + * } |
| 52 | + * |
| 53 | + * function wrapWithLogging(f) { |
| 54 | + * return (x) => { |
| 55 | + * console.log(x); |
| 56 | + * return f(x); // step: f -> anonymous function |
| 57 | + * } |
| 58 | + * } |
| 59 | + * wrapWithLogging(g); // step: g -> wrapWithLogging(g) |
| 60 | + * ``` |
| 61 | + */ |
| 62 | + cached |
| 63 | + predicate functionForwardingStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 64 | + exists(DataFlow::FunctionNode function, DataFlow::CallNode call | |
| 65 | + call.flowsTo(function.getReturnNode()) and |
| 66 | + forall(int i | exists([call.getArgument(i), function.getParameter(i)]) | |
| 67 | + forwardsParameter(function, i, call) |
| 68 | + ) and |
| 69 | + pred = call.getCalleeNode() and |
| 70 | + succ = function |
| 71 | + ) |
| 72 | + or |
| 73 | + // Given a generic wrapper function like, |
| 74 | + // |
| 75 | + // function wrap(f) { return (x, y) => f(x, y) }; |
| 76 | + // |
| 77 | + // add steps through calls to that function: `g -> wrap(g)` |
| 78 | + exists(DataFlow::FunctionNode wrapperFunction, DataFlow::SourceNode param, DataFlow::Node paramUse | |
| 79 | + FlowSteps::argumentPassing(succ, pred, wrapperFunction.getFunction(), param) and |
| 80 | + param.flowsTo(paramUse) and |
| 81 | + functionForwardingStep(paramUse, wrapperFunction.getReturnNode().getALocalSource()) |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Holds if the function in `succ` forwards all its arguments to a call to `pred`. |
| 87 | + * This can thus be seen as a step `pred -> succ` used for tracking function values |
| 88 | + * through "wrapper functions", since the `succ` function partially replicates behavior of `pred`. |
| 89 | + * |
| 90 | + * This is similar to `functionForwardingStep` except the innermost forwarding call does not |
| 91 | + * need flow to the return value; this can be useful for tracking callback-style functions |
| 92 | + * where the result tends to be unused. |
| 93 | + * |
| 94 | + * Examples: |
| 95 | + * ```js |
| 96 | + * function f(x, callback) { |
| 97 | + * g(x, callback); // step: g -> f |
| 98 | + * } |
| 99 | + * |
| 100 | + * function doExec(x, callback) { |
| 101 | + * console.log(x); |
| 102 | + * exec(x, callback); // step: exec -> doExec |
| 103 | + * } |
| 104 | + * |
| 105 | + * function doEither(x, y) { |
| 106 | + * if (x > y) { |
| 107 | + * return foo(x, y); // step: foo -> doEither |
| 108 | + * } else { |
| 109 | + * return bar(x, y); // step: bar -> doEither |
| 110 | + * } |
| 111 | + * } |
| 112 | + * |
| 113 | + * function wrapWithLogging(f) { |
| 114 | + * return (x) => { |
| 115 | + * console.log(x); |
| 116 | + * return f(x); // step: f -> anonymous function |
| 117 | + * } |
| 118 | + * } |
| 119 | + * wrapWithLogging(g); // step: g -> wrapWithLogging(g) |
| 120 | + * ``` |
| 121 | + */ |
| 122 | + cached |
| 123 | + predicate functionOneWayForwardingStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 124 | + exists(DataFlow::FunctionNode function, DataFlow::CallNode call | |
| 125 | + call.getContainer() = function.getFunction() and |
| 126 | + forall(int i | exists(function.getParameter(i)) | forwardsParameter(function, i, call)) and |
| 127 | + pred = call.getCalleeNode() and |
| 128 | + succ = function |
| 129 | + ) |
| 130 | + or |
| 131 | + // Given a generic wrapper function like, |
| 132 | + // |
| 133 | + // function wrap(f) { return (x, y) => f(x, y) }; |
| 134 | + // |
| 135 | + // add steps through calls to that function: `g -> wrap(g)` |
| 136 | + exists(DataFlow::FunctionNode wrapperFunction, DataFlow::SourceNode param, DataFlow::Node paramUse | |
| 137 | + FlowSteps::argumentPassing(succ, pred, wrapperFunction.getFunction(), param) and |
| 138 | + param.flowsTo(paramUse) and |
| 139 | + functionOneWayForwardingStep(paramUse, wrapperFunction.getReturnNode().getALocalSource()) |
| 140 | + ) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +import Cached |
0 commit comments