-
Notifications
You must be signed in to change notification settings - Fork 1.8k
JS: Add model of d3 selections #5379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
18cfe72
JS: Add model of d3
asgerf 0c6e161
JS: Add source to XssThroughDom
asgerf 773cf0d
JS: Autoformat
asgerf 3b11958
JS: Expand D3 model a bit
asgerf 2f3a76c
JS: Handle global variable d3
asgerf a03cb11
JS: Include $().prop() source in XssThroughDom
asgerf 5d6a933
JS: Autoformat
asgerf a2d1e88
JS: Update more test expectations
asgerf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
lgtm,codescanning | ||
* Support for `d3` has improved. The XSS queries now recognize HTML injection sinks | ||
from the `d3` API. | ||
Affected packages are | ||
[d3](https://npmjs.com/package/d3), | ||
[d3-selection](https://npmjs.com/package/d3-selection). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** Provides classes and predicates modelling aspects of the `d3` library. */ | ||
|
||
private import javascript | ||
private import semmle.javascript.security.dataflow.Xss | ||
|
||
/** Provides classes and predicates modelling aspects of the `d3` library. */ | ||
module D3 { | ||
/** The global variable `d3` as an entry point for API graphs. */ | ||
private class D3GlobalEntry extends API::EntryPoint { | ||
D3GlobalEntry() { this = "D3GlobalEntry" } | ||
|
||
override DataFlow::SourceNode getAUse() { result = DataFlow::globalVarRef("d3") } | ||
|
||
override DataFlow::Node getARhs() { none() } | ||
} | ||
|
||
/** Gets an API node referring to the `d3` module. */ | ||
API::Node d3() { | ||
result = API::moduleImport("d3") | ||
or | ||
// recognize copies of d3 in a scope | ||
result = API::moduleImport(any(string s | s.regexpMatch("@.*/d3(-\\w+)?"))) | ||
or | ||
result = API::moduleImport("d3-node").getInstance().getMember("d3") | ||
or | ||
result = API::root().getASuccessor(any(D3GlobalEntry i)) | ||
} | ||
|
||
/** | ||
* Gets an API node referring to the given module, or to `d3`. | ||
* | ||
* Useful for accessing modules that are re-exported by `d3`. | ||
*/ | ||
bindingset[name] | ||
API::Node d3Module(string name) { | ||
result = d3() // all d3 modules are re-exported as part of 'd3' | ||
or | ||
result = API::moduleImport(name) | ||
} | ||
|
||
/** Gets an API node referring to a `d3` selection object, such as `d3.selection()`. */ | ||
API::Node d3Selection() { | ||
result = d3Module("d3-selection").getMember(["selection", "select", "selectAll"]).getReturn() | ||
or | ||
exists(API::CallNode call, string name | | ||
call = d3Selection().getMember(name).getACall() and | ||
result = call.getReturn() | ||
| | ||
name = | ||
[ | ||
"select", "selectAll", "filter", "merge", "selectChild", "selectChildren", "selection", | ||
"insert", "remove", "clone", "sort", "order", "raise", "lower", "append", "data", "join", | ||
"enter", "exit", "call" | ||
] | ||
or | ||
name = ["text", "html", "datum"] and | ||
call.getNumArgument() > 0 // exclude 0-argument version, which returns the current value | ||
or | ||
name = ["attr", "classed", "style", "property", "on"] and | ||
call.getNumArgument() > 1 // exclude 1-argument version, which returns the current value | ||
or | ||
// Setting multiple things at once | ||
name = ["attr", "classed", "style", "property", "on"] and | ||
call.getArgument(0).getALocalSource() instanceof DataFlow::ObjectLiteralNode | ||
) | ||
or | ||
result = d3Selection().getMember("call").getParameter(0).getParameter(0) | ||
} | ||
|
||
private class D3XssSink extends DomBasedXss::Sink { | ||
D3XssSink() { | ||
exists(API::Node htmlArg | | ||
htmlArg = d3Selection().getMember("html").getParameter(0) and | ||
this = [htmlArg, htmlArg.getReturn()].getARhs() | ||
) | ||
} | ||
} | ||
|
||
private class D3DomValueSource extends DOM::DomValueSource::Range { | ||
D3DomValueSource() { | ||
this = d3Selection().getMember("each").getReceiver().getAnImmediateUse() | ||
or | ||
this = d3Selection().getMember("node").getReturn().getAnImmediateUse() | ||
or | ||
this = d3Selection().getMember("nodes").getReturn().getUnknownMember().getAnImmediateUse() | ||
} | ||
} | ||
|
||
private class D3AttributeDefinition extends DOM::AttributeDefinition { | ||
DataFlow::CallNode call; | ||
|
||
D3AttributeDefinition() { | ||
call = d3Selection().getMember("attr").getACall() and | ||
call.getNumArgument() > 1 and | ||
this = call.asExpr() | ||
} | ||
|
||
override string getName() { result = call.getArgument(0).getStringValue() } | ||
|
||
override DataFlow::Node getValueNode() { result = call.getArgument(1) } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
javascript/ql/test/query-tests/Security/CWE-079/DomBasedXss/d3.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const d3 = require('d3'); | ||
|
||
function getTaint() { | ||
return window.name; | ||
} | ||
|
||
function doSomething() { | ||
d3.select('#main') | ||
.attr('width', 100) | ||
.style('color', 'red') | ||
.html(getTaint()) // NOT OK | ||
.html(d => getTaint()) // NOT OK | ||
.call(otherFunction) | ||
.html(d => getTaint()); // NOT OK | ||
} | ||
|
||
|
||
function otherFunction(selection) { | ||
selection | ||
.attr('foo', 'bar') | ||
.html(getTaint()); // NOT OK | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.