|
| 1 | +/** |
| 2 | + * For internal use only. |
| 3 | + * |
| 4 | + * A taint-tracking configuration for reasoning about XSS through the DOM. |
| 5 | + * Defines shared code used by the XSS Through DOM boosted query. |
| 6 | + */ |
| 7 | + |
| 8 | +private import semmle.javascript.heuristics.SyntacticHeuristics |
| 9 | +private import semmle.javascript.security.dataflow.DomBasedXssCustomizations |
| 10 | +private import semmle.javascript.dataflow.InferredTypes |
| 11 | +private import semmle.javascript.security.dataflow.XssThroughDomCustomizations::XssThroughDom as XssThroughDom |
| 12 | +private import semmle.javascript.security.dataflow.UnsafeJQueryPluginCustomizations::UnsafeJQueryPlugin as UnsafeJQuery |
| 13 | +import AdaptiveThreatModeling |
| 14 | + |
| 15 | +class XssThroughDomAtmConfig extends AtmConfig { |
| 16 | + XssThroughDomAtmConfig() { this = "XssThroughDomAtmConfig" } |
| 17 | + |
| 18 | + override predicate isKnownSource(DataFlow::Node source) { |
| 19 | + source instanceof XssThroughDom::Source |
| 20 | + } |
| 21 | + |
| 22 | + override EndpointType getASinkEndpointType() { result instanceof XssSinkType } |
| 23 | + |
| 24 | + override predicate isSanitizer(DataFlow::Node node) { |
| 25 | + super.isSanitizer(node) or |
| 26 | + node instanceof DomBasedXss::Sanitizer |
| 27 | + } |
| 28 | + |
| 29 | + override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) { |
| 30 | + guard instanceof TypeTestGuard or |
| 31 | + guard instanceof UnsafeJQuery::PropertyPresenceSanitizer or |
| 32 | + guard instanceof UnsafeJQuery::NumberGuard or |
| 33 | + guard instanceof PrefixStringSanitizer or |
| 34 | + guard instanceof QuoteGuard or |
| 35 | + guard instanceof ContainsHtmlGuard |
| 36 | + } |
| 37 | + |
| 38 | + override predicate isSanitizerEdge(DataFlow::Node pred, DataFlow::Node succ) { |
| 39 | + DomBasedXss::isOptionallySanitizedEdge(pred, succ) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * A test of form `typeof x === "something"`, preventing `x` from being a string in some cases. |
| 45 | + * |
| 46 | + * This sanitizer helps prune infeasible paths in type-overloaded functions. |
| 47 | + */ |
| 48 | +class TypeTestGuard extends TaintTracking::SanitizerGuardNode, DataFlow::ValueNode { |
| 49 | + override EqualityTest astNode; |
| 50 | + Expr operand; |
| 51 | + boolean polarity; |
| 52 | + |
| 53 | + TypeTestGuard() { |
| 54 | + exists(TypeofTag tag | TaintTracking::isTypeofGuard(astNode, operand, tag) | |
| 55 | + // typeof x === "string" sanitizes `x` when it evaluates to false |
| 56 | + tag = "string" and |
| 57 | + polarity = astNode.getPolarity().booleanNot() |
| 58 | + or |
| 59 | + // typeof x === "object" sanitizes `x` when it evaluates to true |
| 60 | + tag != "string" and |
| 61 | + polarity = astNode.getPolarity() |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + override predicate sanitizes(boolean outcome, Expr e) { |
| 66 | + polarity = outcome and |
| 67 | + e = operand |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +private import semmle.javascript.security.dataflow.Xss::Shared as Shared |
| 72 | + |
| 73 | +private class PrefixStringSanitizer extends TaintTracking::SanitizerGuardNode, |
| 74 | + DomBasedXss::PrefixStringSanitizer { |
| 75 | + PrefixStringSanitizer() { this = this } |
| 76 | +} |
| 77 | + |
| 78 | +private class PrefixString extends DataFlow::FlowLabel, DomBasedXss::PrefixString { |
| 79 | + PrefixString() { this = this } |
| 80 | +} |
| 81 | + |
| 82 | +private class QuoteGuard extends TaintTracking::SanitizerGuardNode, Shared::QuoteGuard { |
| 83 | + QuoteGuard() { this = this } |
| 84 | +} |
| 85 | + |
| 86 | +private class ContainsHtmlGuard extends TaintTracking::SanitizerGuardNode, Shared::ContainsHtmlGuard { |
| 87 | + ContainsHtmlGuard() { this = this } |
| 88 | +} |
0 commit comments