Skip to content

Commit cb48918

Browse files
validation: restrict maximum number of errors to 100 by default (#3283)
1 parent 5ed10ef commit cb48918

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/validation/validate.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,24 @@ import { SDLValidationContext, ValidationContext } from './ValidationContext';
2828
* (see the language/visitor API). Visitor methods are expected to return
2929
* GraphQLErrors, or Arrays of GraphQLErrors when invalid.
3030
*
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+
*
3135
* Optionally a custom TypeInfo instance may be provided. If not provided, one
3236
* will be created from the provided schema.
3337
*/
3438
export function validate(
3539
schema: GraphQLSchema,
3640
documentAST: DocumentNode,
3741
rules: ReadonlyArray<ValidationRule> = specifiedRules,
38-
options: { maxErrors?: number } = { maxErrors: undefined },
42+
options?: { maxErrors?: number },
3943

4044
/** @deprecated will be removed in 17.0.0 */
4145
typeInfo: TypeInfo = new TypeInfo(schema),
4246
): ReadonlyArray<GraphQLError> {
47+
const maxErrors = options?.maxErrors ?? 100;
48+
4349
devAssert(documentAST, 'Must provide document.');
4450
// If the schema used for validation is invalid, throw an error.
4551
assertValidSchema(schema);
@@ -51,7 +57,7 @@ export function validate(
5157
documentAST,
5258
typeInfo,
5359
(error) => {
54-
if (options.maxErrors != null && errors.length >= options.maxErrors) {
60+
if (errors.length >= maxErrors) {
5561
errors.push(
5662
new GraphQLError(
5763
'Too many validation errors, error limit reached. Validation aborted.',

0 commit comments

Comments
 (0)