fix(graphql): memoize fragment depth to bound depth-limit walk - #698
Draft
JiahuiWho wants to merge 2 commits into
Draft
fix(graphql): memoize fragment depth to bound depth-limit walk#698JiahuiWho wants to merge 2 commits into
JiahuiWho wants to merge 2 commits into
Conversation
JiahuiWho
force-pushed
the
fix/graphql-depth-limit-fragment-memoization
branch
from
August 22, 2026 20:03
5f74669 to
6b415aa
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Memoizes GraphQL fragment-depth calculations to mitigate fragment fan-out DoS attacks.
Changes:
- Adds per-operation fragment-depth caching.
- Adds regression coverage for repeated fragment fan-out.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
internal/serve/graphql/depth_limit.go |
Implements memoized depth traversal. |
internal/serve/graphql/depth_limit_test.go |
Tests depth correctness and fan-out termination. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Cache each fragment's computed depth by name in
selectionSetDepthso a fragment is expanded at most once per operation, instead of being re-expanded on every path that reaches it.Why
selectionSetDepthre-expanded a fragment every time it was spread. Since a fragment spread adds no level of its own, that work is redundant, and when fragments fan out into one another (each spreading the next more than once), it compounds into work that grows exponentially with the fragment count, even for a small document with shallow actual depth. Since the reported depth stays low, neither the depth nor complexity limit rejects it.Memoizing each fragment's depth by name bounds the walk to O(fragments + selections) and returns the identical depth value.
Known limitations
N/A
Issue that this PR addresses
https://hackerone.com/reports/3959812
Checklist
PR Structure
allif the changes are broad or impact many packages.Thoroughness
Release