@@ -28,18 +28,24 @@ import { SDLValidationContext, ValidationContext } from './ValidationContext';
28
28
* (see the language/visitor API). Visitor methods are expected to return
29
29
* GraphQLErrors, or Arrays of GraphQLErrors when invalid.
30
30
*
31
+ * Validate will stop validation after a `maxErrors` limit has been reached.
32
+ * Attackers can send pathologically invalid queries to induce a DoS attack,
33
+ * so by default `maxErrors` set to 100 errors.
34
+ *
31
35
* Optionally a custom TypeInfo instance may be provided. If not provided, one
32
36
* will be created from the provided schema.
33
37
*/
34
38
export function validate (
35
39
schema : GraphQLSchema ,
36
40
documentAST : DocumentNode ,
37
41
rules : ReadonlyArray < ValidationRule > = specifiedRules ,
38
- options : { maxErrors ?: number } = { maxErrors : undefined } ,
42
+ options ? : { maxErrors ?: number } ,
39
43
40
44
/** @deprecated will be removed in 17.0.0 */
41
45
typeInfo : TypeInfo = new TypeInfo ( schema ) ,
42
46
) : ReadonlyArray < GraphQLError > {
47
+ const maxErrors = options ?. maxErrors ?? 100 ;
48
+
43
49
devAssert ( documentAST , 'Must provide document.' ) ;
44
50
// If the schema used for validation is invalid, throw an error.
45
51
assertValidSchema ( schema ) ;
@@ -51,7 +57,7 @@ export function validate(
51
57
documentAST ,
52
58
typeInfo ,
53
59
( error ) => {
54
- if ( options . maxErrors != null && errors . length >= options . maxErrors ) {
60
+ if ( errors . length >= maxErrors ) {
55
61
errors . push (
56
62
new GraphQLError (
57
63
'Too many validation errors, error limit reached. Validation aborted.' ,
0 commit comments