Protecting GraphQL APIs from malicious queries
Jun 12, 2023John Cosgrove, Ilya Andreev

Summary
This post details the development of a custom, low-latency, no-heap-allocation GraphQL parser for Cloudflare's API Gateway. The parser tokenizes GraphQL queries to calculate query depth and size without building a full Abstract Syntax Tree, enabling the collection of these metrics for security analysis. It handles both JSON-encoded and raw GraphQL requests, using `serde_raw_value` for JSON parsing to avoid heap allocations. The parser achieves a p95 parsing time of 25 microseconds and introduces three new Wirefilter fields for customers to implement GraphQL-specific security rules.
More from the Website Security & Threat Management trail
- We tested our own WAF with frontier AI models. Here’s what we found
- When scanners miss the attack: how Cloudflare Client-Side Security protects storefronts
- Introducing automatic remediation policies with Cloudflare CASB
- Introducing context-aware vulnerability discovery and remediation with Cloudflare Managed Defense and OpenAI Daybreak Models
- Introducing Adaptive Intelligence: undermining the economics of every bot attack