Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 876ba7e

Browse files
committed
add typeof sanitizer to js/shell-command-constructed-from-input
1 parent 0a0137b commit 876ba7e

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,25 @@ module UnsafeShellCommandConstruction {
189189
)
190190
}
191191
}
192+
193+
/**
194+
* A check of the form `type x === "X"`, where x is "number", or "boolean",
195+
* which sanitized `x` in its "then" branch.
196+
*/
197+
class TypeOfSanitizer extends TaintTracking::SanitizerGuardNode, DataFlow::ValueNode {
198+
Expr x;
199+
override EqualityTest astNode;
200+
201+
TypeOfSanitizer() {
202+
exists(StringLiteral str, TypeofExpr typeof | astNode.hasOperands(str, typeof) |
203+
str.getValue() = ["number", "boolean"] and // "undefined" is already handled in TaintTracking.qll
204+
typeof.getOperand() = x
205+
)
206+
}
207+
208+
override predicate sanitizes(boolean outcome, Expr e) {
209+
outcome = astNode.getPolarity() and
210+
e = x
211+
}
212+
}
192213
}

javascript/ql/test/query-tests/Security/CWE-078/lib/lib.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,9 @@ module.exports.unproblematic = function() {
339339
module.exports.problematic = function(n) {
340340
cp.exec("rm -rf " + id(n)); // NOT OK
341341
};
342+
343+
module.exports.typeofNumber = function(n) {
344+
if (typeof n === "number") {
345+
cp.exec("rm -rf " + n); // OK
346+
}
347+
};

0 commit comments

Comments
 (0)