Skip to content

Commit fef257b

Browse files
author
Esben Sparre Andreasen
committed
JS: remove emptiness checks from the type confusion x.length sinks
1 parent 44ae7b6 commit fef257b

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

change-notes/1.18/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
| Reflected cross-site scripting | Fewer false-positive results | This rule now treats header names case-insensitively. |
9696
| Server-side URL redirect | More true-positive results | This rule now treats header names case-insensitively. |
9797
| Superfluous trailing arguments | Fewer false-positive results | This rule now ignores calls to some empty functions. |
98+
| Type confusion through parameter tampering | Fewer false-positive results | This rule no longer flags emptiness checks. |
9899
| Uncontrolled command line | More true-positive results | This rule now recognizes indirect command injection through `sh -c` and similar. |
99100
| Unused variable | Fewer results | This rule no longer flags class expressions that could be made anonymous. While technically true, these results are not interesting. |
100101
| Unused variable | Renamed | This rule has been renamed to "Unused variable, import, function or class" to reflect the fact that it flags different kinds of unused program elements. |

javascript/ql/src/semmle/javascript/security/dataflow/TypeConfusionThroughParameterTampering.qll

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,22 @@ module TypeConfusionThroughParameterTampering {
101101

102102
LengthAccess() {
103103
exists (DataFlow::PropRead read |
104-
read.accesses(this, "length")
104+
read.accesses(this, "length") and
105+
// exclude truthiness checks on the length: an array/string confusion can not control an emptiness check
106+
not (
107+
exists (ConditionGuardNode cond |
108+
read.asExpr() = cond.getTest()
109+
)
110+
or
111+
exists (EqualityTest eq, Expr zero |
112+
zero.getIntValue() = 0 and
113+
eq.hasOperands(read.asExpr(), zero)
114+
)
115+
or
116+
exists (LogNotExpr eq |
117+
eq.getOperand() = read.asExpr()
118+
)
119+
)
105120
)
106121
}
107122

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,21 @@ express().get('/some/path/:foo', function(req, res) {
5050
var foo = req.params.foo;
5151
foo.indexOf(); // OK
5252
});
53+
54+
express().get('/some/path/:foo', function(req, res) {
55+
if (req.query.path.length) {} // OK
56+
req.query.path.length == 0; // OK
57+
!req.query.path.length == 0; // OK
58+
});
59+
60+
express().get('/some/path/:foo', function(req, res) {
61+
let p = req.query.path;
62+
63+
if (typeof p !== 'string') {
64+
return;
65+
}
66+
67+
while (p.length) { // OK
68+
p = p.substr(1);
69+
}
70+
});

0 commit comments

Comments
 (0)