GraphQL API Security Review: Authorization, Cost Limits, and Errors

GraphQL lets a client request a flexible selection of related data through a schema. That flexibility can reduce over-fetching, but it also moves query shape and complexity into caller-controlled input. A single endpoint may expose many object types, relationships, and mutations, so reviewing only the HTTP route or a top-level authentication check misses important authorization and resource-consumption risks.

A secure GraphQL service validates every request, enforces access at the object and operation level, limits expensive query patterns, and returns only the information a caller is allowed to see. The schema is part of the public attack surface even if an API gateway sees only POST /graphql.

Enforce authorization in resolvers and data access

Figure: GraphQL API Security Review: Authorization, Cost Limits, and Errors
Figure: GraphQL API Security Review: Authorization, Cost Limits, and Errors

Authentication identifies the caller; it does not establish that the caller may read every node returned by a query. Check authorization when resolving protected objects and fields, including nested relationships. Enforce tenant ownership in the data access path so a caller cannot substitute another identifier in a node lookup, list filter, or mutation input. Apply permission checks to both edges and nodes: access to a parent object does not necessarily grant access to every related record.

Mutations need the same attention. Validate who may perform each state change, whether the target belongs to the caller's tenant, and whether the requested properties are writable. Avoid generic update resolvers that accept arbitrary fields from input. Use explicit input types and server-side allowlists so clients cannot modify role, ownership, billing, or internal state fields just because those fields exist in the schema.

Centralize authorization logic where possible, but keep context-specific decisions explicit. A generic “authenticated” check can be useful as a first gate and still be insufficient for a sensitive resolver. Add tests for users with different roles and tenants, including direct node access and deeply nested relationships.

Bound query cost and resource use

GraphQL queries can nest relationships and request many records. Apply depth limits, maximum page sizes, query-cost analysis, execution timeouts, and per-user or per-client rate limits. Cost models should account for list multipliers and resolver behavior: a shallow query can still be expensive if it requests a huge page or triggers costly downstream work. Enforce pagination on list fields and require bounded arguments.

Batching can reduce database round trips, but batching can also allow many operations in one HTTP request and bypass request-count limits. Set limits on batch size and total computed cost. Rate-limit expensive mutations and workflows based on user identity, tenant, and operation where appropriate. Use a shared limiter when the service runs on multiple instances; per-process counters may multiply the effective limit.

Test resource controls with representative high-cost queries and concurrent callers. Ensure the server rejects work before it overwhelms database pools or downstream services. Measure how limits affect legitimate complex queries and provide a safe error that helps clients adjust without exposing internal implementation details.

Reduce information leakage from schema and errors

Introspection and interactive query tools are useful during development and may reveal implementation details in production. Decide whether introspection is required for public clients or internal tooling, and restrict it accordingly rather than assuming that disabling it is the main security control. A hidden schema does not fix broken authorization. Maintain a safe schema publication process for legitimate client generation needs.

Return errors that describe the action a client can take without revealing stack traces, SQL statements, internal hostnames, or secret values. Validation errors should not echo sensitive input. Configure production diagnostics so detailed failures go to protected logs with request identifiers while the client receives a controlled response. Avoid including resolver internals in error extensions that are visible to every caller.

Validate inputs and protect downstream services

Use schema types for shape validation, then enforce business constraints such as string lengths, allowed enumerations, numeric ranges, and valid state transitions. Treat custom scalars and JSON fields carefully; they can bypass the safety of strongly typed fields if they accept arbitrary data. Validate external identifiers before using them in database or service queries, and use parameterized data access.

GraphQL resolvers often call other APIs. Apply timeouts, response-size limits, authentication, and error handling to those calls. Do not assume that a value returned by one internal service is trustworthy in a different context. Keep resolver work bounded and avoid uncontrolled fan-out. Cache only when authorization and tenant context are included in the cache key, or a response could cross user boundaries.

Review deployment configuration

Use HTTPS, restrict allowed origins deliberately, and implement CSRF protection when the API authenticates with browser cookies. CORS is not authorization. Apply request-size limits at the gateway and application layers, and limit JSON complexity before expensive execution. Keep GraphiQL or similar consoles disabled or authenticated in production unless there is a clear operational need.

Log operation names, caller identity, cost estimate, duration, and outcome in a privacy-conscious way. Full query bodies may contain sensitive values or personal data; avoid retaining them without a clear need and controls. Alert on repeated validation failures, unusually expensive operations, denied object access, and a sudden increase in resolver latency.

GraphQL review checklist

Test object and field authorization at every relationship; restrict mutation inputs and state changes; bound depth, list sizes, total cost, time, and batching; rate-limit expensive actions; protect cookie-authenticated requests from CSRF; validate custom scalar data; sanitize client-facing errors; configure schema tools intentionally; secure downstream calls; and inspect logs for sensitive query data. Include negative cases and cost tests in regression coverage.

GraphQL security is not a property of the schema language alone. It comes from the policy in resolvers and data access, resource bounds, safe downstream behavior, and carefully controlled production configuration. Review the complete query execution path, because attackers can compose individually valid fields into a request the service cannot safely afford or authorize.

References