|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about [immutable](https://www.npmjs.com/package/immutable). |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +/** |
| 8 | + * Provides classes implementing data-flow for Immutable. |
| 9 | + * |
| 10 | + * The implemention rely on the flowsteps implemented in `Collections.qll`. |
| 11 | + */ |
| 12 | +private module Immutable { |
| 13 | + /** |
| 14 | + * An API entrypoint for the global `Immutable` variable. |
| 15 | + */ |
| 16 | + private class ImmutableGlobalEntry extends API::EntryPoint { |
| 17 | + ImmutableGlobalEntry() { this = "ImmutableGlobalEntry" } |
| 18 | + |
| 19 | + override DataFlow::SourceNode getAUse() { result = DataFlow::globalVarRef("Immutable") } |
| 20 | + |
| 21 | + override DataFlow::Node getARhs() { none() } |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * An import of the `Immutable` library. |
| 26 | + */ |
| 27 | + API::Node immutableImport() { |
| 28 | + result = API::moduleImport("immutable") |
| 29 | + or |
| 30 | + result = API::root().getASuccessor(any(ImmutableGlobalEntry i)) |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * An instance of any immutable collection. |
| 35 | + * |
| 36 | + * This predicate keeps track of which values in the program are Immutable collections. |
| 37 | + */ |
| 38 | + API::Node immutableCollection() { |
| 39 | + // keep this predicate in sync with the constructors defined in `storeStep`/`step`. |
| 40 | + result = |
| 41 | + immutableImport() |
| 42 | + .getMember(["Map", "OrderedMap", "List", "Stack", "Set", "OrderedSet", "fromJS", "merge"]) |
| 43 | + .getReturn() |
| 44 | + or |
| 45 | + result = immutableImport().getMember("Record").getReturn().getReturn() |
| 46 | + or |
| 47 | + result = |
| 48 | + immutableImport() |
| 49 | + .getMember(["List", "Set", "OrderedSet", "Stack"]) |
| 50 | + .getMember("of") |
| 51 | + .getReturn() |
| 52 | + or |
| 53 | + result = immutableCollection().getMember(["set", "map", "filter", "push", "merge"]).getReturn() |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets the immutable collection where `pred` has been stored using the name `prop`. |
| 58 | + */ |
| 59 | + DataFlow::SourceNode storeStep(DataFlow::Node pred, string prop) { |
| 60 | + // Immutable.Map() and Immutable.fromJS(). |
| 61 | + exists(DataFlow::CallNode call | |
| 62 | + call = immutableImport().getMember(["Map", "OrderedMap", "fromJS"]).getACall() |
| 63 | + | |
| 64 | + pred = call.getOptionArgument(0, prop) and |
| 65 | + result = call |
| 66 | + ) |
| 67 | + or |
| 68 | + // Immutable.List() |
| 69 | + exists(DataFlow::CallNode call, DataFlow::ArrayCreationNode arr | |
| 70 | + call = immutableImport().getMember(["List", "Stack", "Set", "OrderedSet"]).getACall() |
| 71 | + | |
| 72 | + arr = call.getArgument(0).getALocalSource() and |
| 73 | + exists(int i | |
| 74 | + prop = DataFlow::PseudoProperties::arrayElement(i) and |
| 75 | + pred = arr.getElement(i) and |
| 76 | + result = call |
| 77 | + ) |
| 78 | + ) |
| 79 | + or |
| 80 | + // collection.set(key, value) |
| 81 | + exists(DataFlow::CallNode call | call = immutableCollection().getMember("set").getACall() | |
| 82 | + call.getArgument(0).mayHaveStringValue(prop) and |
| 83 | + pred = call.getArgument(1) and |
| 84 | + result = call |
| 85 | + ) |
| 86 | + or |
| 87 | + // list.push(x) |
| 88 | + exists(DataFlow::CallNode call | call = immutableCollection().getMember("push").getACall() | |
| 89 | + pred = call.getArgument(0) and |
| 90 | + result = call and |
| 91 | + prop = DataFlow::PseudoProperties::arrayElement() |
| 92 | + ) |
| 93 | + or |
| 94 | + // Immutable.Record({defaults})({values}). |
| 95 | + exists(API::CallNode factoryCall, API::CallNode recordCall | |
| 96 | + factoryCall = immutableImport().getMember("Record").getACall() and |
| 97 | + recordCall = factoryCall.getReturn().getACall() |
| 98 | + | |
| 99 | + pred = [factoryCall, recordCall].getOptionArgument(0, prop) and |
| 100 | + result = recordCall |
| 101 | + ) |
| 102 | + or |
| 103 | + // List/Set/Stack.of(values) |
| 104 | + exists(API::CallNode call | |
| 105 | + call = |
| 106 | + immutableImport() |
| 107 | + .getMember(["List", "Set", "OrderedSet", "Stack"]) |
| 108 | + .getMember("of") |
| 109 | + .getACall() |
| 110 | + | |
| 111 | + pred = call.getAnArgument() and |
| 112 | + result = call and |
| 113 | + prop = DataFlow::PseudoProperties::arrayElement() |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Gets the value that was stored in the immutable collection `pred` under the name `prop`. |
| 119 | + */ |
| 120 | + DataFlow::Node loadStep(DataFlow::Node pred, string prop) { |
| 121 | + // map.get() |
| 122 | + exists(DataFlow::MethodCallNode call | |
| 123 | + call = immutableCollection().getMember("get").getACall() |
| 124 | + | |
| 125 | + call.getArgument(0).mayHaveStringValue(prop) and |
| 126 | + pred = call.getReceiver() and |
| 127 | + result = call |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Gets an immutable collection that contains all the elements from `pred`. |
| 133 | + */ |
| 134 | + DataFlow::SourceNode step(DataFlow::Node pred) { |
| 135 | + // map.set() / list.push() copies all existing values |
| 136 | + exists(DataFlow::CallNode call | |
| 137 | + call = immutableCollection().getMember(["set", "push"]).getACall() |
| 138 | + | |
| 139 | + pred = call.getReceiver() and |
| 140 | + result = call |
| 141 | + ) |
| 142 | + or |
| 143 | + // toJS()/toList() on any immutable collection converts it to a plain JavaScript object/array (and vice versa for `fromJS`). |
| 144 | + exists(DataFlow::CallNode call | |
| 145 | + call = immutableCollection().getMember(["toJS", "toList"]).getACall() |
| 146 | + | |
| 147 | + pred = call.getReceiver() and |
| 148 | + result = call |
| 149 | + ) |
| 150 | + or |
| 151 | + // Immutable.merge(x, y) |
| 152 | + exists(DataFlow::CallNode call | call = immutableImport().getMember("merge").getACall() | |
| 153 | + pred = call.getAnArgument() and |
| 154 | + result = call |
| 155 | + ) |
| 156 | + or |
| 157 | + // collection.merge(other) |
| 158 | + exists(DataFlow::CallNode call | call = immutableCollection().getMember("merge").getACall() | |
| 159 | + pred = [call.getAnArgument(), call.getReceiver()] and |
| 160 | + result = call |
| 161 | + ) |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * A dataflow step for an immutable collection. |
| 166 | + */ |
| 167 | + class ImmutableConstructionStep extends DataFlow::AdditionalFlowStep { |
| 168 | + ImmutableConstructionStep() { this = [loadStep(_, _), storeStep(_, _), step(_)] } |
| 169 | + |
| 170 | + override predicate loadStep(DataFlow::Node pred, DataFlow::Node succ, string prop) { |
| 171 | + this = loadStep(pred, prop) and |
| 172 | + succ = this |
| 173 | + } |
| 174 | + |
| 175 | + override predicate storeStep(DataFlow::Node pred, DataFlow::SourceNode succ, string prop) { |
| 176 | + this = storeStep(pred, prop) and |
| 177 | + succ = this |
| 178 | + } |
| 179 | + |
| 180 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 181 | + this = step(pred) and |
| 182 | + succ = this |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments