Skip to content
API Design Learn/GraphQL Real-Time & Security
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Guarding Against Malicious Queries

4 min read

You'll learn to

  • -Explain why GraphQL's freedom to nest and widen queries is also its biggest attack surface
  • -Apply a depth limit and a cost budget, and see a deep query and a wide one rejected for different reasons
  • -Decide where introspection and persisted queries belong in production

GraphQL lets a client build any query it likes against the schema's whole graph of types. That freedom is also its largest attack surface. A schema where User -> friends -> friends -> friends can nest without limit lets a malicious or just careless client write a single query that costs the server exponentially more than a normal request.

An Analogy: The All-You-Can-Order Kitchen

Imagine a kitchen that lets you write any order you like on the slip. One customer writes "a dish, whose side dish is a dish, whose side dish is a dish" fifty times. Another orders a hundred thousand portions of one dish. Neither slip is wrong in form, and either can stop the kitchen working. The kitchen needs two checks before cooking: how deep is the order, and how big is it overall.

Depth Limiting

An unbounded, exponentially expensive query the schema should reject
query MaliciousDepthBomb {
  user(id: "1") {
    friends {
      friends {
        friends {
          friends {
            friends { name }  # nesting indefinitely deeper...
          }
        }
      }
    }
  }
}

Depth limiting rejects a query whose nesting exceeds a configured maximum, commonly around 7 to 10 levels, tuned to what real queries need. It happens before any execution, and it is cheap, because counting the nesting in the query text costs almost nothing next to resolving even one deep level.

Complexity Analysis: A Finer Budget

Assigning each field a cost, and rejecting queries that exceed a total budget
type Query {
  users(first: Int): [User!]!   # cost scales with 'first' - fetching 1000 is expensive
}
# A complexity analyzer estimates cost per field (often "1 + cost of nested
# fields, multiplied by any 'first'/'limit' argument") and sums it across the
# whole query, rejecting anything over a configured budget - e.g. 1000 points -
# BEFORE execution.

Depth alone does not capture cost. A shallow query asking for users(first: 100000) can be far more expensive than a deep query touching a few records at each level. Complexity analysis gives each field a cost, scaled by pagination arguments like first, adds it up, and rejects a query over a budget before it runs. That catches the wide attack that depth limiting misses.

The demo checks three queries against a depth limit of 6 and a budget of 1,000. The normal page passes. The friends-of-friends bomb is stopped by depth. The shallow query for a hundred thousand users has a depth of only 2 and is stopped by cost. The last two lines show the introspection rule below.

Press Run: one query fails on depth, another on cost, and introspection is a one-line rule

Introspection: Useful in Development, Risky in Production

Introspection lets a client query the schema itself: every type, field and argument. That is valuable for tooling, such as autocomplete, schema-aware editors and generated documentation. In production it hands an attacker a complete map of your API with no effort. Many production APIs therefore turn introspection off for unauthenticated or non-internal callers, and keep it on in development and staging.

Persisted queries are stronger than complexity limits alone for a public API with known client apps. The client sends a hash of a pre-approved query instead of the query text, and the server only runs queries it has allow-listed in advance. That removes the arbitrary-query attack surface for any caller using the mechanism.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Design Query Fortress in the API Design Lab's GraphQL Mastery act.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!