Skip to content
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

JS: Added support for Array.prototype.[findLastIndex, findLast] ES2022 feature #18005

Merged
merged 12 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions javascript/ql/lib/semmle/javascript/Arrays.qll
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,18 @@ private module ArrayLibraries {
)
}
}

/**
* Defines a data flow step that tracks the flow of data through callback functions in arrays.
*/
private class ArrayCallBackDataFlowStep extends PreCallGraphStep {
override predicate loadStep(DataFlow::Node obj, DataFlow::Node element, string prop) {
exists(DataFlow::MethodCallNode call |
call.getMethodName() = ["findLast", "find", "findLastIndex"] and
prop = arrayLikeElement() and
obj = call.getReceiver() and
element = call.getCallback(0).getParameter(0)
)
}
}
}
3 changes: 3 additions & 0 deletions javascript/ql/test/library-tests/Arrays/DataFlow.expected
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
| arrays.js:53:4:53:11 | "source" | arrays.js:54:10:54:18 | ary.pop() |
| arrays.js:99:31:99:38 | "source" | arrays.js:100:8:100:17 | arr8.pop() |
| arrays.js:103:55:103:62 | "source" | arrays.js:105:8:105:25 | arr8_variant.pop() |
| arrays.js:114:19:114:26 | "source" | arrays.js:115:50:115:53 | item |
| arrays.js:114:19:114:26 | "source" | arrays.js:116:10:116:16 | element |
| arrays.js:120:19:120:26 | "source" | arrays.js:121:46:121:49 | item |
| arrays.js:120:19:120:26 | "source" | arrays.js:122:10:122:16 | element |
| arrays.js:126:19:126:26 | "source" | arrays.js:127:55:127:58 | item |
3 changes: 3 additions & 0 deletions javascript/ql/test/library-tests/Arrays/TaintFlow.expected
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
| arrays.js:96:9:96:16 | "source" | arrays.js:96:8:96:36 | ["sourc ... => !!x) |
| arrays.js:99:31:99:38 | "source" | arrays.js:100:8:100:17 | arr8.pop() |
| arrays.js:103:55:103:62 | "source" | arrays.js:105:8:105:25 | arr8_variant.pop() |
| arrays.js:114:19:114:26 | "source" | arrays.js:115:50:115:53 | item |
| arrays.js:114:19:114:26 | "source" | arrays.js:116:10:116:16 | element |
| arrays.js:120:19:120:26 | "source" | arrays.js:121:46:121:49 | item |
| arrays.js:120:19:120:26 | "source" | arrays.js:122:10:122:16 | element |
| arrays.js:126:19:126:26 | "source" | arrays.js:127:55:127:58 | item |
6 changes: 3 additions & 3 deletions javascript/ql/test/library-tests/Arrays/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@

{ // Test for findLast function
const list = ["source"];
const element = list.findLast((item) => sink(item)); // NOT OK -- Not caught, currently missing dataflow tracking.
const element = list.findLast((item) => sink(item)); // NOT OK
sink(element); // NOT OK
}

{ // Test for find function
const list = ["source"];
const element = list.find((item) => sink(item)); // NOT OK -- Not caught, currently missing dataflow tracking.
const element = list.find((item) => sink(item)); // NOT OK
sink(element); // NOT OK
}

{ // Test for findLastIndex function
const list = ["source"];
const element = list.findLastIndex((item) => sink(item)); // NOT OK -- Not caught, currently missing dataflow tracking.
const element = list.findLastIndex((item) => sink(item)); // NOT OK
sink(element); // OK
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test like:

    const element = source.findLastIndex((item) => sink(item)); // NOT OK - only found with taint-tracking. 
    sink(element); // OK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds test case: f1e95a8

});