|
| 1 | +/** |
| 2 | + * Provides a taint tracking configuration for reasoning about |
| 3 | + * prototype-polluting assignments. |
| 4 | + * |
| 5 | + * Note, for performance reasons: only import this file if |
| 6 | + * `PrototypePollutingAssignment::Configuration` is needed, otherwise |
| 7 | + * `PrototypePollutingAssignmentCustomizations` should be imported instead. |
| 8 | + */ |
| 9 | + |
| 10 | +private import javascript |
| 11 | +private import semmle.javascript.DynamicPropertyAccess |
| 12 | + |
| 13 | +module PrototypePollutingAssignment { |
| 14 | + private import PrototypePollutingAssignmentCustomizations::PrototypePollutingAssignment |
| 15 | + |
| 16 | + // Materialize flow labels |
| 17 | + private class ConcreteObjectPrototype extends ObjectPrototype { } |
| 18 | + |
| 19 | + /** A taint-tracking configuration for reasoning about prototype-polluting assignments. */ |
| 20 | + class Configuration extends TaintTracking::Configuration { |
| 21 | + Configuration() { this = "PrototypePollutingAssignment" } |
| 22 | + |
| 23 | + override predicate isSource(DataFlow::Node node) { node instanceof Source } |
| 24 | + |
| 25 | + override predicate isSink(DataFlow::Node node, DataFlow::FlowLabel lbl) { |
| 26 | + node.(Sink).getAFlowLabel() = lbl |
| 27 | + } |
| 28 | + |
| 29 | + override predicate isSanitizer(DataFlow::Node node) { |
| 30 | + node instanceof Sanitizer |
| 31 | + or |
| 32 | + // Concatenating with a string will in practice prevent the string `__proto__` from arising. |
| 33 | + node instanceof StringOps::ConcatenationRoot |
| 34 | + } |
| 35 | + |
| 36 | + override predicate isAdditionalFlowStep( |
| 37 | + DataFlow::Node pred, DataFlow::Node succ, DataFlow::FlowLabel inlbl, |
| 38 | + DataFlow::FlowLabel outlbl |
| 39 | + ) { |
| 40 | + // Step from x -> obj[x] while switching to the ObjectPrototype label |
| 41 | + // (If `x` can have the value `__proto__` then the result can be Object.prototype) |
| 42 | + exists(DataFlow::PropRead read | |
| 43 | + pred = read.getPropertyNameExpr().flow() and |
| 44 | + succ = read and |
| 45 | + inlbl.isTaint() and |
| 46 | + outlbl instanceof ObjectPrototype and |
| 47 | + // Exclude cases where the property name came from a property enumeration. |
| 48 | + // If the property name is an own property of the base object, the read won't |
| 49 | + // return Object.prototype. |
| 50 | + not read = any(EnumeratedPropName n).getASourceProp() and |
| 51 | + // Exclude cases where the read has no prototype, or a prototype other than Object.prototype. |
| 52 | + not read = prototypeLessObject().getAPropertyRead() and |
| 53 | + // Exclude cases where this property has just been assigned to |
| 54 | + not read.(DynamicPropRead).hasDominatingAssignment() |
| 55 | + ) |
| 56 | + or |
| 57 | + // Same as above, but for property projection. |
| 58 | + exists(PropertyProjection proj | |
| 59 | + proj.isSingletonProjection() and |
| 60 | + pred = proj.getASelector() and |
| 61 | + succ = proj and |
| 62 | + inlbl.isTaint() and |
| 63 | + outlbl instanceof ObjectPrototype |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + override predicate isLabeledBarrier(DataFlow::Node node, DataFlow::FlowLabel lbl) { |
| 68 | + // Don't propagate the receiver into method calls, as the method lookup will fail on Object.prototype. |
| 69 | + node = any(DataFlow::MethodCallNode m).getReceiver() and |
| 70 | + lbl instanceof ObjectPrototype |
| 71 | + } |
| 72 | + |
| 73 | + override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) { |
| 74 | + guard instanceof PropertyPresenceCheck or |
| 75 | + guard instanceof InExprCheck or |
| 76 | + guard instanceof InstanceofCheck or |
| 77 | + guard instanceof IsArrayCheck or |
| 78 | + guard instanceof EqualityCheck |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** Gets a data flow node referring to an object created with `Object.create`. */ |
| 83 | + DataFlow::SourceNode prototypeLessObject() { |
| 84 | + result = prototypeLessObject(DataFlow::TypeTracker::end()) |
| 85 | + } |
| 86 | + |
| 87 | + private DataFlow::SourceNode prototypeLessObject(DataFlow::TypeTracker t) { |
| 88 | + t.start() and |
| 89 | + // We assume the argument to Object.create is not Object.prototype, since most |
| 90 | + // users wouldn't bother to call Object.create in that case. |
| 91 | + result = DataFlow::globalVarRef("Object").getAMemberCall("create") |
| 92 | + or |
| 93 | + // Allow use of AdditionalFlowSteps and AdditionalTaintSteps to track a bit further |
| 94 | + exists(DataFlow::Node mid | |
| 95 | + prototypeLessObject(t.continue()).flowsTo(mid) and |
| 96 | + any(DataFlow::AdditionalFlowStep s).step(mid, result) |
| 97 | + ) |
| 98 | + or |
| 99 | + exists(DataFlow::TypeTracker t2 | result = prototypeLessObject(t2).track(t2, t)) |
| 100 | + } |
| 101 | + |
| 102 | + /** Holds if `Object.prototype` has a member named `prop`. */ |
| 103 | + private predicate isPropertyPresentOnObjectPrototype(string prop) { |
| 104 | + exists(ExternalInstanceMemberDecl decl | |
| 105 | + decl.getBaseName() = "Object" and |
| 106 | + decl.getName() = prop |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + /** A check of form `e.prop` where `prop` is not present on `Object.prototype`. */ |
| 111 | + private class PropertyPresenceCheck extends TaintTracking::LabeledSanitizerGuardNode, |
| 112 | + DataFlow::ValueNode { |
| 113 | + override PropAccess astNode; |
| 114 | + |
| 115 | + PropertyPresenceCheck() { not isPropertyPresentOnObjectPrototype(astNode.getPropertyName()) } |
| 116 | + |
| 117 | + override predicate sanitizes(boolean outcome, Expr e, DataFlow::FlowLabel label) { |
| 118 | + e = astNode.getBase() and |
| 119 | + outcome = true and |
| 120 | + label instanceof ObjectPrototype |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** A check of form `"prop" in e` where `prop` is not present on `Object.prototype`. */ |
| 125 | + private class InExprCheck extends TaintTracking::LabeledSanitizerGuardNode, DataFlow::ValueNode { |
| 126 | + override InExpr astNode; |
| 127 | + |
| 128 | + InExprCheck() { |
| 129 | + not isPropertyPresentOnObjectPrototype(astNode.getLeftOperand().getStringValue()) |
| 130 | + } |
| 131 | + |
| 132 | + override predicate sanitizes(boolean outcome, Expr e, DataFlow::FlowLabel label) { |
| 133 | + e = astNode.getRightOperand() and |
| 134 | + outcome = true and |
| 135 | + label instanceof ObjectPrototype |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** A check of form `e instanceof X`, which is always false for `Object.prototype`. */ |
| 140 | + private class InstanceofCheck extends TaintTracking::LabeledSanitizerGuardNode, |
| 141 | + DataFlow::ValueNode { |
| 142 | + override InstanceofExpr astNode; |
| 143 | + |
| 144 | + override predicate sanitizes(boolean outcome, Expr e, DataFlow::FlowLabel label) { |
| 145 | + e = astNode.getLeftOperand() and |
| 146 | + outcome = true and |
| 147 | + label instanceof ObjectPrototype |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** A call to `Array.isArray`, which is false for `Object.prototype`. */ |
| 152 | + private class IsArrayCheck extends TaintTracking::LabeledSanitizerGuardNode, DataFlow::CallNode { |
| 153 | + IsArrayCheck() { this = DataFlow::globalVarRef("Array").getAMemberCall("isArray") } |
| 154 | + |
| 155 | + override predicate sanitizes(boolean outcome, Expr e, DataFlow::FlowLabel label) { |
| 156 | + e = getArgument(0).asExpr() and |
| 157 | + outcome = true and |
| 158 | + label instanceof ObjectPrototype |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sanitizer guard of form `x !== "__proto__"`. |
| 164 | + */ |
| 165 | + private class EqualityCheck extends TaintTracking::SanitizerGuardNode, DataFlow::ValueNode { |
| 166 | + override EqualityTest astNode; |
| 167 | + |
| 168 | + EqualityCheck() { astNode.getAnOperand().getStringValue() = "__proto__" } |
| 169 | + |
| 170 | + override predicate sanitizes(boolean outcome, Expr e) { |
| 171 | + e = astNode.getAnOperand() and |
| 172 | + outcome = astNode.getPolarity().booleanNot() |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments