Skip to content

Commit 97de35c

Browse files
authored
Merge pull request #17965 from Napalys/napalys/immutable-array-operations
JS: Added support for toSorted and toReversed
2 parents 2bb5603 + ef18a6e commit 97de35c

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added taint-steps for `Array.prototype.toReversed`.
5+
* Added taint-steps for `Array.prototype.toSorted`.

javascript/ql/lib/semmle/javascript/Arrays.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,18 @@ private module ArrayLibraries {
458458
)
459459
}
460460
}
461+
462+
/**
463+
* A taint propagating data flow edge arising from array transformation operations
464+
* that return a new array instead of modifying the original array in place.
465+
*/
466+
private class ImmutableArrayTransformStep extends TaintTracking::SharedTaintStep {
467+
override predicate heapStep(DataFlow::Node pred, DataFlow::Node succ) {
468+
exists(DataFlow::MethodCallNode call |
469+
call.getMethodName() in ["toSorted", "toReversed"] and
470+
pred = call.getReceiver() and
471+
succ = call
472+
)
473+
}
474+
}
461475
}

javascript/ql/test/library-tests/TaintTracking/BasicTaintTracking.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ typeInferenceMismatch
234234
| tst.js:2:13:2:20 | source() | tst.js:51:10:51:31 | seriali ... ript(x) |
235235
| tst.js:2:13:2:20 | source() | tst.js:54:14:54:19 | unsafe |
236236
| tst.js:2:13:2:20 | source() | tst.js:61:10:61:20 | x.reverse() |
237+
| tst.js:2:13:2:20 | source() | tst.js:63:10:63:21 | x.toSorted() |
238+
| tst.js:2:13:2:20 | source() | tst.js:65:10:65:16 | xSorted |
239+
| tst.js:2:13:2:20 | source() | tst.js:67:10:67:23 | x.toReversed() |
240+
| tst.js:2:13:2:20 | source() | tst.js:69:10:69:18 | xReversed |
237241
| xml.js:5:18:5:25 | source() | xml.js:8:14:8:17 | text |
238242
| xml.js:12:17:12:24 | source() | xml.js:13:14:13:19 | result |
239243
| xml.js:23:18:23:25 | source() | xml.js:20:14:20:17 | attr |

javascript/ql/test/library-tests/TaintTracking/tst.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ function test() {
5959
tagged`foo ${"safe"} bar ${x} baz`;
6060

6161
sink(x.reverse()); // NOT OK
62+
63+
sink(x.toSorted()) // NOT OK
64+
const xSorted = x.toSorted();
65+
sink(xSorted) // NOT OK
66+
67+
sink(x.toReversed()) // NOT OK
68+
const xReversed = x.toReversed();
69+
sink(xReversed) // NOT OK
6270
}

0 commit comments

Comments
 (0)