Skip to content

Commit 3791b15

Browse files
authored
Merge pull request #7892 from erik-krogh/nanSan
JS: Add a `isNaN` sanitizer, and use it in queries that already had a typeof check
2 parents 2ffd79d + eb56a5a commit 3791b15

12 files changed

+191
-3
lines changed

javascript/ql/lib/semmle/javascript/dataflow/TaintTracking.qll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,36 @@ module TaintTracking {
11661166
)
11671167
}
11681168

1169+
/** Holds if `guard` is a test that checks if `operand` is a number. */
1170+
predicate isNumberGuard(DataFlow::Node guard, Expr operand, boolean polarity) {
1171+
exists(DataFlow::CallNode isNaN |
1172+
isNaN = DataFlow::globalVarRef("isNaN").getACall() and guard = isNaN and polarity = false
1173+
|
1174+
operand = isNaN.getArgument(0).asExpr()
1175+
or
1176+
exists(DataFlow::CallNode parse |
1177+
parse = DataFlow::globalVarRef(["parseInt", "parseFloat"]).getACall()
1178+
|
1179+
parse = isNaN.getArgument(0) and
1180+
operand = parse.getArgument(0).asExpr()
1181+
)
1182+
or
1183+
exists(UnaryExpr unary | unary.getOperator() = ["+", "-"] |
1184+
unary = isNaN.getArgument(0).asExpr() and
1185+
operand = unary.getOperand()
1186+
)
1187+
or
1188+
exists(BinaryExpr bin | bin.getOperator() = ["+", "-"] |
1189+
bin = isNaN.getArgument(0).asExpr() and
1190+
operand = bin.getAnOperand() and
1191+
bin.getAnOperand() instanceof NumberLiteral
1192+
)
1193+
)
1194+
or
1195+
isTypeofGuard(guard.asExpr(), operand, "number") and
1196+
polarity = guard.asExpr().(EqualityTest).getPolarity()
1197+
}
1198+
11691199
/** DEPRECATED. This class has been renamed to `MembershipTestSanitizer`. */
11701200
deprecated class StringInclusionSanitizer = MembershipTestSanitizer;
11711201

javascript/ql/lib/semmle/javascript/security/TaintedObject.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ module TaintedObject {
110110
}
111111
}
112112

113+
/** A guard that checks whether `x` is a number. */
114+
class NumberGuard extends SanitizerGuard instanceof DataFlow::CallNode {
115+
Expr x;
116+
boolean polarity;
117+
118+
NumberGuard() { TaintTracking::isNumberGuard(this, x, polarity) }
119+
120+
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = polarity }
121+
}
122+
113123
/**
114124
* A sanitizer guard that validates an input against a JSON schema.
115125
*/

javascript/ql/lib/semmle/javascript/security/dataflow/PrototypePollutingAssignmentQuery.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class Configuration extends TaintTracking::Configuration {
122122
guard instanceof InstanceofCheck or
123123
guard instanceof IsArrayCheck or
124124
guard instanceof TypeofCheck or
125+
guard instanceof NumberGuard or
125126
guard instanceof EqualityCheck or
126127
guard instanceof IncludesCheck
127128
}
@@ -228,6 +229,16 @@ private class TypeofCheck extends TaintTracking::LabeledSanitizerGuardNode, Data
228229
}
229230
}
230231

232+
/** A guard that checks whether `x` is a number. */
233+
class NumberGuard extends TaintTracking::SanitizerGuardNode instanceof DataFlow::CallNode {
234+
Expr x;
235+
boolean polarity;
236+
237+
NumberGuard() { TaintTracking::isNumberGuard(this, x, polarity) }
238+
239+
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = polarity }
240+
}
241+
231242
/** A call to `Array.isArray`, which is false for `Object.prototype`. */
232243
private class IsArrayCheck extends TaintTracking::LabeledSanitizerGuardNode, DataFlow::CallNode {
233244
IsArrayCheck() { this = DataFlow::globalVarRef("Array").getAMemberCall("isArray") }

javascript/ql/lib/semmle/javascript/security/dataflow/UnsafeJQueryPluginCustomizations.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ module UnsafeJQueryPlugin {
153153
}
154154
}
155155

