Lesson 4 of 5 · 18 min · Intermediate

GraphQL testing

Introspection, batching, depth limits and field-level authorisation.

In this lesson you will

  • Explain GraphQL queries, mutations, schemas and resolvers
  • Discover a schema with introspection, or without it
  • Test field-level authorisation, batching and query cost
  • Recommend GraphQL-specific hardening

GraphQL replaces dozens of REST endpoints with one flexible endpoint where the client describes exactly what it wants. That flexibility moves a lot of security responsibility into places developers don't always think about: individual fields, query depth and batched operations. This lesson shows you how to test it.

Lab only. Practise on the Damn Vulnerable GraphQL Application (DVGA) and the PortSwigger Web Security Academy's GraphQL labs. Query-cost tests can slow down or crash a server: on client systems, agree them in the rules of engagement first.

GraphQL in five minutes

One endpoint, typed schema

A GraphQL API usually lives at a single URL such as /graphql or /api/graphql. Its schema defines types, fields and operations:

graphql
type User {
  id: ID!
  email: String!
  role: String!
  orders: [Order!]!
}
type Query {
  me: User
  user(id: ID!): User
}
type Mutation {
  updateEmail(email: String!): User
}

Queries and mutations

A query reads data; a mutation changes it. The client asks for exactly the fields it wants:

graphql
query {
  user(id: "42") {
    email
    orders { id total }
  }
}

Requests are normally a JSON POST: {"query": "...", "variables": {...}}.

Resolvers

Each field is filled by a resolver function on the server. Authorisation must be enforced in resolvers, or in a layer they all use. If the user resolver checks permissions but the nested orders resolver doesn't, data leaks through the nesting.

Finding the endpoint and schema

Locate it

Common paths: /graphql, /api/graphql, /graphql/v1, /query, /gql. A simple test query identifies a GraphQL endpoint:

json
{"query": "{ __typename }"}

A response like {"data":{"__typename":"Query"}} confirms it.

Introspection

GraphQL can describe its own schema. If introspection is enabled, one query returns every type, field, argument and mutation. Tools make this easy:

  • InQL (Burp extension) runs introspection and generates ready-to-send queries.
  • GraphQL Voyager turns the schema into an interactive map of relationships.

Introspection enabled in production is usually a low-severity finding on its own, but it hands an attacker the complete API map.

When introspection is disabled

  • Field suggestions: many servers reply to a mistyped field with "Did you mean email?". Tools such as Clairvoyance use these hints to rebuild the schema.
  • Client code: web and mobile apps contain their queries. Search JavaScript bundles for query and mutation.
  • Traffic: every query the app sends shows real field names.

Authorisation in GraphQL

All of BOLA and BFLA applies, with GraphQL twists:

  • Object-level: change the id argument in user(id: ...) or order(id: ...).
  • Field-level: a type may expose sensitive fields such as role, passwordResetToken or internalNotes. Ask for them directly: does the server return them to a normal user?
  • Through relationships: query an object you're allowed to see, then traverse to one you aren't: me { company { employees { email salary } } }.
  • Mutations: can a normal user call admin mutations such as updateUserRole or deleteUser?
  • Alternative entry points: the same data may be reachable through several queries; each needs the same check.

Batching and aliases

GraphQL lets you send many operations in one HTTP request:

graphql
mutation {
  a1: verifyOtp(code: "000001") { success }
  a2: verifyOtp(code: "000002") { success }
  a3: verifyOtp(code: "000003") { success }
}

Many servers also accept an array of queries in one request. If rate limiting counts HTTP requests rather than operations, batching lets an attacker try hundreds of OTPs or passwords per request. Test this against your own test account, with a small batch, to prove the bypass.

Query cost and denial of service

Because the client controls the shape of the query, it can ask for enormous amounts of work:

  • Deep nesting: user { friends { friends { friends { ... } } } }.
  • Large lists: orders(first: 100000).
  • Field duplication and fragments that multiply work.

Unbounded queries can exhaust the server's CPU, memory or database. This is API4 (unrestricted resource consumption). Prove the absence of limits with modestly sized queries and timing comparisons; don't try to take the service down.

Other GraphQL issues

  • Injection: arguments still reach databases and other back ends. Test them like any other input (SQL injection, NoSQL injection and the rest).
  • CSRF: if the endpoint accepts GET requests or application/x-www-form-urlencoded bodies with cookie authentication, cross-site requests may be able to run mutations.
  • Verbose errors: stack traces and resolver errors that reveal internals.
  • Debug interfaces: GraphiQL or Playground consoles left enabled in production.

How it gets fixed

  • Enforce authorisation in a shared layer used by every resolver, including nested fields and mutations.
  • Disable introspection, field suggestions and debug consoles in production, as defence in depth (not as a substitute for authorisation).
  • Limit query depth, complexity/cost and list sizes; set timeouts.
  • Rate limit by operation, not just by HTTP request, and cap batch sizes.
  • Accept only POST with application/json for state-changing operations.
  • References: the OWASP GraphQL Cheat Sheet and API Top 10 items API1, API3, API4 and API5.

Lab

  1. Start DVGA in your lab. Confirm the endpoint with { __typename }.
  2. Load the schema with InQL and explore it in GraphQL Voyager. Which types and fields look sensitive?
  3. Find a query or mutation that should be restricted and call it as a normal user.
  4. Use aliases to send several operations in one request. Does rate limiting count them individually?
  5. Work through the free GraphQL API vulnerabilities labs on the PortSwigger Web Security Academy.

Checkpoint

  1. Why can a GraphQL API leak data through nested fields even if top-level queries are protected?
  2. How can you discover a schema when introspection is disabled?
  3. How does batching bypass naive rate limiting?
  4. Which API Top 10 item covers unbounded query depth and size?