Skip to content

Commit 5340530

Browse files
committed
use the number guard in existing queries that contained typeof checks
1 parent d6721ec commit 5340530

10 files changed

+74
-20
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,25 @@ 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+
)
1183+
or
1184+
isTypeofGuard(guard.asExpr(), operand, "number") and
1185+
polarity = guard.asExpr().(EqualityTest).getPolarity()
1186+
}
1187+
11691188
/** DEPRECATED. This class has been renamed to `MembershipTestSanitizer`. */
11701189
deprecated class StringInclusionSanitizer = MembershipTestSanitizer;
11711190

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: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,14 @@ module UnsafeShellCommandConstruction {
286286
}
287287
}
288288

289-
/**
290-
* A guard of the form `isNaN(x)`, which sanitizes `x` in its "else" branch.
291-
*/
292-
class NaNGuard extends TaintTracking::SanitizerGuardNode instanceof DataFlow::CallNode {
289+
/** A guard that checks whether `x` is a number. */
290+
class NumberGuard extends TaintTracking::SanitizerGuardNode instanceof DataFlow::CallNode {
293291
Expr x;
292+
boolean polarity;
294293

295-
NaNGuard() {
296-
this = DataFlow::globalVarRef("isNaN").getACall() and
297-
(
298-
x = this.getArgument(0).asExpr()
299-
or
300-
exists(DataFlow::CallNode parse |
301-
parse = DataFlow::globalVarRef(["parseInt", "parseFloat"]).getACall()
302-
|
303-
parse = this.getArgument(0) and
304-
x = parse.getArgument(0).asExpr()
305-
)
306-
)
307-
}
294+
NumberGuard() { TaintTracking::isNumberGuard(this, x, polarity) }
308295

309-
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = false }
296+
override predicate sanitizes(boolean outcome, Expr e) { e = x and outcome = polarity }
310297
}
311298

312299
private import semmle.javascript.dataflow.internal.AccessPaths

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Configuration extends TaintTracking::Configuration {
2525
override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode guard) {
2626
guard instanceof PathExistsSanitizerGuard or
2727
guard instanceof TaintTracking::AdHocWhitelistCheckSanitizer or
28-
guard instanceof NaNGuard or
28+
guard instanceof NumberGuard or
2929
guard instanceof TypeOfSanitizer
3030
}
3131

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)