☼
⌃
☰

GraphQL

Table of Contents

A query language for APIs that lets clients specify exactly what data they need. Unlike REST which exposes multiple endpoints per resource, GraphQL exposes a single endpoint and the client defines the shape of the response. Common in modern SPAs and mobile apps — increasingly encountered on web pentests.

Related: REST API Testing Methodology Broken Object Level Authorisation SPA Penetration Testing

Core Concepts

Concept Description
Schema Type definitions describing all available data and operations
Query Read operation — fetch data
Mutation Write operation — create, update, delete
Subscription Real-time — server pushes updates to client
Resolver Server-side function that fulfils each field in a query
Introspection Built-in mechanism to query the schema itself

Basic Syntax

Query

GRAPHQL
# Basic query — fetch specific fields
query {
  users {
    id
    name
    email
  }
}

# Query with arguments
query {
  user(id: 5) {
    name
    email
    role
  }
}

# Named query with variables
query GetUser($userId: ID!) {
  user(id: $userId) {
    name
    email
  }
}

Variables passed separately as JSON:

JSON
{ "userId": "5" }

Mutation

GRAPHQL
# Create
mutation {
  createUser(input: { name: "Alice", email: "alice@example.com" }) {
    id
    name
  }
}

# Update
mutation {
  updateUser(id: 5, input: { role: "admin" }) {
    id
    role
  }
}

Fragments — reusable field sets

GRAPHQL
fragment UserFields on User {
  id
  name
  email
  role
}

query {
  users {
    ...UserFields
  }
}

HTTP Mechanics

GraphQL typically runs over POST to a single endpoint:

SHELL
# Standard POST query
curl -X POST https://target.com/graphql \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"query": "{ users { id name email } }"}'

# With variables
curl -X POST https://target.com/graphql \
  -H "Content-Type: application/json" \
  -d '{
    "query": "query GetUser($id: ID!) { user(id: $id) { name email } }",
    "variables": {"id": "5"}
  }'

# Some APIs also accept GET
curl "https://target.com/graphql?query={users{id,name,email}}"

Common endpoint paths: /graphql, /api/graphql, /v1/graphql, /query

Security Considerations

Introspection

Introspection allows clients to query the full schema — all types, fields, queries, and mutations. In production, this is often a finding in itself (Information Disclosure) and also provides the complete attack surface map.

GRAPHQL
# Full introspection query — reveals entire schema
{
  __schema {
    types {
      name
      fields {
        name
        type {
          name
          kind
        }
      }
    }
  }
}

# List all available queries and mutations
{
  __schema {
    queryType { fields { name description } }
    mutationType { fields { name description } }
  }
}
SHELL
# Via curl
curl -X POST https://target.com/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "{ __schema { types { name } } }"}'

# InQL (Burp extension) automates introspection and generates
# a browsable schema map

BOLA / IDOR via GraphQL

Object-level authorisation is enforced at the resolver level — and is frequently missed. Test by substituting IDs:

GRAPHQL
# Request another user's data while authenticated as a different user
query {
  user(id: 2) {
    email
    creditCards {
      number
    }
  }
}

See BOLA for full methodology.

Batching Attacks

GraphQL supports sending multiple operations in a single request. This can bypass per-request rate limiting on operations like OTP validation or login:

JSON
[
  {"query": "mutation { login(username: \"admin\", password: \"pass1\") { token } }"},
  {"query": "mutation { login(username: \"admin\", password: \"pass2\") { token } }"},
  {"query": "mutation { login(username: \"admin\", password: \"pass3\") { token } }"}
]

If the rate limiter counts requests rather than operations inside a request, this bypasses it. Also useful for BOLA enumeration.

Field Suggestion / Clues

When introspection is disabled, GraphQL often returns field suggestions when a query contains a typo — leaking schema information:

GRAPHQL
# Deliberately mistype a field name
query { usr { id } }
# Response may include: "Did you mean 'user'?"

query { user { passward } }
# Response may include: "Did you mean 'password'?"

Nested Query Depth Attack (DoS)

Without depth limiting, deeply nested queries can exhaust server resources:

GRAPHQL
query {
  user {
    friends {
      friends {
        friends {
          friends {
            name
          }
        }
      }
    }
  }
}

Not typically exploited for access, but worth noting as a finding if there is no depth limit enforced.

Injections via GraphQL Arguments

GraphQL arguments are passed to resolvers which may construct SQL or other queries unsafely:

GRAPHQL
# SQL injection via argument
query {
  user(id: "1 OR 1=1") {
    name
  }
}

# NoSQL injection
query {
  users(filter: "{\"$where\": \"this.role == 'admin'\"}") {
    name
    email
  }
}

Tips

  • Always run introspection first — it maps the entire attack surface in one request
  • If introspection is disabled, use field suggestion bruteforcing or tools like Clairvoyance to reconstruct the schema
  • Batching bypasses are the most commonly missed GraphQL-specific finding — always test rate-limited operations via batch
  • Authorisation is per-resolver in GraphQL — a field may be accessible through a nested query even when the direct query is protected; test both paths
  • The __typename field is always available and never blocked — useful to confirm GraphQL is running when other introspection is disabled