Skip to content

Commit 3f0a54c

Browse files
committed
Added support for Array.prototype.toSorted function
1 parent def8d75 commit 3f0a54c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

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() = "toSorted" and
470+
pred = call.getReceiver() and
471+
succ = call
472+
)
473+
}
474+
}
461475
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ 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 |
237239
| xml.js:5:18:5:25 | source() | xml.js:8:14:8:17 | text |
238240
| xml.js:12:17:12:24 | source() | xml.js:13:14:13:19 | result |
239241
| xml.js:23:18:23:25 | source() | xml.js:20:14:20:17 | attr |

javascript/ql/test/query-tests/NodeJS/DubiousImport/externs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,18 @@ Array.prototype.slice = function(opt_begin, opt_end) {};
371371
*/
372372
Array.prototype.sort = function(opt_compareFunction) {};
373373

374+
/**
375+
* Returns a new array with the elements sorted.
376+
*
377+
* @param {function(T,T):number=} opt_compareFunction Specifies a function that
378+
* defines the sort order. If omitted, the array elements are converted to strings,
379+
* then sorted according to each character's Unicode code point value.
380+
* @this {{length: number}|Array.}
381+
* @template T
382+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted
383+
*/
384+
Array.prototype.toSorted = function(opt_compareFunction) {};
385+
374386
/**
375387
* Changes the content of an array, adding new elements while removing old
376388
* elements.

0 commit comments

Comments
 (0)