GraphQL Architecture & Defense Playbook
Securing GraphQL APIs: Disabling production introspection, implementing query depth and complexity limits, defeating query batching brute force, and enforcing resolver-level authorization.
Securing GraphQL APIs: Disabling production introspection, implementing query depth and complexity limits, defeating query batching brute force, and enforcing resolver-level authorization.
1. Disabling Introspection in Production
Introspection allows attackers to discover hidden mutations, internal models, and administrative tools. Disable introspection when NODE_ENV === 'production':
// Apollo Server production configuration
const { ApolloServer } = require('@apollo/server');
const { ApolloServerPluginLandingPageDisabled } = require('@apollo/server/plugin/disabled');
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: process.env.NODE_ENV !== 'production',
plugins: process.env.NODE_ENV === 'production'
? [ApolloServerPluginLandingPageDisabled()]
: []
});2. Enforcing Query Depth and Complexity Rules
Prevent circular query denial-of-service attacks by enforcing strict AST validation rules before query execution:
const depthLimit = require('graphql-depth-limit');
const { createComplexityLimitRule } = require('graphql-validation-complexity');
const server = new ApolloServer({
typeDefs,
resolvers,
validationRules: [
depthLimit(5), // Rejects queries deeper than 5 nested levels
createComplexityLimitRule(1000, {
onCost: (cost) => console.log('Query cost:', cost)
})
]
});