You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ajv treats JSON schemas as trusted as your application code. This security model is based on the most common use case, when the schemas are static and bundled together with the application.
614
+
615
+
If your schemas are received from untrusted sources (or generated from untrusted data) there may be several scenarios you may want to prevent:
616
+
- compiling schemas can cause stack overflow (if they are too deep)
617
+
- compiling schemas can be slow (e.g. [#557](https://github.com/epoberezkin/ajv/issues/557))
618
+
- validating certain data can be slow
619
+
620
+
It is difficult to predict all the scenarios, but at the very least it is recommended to limit the size of untrusted JSON Schemas (e.g. as JSON string length) and the maximum schema object depth (that can be high for relatively small JSON strings). Even that would not prevent slow regular expressions in schemas.
621
+
622
+
Regardless the measures you take, using untrusted schemas increases security risks.
623
+
624
+
625
+
##### Circular references in JavaScript objects
626
+
627
+
Ajv does not support schemas and validated data that have circular references in objects. See [issue #802](https://github.com/epoberezkin/ajv/issues/802).
628
+
629
+
An attempt to compile such schemas or validate such data would cause stack overflow (or will not complete in case of asynchronous validation). Untrusted data can lead to circular references, depending on the parser you use.
630
+
631
+
632
+
##### Security risks of trusted schemas
633
+
634
+
Some keywords in JSON Schemas can lead to very slow validation for certain data. These keywords include (but, most likely, not limited to):
635
+
636
+
-`pattern` and `format` for large strings - use `maxLength` to mitigate
637
+
-`uniqueItems` for large non-scalar arrays - use `maxItems` to mitigate
638
+
-`patternProperties` for large property names - use `propertyNames` to mitigate
639
+
640
+
__Please note__: The suggestions above to prevent slow validation would only work if you do NOT use `allErrors: true` in production code (using it would continue validation after validation errors).
641
+
642
+
You can validate your JSON schemas against [this meta-schema](https://github.com/epoberezkin/ajv/blob/master/lib/refs/json-schema-secure.json) to check that these recommendations are followed:
__Please note__: even following all these recommendation is not a guarantee that validation of untrusted data is absolutely safe - it can still lead to some undesirable situations.
655
+
656
+
608
657
## Filtering data
609
658
610
659
With [option `removeAdditional`](#options) (added by [andyscott](https://github.com/andyscott)) you can filter data during the validation.
0 commit comments