Skip to content

Commit 3fe249f

Browse files
author
Max Schaefer
committed
Address review comments.
1 parent 2e252ba commit 3fe249f

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,16 @@ module DOM {
355355
)
356356
or
357357
// A receiver node of an event handler on a DOM node
358-
exists(string handler | handler.matches("on%") |
359-
this = domValueRef().getAPropertySource(handler).(DataFlow::FunctionNode).getReceiver()
358+
exists(DataFlow::SourceNode domNode, DataFlow::FunctionNode eventHandler |
359+
// NOTE: we do not use `getABoundFunctionValue()`, since bound functions tend to have
360+
// a different receiver anyway
361+
eventHandler = domNode.getAPropertySource(any(string n | n.matches("on%")))
362+
or
363+
eventHandler =
364+
domNode.getAMethodCall("addEventListener").getArgument(1).getAFunctionValue()
365+
|
366+
domNode = domValueRef() and
367+
this = eventHandler.getReceiver()
360368
)
361369
or
362370
this = DataFlow::thisNode(any(EventHandlerCode evt))

javascript/ql/test/library-tests/DOM/Customizations.expected

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
test_documentRef
22
| customization.js:2:13:2:31 | customGetDocument() |
33
| event-handler-receiver.js:1:1:1:8 | document |
4+
| event-handler-receiver.js:5:1:5:8 | document |
45
| nameditems.js:1:1:1:8 | document |
56
test_locationRef
67
| customization.js:3:3:3:14 | doc.location |
7-
| event-handler-receiver.js:2:49:2:56 | location |
88
test_domValueRef
99
| customization.js:4:3:4:20 | doc.getElementById |
1010
| customization.js:4:3:4:28 | doc.get ... 'test') |
@@ -13,6 +13,10 @@ test_domValueRef
1313
| event-handler-receiver.js:1:1:1:32 | documen ... my-id') |
1414
| event-handler-receiver.js:1:44:1:43 | this |
1515
| event-handler-receiver.js:2:3:2:17 | this.parentNode |
16+
| event-handler-receiver.js:5:1:5:23 | documen ... entById |
17+
| event-handler-receiver.js:5:1:5:32 | documen ... my-id') |
18+
| event-handler-receiver.js:5:60:5:59 | this |
19+
| event-handler-receiver.js:6:3:6:17 | this.parentNode |
1620
| nameditems.js:1:1:1:23 | documen ... entById |
1721
| nameditems.js:1:1:1:30 | documen ... ('foo') |
1822
| nameditems.js:1:1:2:19 | documen ... em('x') |

javascript/ql/test/library-tests/DOM/event-handler-receiver.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<body>
44
<button onclick="alert(this.tagName);">Click me</button>
55
</body>
6-
</html>
6+
</html>
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
document.getElementById('my-id').onclick = function() {
2-
this.parentNode.innerHTML = '<h2><a href="' + location.href + '">A link</a></h2>'; // NOT OK
1+
document.getElementById('my-id').onclick = function () {
2+
this.parentNode.innerHTML = '<b>hello</b>'; // `this` is a DOM element
33
};
4+
5+
document.getElementById('my-id').addEventListener("click", function (ev) {
6+
this.parentNode.innerHTML = '<b>hello</b>'; // `this` is a DOM element
7+
});

0 commit comments

Comments
 (0)