156+
/** A guard that checks whether `x` is a number. */
157+
class NumberGuard extends TaintTracking::SanitizerGuardNode instanceof DataFlow::CallNode {
158+
Expr x;
159+
boolean polarity;
160+
161+
NumberGuard() { TaintTracking::isNumberGuard(this, x, polarity) }
162+
163+
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = polarity }
164+
}
165+
156166
/**
157167
* The client-provided options object for a jQuery plugin, considered as a source for unsafe jQuery plugins.
158168
*/

javascript/ql/lib/semmle/javascript/security/dataflow/UnsafeJQueryPluginQuery.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class Configuration extends TaintTracking::Configuration {
4646
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode node) {
4747
super.isSanitizerGuard(node) or
4848
node instanceof IsElementSanitizer or
49-
node instanceof PropertyPresenceSanitizer
49+
node instanceof PropertyPresenceSanitizer or
50+
node instanceof NumberGuard
5051
}
5152
}
5253

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ module UnsafeShellCommandConstruction {
286286
}
287287
}
288288

289+
/** A guard that checks whether `x` is a number. */
290+
class NumberGuard extends TaintTracking::SanitizerGuardNode instanceof DataFlow::CallNode {
291+
Expr x;
292+
boolean polarity;
293+
294+
NumberGuard() { TaintTracking::isNumberGuard(this, x, polarity) }
295+
296+
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = polarity }
297+
}
298+
289299
private import semmle.javascript.dataflow.internal.AccessPaths
290300
private import semmle.javascript.dataflow.InferredTypes
291301

javascript/ql/lib/semmle/javascript/security/dataflow/UnsafeShellCommandConstructionQuery.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class Configuration extends TaintTracking::Configuration {
2424

2525
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) {
2626
guard instanceof PathExistsSanitizerGuard or
27-
guard instanceof TaintTracking::AdHocWhitelistCheckSanitizer
27+
guard instanceof TaintTracking::AdHocWhitelistCheckSanitizer or
28+
guard instanceof NumberGuard or
29+
guard instanceof TypeOfSanitizer
2830
}
2931

3032
// override to require that there is a path without unmatched return steps

javascript/ql/lib/semmle/javascript/security/dataflow/UnvalidatedDynamicMethodCallCustomizations.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,14 @@ module UnvalidatedDynamicMethodCall {
108108
label instanceof MaybeNonFunction
109109
}
110110
}
111+
112+
/** A guard that checks whether `x` is a number. */
113+
class NumberGuard extends TaintTracking::SanitizerGuardNode instanceof DataFlow::CallNode {
114+
Expr x;
115+
boolean polarity;
116+
117+
NumberGuard() { TaintTracking::isNumberGuard(this, x, polarity) }
118+
119+
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = polarity }
120+
}
111121
}

javascript/ql/lib/semmle/javascript/security/dataflow/UnvalidatedDynamicMethodCallQuery.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Configuration extends TaintTracking::Configuration {
4040

4141
override predicate isSanitizer(DataFlow::Node nd) { super.isSanitizer(nd) }
4242

43+
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) {
44+
guard instanceof NumberGuard or
45+
guard instanceof FunctionCheck
46+
}
47+
4348
override predicate isAdditionalFlowStep(
4449
DataFlow::Node src, DataFlow::Node dst, DataFlow::FlowLabel srclabel,
4550
DataFlow::FlowLabel dstlabel

javascript/ql/lib/semmle/javascript/security/dataflow/XssThroughDomQuery.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Configuration extends TaintTracking::Configuration {
2828
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) {
2929
guard instanceof TypeTestGuard or
3030
guard instanceof UnsafeJQuery::PropertyPresenceSanitizer or
31+
guard instanceof UnsafeJQuery::NumberGuard or
3132
guard instanceof DomBasedXss::SanitizerGuard
3233
}
3334

0 commit comments

Comments
 (0)