GraphQL security: why one endpoint needs different limits
GraphQL security is the set of controls that keep a GraphQL API from being abused through the very flexibility that makes it useful. The client writes the query, so it also chooses how deep, how wide and how many operations one request asks for. That shifts risk toward resource use and authorization, which the OWASP API Security Top 10 2023 names as API4 and API1 and API3. This guide belongs to API security and covers what to set in the server and what a filter in front of it can and cannot see.
Quick facts
- Defined by
- The GraphQL specification, which describes the query language and how a service runs it
- Operations
- Query (read), mutation (write, then fetch) and subscription (long-lived, driven by source events), per section 2.3 of the spec
- Transport
- Not fixed by the spec: it requires no specific serialization format, and subscriptions need no specific transport
- Main risks
- Unrestricted resource consumption, broken object and property level authorization, an exposed schema, verbose errors
- OWASP sources
- GraphQL Cheat Sheet and API4:2023
- Does a WAF fix a GraphQL flaw?
- No. Depth limits, cost checks and authorization live in the server code. This site does not document Bridge WAF as parsing GraphQL queries
Sources: the GraphQL specification, October 2021 edition; the OWASP GraphQL Cheat Sheet; and the OWASP API Security Top 10 2023.
What changes with GraphQL
A conventional REST endpoint has a fixed cost: one route, one handler, a response whose shape the server decided. A GraphQL service publishes a typed schema and lets the client describe the response it wants. The spec says a field is one piece of information available to request within a selection set, and that a client may rename a field in the response with an alias (sections 2.5 and 2.7). Fragments let a client reuse selections, and variables parameterize an operation (sections 2.8 and 2.10).
Two consequences follow. First, a request is not a fixed unit of work. The same URL can carry a one-line lookup or a query that touches thousands of records, and a count of requests per minute does not tell them apart. Second, the route no longer tells you what is being accessed. Access rules written per URL do not carry over, because the object and the fields are named inside the body. Query cost is one case of unrestricted resource consumption.
The spec does not pick a transport. It says GraphQL does not require a specific serialization format (section 7.2), and for subscriptions it requires no specific transport (section 6.2.3). A deployment chooses one. When that choice is HTTP, a filter in front of the server sees an ordinary request with the operation text in its body, and that body is where everything below happens. For the verbs involved, see HTTP methods.
One more rule worth knowing: for a mutation, the spec says the top-level fields should be executed serially, and that they are expected to cause side effects on the underlying data (section 6.2.2). Read-only fields may resolve in any order. So a request holding several mutations changes data several times, which matters for the batching problem below.
Depth, aliases and batching
API4:2023, Unrestricted Resource Consumption, opens with the observation that satisfying a request takes network bandwidth, CPU, memory and storage. It lists the number of operations in one client request, with GraphQL batching as the example, among the things an API should bound. The GraphQL Cheat Sheet turns that into three separate problems.
Depth and amount
A schema can contain cycles: an author has books, and a book has an author. Nothing in the query language stops a client from alternating between the two many times.
query {
author(id: "a1") {
books {
author {
books { title }
}
}
}
}
The cheat sheet's advice is to set limits on both depth and amount, and to add pagination so a single response cannot return an unbounded list. It also recommends timeouts at the application layer, the infrastructure layer or both, and says application-level timeouts are usually the more effective. Where depth and pagination are not enough, it suggests query cost analysis with a maximum allowed cost per query, after first confirming you need it.
Aliases and batching
An alias renames a field in the response, which also means the same field can be requested several times in one operation under different names. The cheat sheet shows aliases as one way to do batching: many logical operations in one HTTP request. A limit of so many requests per minute counts this as one.
query {
first: product(id: "1") { name }
second: product(id: "2") { name }
}
API4:2023 turns this into a scenario. A GraphQL endpoint lets users upload a profile picture and generates several thumbnails from each upload. The API has rate limiting and a size check, but an attacker can send many upload mutations in a single batched request, and because the API does not limit how often the operation can be attempted, server memory runs out. The same mechanism applies to anything you meant to slow down, such as password checks or code guesses, which is why the cheat sheet says to prevent batching for sensitive objects that must not be brute forced and to limit how many queries can run at once.
The lesson is that the unit to limit is the operation, not the request. A limit by cost, depth or operation count needs to know what the query asks for, and that knowledge sits inside the GraphQL server. Rate limiting at the edge still helps, but as a coarser second layer: it caps how often one client can call at all.
Authorization at every field
Authentication tells the server who is calling. Authorization decides what that caller may see and change, and GraphQL makes it easy to miss a spot. Every object can be reached by several paths in the schema, so a check at the entry point is not enough. The cheat sheet's rule is to always validate that the requester is authorized to view or modify the data requested, with checks on both edges and nodes of the graph, and to apply access control to mutations as well.
Two OWASP categories map directly onto this. API1:2023, Broken Object Level Authorization, is the case where a caller supplies the identifier of an object that is not theirs; it is described on the BOLA page and is the same idea as IDOR. API3:2023, Broken Object Property Level Authorization, covers the case where the object is allowed but one of its fields is not. For GraphQL that second case is concrete: the schema may expose a field, and the cheat sheet says to add a code check so that only permitted requesters can read it.
Put the rule where the data is resolved, in the resolver or in a layer under it, not in the client and not in the query validation step. A hidden field in the UI protects nothing while the schema still allows the query. Authentication choices, such as tokens, are on the API authentication page.
Introspection, errors and input
The spec describes GraphQL as introspective: a service's type system can be queried through the language itself (section 1). That is how schema explorers and code generators work, and it is also a map of the API for anyone who can send a query. The cheat sheet says to disable introspection queries system-wide in any publicly accessible environment, or, if the platform cannot do that, to filter them for approved consumers. It says to disable GraphiQL and similar exploration tools in public environments too, and to turn off field-suggestion hints once introspection is off, since suggestions can leak names.
Treat that as hygiene, not protection. A schema that cannot be listed can still be probed, and the objects behind it are safe only if the authorization section above is done. Introspection is one reason to test your API from the outside, since a tester starts from whatever the schema reveals.
Errors
The cheat sheet says GraphQL servers should not return stack traces or run in debug mode when reachable by the public. Mask the error message, and log the detail on your side instead.
Input and injection
Variables parameterize an operation, as section 2.10 describes, but that is the client side of the request. The server still passes values into databases, files and other services. The cheat sheet says to validate all incoming data against an allowlist of valid values, to use specific types, scalars and enums in the schema, and to choose libraries with safe APIs such as parameterized statements. It also warns against letting user input decide where the server connects. Those are the same defenses described for SQL injection.
A server-side checklist
These items come from the cheat sheet and API4:2023. They are controls to set in the GraphQL server, its gateway or its code,.
- Bound depth and amount. Set a maximum query depth and require pagination on lists.
- Bound the number of operations. Limit how many queries or aliased calls one request may carry, and block batching for sensitive objects.
- Consider a cost limit. Query cost analysis assigns a price to a query and rejects the ones above a maximum. Confirm you need it first.
- Set timeouts. Prefer them in the application, and cap memory and CPU in the runtime as API4:2023 advises.
- Authorize every object and every sensitive field, including mutations.
- Switch off introspection and schema tools where the API is publicly reachable, or filter them for approved users.
- Mask errors and keep debug mode off.
- Validate input with allowlists and typed schema, and use parameterized queries behind the resolvers.
Where a WAF helps and where it does not
A filter in front of a GraphQL endpoint sees HTTP requests. It does not know your schema, your object ownership or what a query will cost. It cannot tell that a particular field should be hidden from a particular user, so it cannot fix the authorization flaws above, and it cannot replace depth, cost or batching limits that depend on understanding the query. This site does not document Bridge WAF as parsing GraphQL operations, so do not rely on it for query-aware rules.
What the layer can add is coarser. Per-domain rate limits cap how often one client can call the API, a ceiling that holds even when the application's own checks are incomplete; see per-IP vs per-user limits for what counts as one client, and 429 responses for what a throttled client receives. Named attack categories such as SQL injection can be matched in request bodies, which helps with the input problems above but does not replace parameterized queries. Because batching packs many calls into one request, a request-count limit is a weak guard against it. Complexity-based limits are a concept that belongs to the GraphQL server, and nothing here claims Bridge WAF offers them.
Start in monitor mode: matches are logged, every request is still served, and you can read the monitor-mode log for a while before turning on block. The three modes are in the box below.
Questions
Is GraphQL less secure than REST?
Not by nature, but the risks shift. The client chooses the shape and size of each query, so resource use and field-level authorization need explicit controls in the server. Fixed routes and per-URL rules cover less than they do in a REST API.
Should GraphQL introspection be turned off?
The OWASP GraphQL Cheat Sheet says to disable introspection system-wide in any publicly accessible environment, or to filter it for approved consumers. It reveals the schema, but hiding the schema is not a substitute for authorization checks on every object and field.
What is a GraphQL batching attack?
It packs many operations into one HTTP request, for example through aliases or an array of queries. A limit that counts requests sees one, while the server does the work of many. The cheat sheet advises limiting how many queries can run at once and blocking batching for sensitive objects.
How do you limit an expensive GraphQL query?
In the server: set a maximum depth, require pagination, add timeouts and, if needed, query cost analysis with a maximum cost per query. These checks need to read the query, so they belong in the GraphQL layer, not in a generic edge filter.
Can a WAF protect a GraphQL API?
Only partly. A WAF can log and rate-limit requests and match named attack categories in request bodies, but it does not know your schema or who owns which object. Authorization and query limits stay in the application.