Web Security & OWASP (50 Qs)•Part 5: API Security, DevSecOps & Enterprise Incident Handling (43 - 50)
GraphQL Security Architecture: Query depth limiting, complexity analysis, and disabling introspection.
Unique Security Challenges in GraphQL
Unlike REST APIs with fixed server-defined endpoints, GraphQL gives clients the power to request arbitrary nested schemas. Without safeguards, attackers can construct malicious queries that cause server denial of service.
graphql
# 1. Recursive Circular Query DOS Attack:
query EvilDeepQuery {
author {
books {
author {
books {
author {
books { # Infinite recursion! Server crashes with out-of-memory!
title
}
}
}
}
}
}
}
Production GraphQL Hardening Suite
- Query Depth Limiting: Reject queries whose AST nesting exceeds a strict threshold (e.g., max depth of 5 levels):
typescript
import depthLimit from 'graphql-depth-limit';
const server = new ApolloServer({
schema,
validationRules: [depthLimit(5)], // Rejects any query deeper than 5 levels!
});
- Query Complexity Analysis: Assign cost points to each field (e.g., scalar fields = 1 point; list fields = 10 points). Reject queries exceeding 100 total points.
- Disable Introspection in Production: Introspection allows attackers to map your entire database schema, internal types, and deprecated fields. Always disable introspection in production:
typescript
const server = new ApolloServer({
introspection: process.env.NODE_ENV !== 'production',
});