Skip to content

Commit b622099

Browse files
author
Max Schaefer
committed
JavaScript: Restrict setAttribute sink to potentially dangerous attribute names.
1 parent 78ce290 commit b622099

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

javascript/ql/src/semmle/javascript/security/dataflow/DOM.qll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ class DomMethodCallExpr extends MethodCallExpr {
8181
or
8282
name = "appendChild" and argPos = 0
8383
or
84-
name = "setAttribute" and argPos = 1
85-
or
86-
name = "setAttributeNS" and argPos = 2
84+
(
85+
name = "setAttribute" and argPos = 1
86+
or
87+
name = "setAttributeNS" and argPos = 2
88+
) and
89+
// restrict to potentially dangerous attributes
90+
exists(string attr |
91+
attr = "action" or attr = "formaction" or attr = "href" or attr = "src" |
92+
getArgument(argPos-1).getStringValue().toLowerCase() = attr
93+
)
8794
)
8895
}
8996
}

javascript/ql/test/query-tests/Security/CWE-079/Xss.expected

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ nodes
5454
| tst3.js:2:42:2:56 | window.location |
5555
| tst3.js:2:42:2:63 | window. ... .search |
5656
| tst3.js:2:42:2:73 | window. ... bstr(1) |
57-
| tst3.js:4:23:4:26 | data |
58-
| tst3.js:4:23:4:29 | data[p] |
57+
| tst3.js:4:25:4:28 | data |
58+
| tst3.js:4:25:4:32 | data.src |
59+
| tst3.js:5:26:5:29 | data |
60+
| tst3.js:5:26:5:31 | data.p |
5961
| tst.js:2:7:2:39 | target |
6062
| tst.js:2:16:2:32 | document.location |
6163
| tst.js:2:16:2:39 | documen ... .search |
@@ -222,12 +224,14 @@ edges
222224
| translate.js:6:16:6:39 | documen ... .search | translate.js:6:7:6:39 | target |
223225
| translate.js:7:42:7:47 | target | translate.js:7:42:7:60 | target.substring(1) |
224226
| translate.js:7:42:7:60 | target.substring(1) | translate.js:9:27:9:50 | searchP ... 'term') |
225-
| tst3.js:2:12:2:75 | JSON.pa ... tr(1))) | tst3.js:4:23:4:26 | data |
227+
| tst3.js:2:12:2:75 | JSON.pa ... tr(1))) | tst3.js:4:25:4:28 | data |
228+
| tst3.js:2:12:2:75 | JSON.pa ... tr(1))) | tst3.js:5:26:5:29 | data |
226229
| tst3.js:2:23:2:74 | decodeU ... str(1)) | tst3.js:2:12:2:75 | JSON.pa ... tr(1))) |
227230
| tst3.js:2:42:2:56 | window.location | tst3.js:2:42:2:63 | window. ... .search |
228231
| tst3.js:2:42:2:63 | window. ... .search | tst3.js:2:42:2:73 | window. ... bstr(1) |
229232
| tst3.js:2:42:2:73 | window. ... bstr(1) | tst3.js:2:23:2:74 | decodeU ... str(1)) |
230-
| tst3.js:4:23:4:26 | data | tst3.js:4:23:4:29 | data[p] |
233+
| tst3.js:4:25:4:28 | data | tst3.js:4:25:4:32 | data.src |
234+
| tst3.js:5:26:5:29 | data | tst3.js:5:26:5:31 | data.p |
231235
| tst.js:2:7:2:39 | target | tst.js:5:18:5:23 | target |
232236
| tst.js:2:7:2:39 | target | tst.js:12:28:12:33 | target |
233237
| tst.js:2:7:2:39 | target | tst.js:23:42:23:47 | target |
@@ -355,7 +359,8 @@ edges
355359
| string-manipulations.js:9:16:9:58 | String. ... n.href) | string-manipulations.js:9:36:9:52 | document.location | string-manipulations.js:9:16:9:58 | String. ... n.href) | Cross-site scripting vulnerability due to $@. | string-manipulations.js:9:36:9:52 | document.location | user-provided value |
356360
| string-manipulations.js:10:16:10:45 | String( ... n.href) | string-manipulations.js:10:23:10:39 | document.location | string-manipulations.js:10:16:10:45 | String( ... n.href) | Cross-site scripting vulnerability due to $@. | string-manipulations.js:10:23:10:39 | document.location | user-provided value |
357361
| translate.js:9:27:9:50 | searchP ... 'term') | translate.js:6:16:6:32 | document.location | translate.js:9:27:9:50 | searchP ... 'term') | Cross-site scripting vulnerability due to $@. | translate.js:6:16:6:32 | document.location | user-provided value |
358-
| tst3.js:4:23:4:29 | data[p] | tst3.js:2:42:2:56 | window.location | tst3.js:4:23:4:29 | data[p] | Cross-site scripting vulnerability due to $@. | tst3.js:2:42:2:56 | window.location | user-provided value |
362+
| tst3.js:4:25:4:32 | data.src | tst3.js:2:42:2:56 | window.location | tst3.js:4:25:4:32 | data.src | Cross-site scripting vulnerability due to $@. | tst3.js:2:42:2:56 | window.location | user-provided value |
363+
| tst3.js:5:26:5:31 | data.p | tst3.js:2:42:2:56 | window.location | tst3.js:5:26:5:31 | data.p | Cross-site scripting vulnerability due to $@. | tst3.js:2:42:2:56 | window.location | user-provided value |
359364
| tst.js:5:18:5:23 | target | tst.js:2:16:2:32 | document.location | tst.js:5:18:5:23 | target | Cross-site scripting vulnerability due to $@. | tst.js:2:16:2:32 | document.location | user-provided value |
360365
| tst.js:8:18:8:126 | "<OPTIO ... PTION>" | tst.js:8:37:8:53 | document.location | tst.js:8:18:8:126 | "<OPTIO ... PTION>" | Cross-site scripting vulnerability due to $@. | tst.js:8:37:8:53 | document.location | user-provided value |
361366
| tst.js:12:5:12:42 | '<div s ... 'px">' | tst.js:2:16:2:32 | document.location | tst.js:12:5:12:42 | '<div s ... 'px">' | Cross-site scripting vulnerability due to $@. | tst.js:2:16:2:32 | document.location | user-provided value |
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
var foo = document.getElementById("foo");
22
var data = JSON.parse(decodeURIComponent(window.location.search.substr(1)));
3+
4+
foo.setAttribute("src", data.src); // NOT OK
5+
foo.setAttribute("HREF", data.p); // NOT OK
6+
foo.setAttribute("width", data.w); // OK
7+
38
for (var p in data)
4-
foo.setAttribute(p, data[p]);
9+
foo.setAttribute(p, data[p]); // not flagged since attribute name is unknown

0 commit comments

Comments
 (0)