Skip to content

Lesson 8 of 9 · 26m

Security Hardening for Public and Partner GraphQL APIs

The controls that matter before exposure: introspection policy, operation limits, persisted queries, abuse resistance, and data leakage prevention.

GraphQL is not inherently insecure, but its flexibility changes the attack surface. A single endpoint accepts executable documents, so input validation now includes parsing, static analysis, authorization, and resource budgeting. If you expose /graphql with tutorial defaults, you are one curious scanner away from schema disclosure and expensive nested queries.

Decide what introspection means in your threat model

Introspection is useful for development, codegen, and internal tooling. It also hands attackers a complete map. Public APIs usually disable it in production or gate it behind privileged scopes:

const server = new ApolloServer({
  schema,
  introspection: process.env.NODE_ENV !== "production",
});

Do not rely on disabled introspection as a security boundary. Authorization and validation still matter; schema secrecy is only friction.

Constrain executable input

Before execution, reject operations that exceed explicit limits:

  • Maximum depth, commonly 8-15 for product APIs.
  • Maximum aliases and directives.
  • Maximum operation length and token count.
  • Maximum cost using field weights.
  • Maximum list multiplicity from first/last arguments.
  • Query timeout at both gateway and resolver layers.

A small example policy:

graphql_security:
  max_depth: 10
  max_cost: 5000
  max_aliases: 20
  max_operation_bytes: 32768
  execution_timeout_ms: 8000
  default_page_size: 25
  max_page_size: 100

These limits are application policy, not library decoration. Enforce them in the execution path.

Persisted operations for first-party clients

For your own web and mobile applications, compile operations at build time and publish hashes. In production, accept either only those hashes or a strict allowlist plus limited introspection. This removes arbitrary query construction from the public input space.

{
  "operationHash": "sha256:8f2c1ab7...",
  "variables": { "workspaceId": "wrk_123" }
}

Partner APIs often cannot do this because partners write their own queries. In that case, use client-specific quotas, cost budgets, schema visibility, and contract tests.

Hide existence carefully

Authorization errors can leak information. If invoice(id:) returns FORBIDDEN for existing invoices but NOT_FOUND for missing ones, enumeration becomes possible. Many systems normalize to not-found for inaccessible objects. Internal audit logs should record the real reason while external responses remain uniform.

Protect files, subscriptions, and debug paths

GraphQL file uploads are multipart parsers and need size limits, content-type validation, malware scanning, and storage isolation. Subscriptions add connection lifecycle abuse: authenticate the connection, authorize every event payload, and cap concurrent subscriptions. Disable GraphiQL/Playground or protect it in production, especially if it stores headers or credentials in local storage.

CSRF is not dead

If the endpoint uses cookie authentication and accepts simple POSTs, cross-site request forgery still applies. Require custom headers, use SameSite cookies correctly, validate Origin/Referer where appropriate, or use bearer tokens. A content-type requirement is not a complete CSRF design by itself.

Field notes

  • Error messages must not include SQL, resolver paths to internal services, or upstream tokens.
  • Rate-limit expensive fields separately; a login mutation and a search connection do not deserve the same budget.
  • Signing cursors prevents casual tampering but does not replace authorization on every object.
  • Logs need operation hashes and sanitized variables, not raw secrets or personal data.
  • Security tests should include adversarial documents generated from the real schema.

Was this lesson helpful?

Sign in to rate