go-ethereum/graphql
gohan 1c74f23761
graphql: add query depth limit to prevent DoS attacks (#32344)
## Summary

This PR addresses a DoS vulnerability in the GraphQL service by
implementing a maximum query depth limit. While #26026 introduced
timeout handling, it didn't fully mitigate the attack vector where
deeply nested queries can still consume excessive CPU and memory
resources before the timeout is reached.

## Changes
- Added `maxQueryDepth` constant (set to 20) to limit the maximum
nesting depth of GraphQL queries
- Applied the depth limit using `graphql.MaxDepth()` option when parsing
the schema
- Added test case `TestGraphQLMaxDepth` to verify that queries exceeding
the depth limit are properly rejected

## Security Impact

Without query depth limits, malicious actors could craft deeply nested
queries that:
  - Consume excessive CPU cycles during query parsing and execution
  - Allocate large amounts of memory for nested result structures
- Potentially cause service degradation or outages even with timeout
protection

This fix complements the existing timeout mechanism by preventing
resource-intensive queries from being executed in the first place.

## Testing

Added `TestGraphQLMaxDepth` which verifies that queries with nesting
depth > 20 are rejected with a `MaxDepthExceeded` error.

## References
  - Original issue: #26026
- Related security best practices:
https://www.howtographql.com/advanced/4-security/

---------

Co-authored-by: Felix Lange <fjl@twurst.com>
2025-08-19 19:47:47 +08:00
..
internal/graphiql all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
graphiql.go graphql: upgrade UI to v2 (#27294) 2023-05-22 08:15:05 -04:00
graphql.go ethapi: reduce some of the wasted effort in GetTransactionReceipt (#32021) 2025-07-01 15:18:49 +08:00
graphql_test.go graphql: add query depth limit to prevent DoS attacks (#32344) 2025-08-19 19:47:47 +08:00
schema.go internal/ethapi: remove td field from block (#30386) 2024-10-01 11:36:56 +02:00
service.go graphql: add query depth limit to prevent DoS attacks (#32344) 2025-08-19 19:47:47 +08:00