Search by

emran-alhaddad / statamic-graphql-protect

emran-alhaddad

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

Statistics

Installs: 112

Dependents: 0

Suggesters: 0

Stars: 4

Open Issues: 0

v1.4.0 2026-11-25 09:12 UTC

This package is auto-updated.

Last update: 2026-11-25 09:35:31 UTC


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

Statamic Addon Latest Version Total Downloads License Maintenance PRs Welcome

πŸš€ Features

  • πŸ” Token-protected /graphql endpoint
  • πŸ›‘οΈ 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, /graphql will 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

  1. Headers
Key Value
X-Statamic-GraphQL-Token your-token
Content-Type application/json
  1. Body β†’ Raw β†’ JSON
{ "query": "{ ping }" }
  1. 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/cms constraint)
  • βœ… 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.