-
Notifications
You must be signed in to change notification settings - Fork 1.8k
JS: Handle spread/rest in API graphs #19108
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
7 commits
Select commit
Hold shift + click to select a range
ff99d5c
JS: Add test for API graph through spread args
asgerf 1ad471c
JS: Track through spread/rest params in API graphs
asgerf b834ffe
JS: Fix a bad join order
asgerf f64bdcc
Update javascript/ql/lib/semmle/javascript/ApiGraphs.qll
asgerf 149ec20
JS: Add comment about internal edge
asgerf e1784bb
JS: Fix handling of spread args on a bound function
asgerf 4746cfd
JS: Add clarifying comment
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 |
---|---|---|
|
@@ -318,6 +318,11 @@ module API { | |
Node getParameter(int i) { | ||
Stages::ApiStage::ref() and | ||
result = this.getASuccessor(Label::parameter(i)) | ||
or | ||
exists(int spreadIndex, string arrayProp | | ||
result = this.getASuccessor(Label::spreadArgument(spreadIndex)).getMember(arrayProp) and | ||
i = spreadIndex + arrayProp.toInt() | ||
) | ||
} | ||
|
||
/** | ||
|
@@ -860,6 +865,23 @@ module API { | |
.getStaticMember(name, DataFlow::MemberKind::getter()) | ||
.getAReturn() | ||
) | ||
or | ||
// Handle rest parameters escaping into external code. For example: | ||
// | ||
// function foo(...rest) { | ||
// externalFunc(rest); | ||
// } | ||
// | ||
// Here, 'rest' reaches a def-node at the call to externalFunc, so we need to ensure | ||
// the arguments passed to 'foo' are stored in the 'rest' array. | ||
exists(Function fun, DataFlow::InvokeNode invoke, int argIndex, Parameter rest | | ||
fun.getRestParameter() = rest and | ||
rest.flow() = pred and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed. It would be equivalent to |
||
invoke.getACallee() = fun and | ||
invoke.getArgument(argIndex) = rhs and | ||
argIndex >= rest.getIndex() and | ||
lbl = Label::member((argIndex - rest.getIndex()).toString()) | ||
) | ||
) | ||
or | ||
exists(DataFlow::ClassNode cls, string name | | ||
|
@@ -888,6 +910,11 @@ module API { | |
i = -1 and lbl = Label::receiver() | ||
) | ||
or | ||
exists(int i | | ||
spreadArgumentPassing(base, i, rhs) and | ||
lbl = Label::spreadArgument(i) | ||
) | ||
or | ||
exists(DataFlow::SourceNode src, DataFlow::PropWrite pw | | ||
use(base, src) and pw = trackUseNode(src).getAPropertyWrite() and rhs = pw.getRhs() | ||
| | ||
|
@@ -931,6 +958,29 @@ module API { | |
) | ||
} | ||
|
||
pragma[nomagic] | ||
private int firstSpreadIndex(InvokeExpr expr) { | ||
result = min(int i | expr.getArgument(i) instanceof SpreadElement) | ||
} | ||
|
||
pragma[nomagic] | ||
private InvokeExpr getAnInvocationWithSpread(DataFlow::SourceNode node, int i) { | ||
result = node.getAnInvocation().asExpr() and | ||
i = firstSpreadIndex(result) | ||
} | ||
|
||
private predicate spreadArgumentPassing(TApiNode base, int i, DataFlow::Node spreadArray) { | ||
exists( | ||
DataFlow::Node use, DataFlow::SourceNode pred, int bound, InvokeExpr invoke, int spreadPos | ||
| | ||
use(base, use) and | ||
pred = trackUseNode(use, _, bound, "") and | ||
invoke = getAnInvocationWithSpread(pred, spreadPos) and | ||
spreadArray = invoke.getArgument(spreadPos).(SpreadElement).getOperand().flow() and | ||
i = bound + spreadPos | ||
) | ||
} | ||
|
||
/** | ||
* Holds if `rhs` is the right-hand side of a definition of node `nd`. | ||
*/ | ||
|
@@ -1579,6 +1629,9 @@ module API { | |
/** Gets the edge label for the receiver. */ | ||
LabelReceiver receiver() { any() } | ||
|
||
/** Gets the edge label for a spread argument passed at index `i`. */ | ||
LabelSpreadArgument spreadArgument(int i) { result.getIndex() = i } | ||
|
||
/** Gets the `return` edge label. */ | ||
LabelReturn return() { any() } | ||
|
||
|
@@ -1628,6 +1681,7 @@ module API { | |
} or | ||
MkLabelReceiver() or | ||
MkLabelReturn() or | ||
MkLabelSpreadArgument(int index) { index = [0 .. 10] } or | ||
MkLabelDecoratedClass() or | ||
MkLabelDecoratedMember() or | ||
MkLabelDecoratedParameter() or | ||
|
@@ -1743,6 +1797,21 @@ module API { | |
override string toString() { result = "getReceiver()" } | ||
} | ||
|
||
/** A label representing an array passed as a spread argument at a given index. */ | ||
class LabelSpreadArgument extends ApiLabel, MkLabelSpreadArgument { | ||
private int index; | ||
|
||
LabelSpreadArgument() { this = MkLabelSpreadArgument(index) } | ||
|
||
/** Gets the argument index at which the spread argument appears. */ | ||
int getIndex() { result = index } | ||
|
||
override string toString() { | ||
// Note: This refers to the internal edge that has no corresponding method on API::Node | ||
result = "getSpreadArgument(" + index + ")" | ||
} | ||
} | ||
|
||
/** A label for a function that is a wrapper around another function. */ | ||
class LabelForwardingFunction extends ApiLabel, MkLabelForwardingFunction { | ||
override string toString() { result = "getForwardingFunction()" } | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is modelling the parameters at (or after) the rest parameter, and modelling each entry as an array store into the spread argument.
And this is then only used for the
getParameter
method to be able to retrieve the correct parameter when there are spread arguments?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Added a comment to clarify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, actually no, it's more than that. If a library model expects an argument to contain array elements, this will allow the model to find those array elements when the array was created by passing arguments into a rest parameter.