Skip to content

Commit a97aefe

Browse files
authored
Merge pull request #1835 from xiemaisi/js/dom-fixes
Approved by asger-semmle
2 parents 3e716bf + b622099 commit a97aefe

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Improvements to JavaScript analysis
2+
3+
## General improvements
4+
5+
## New queries
6+
7+
| **Query** | **Tags** | **Purpose** |
8+
|---------------------------------------------------------------------------|-------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
9+
10+
11+
## Changes to existing queries
12+
13+
| **Query** | **Expected impact** | **Change** |
14+
|--------------------------------|------------------------------|---------------------------------------------------------------------------|
15+
| Client-side cross-site scripting (`js/xss`) | More results | More potential vulnerabilities involving functions that manipulate DOM attributes are now recognized. |
16+
17+
## Changes to QL libraries

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,26 @@ class DomMethodCallExpr extends MethodCallExpr {
7171
or
7272
name = "writeln"
7373
or
74-
name = "insertAdjacentHTML" and argPos = 0
74+
name = "insertAdjacentHTML" and argPos = 1
7575
or
76-
name = "insertAdjacentElement" and argPos = 0
76+
name = "insertAdjacentElement" and argPos = 1
7777
or
7878
name = "insertBefore" and argPos = 0
7979
or
8080
name = "createElement" and argPos = 0
8181
or
8282
name = "appendChild" and argPos = 0
8383
or
84-
name = "setAttribute" and argPos = 0
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+
)
8594
)
8695
}
8796
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ nodes
4949
| translate.js:7:42:7:47 | target |
5050
| translate.js:7:42:7:60 | target.substring(1) |
5151
| translate.js:9:27:9:50 | searchP ... 'term') |
52+
| tst3.js:2:12:2:75 | JSON.pa ... tr(1))) |
53+
| tst3.js:2:23:2:74 | decodeU ... str(1)) |
54+
| tst3.js:2:42:2:56 | window.location |
55+
| tst3.js:2:42:2:63 | window. ... .search |
56+
| tst3.js:2:42:2:73 | window. ... bstr(1) |
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 |
5261
| tst.js:2:7:2:39 | target |
5362
| tst.js:2:16:2:32 | document.location |
5463
| tst.js:2:16:2:39 | documen ... .search |
@@ -215,6 +224,14 @@ edges
215224
| translate.js:6:16:6:39 | documen ... .search | translate.js:6:7:6:39 | target |
216225
| translate.js:7:42:7:47 | target | translate.js:7:42:7:60 | target.substring(1) |
217226
| translate.js:7:42:7:60 | target.substring(1) | translate.js:9:27:9:50 | searchP ... 'term') |
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 |
229+
| tst3.js:2:23:2:74 | decodeU ... str(1)) | tst3.js:2:12:2:75 | JSON.pa ... tr(1))) |
230+
| tst3.js:2:42:2:56 | window.location | tst3.js:2:42:2:63 | window. ... .search |
231+
| tst3.js:2:42:2:63 | window. ... .search | tst3.js:2:42:2:73 | window. ... bstr(1) |
232+
| tst3.js:2:42:2:73 | window. ... bstr(1) | tst3.js:2:23:2:74 | decodeU ... str(1)) |
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 |
218235
| tst.js:2:7:2:39 | target | tst.js:5:18:5:23 | target |
219236
| tst.js:2:7:2:39 | target | tst.js:12:28:12:33 | target |
220237
| tst.js:2:7:2:39 | target | tst.js:23:42:23:47 | target |
@@ -342,6 +359,8 @@ edges
342359
| 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 |
343360
| 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 |
344361
| 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 |
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 |
345364
| 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 |
346365
| 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 |
347366
| 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var foo = document.getElementById("foo");
2+
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+
8+
for (var p in data)
9+
foo.setAttribute(p, data[p]); // not flagged since attribute name is unknown

0 commit comments

Comments
 (0)