Skip to content

Commit 2b5b875

Browse files
authored
Merge pull request #1316 from asger-semmle/incorrect-suffix-check-fps
Approved by esben-semmle, xiemaisi
2 parents 3af3c54 + b9ade67 commit 2b5b875

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

change-notes/1.21/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| Type confusion through parameter tampering | Fewer false-positive results | This rule now recognizes additional emptiness checks. |
4242
| Useless assignment to property | Fewer false-positive results | This rule now ignore reads of additional getters. |
4343
| Unreachable statement | Unreachable throws no longer give an alert | This ignores unreachable throws, as they could be intentional (for example, to placate the TS compiler). |
44+
| Incorrect suffix check | Fewer false-positive results | This rule now recognizes valid checks in more cases. |
4445

4546
## Changes to QL libraries
4647

javascript/ql/src/Security/CWE-020/IncorrectSuffixCheck.ql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ class UnsafeIndexOfComparison extends EqualityTest {
139139
not test.isInclusive() and
140140
value = -1
141141
)
142+
) and
143+
// Check for indexOf being <0, or <=-1
144+
not exists(RelationalComparison test |
145+
test.getLesserOperand() = indexOf.getAnEquivalentIndexOfCall().getAUse() and
146+
exists(int value | value = test.getGreaterOperand().getIntValue() |
147+
value < 0
148+
or
149+
not test.isInclusive() and
150+
value = 0
151+
)
142152
)
143153
}
144154

javascript/ql/test/query-tests/Security/CWE-020/tst.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,13 @@ function withIndexOfCheckBad(x, y) {
7979
function plus(x, y) {
8080
return x.indexOf("." + y) === x.length - (y.length + 1); // NOT OK
8181
}
82+
83+
function withIndexOfCheckLower(x, y) {
84+
let index = x.indexOf(y);
85+
return !(index < 0) && index === x.length - y.length - 1; // OK
86+
}
87+
88+
function withIndexOfCheckLowerEq(x, y) {
89+
let index = x.indexOf(y);
90+
return !(index <= -1) && index === x.length - y.length - 1; // OK
91+
}

0 commit comments

Comments
 (0)