Skip to content

Commit 9ca25d5

Browse files
committed
JS: Support .hash extraction via a few more methods
1 parent 19db418 commit 9ca25d5

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,29 @@ module DomBasedXss {
5252
}
5353

5454
override predicate isAdditionalLoadStep(DataFlow::Node pred, DataFlow::Node succ, string prop) {
55-
exists(DataFlow::MethodCallNode call, string name |
56-
name = "substr" or name = "substring" or name = "slice"
57-
|
58-
call.getMethodName() = name and
55+
exists(DataFlow::MethodCallNode call |
56+
call.getMethodName() = ["substr", "substring", "slice"] and
5957
not call.getArgument(0).getIntValue() = 0 and
6058
pred = call.getReceiver() and
6159
succ = call and
6260
prop = urlSuffixPseudoProperty()
6361
)
62+
or
63+
exists(DataFlow::MethodCallNode call |
64+
call.getMethodName() = "exec" and pred = call.getArgument(0)
65+
or
66+
call.getMethodName() = "match" and pred = call.getReceiver()
67+
|
68+
succ = call and
69+
prop = urlSuffixPseudoProperty()
70+
)
71+
or
72+
exists(StringSplitCall split |
73+
split.getSeparator() = ["#", "?"] and
74+
pred = split.getBaseString() and
75+
succ = split.getASubstringRead(1) and
76+
prop = urlSuffixPseudoProperty()
77+
)
6478
}
6579

6680
override predicate isSanitizerEdge(DataFlow::Node pred, DataFlow::Node succ) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,17 @@ nodes
459459
| tst.js:422:17:422:46 | window. ... bstr(1) |
460460
| tst.js:423:18:423:24 | payload |
461461
| tst.js:423:18:423:24 | payload |
462+
| tst.js:425:7:425:55 | match |
463+
| tst.js:425:15:425:29 | window.location |
464+
| tst.js:425:15:425:29 | window.location |
465+
| tst.js:425:15:425:55 | window. ... (\\w+)/) |
466+
| tst.js:427:20:427:24 | match |
467+
| tst.js:427:20:427:27 | match[1] |
468+
| tst.js:427:20:427:27 | match[1] |
469+
| tst.js:430:18:430:32 | window.location |
470+
| tst.js:430:18:430:32 | window.location |
471+
| tst.js:430:18:430:51 | window. ... '#')[1] |
472+
| tst.js:430:18:430:51 | window. ... '#')[1] |
462473
| typeahead.js:20:13:20:45 | target |
463474
| typeahead.js:20:22:20:38 | document.location |
464475
| typeahead.js:20:22:20:38 | document.location |
@@ -893,6 +904,16 @@ edges
893904
| tst.js:422:17:422:31 | window.location | tst.js:422:17:422:46 | window. ... bstr(1) |
894905
| tst.js:422:17:422:31 | window.location | tst.js:422:17:422:46 | window. ... bstr(1) |
895906
| tst.js:422:17:422:46 | window. ... bstr(1) | tst.js:422:7:422:46 | payload |
907+
| tst.js:425:7:425:55 | match | tst.js:427:20:427:24 | match |
908+
| tst.js:425:15:425:29 | window.location | tst.js:425:15:425:55 | window. ... (\\w+)/) |
909+
| tst.js:425:15:425:29 | window.location | tst.js:425:15:425:55 | window. ... (\\w+)/) |
910+
| tst.js:425:15:425:55 | window. ... (\\w+)/) | tst.js:425:7:425:55 | match |
911+
| tst.js:427:20:427:24 | match | tst.js:427:20:427:27 | match[1] |
912+
| tst.js:427:20:427:24 | match | tst.js:427:20:427:27 | match[1] |
913+
| tst.js:430:18:430:32 | window.location | tst.js:430:18:430:51 | window. ... '#')[1] |
914+
| tst.js:430:18:430:32 | window.location | tst.js:430:18:430:51 | window. ... '#')[1] |
915+
| tst.js:430:18:430:32 | window.location | tst.js:430:18:430:51 | window. ... '#')[1] |
916+
| tst.js:430:18:430:32 | window.location | tst.js:430:18:430:51 | window. ... '#')[1] |
896917
| typeahead.js:20:13:20:45 | target | typeahead.js:21:12:21:17 | target |
897918
| typeahead.js:20:22:20:38 | document.location | typeahead.js:20:22:20:45 | documen ... .search |
898919
| typeahead.js:20:22:20:38 | document.location | typeahead.js:20:22:20:45 | documen ... .search |
@@ -1021,6 +1042,8 @@ edges
10211042
| tst.js:412:18:412:30 | target.taint7 | tst.js:387:16:387:32 | document.location | tst.js:412:18:412:30 | target.taint7 | Cross-site scripting vulnerability due to $@. | tst.js:387:16:387:32 | document.location | user-provided value |
10221043
| tst.js:415:18:415:30 | target.taint8 | tst.js:387:16:387:32 | document.location | tst.js:415:18:415:30 | target.taint8 | Cross-site scripting vulnerability due to $@. | tst.js:387:16:387:32 | document.location | user-provided value |
10231044
| tst.js:423:18:423:24 | payload | tst.js:422:17:422:31 | window.location | tst.js:423:18:423:24 | payload | Cross-site scripting vulnerability due to $@. | tst.js:422:17:422:31 | window.location | user-provided value |
1045+
| tst.js:427:20:427:27 | match[1] | tst.js:425:15:425:29 | window.location | tst.js:427:20:427:27 | match[1] | Cross-site scripting vulnerability due to $@. | tst.js:425:15:425:29 | window.location | user-provided value |
1046+
| tst.js:430:18:430:51 | window. ... '#')[1] | tst.js:430:18:430:32 | window.location | tst.js:430:18:430:51 | window. ... '#')[1] | Cross-site scripting vulnerability due to $@. | tst.js:430:18:430:32 | window.location | user-provided value |
10241047
| typeahead.js:25:18:25:20 | val | typeahead.js:20:22:20:38 | document.location | typeahead.js:25:18:25:20 | val | Cross-site scripting vulnerability due to $@. | typeahead.js:20:22:20:38 | document.location | user-provided value |
10251048
| v-html.vue:2:8:2:23 | v-html=tainted | v-html.vue:6:42:6:58 | document.location | v-html.vue:2:8:2:23 | v-html=tainted | Cross-site scripting vulnerability due to $@. | v-html.vue:6:42:6:58 | document.location | user-provided value |
10261049
| winjs.js:3:43:3:49 | tainted | winjs.js:2:17:2:33 | document.location | winjs.js:3:43:3:49 | tainted | Cross-site scripting vulnerability due to $@. | winjs.js:2:17:2:33 | document.location | user-provided value |

javascript/ql/test/query-tests/Security/CWE-079/tst.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,11 @@ function test() {
421421
function hash2() {
422422
var payload = window.location.hash.substr(1);
423423
document.write(payload); // NOT OK
424+
425+
let match = window.location.hash.match(/hello (\w+)/);
426+
if (match) {
427+
document.write(match[1]); // NOT OK
428+
}
429+
430+
document.write(window.location.hash.split('#')[1]); // NOT OK
424431
}

0 commit comments

Comments
 (0)