|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with JSON schema libraries. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +/** |
| 8 | + * Provides classes and predicates for working with JSON schema libraries. |
| 9 | + */ |
| 10 | +module JsonSchema { |
| 11 | + /** A call that validates an input against a JSON schema. */ |
| 12 | + abstract class ValidationCall extends DataFlow::CallNode { |
| 13 | + /** Gets the data flow node whose value is being validated. */ |
| 14 | + abstract DataFlow::Node getInput(); |
| 15 | + |
| 16 | + /** Gets the return value that indicates successful validation. */ |
| 17 | + boolean getPolarity() { result = true } |
| 18 | + } |
| 19 | + |
| 20 | + /** A data flow node that is used a JSON schema. */ |
| 21 | + abstract class SchemaRoot extends DataFlow::Node { } |
| 22 | + |
| 23 | + /** An object literal with a `$schema` property indicating it is the root of a JSON schema. */ |
| 24 | + private class SchemaNodeByTag extends SchemaRoot, DataFlow::ObjectLiteralNode { |
| 25 | + SchemaNodeByTag() { |
| 26 | + getAPropertyWrite("$schema").getRhs().getStringValue().matches("%//json-schema.org%") |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** Gets a data flow node that is part of a JSON schema. */ |
| 31 | + private DataFlow::SourceNode getAPartOfJsonSchema(DataFlow::TypeBackTracker t) { |
| 32 | + t.start() and |
| 33 | + result = any(SchemaRoot n).getALocalSource() |
| 34 | + or |
| 35 | + result = getAPartOfJsonSchema(t.continue()).getAPropertySource() |
| 36 | + or |
| 37 | + exists(DataFlow::TypeBackTracker t2 | result = getAPartOfJsonSchema(t2).backtrack(t2, t)) |
| 38 | + } |
| 39 | + |
| 40 | + /** Gets a data flow node that is part of a JSON schema. */ |
| 41 | + DataFlow::SourceNode getAPartOfJsonSchema() { |
| 42 | + result = getAPartOfJsonSchema(DataFlow::TypeBackTracker::end()) |
| 43 | + } |
| 44 | + |
| 45 | + /** Provides a model of the `ajv` library. */ |
| 46 | + module Ajv { |
| 47 | + /** A method on `Ajv` that returns `this`. */ |
| 48 | + private string chainedMethod() { |
| 49 | + result = |
| 50 | + ["addSchema", "addMetaSchema", "removeSchema", "addFormat", "addKeyword", "removeKeyword"] |
| 51 | + } |
| 52 | + |
| 53 | + /** An instance of `ajv`. */ |
| 54 | + class Instance extends API::InvokeNode { |
| 55 | + Instance() { this = API::moduleImport("ajv").getAnInstantiation() } |
| 56 | + |
| 57 | + /** Gets the data flow node holding the options passed to this `Ajv` instance. */ |
| 58 | + DataFlow::Node getOptionsArg() { result = getArgument(0) } |
| 59 | + |
| 60 | + /** Gets an API node that refers to this object. */ |
| 61 | + API::Node ref() { |
| 62 | + result = getReturn() |
| 63 | + or |
| 64 | + result = ref().getMember(chainedMethod()).getReturn() |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets an API node for a function produced by `new Ajv().compile()` or similar. |
| 69 | + * |
| 70 | + * Note that this does not include the instance method `new Ajv().validate` as its |
| 71 | + * signature is different. |
| 72 | + */ |
| 73 | + API::Node getAValidationFunction() { |
| 74 | + result = ref().getMember(["compile", "getSchema"]).getReturn() |
| 75 | + or |
| 76 | + result = ref().getMember("compileAsync").getPromised() |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets an API node that refers to an error produced by this Ajv instance. |
| 81 | + */ |
| 82 | + API::Node getAValidationError() { |
| 83 | + exists(API::Node base | base = [ref(), getAValidationFunction()] | |
| 84 | + result = base.getMember("errors") |
| 85 | + or |
| 86 | + result = base.getMember("errorsText").getReturn() |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** A call to the `validate` method of `ajv`. */ |
| 92 | + class AjvValidationCall extends ValidationCall { |
| 93 | + Instance instance; |
| 94 | + int argIndex; |
| 95 | + |
| 96 | + AjvValidationCall() { |
| 97 | + this = instance.ref().getMember("validate").getACall() and argIndex = 1 |
| 98 | + or |
| 99 | + this = instance.getAValidationFunction().getACall() and argIndex = 0 |
| 100 | + } |
| 101 | + |
| 102 | + override DataFlow::Node getInput() { result = getArgument(argIndex) } |
| 103 | + |
| 104 | + /** Gets the argument holding additional options to the call. */ |
| 105 | + DataFlow::Node getOwnOptionsArg() { result = getArgument(argIndex + 1) } |
| 106 | + |
| 107 | + /** Gets a data flow passed as the extra options to this validation call or to the underlying `Ajv` instance. */ |
| 108 | + DataFlow::Node getAnOptionsArg() { |
| 109 | + result = getOwnOptionsArg() |
| 110 | + or |
| 111 | + result = instance.getOptionsArg() |
| 112 | + } |
| 113 | + |
| 114 | + /** Gets the ajv instance doing the validation. */ |
| 115 | + Instance getAjvInstance() { result = instance } |
| 116 | + } |
| 117 | + |
| 118 | + private class AjvSchemaNode extends SchemaRoot { |
| 119 | + AjvSchemaNode() { |
| 120 | + this = |
| 121 | + any(Instance i) |
| 122 | + .ref() |
| 123 | + .getMember(["addSchema", "validate", "compile", "compileAsync"]) |
| 124 | + .getParameter(0) |
| 125 | + .getARhs() |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments