emran-alhaddad / statamic-graphql-protect
Protect Statamic /graphql (rebing/graphql-laravel) with token + optional IP allow list.
Package info
github.com/emran-alhaddad/statamic-graphql-protect
Type:statamic-addon
pkg:composer/emran-alhaddad/statamic-graphql-protect
Requires
- php: ^8.1
- statamic/cms: ^4.0|^5.0|^6.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
π Secure your Statamic GraphQL endpoint
A lightweight, production-ready addon that protects Statamicβs /graphql endpoint using a token-based middleware with optional IP allow-listing.
Perfect for headless setups (Next.js, Nuxt, mobile apps) where public GraphQL access must be locked down without changing existing queries.
π Statamic Marketplace:
https://statamic.com/addons/emran-alhaddad/statamic-graphql-protect
π¦ Package Information
π Features
- π Token-protected
/graphqlendpoint - π‘οΈ Optional IP allow-list
- π Query depth and complexity ceilings
- π Introspection switch that works in production
- π§© Works with Statamicβs GraphQL layer (Rebing GraphQL under the hood)
- βοΈ Fully configurable via
.env - ποΈ Middleware-based (no route overrides)
- π§Ό No Statamic core hacks or forks
π¦ Installation
Install via Composer:
composer require emran-alhaddad/statamic-graphql-protect
Publish the config file:
php artisan vendor:publish --tag=statamic-graphql-protect
βοΈ Configuration
php artisan vendor:publish --tag=statamic-graphql-protect
Published config file:
config/statamic-graphql-protect.php
Upgrading from 1.2.0 or earlier? The config used to publish into
public/vendor/statamic-graphql-protect/config/, inside your webroot. Delete that directory and re-publish:rm -rf public/vendor/statamic-graphql-protect php artisan vendor:publish --tag=statamic-graphql-protect
Example .env configuration:
STATAMIC_GRAPHQL_PROTECT_ENABLED=true STATAMIC_GRAPHQL_TOKEN=long-secreat-token STATAMIC_GRAPHQL_TOKEN_HEADER=X-Statamic-GraphQL-Token STATAMIC_GRAPHQL_ALLOWED_IPS=127.0.0.1,1.2.3.4 # Query limits (see below) STATAMIC_GRAPHQL_MAX_DEPTH=15 STATAMIC_GRAPHQL_MAX_COMPLEXITY=0 # Introspection: leave unset for Statamic's default STATAMIC_GRAPHQL_INTROSPECTION=false
| Variable | Default | What it does |
|---|---|---|
STATAMIC_GRAPHQL_PROTECT_ENABLED |
true |
Master switch for the addon |
STATAMIC_GRAPHQL_TOKEN |
(empty) | The shared secret callers must send |
STATAMIC_GRAPHQL_TOKEN_HEADER |
X-Statamic-GraphQL-Token |
Header the token is read from |
STATAMIC_GRAPHQL_ALLOWED_IPS |
(empty) | Comma-separated allow-list; empty means any IP |
STATAMIC_GRAPHQL_MAX_DEPTH |
15 |
Max query nesting; 0 disables the limit |
STATAMIC_GRAPHQL_MAX_COMPLEXITY |
0 (off) |
Max query complexity; measure before enabling |
STATAMIC_GRAPHQL_INTROSPECTION |
(unset) | true / false; unset leaves Statamic's own setting alone |
Your frontend must send the header:
X-Statamic-GraphQL-Token: <your-token>
β οΈ REQUIRED: Register Middleware in Statamic GraphQL Config
This addon does not automatically inject itself into every Statamic install.
You must manually register the middleware.
Open:
config/statamic/graphql.php
Add the middleware to the middleware array:
return [ // ... 'middleware' => [ \Emran\StatamicGraphqlProtect\Http\Middleware\ProtectGraphql::class, // other GraphQL middleware (if any) ], // ... ];
β Without this step,
/graphqlwill remain public.
π Query Limits
A token keeps strangers out. It does not stop one authenticated caller from sending a deeply nested query that pins your server:
{ entries { data { ...on Article { related { related { related { ... } } } } } } }
GraphQL ships with no ceiling on this, so the addon sets rebing's
query_max_depth and query_max_complexity for you:
STATAMIC_GRAPHQL_MAX_DEPTH=15 STATAMIC_GRAPHQL_MAX_COMPLEXITY=0
Depth is on by default at 15. It counts nesting only, a legitimate Statamic query rarely passes 10, and it stops the recursive-nesting bomb. Safe to apply without knowing your schema.
Complexity is off by default, and that is deliberate. Complexity counts fields multiplied out across lists, so the right ceiling depends entirely on your own queries β a number guessed for someone else's site turns legitimate requests into errors. Measure your heaviest real query, then set this above it:
STATAMIC_GRAPHQL_MAX_COMPLEXITY=5000 # a reasonable starting point
Set either to 0 to leave that limit to your own config. Both are applied
during register(), because rebing reads them when its provider boots β
after every register() has run.
π Introspection
Introspection lets a caller download your entire schema. Statamic's default is
auto: on locally, off everywhere else β which means there is no supported way
to turn it on in production when you need it, and no single switch to force
it off.
STATAMIC_GRAPHQL_INTROSPECTION=false # off everywhere, production included STATAMIC_GRAPHQL_INTROSPECTION=true # on everywhere # unset # keep Statamic's 'auto'
The addon writes statamic.graphql.introspection, not rebing's
graphql.security.disable_introspection. Statamic derives the rebing key from
its own at boot and overwrites anything you set there by hand, so that is the
only key that decides the outcome.
Disabling introspection is obscurity, not security β it hides the schema, it does not block queries. The token middleware is the actual gate.
π§ͺ Example Usage
β Request without token (blocked)
curl https://your-domain.com/graphql \ -H "Content-Type: application/json" \ -d '{"query":"{ ping }"}'
Response:
{ "message": "Unauthorized." }
β Request with token (allowed)
curl https://your-domain.com/graphql \ -H "Content-Type: application/json" \ -H "X-Statamic-GraphQL-Token: your-token" \ -d '{"query":"{ ping }"}'
Response:
{ "data": { "ping": "pong" } }
π§ Postman Setup
- Headers
| Key | Value |
|---|---|
| X-Statamic-GraphQL-Token | your-token |
| Content-Type | application/json |
- Body β Raw β JSON
{ "query": "{ ping }" }
- Send β
Statamic's GraphQL cache
You can leave it on. Since 1.4.0 the token check runs in front of it.
Worth knowing why, because it decides where the check has to sit. Statamic caches a GraphQL response under a key built from the query and its variables only β no headers, no token:
'gql-cache:'.md5($query).'_'.md5(json_encode($vars))
and on a hit it returns the stored body without calling the rest of the
middleware stack. So any token check placed behind the cache is skipped on
every hit: one authorised request warms the entry, and the same query from a
stranger is then served the cached 200.
Statamic builds the route as
[HandleAuthentication, CacheResponse, ...statamic.graphql.middleware], so
adding this middleware through config puts it behind the cache. The addon
therefore prepends itself to the global stack and scopes itself to the
GraphQL prefix β the one placement that runs first either way.
If you register the middleware yourself, register it globally too:
// bootstrap/app.php ->withMiddleware(function (Middleware $middleware) { $middleware->prepend(\Emran\StatamicGraphqlProtect\Http\Middleware\ProtectGraphql::class); })
Caching at the CDN / Next.js layer is still the better place for it, but it is no longer a security requirement.
π Compatibility
- β
Statamic 4, 5, and 6 (per your projectβs
statamic/cmsconstraint) - β PHP 8.1+ (newer Statamic/Laravel stacks may require a higher minimum)
- β Headless & traditional installs
- β Next.js / Nuxt / Mobile apps
π Directory Structure
statamic-graphql-protect/ ββ src/ β ββ Http/Middleware/ProtectGraphql.php β ββ ServiceProvider.php ββ config/statamic-graphql-protect.php ββ composer.json ββ README.md ββ LICENSE
π©Ί Troubleshooting
βUnauthorized.β (missing or wrong token)
- Header name mismatch
- Token contains whitespace
- Middleware not registered in
config/statamic/graphql.php
Works in cURL but not Postman
- Use Body β Raw β JSON
- Ensure headers are manually added
Random 200 / 401 responses
On 1.4.0 or newer this should not happen β the check runs before the cache.
If you are on an older version, upgrade, or disable the GraphQL cache in
config/statamic/graphql.php and run php artisan cache:clear.
A 401 on every request, even with the right token
The token is empty. The middleware fails closed on purpose: an unset
STATAMIC_GRAPHQL_TOKEN locks the endpoint rather than opening it. Check that
you edited .env and not .env.example, then php artisan config:clear.
π€ Author
Emran Alhaddad GitHub: https://github.com/emran-alhaddad Statamic Addons: https://statamic.com/addons/emran-alhaddad
π License
MIT License
See the LICENSE file for full details.