Summary
Mercurius, a GraphQL adapter for Fastify, fails to apply the configured queryDepth limit to subscription operations received over WebSocket connections.
As a result, a remote client can send arbitrarily deep subscription queries that bypass depth restrictions, potentially causing denial of service on schemas with recursive or highly nested types.
Affected context
This vulnerability appears in Mercurius-based GraphQL APIs that:
- Use WebSocket-based GraphQL subscriptions
- Rely on Mercurius' queryDepth configuration to limit query complexity
- Expose schemas containing recursive types or deeply nestable fields
The issue is specific to subscription operations over WebSocket; HTTP queries and mutations still correctly enforce the depth limit, which can create a false sense of protection if subscriptions are assumed to be covered by the same policy.
Exploit preconditions
- Attack surface: Public or internal GraphQL endpoints that accept subscriptions over WebSocket using Mercurius.
- Required access: Ability to open a WebSocket connection as a client (typically unauthenticated or minimally authenticated, depending on API design).
- Trigger point: Sending a deeply nested GraphQL subscription operation that exceeds the intended queryDepth configuration.
- Why exploitation is possible: The subscription handling path parses and executes queries without invoking the queryDepth validation logic, effectively skipping an intended authorization/policy check on query complexity.
Why this happens
The core issue is a missing enforcement of an existing security/policy control in one execution path.
Mercurius implements a queryDepth mechanism intended to restrict how deeply nested GraphQL operations can be, limiting resource consumption and preventing abusive queries.
While this depth check is applied for HTTP-based queries and mutations, the subscription-over-WebSocket code path processes operations without calling the same validation routine.
This maps to CWE-863: Incorrect Authorization in the sense of incomplete policy enforcement: a configured authorization/guard (maximum query depth) is selectively applied, leaving a gap where certain operation types are not subject to the intended restriction.
Potential impact
- Remote clients can submit extremely deep subscription queries that bypass configured depth limits.
- Servers may experience excessive CPU and memory usage as recursive or deeply nested fields are resolved on each subscription event.
- Degradation of performance for other users, including increased latency and timeouts.
- Complete denial of service if resource exhaustion leads to process crashes or unresponsiveness.
- Amplified impact on multi-tenant or shared infrastructure where one abusive client can affect many applications.
General mitigation guidance
Mitigation focuses on ensuring consistent application of query complexity controls across all GraphQL operation types and transport mechanisms.
Recommended patterns include:
- Centralize query validation so that depth and complexity checks run for queries, mutations, and subscriptions, regardless of protocol (HTTP or WebSocket).
- Apply a unified request/operation pipeline that enforces limits (depth, cost, rate) before execution begins.
- Introduce additional complexity limits such as maximum selection count, maximum field aliases, and recursion guards, especially for schemas with recursive types.
- Enforce strict authentication and authorization policies for subscription endpoints, reducing anonymous or low-trust access to long-lived connections.
- Implement rate limiting and connection quotas per client, token, or IP to limit abusive subscription usage.
- Design schemas to avoid unbounded recursion or provide server-side caps on list sizes and pagination to limit per-event workload.
How teams detect issues like this
Detecting this kind of issue typically involves both code-level and operational strategies:
- Static/Code review:
- Verify that query validation (depth, complexity, cost) is invoked in all operation handlers, including WebSocket subscription handlers.
- Look for duplicated or divergent code paths for HTTP and WebSocket handling; ensure shared middleware or validation logic is factored into common utilities.
- Check configuration handling to confirm that queryDepth or similar limits are not conditionally bypassed for subscriptions.
- Automated analysis:
- Use static analysis or linters with custom rules to detect GraphQL execution calls that bypass registered validation middleware.
- Adopt security-focused code scanning that flags inconsistent enforcement of policy checks across protocol implementations.
- Runtime monitoring:
- Instrument GraphQL servers to log operation depth/complexity and monitor for anomalous patterns, such as extremely nested subscription operations.
- Track resource utilization (CPU, memory, event-loop lag) correlated with subscription activity, and alert on spikes tied to a small set of clients or operations.
- Apply WebSocket connection and message-level observability (including basic shape/size of queries) to identify abuse patterns.

