Skip to content

Commit 8b4c3c4

Browse files
committed
refactor ValidationCall back to a CallNode
1 parent be7abed commit 8b4c3c4

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

javascript/ql/src/semmle/javascript/JsonSchema.qll

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@ import javascript
88
* Provides classes and predicates for working with JSON schema libraries.
99
*/
1010
module JsonSchema {
11-
/** A node that validates an input against a JSON schema. */
12-
abstract class ValidationCall extends DataFlow::Node {
11+
/** A call that validates an input against a JSON schema. */
12+
abstract class ValidationCall extends DataFlow::CallNode {
1313
/** Gets the data flow node whose value is being validated. */
1414
abstract DataFlow::Node getInput();
1515

16-
/** Gets the return value that indicates successful validation. */
16+
/**
17+
* Gets if the return value indicates successfull or unsuccessful validation.
18+
* Is not defined if the return value from this call does not directly
19+
* indicate success.
20+
*/
1721
boolean getPolarity() { result = true }
22+
23+
/**
24+
* Gets a value that indicates whether the validation was successful.
25+
*/
26+
DataFlow::Node getAValidationResultAccess(boolean polarity) {
27+
result = this and polarity = getPolarity()
28+
}
1829
}
1930

2031
/** A data flow node that is used a JSON schema. */
@@ -89,7 +100,7 @@ module JsonSchema {
89100
}
90101

91102
/** A call to the `validate` method of `ajv`. */
92-
class AjvValidationCall extends ValidationCall, DataFlow::CallNode {
103+
class AjvValidationCall extends ValidationCall {
93104
Instance instance;
94105
int argIndex;
95106

@@ -161,20 +172,20 @@ module JsonSchema {
161172
}
162173

163174
/**
164-
* A read of the `error` property from a validation result, seen as a `ValidationCall`.
165-
* If `error` exists, then the validation failed.
175+
* A call to the `validate` method from the [`joi`](https://npmjs.org/package/joi) library.
176+
* The `error` property in the result indicates whether the validation was successful.
166177
*/
167-
class JoiValidationErrorRead extends ValidationCall {
168-
API::CallNode validateCall;
178+
class JoiValidationErrorRead extends ValidationCall, API::CallNode {
179+
JoiValidationErrorRead() { this = objectSchema().getMember("validate").getACall() }
169180

170-
JoiValidationErrorRead() {
171-
validateCall = objectSchema().getMember("validate").getACall() and
172-
this = validateCall.getReturn().getMember("error").getAnImmediateUse()
173-
}
181+
override DataFlow::Node getInput() { result = this.getArgument(0) }
174182

175-
override DataFlow::Node getInput() { result = validateCall.getArgument(0) }
183+
override boolean getPolarity() { none() }
176184

177-
override boolean getPolarity() { result = false }
185+
override DataFlow::Node getAValidationResultAccess(boolean polarity) {
186+
result = this.getReturn().getMember("error").getAnImmediateUse() and
187+
polarity = false
188+
}
178189
}
179190
}
180191
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ module TaintedObject {
115115
*/
116116
private class JsonSchemaValidationGuard extends SanitizerGuard {
117117
JsonSchema::ValidationCall call;
118+
boolean polarity;
118119

119-
JsonSchemaValidationGuard() { this = call }
120+
JsonSchemaValidationGuard() { this = call.getAValidationResultAccess(polarity) }
120121

121122
override predicate sanitizes(boolean outcome, Expr e, FlowLabel label) {
122-
outcome = call.getPolarity() and
123+
outcome = polarity and
123124
e = call.getInput().asExpr() and
124125
label = label()
125126
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ module ExceptionXss {
618618
JsonSchemaValidationError() {
619619
this = any(JsonSchema::Ajv::Instance i).getAValidationError().getAnImmediateUse()
620620
or
621-
this = any(JsonSchema::Joi::JoiValidationErrorRead r)
621+
this = any(JsonSchema::Joi::JoiValidationErrorRead r).getAValidationResultAccess(_)
622622
}
623623

624624
override string getDescription() { result = "JSON schema validation error" }

0 commit comments

Comments
 (0)