Course contentsShow
← Security
Lesson 1001 of 3,05221. Web Security - API SecurityFree lesson

Query Depth and Complexity Attacks

Understanding deeply nested queries that exploit GraphQL's graph traversal to cause resource exhaustion and denial of service.

Query Depth and Complexity Attacks

What you'll learn: How attackers exploit GraphQL's flexible query structure to send deeply nested or complex queries that overwhelm server resources and cause denial of service.

The Core Problem

Unlike REST APIs where endpoints are fixed, GraphQL allows clients to request exactly what data they need—including nested relationships. While powerful, this flexibility becomes dangerous when attackers craft malicious queries that traverse deep into your data graph.

Think of it like asking a librarian to fetch you a book, then fetch every book that book references, then every book those books reference, and so on—50 levels deep. The librarian (your server) exhausts themselves following this chain, potentially grinding to a halt.

How Depth Attacks Work

A depth attack exploits nested relationships:

query MaliciousDepth {
  user {
    posts {
      author {
        posts {
          author {
            posts {
              # ... continues 100+ levels deep
            }
          }
        }
      }
    }
  }
}

Even one such query can trigger thousands of database queries as GraphQL resolvers recursively fetch data.

How Complexity Attacks Work

Complexity attacks request massive amounts of data in a single query:

query MaliciousComplexity {
  users(limit: 10000) {
    posts(limit: 1000) {
      comments(limit: 500) {
        author { id, email, profile }
      }
    }
  }
}

This innocent-looking query could trigger billions of operations (10,000 × 1,000 × 500 = 5 billion comment fetches).

The Resource Exhaustion Impact

These attacks cause:

  • CPU exhaustion from resolver execution
  • Memory exhaustion from holding massive result sets
  • Database overload from query storms
  • Application crashes and service unavailability

Key Takeaway: GraphQL's flexibility allows deeply nested and complex queries that can exhaust server resources; defending requires query depth limits, complexity analysis, and rate limiting before execution begins.