diff --git a/src/validation/validate.ts b/src/validation/validate.ts index f6bc7d0b7b..391083af8a 100644 --- a/src/validation/validate.ts +++ b/src/validation/validate.ts @@ -28,6 +28,10 @@ import { SDLValidationContext, ValidationContext } from './ValidationContext'; * (see the language/visitor API). Visitor methods are expected to return * GraphQLErrors, or Arrays of GraphQLErrors when invalid. * + * Validate will stop validation after a `maxErrors` limit has been reached. + * Attackers can send pathologically invalid queries to induce a DoS attack, + * so by default `maxErrors` set to 100 errors. + * * Optionally a custom TypeInfo instance may be provided. If not provided, one * will be created from the provided schema. */ @@ -35,11 +39,13 @@ export function validate( schema: GraphQLSchema, documentAST: DocumentNode, rules: ReadonlyArray = specifiedRules, - options: { maxErrors?: number } = { maxErrors: undefined }, + options?: { maxErrors?: number }, /** @deprecated will be removed in 17.0.0 */ typeInfo: TypeInfo = new TypeInfo(schema), ): ReadonlyArray { + const maxErrors = options?.maxErrors ?? 100; + devAssert(documentAST, 'Must provide document.'); // If the schema used for validation is invalid, throw an error. assertValidSchema(schema); @@ -51,7 +57,7 @@ export function validate( documentAST, typeInfo, (error) => { - if (options.maxErrors != null && errors.length >= options.maxErrors) { + if (errors.length >= maxErrors) { errors.push( new GraphQLError( 'Too many validation errors, error limit reached. Validation aborted.',