Skip to content

Conversation

erikhfj
Copy link
Member

@erikhfj erikhfj commented Nov 26, 2026

Summary

Closes #143.

Follow-up to @evenkkl's review of #136: fragments targeting __Schema (for example query { __schema { ...S0 } } with S0..S10 mutually spreading each other) previously bypassed createQueryLimitsRule's introspection subtree skip and triggered multi-second (or multi-minute) validation inside graphql-js built-in rules (NoFragmentCyclesRule / OverlappingFieldsCanBeMergedRule) in both dev and production before noIntrospectionRule finished rejecting the request.

Changes

  • server/src/security.ts: Added DEFAULT_MAX_FRAGMENT_SPREADS = 20 and checkFragmentSpreadLimit(document, maxFragmentSpreads), which walks the parsed AST iteratively without expanding spreads and short-circuits immediately once total FragmentSpread nodes exceed the cap.
  • server/src/app.ts: Wired GRAPHQL_MAX_FRAGMENT_SPREADS in configFromEnv and ServerConfig.maxFragmentSpreads, and ran checkFragmentSpreadLimit(params.documentAST, maxFragmentSpreads) at the start of securityPlugin.onValidate via setResult([error]) so graphql-js validation is skipped entirely when the cap is exceeded.
  • server/src/security.test.ts: Added unit and HTTP regression tests covering:
    • The 11-fragment cyclic __Schema bomb in both dev (isProduction: false) and prod (isProduction: true), asserting rejection before validation in < 100 ms (measured ~2 ms unit / ~25 ms combined dev+prod HTTP test).
    • Queries at (20) vs over (21) the fragment spread cap, plus getIntrospectionQuery() (8 spreads) continuing to succeed in dev and reject in prod.
    • configFromEnv parsing of GRAPHQL_MAX_FRAGMENT_SPREADS.
  • README.md & docs/AI-USAGE.md: Documented GRAPHQL_MAX_FRAGMENT_SPREADS in the server environment variables table and logged #143 in docs/AI-USAGE.md.

Verification

  • pnpm format:check && pnpm lint && pnpm typecheck && pnpm test && pnpm build — all passed locally (server/src/security.test.ts: 24/24 passed).

@erikhfj erikhfj requested a review from a team as a code owner November 26, 2026 21:20
Copy link
Member

@robertky robertky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the fragment-spread limit implementation. It counts spread nodes in the parsed document and rejects queries over the configured limit before GraphQL validation runs, avoiding recursive fragment expansion in the limit check. The default is configurable and documented, and the tests cover the limit, introspection, HTTP behavior, and the cyclic-fragment regression case. The current CI checks are passing. Approving.

@erikhfj erikhfj merged commit 97c413f into main Nov 26, 2026
3 checks passed
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cap total fragment spreads in GraphQL onValidate to prevent __Schema validation DoS
2 participants