A REST API has many routes, and each one gets reviewed, rate limited and protected on its own. A GraphQL API has one route, and the client decides what it does. The same POST /graphql can read one field or walk the whole object graph, send one mutation or a thousand, and ask the server to describe itself in full.
That shifts where security has to live. An auth check on the endpoint proves only that the caller is signed in. Whether they may read this invoice, this customer's email or this admin field is decided field by field, in resolvers. This post covers the four GraphQL-specific problems: authorization, introspection, query cost and batching, with Apollo-style code.
Authorization belongs in resolvers
Every field in the schema is reachable from every query that can reach its parent type. A nested field opens a path the original author never had in mind.
const resolvers = {
Query: {
invoice: (_parent, { id }, ctx) => ctx.db.invoice.findUnique({ where: { id } }),
},
Invoice: {
customer: (invoice, _args, ctx) =>
ctx.db.customer.findUnique({ where: { id: invoice.customerId } }),
},
};Any signed-in user can fetch any invoice by id, and from it the customer record with every field the Customer type exposes. The OWASP GraphQL Cheat Sheet asks for authorization on every requested object, on both edges and nodes, to prevent IDOR and broken function-level authorization. It also calls out root node and nodes fields, which fetch any object by global id and need the same checks.
import { GraphQLError } from "graphql";
function requireUser(ctx) {
if (!ctx.user) throw new GraphQLError("Not signed in", { extensions: { code: "UNAUTHENTICATED" } });
return ctx.user;
}
const resolvers = {
Query: {
invoice: (_parent, { id }, ctx) => {
const user = requireUser(ctx);
return ctx.db.invoice.findFirst({ where: { id, accountId: user.accountId } });
},
},
Customer: {
email: (customer, _args, ctx) =>
canSeeContactDetails(ctx.user, customer) ? customer.email : null,
},
};Two habits keep this manageable. Put ownership into the query itself, so a resolver cannot return another tenant's row even if a check is forgotten higher up. And put shared rules in a business layer that both GraphQL and REST call, so the two APIs cannot drift apart. Mutations and subscriptions need the same treatment: a subscription resolver that streams events without checking the subscriber is a data feed for anyone with a socket.
Introspection: useful in development, a map in production
Introspection queries (__schema, __type) return every type, field, argument and mutation. In development that powers tooling. In production it hands an attacker the complete list of operations, including the internal ones nobody linked from the UI.
Apollo Server turns introspection off by default when NODE_ENV is production. Problems come from two places: services that do not set NODE_ENV, and configs that pass introspection: true with no environment check. GraphiQL and other playgrounds enabled in production have the same effect, because they fetch the schema by introspection.
Turning introspection off is not a security boundary. The OWASP sheet points out that "Did you mean" field suggestions in error messages can leak field names, and fields can be guessed. Disable suggestions where your server supports it, but treat a hidden schema as noise reduction, not protection. Every field must be safe to call by someone who knows its name.
Depth and cost: one request, a hundred million nodes
GraphQL types often reference each other in cycles: users have friends who are users, orders have products that have orders. A client can nest those relations as deep as the server allows:
query {
me {
friends(first: 100) {
friends(first: 100) {
friends(first: 100) {
friends(first: 100) { name }
}
}
}
}
}That is up to 100 to the fourth power, a hundred million resolver results, from one small HTTP request. The OWASP sheet notes that depth and amount are unlimited by default in most implementations. Three controls, used together:
- Depth limit. Reject queries nested beyond a fixed depth at validation time.
- Amount limit. Cap
first,limitand page size arguments in the schema or resolver, and paginate every list. - Cost analysis. Give fields a cost and reject queries above a budget. It is harder to tune, so start with depth and amount limits.
Timeouts on resolvers and on the whole request are the backstop.
Batching and aliases: a thousand attempts in one request
Aliases let a client call the same field many times in one operation under different names. That turns any rate limit counted per HTTP request into a rate limit per thousand attempts:
mutation {
a1: verifyOtp(code: "000000") { ok }
a2: verifyOtp(code: "000001") { ok }
a3: verifyOtp(code: "000002") { ok }
}Array batching (several operations in one JSON array) does the same at the transport level. Apollo Server rejects batched HTTP requests unless allowBatchedHttpRequests is enabled. Aliases are part of the language, so they cannot be switched off. Count attempts inside the sensitive resolver itself, per account and per target, not at the HTTP layer, and cap the number of root fields or aliases a single operation may contain. The OWASP sheet recommends limiting object requests in code and disabling batching for sensitive objects.
A safer server configuration
import { ApolloServer } from "@apollo/server";
import depthLimit from "graphql-depth-limit";
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: process.env.NODE_ENV !== "production",
validationRules: [depthLimit(8)],
formatError: (formatted) =>
formatted.extensions?.code === "INTERNAL_SERVER_ERROR"
? { message: "Internal error", extensions: { code: "INTERNAL_SERVER_ERROR" } }
: formatted,
});This keeps introspection to non-production, rejects queries deeper than eight levels, and masks internal error messages. Batching stays off because allowBatchedHttpRequests is not set, and Apollo's CSRF prevention stays on by default. Amount limits and per-resolver attempt counting still have to be written into the schema and resolvers.
Checklist
| Area | Check |
|---|---|
| Authorization | Every resolver that loads by id scopes to the caller; sensitive fields have field-level checks; node(id) is checked |
| Mutations and subscriptions | Each one authenticates and authorizes on its own |
| Introspection | Off in production, playgrounds off, suggestions hidden where supported |
| Cost | Depth limit, page size caps, cost budget for heavy fields, timeouts |
| Batching | Array batching off or capped; aliases capped; attempts counted per account in sensitive resolvers |
| Errors | Internal errors masked; no user data or ids in error messages |
Finding GraphQL issues with Vulkro
Vulkro Core checks Apollo, graphql-js, Strawberry, Ariadne, gqlgen and Hot Chocolate servers as part of an ordinary scan:
vulkro scan . --format json > findings.json
vulkro explain GQL-002| Rule | What it flags |
|---|---|
graphql-introspection-enabled-in-production, graphql-introspection-explicitly-enabled |
Introspection or a playground on with no environment gate |
GQL-002, GQL-003 |
No depth limit; no query complexity analysis |
GQL-004, GQL-005 |
Batched requests enabled with no cap; expensive fields aliased many times |
GQL-006, GQL-009 |
Mutation or subscription resolvers with no auth check |
GQL-013 |
A sensitive field with no field-level authorization |
GQL-012, GQL-021 |
No error masking; user data in error messages |
The GraphQL, gRPC and WebSocket page lists the full set. Resolver arguments are also treated as untrusted input, like route parameters, so an args value that reaches a SQL string is reported by the same taint rule as a REST handler (js-taint-sql-001 in JavaScript and TypeScript), while the parent value is not. Whether a given user may see a given record is a business rule, so object-level checks that depend on it are reported as not checked rather than passed.
FAQ
Should GraphQL introspection be disabled in production?
Yes, for public APIs. It gives anyone the full schema, including internal operations. Treat it as noise reduction, not protection: field suggestions and guessing can still reveal names, so every field must enforce its own authorization.
How do you limit GraphQL query depth?
Add a validation rule that rejects queries nested beyond a fixed depth, such as depthLimit(8) from the graphql-depth-limit package, and combine it with caps on list sizes and a request timeout. Cost analysis adds a finer budget once those are in place.
What is a GraphQL alias batching attack?
Aliases let one operation call the same field many times under different names. Attackers use it to send hundreds of login or OTP attempts in one HTTP request, past rate limits that count requests. Count attempts inside the resolver and cap aliases per operation.
See the API security use case and the attack paths docs, or start a 14-day trial. Related: OWASP API Security Top 10 from source code and IDOR prevention in Express and NestJS.