|
| 1 | +/** |
| 2 | + * Provides a taint-tracking configuration for tracking user-controlled objects flowing |
| 3 | + * into a vulnerable `extends` call. |
| 4 | + */ |
| 5 | + |
| 6 | +import javascript |
| 7 | +import semmle.javascript.security.TaintedObject |
| 8 | + |
| 9 | +module PrototypePollution { |
| 10 | + /** |
| 11 | + * Label for wrappers around tainted objects, that is, objects that are |
| 12 | + * not completely user-controlled, but contain a user-controlled object. |
| 13 | + * |
| 14 | + * For example, `options` below is is a tainted wrapper, but is not itself |
| 15 | + * a tainted object: |
| 16 | + * ``` |
| 17 | + * let options = { |
| 18 | + * prefs: { |
| 19 | + * locale: req.query.locale |
| 20 | + * } |
| 21 | + * } |
| 22 | + * ``` |
| 23 | + */ |
| 24 | + module TaintedObjectWrapper { |
| 25 | + private class TaintedObjectWrapper extends DataFlow::FlowLabel { |
| 26 | + TaintedObjectWrapper() { this = "tainted-object-wrapper" } |
| 27 | + } |
| 28 | + |
| 29 | + TaintedObjectWrapper label() { any() } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * A data flow source for prototype pollution. |
| 34 | + */ |
| 35 | + abstract class Source extends DataFlow::Node { |
| 36 | + /** |
| 37 | + * Gets the type of data coming from this source. |
| 38 | + */ |
| 39 | + abstract DataFlow::FlowLabel getAFlowLabel(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * A data flow sink for prototype pollution. |
| 44 | + */ |
| 45 | + abstract class Sink extends DataFlow::Node { |
| 46 | + /** |
| 47 | + * Gets the type of data that can taint this sink. |
| 48 | + */ |
| 49 | + abstract DataFlow::FlowLabel getAFlowLabel(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * A taint tracking configuration for user-controlled objects flowing into deep `extend` calls, |
| 54 | + * leading to prototype pollution. |
| 55 | + */ |
| 56 | + class Configuration extends TaintTracking::Configuration { |
| 57 | + Configuration() { this = "PrototypePollution" } |
| 58 | + |
| 59 | + override predicate isSource(DataFlow::Node node, DataFlow::FlowLabel label) { |
| 60 | + node.(Source).getAFlowLabel() = label |
| 61 | + } |
| 62 | + |
| 63 | + override predicate isSink(DataFlow::Node node, DataFlow::FlowLabel label) { |
| 64 | + node.(Sink).getAFlowLabel() = label |
| 65 | + } |
| 66 | + |
| 67 | + override predicate isAdditionalFlowStep( |
| 68 | + DataFlow::Node src, DataFlow::Node dst, DataFlow::FlowLabel inlbl, DataFlow::FlowLabel outlbl |
| 69 | + ) { |
| 70 | + TaintedObject::step(src, dst, inlbl, outlbl) |
| 71 | + or |
| 72 | + // Track objects are wrapped in other objects |
| 73 | + exists(DataFlow::PropWrite write | |
| 74 | + src = write.getRhs() and |
| 75 | + inlbl = TaintedObject::label() and |
| 76 | + dst = write.getBase().getALocalSource() and |
| 77 | + outlbl = TaintedObjectWrapper::label() |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode node) { |
| 82 | + node instanceof TaintedObject::SanitizerGuard |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * A user-controlled string value, as a source of prototype pollution. |
| 88 | + * |
| 89 | + * Note that values from this type of source will need to flow through a `JSON.parse` call |
| 90 | + * in order to be flagged for prototype pollution. |
| 91 | + */ |
| 92 | + private class RemoteFlowAsSource extends Source { |
| 93 | + RemoteFlowAsSource() { this instanceof RemoteFlowSource } |
| 94 | + |
| 95 | + override DataFlow::FlowLabel getAFlowLabel() { result = DataFlow::FlowLabel::data() } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * A source of user-controlled objects. |
| 100 | + */ |
| 101 | + private class TaintedObjectSource extends Source { |
| 102 | + TaintedObjectSource() { this instanceof TaintedObject::Source } |
| 103 | + |
| 104 | + override DataFlow::FlowLabel getAFlowLabel() { result = TaintedObject::label() } |
| 105 | + } |
| 106 | + |
| 107 | + string getModuleName(ExtendCall call) { |
| 108 | + call = DataFlow::moduleImport(result).getACall() or |
| 109 | + call = DataFlow::moduleMember(result, _).getACall() |
| 110 | + } |
| 111 | + |
| 112 | + class DeepExtendSink extends Sink { |
| 113 | + ExtendCall call; |
| 114 | + |
| 115 | + DeepExtendSink() { |
| 116 | + this = call.getASourceOperand() and |
| 117 | + call.isDeep() and |
| 118 | + exists(string moduleName | moduleName = getModuleName(call) | |
| 119 | + moduleName = "lodash" + any(string s) or |
| 120 | + moduleName = "just-extend" or |
| 121 | + moduleName = "extend" or |
| 122 | + moduleName = "extend2" or |
| 123 | + moduleName = "node.extend" or |
| 124 | + moduleName = "merge" or |
| 125 | + moduleName = "smart-extend" or |
| 126 | + moduleName = "js-extend" or |
| 127 | + moduleName = "deep" or |
| 128 | + moduleName = "defaults-deep" |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + override DataFlow::FlowLabel getAFlowLabel() { |
| 133 | + result = TaintedObject::label() |
| 134 | + or |
| 135 | + result = TaintedObjectWrapper::label() |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments