|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import javascript |
7 | | -import semmle.javascript.security.dataflow.RemoteFlowSources |
8 | | -import semmle.javascript.frameworks.jQuery |
9 | 7 |
|
10 | 8 | module DomBasedXss { |
11 | | - /** |
12 | | - * A data flow source for XSS vulnerabilities. |
13 | | - */ |
14 | | - abstract class Source extends DataFlow::Node { } |
15 | | - |
16 | | - /** |
17 | | - * A data flow sink for XSS vulnerabilities. |
18 | | - */ |
19 | | - abstract class Sink extends DataFlow::Node { |
20 | | - /** |
21 | | - * Gets the kind of vulnerability to report in the alert message. |
22 | | - * |
23 | | - * Defaults to `Cross-site scripting`, but may be overriden for sinks |
24 | | - * that do not allow script injection, but injection of other undesirable HTML elements. |
25 | | - */ |
26 | | - string getVulnerabilityKind() { result = "Cross-site scripting" } |
27 | | - } |
28 | | - |
29 | | - /** |
30 | | - * A sanitizer for XSS vulnerabilities. |
31 | | - */ |
32 | | - abstract class Sanitizer extends DataFlow::Node { } |
| 9 | + import Xss::DomBasedXss |
33 | 10 |
|
34 | 11 | /** |
35 | 12 | * A taint-tracking configuration for reasoning about XSS. |
@@ -68,146 +45,4 @@ module DomBasedXss { |
68 | 45 | class LocationSource extends Source, DataFlow::ValueNode { |
69 | 46 | LocationSource() { isDocumentURL(astNode) } |
70 | 47 | } |
71 | | - |
72 | | - /** |
73 | | - * An expression whose value is interpreted as HTML |
74 | | - * and may be inserted into the DOM through a library. |
75 | | - */ |
76 | | - class LibrarySink extends Sink, DataFlow::ValueNode { |
77 | | - LibrarySink() { |
78 | | - // call to a jQuery method that interprets its argument as HTML |
79 | | - exists(JQueryMethodCall call | call.interpretsArgumentAsHtml(astNode) | |
80 | | - // either the argument is always interpreted as HTML |
81 | | - not call.interpretsArgumentAsSelector(astNode) |
82 | | - or |
83 | | - // or it doesn't start with something other than `<`, and so at least |
84 | | - // _may_ be interpreted as HTML |
85 | | - not exists(DataFlow::Node prefix, string strval | |
86 | | - isPrefixOfJQueryHtmlString(astNode, prefix) and |
87 | | - strval = prefix.asExpr().getStringValue() and |
88 | | - not strval.regexpMatch("\\s*<.*") |
89 | | - ) and |
90 | | - not isDocumentURL(astNode) |
91 | | - ) |
92 | | - or |
93 | | - // call to an Angular method that interprets its argument as HTML |
94 | | - any(AngularJS::AngularJSCall call).interpretsArgumentAsHtml(this.asExpr()) |
95 | | - or |
96 | | - // call to a WinJS function that interprets its argument as HTML |
97 | | - exists(DataFlow::MethodCallNode mcn, string m | |
98 | | - m = "setInnerHTMLUnsafe" or m = "setOuterHTMLUnsafe" |
99 | | - | |
100 | | - mcn.getMethodName() = m and |
101 | | - this = mcn.getArgument(1) |
102 | | - ) |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Holds if `prefix` is a prefix of `htmlString`, which may be intepreted as |
108 | | - * HTML by a jQuery method. |
109 | | - */ |
110 | | - private predicate isPrefixOfJQueryHtmlString(Expr htmlString, DataFlow::Node prefix) { |
111 | | - any(JQueryMethodCall call).interpretsArgumentAsHtml(htmlString) and |
112 | | - prefix = htmlString.flow() |
113 | | - or |
114 | | - exists(DataFlow::Node pred | isPrefixOfJQueryHtmlString(htmlString, pred) | |
115 | | - prefix = StringConcatenation::getFirstOperand(pred) |
116 | | - or |
117 | | - prefix = pred.getAPredecessor() |
118 | | - ) |
119 | | - } |
120 | | - |
121 | | - /** |
122 | | - * An expression whose value is interpreted as HTML or CSS |
123 | | - * and may be inserted into the DOM. |
124 | | - */ |
125 | | - class DomSink extends Sink { |
126 | | - DomSink() { |
127 | | - // Call to a DOM function that inserts its argument into the DOM |
128 | | - any(DomMethodCallExpr call).interpretsArgumentsAsHTML(this.asExpr()) |
129 | | - or |
130 | | - // Assignment to a dangerous DOM property |
131 | | - exists(DomPropWriteNode pw | |
132 | | - pw.interpretsValueAsHTML() and |
133 | | - this = DataFlow::valueNode(pw.getRhs()) |
134 | | - ) |
135 | | - or |
136 | | - // `html` or `source.html` properties of React Native `WebView` |
137 | | - exists(ReactNative::WebViewElement webView, DataFlow::SourceNode source | |
138 | | - source = webView or |
139 | | - source = webView.getAPropertyWrite("source").getRhs().getALocalSource() |
140 | | - | |
141 | | - this = source.getAPropertyWrite("html").getRhs() |
142 | | - ) |
143 | | - } |
144 | | - } |
145 | | - |
146 | | - /** |
147 | | - * An expression whose value is interpreted as HTML by a DOMParser. |
148 | | - */ |
149 | | - class DomParserSink extends Sink { |
150 | | - DomParserSink() { |
151 | | - exists(DataFlow::GlobalVarRefNode domParser | |
152 | | - domParser.getName() = "DOMParser" and |
153 | | - this = domParser.getAnInstantiation().getAMethodCall("parseFromString").getArgument(0) |
154 | | - ) |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - /** |
159 | | - * A React `dangerouslySetInnerHTML` attribute, viewed as an XSS sink. |
160 | | - * |
161 | | - * Any write to the `__html` property of an object assigned to this attribute |
162 | | - * is considered an XSS sink. |
163 | | - */ |
164 | | - class DangerouslySetInnerHtmlSink extends Sink, DataFlow::ValueNode { |
165 | | - DangerouslySetInnerHtmlSink() { |
166 | | - exists(DataFlow::Node danger, DataFlow::SourceNode valueSrc | |
167 | | - exists(JSXAttribute attr | |
168 | | - attr.getName() = "dangerouslySetInnerHTML" and |
169 | | - attr.getValue() = danger.asExpr() |
170 | | - ) |
171 | | - or |
172 | | - exists(ReactElementDefinition def, DataFlow::ObjectLiteralNode props | |
173 | | - props.flowsTo(def.getProps()) and |
174 | | - props.hasPropertyWrite("dangerouslySetInnerHTML", danger) |
175 | | - ) |
176 | | - | |
177 | | - valueSrc.flowsTo(danger) and |
178 | | - valueSrc.hasPropertyWrite("__html", this) |
179 | | - ) |
180 | | - } |
181 | | - } |
182 | | - |
183 | | - /** |
184 | | - * The HTML body of an email, viewed as an XSS sink. |
185 | | - */ |
186 | | - class EmailHtmlBodySink extends Sink { |
187 | | - EmailHtmlBodySink() { this = any(EmailSender sender).getHtmlBody() } |
188 | | - |
189 | | - override string getVulnerabilityKind() { result = "HTML injection" } |
190 | | - } |
191 | | - |
192 | | - |
193 | | - /** |
194 | | - * A write to the `template` option of a Vue instance, viewed as an XSS sink. |
195 | | - */ |
196 | | - class VueTemplateSink extends DomBasedXss::Sink { |
197 | | - VueTemplateSink() { this = any(Vue::Instance i).getTemplate() } |
198 | | - } |
199 | | - |
200 | | - /** |
201 | | - * The tag name argument to the `createElement` parameter of the |
202 | | - * `render` method of a Vue instance, viewed as an XSS sink. |
203 | | - */ |
204 | | - class VueCreateElementSink extends DomBasedXss::Sink { |
205 | | - VueCreateElementSink() { |
206 | | - exists(Vue::Instance i, DataFlow::FunctionNode f | |
207 | | - f.flowsTo(i.getRender()) and |
208 | | - this = f.getParameter(0).getACall().getArgument(0) |
209 | | - ) |
210 | | - } |
211 | | - } |
212 | | - |
213 | 48 | } |
0 commit comments