fix: #143 cap total GraphQL fragment spreads in onValidate #144
+182
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Closes #143.
Follow-up to @evenkkl's review of #136: fragments targeting
__Schema(for examplequery { __schema { ...S0 } }withS0..S10mutually spreading each other) previously bypassedcreateQueryLimitsRule's introspection subtree skip and triggered multi-second (or multi-minute) validation insidegraphql-jsbuilt-in rules (NoFragmentCyclesRule/OverlappingFieldsCanBeMergedRule) in both dev and production beforenoIntrospectionRulefinished rejecting the request.Changes
server/src/security.ts: AddedDEFAULT_MAX_FRAGMENT_SPREADS = 20andcheckFragmentSpreadLimit(document, maxFragmentSpreads), which walks the parsed AST iteratively without expanding spreads and short-circuits immediately once totalFragmentSpreadnodes exceed the cap.server/src/app.ts: WiredGRAPHQL_MAX_FRAGMENT_SPREADSinconfigFromEnvandServerConfig.maxFragmentSpreads, and rancheckFragmentSpreadLimit(params.documentAST, maxFragmentSpreads)at the start ofsecurityPlugin.onValidateviasetResult([error])sographql-jsvalidation is skipped entirely when the cap is exceeded.server/src/security.test.ts: Added unit and HTTP regression tests covering:__Schemabomb 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).20) vs over (21) the fragment spread cap, plusgetIntrospectionQuery()(8 spreads) continuing to succeed in dev and reject in prod.configFromEnvparsing ofGRAPHQL_MAX_FRAGMENT_SPREADS.README.md&docs/AI-USAGE.md: DocumentedGRAPHQL_MAX_FRAGMENT_SPREADSin the server environment variables table and logged#143indocs/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).