CsrfGuard
in package
CSRF guard for the WordPress admin Inertia pipeline.
The admin SPA dispatches through a per-component nonce action (see nonceAction()) over WordPress hooks — not the framework's PSR-15 kernel — so the framework's VerifyCsrfMiddleware (419 + Symfony tokens) does not apply here. This guard enforces the native WordPress nonce instead, via SecuritySupport.
Contract:
- Only state-changing verbs (POST/PUT/PATCH/DELETE) are guarded; safe verbs pass straight through.
- The nonce is read from the
X-WP-Noncerequest header first, then from a_wpnoncebody field (classic form posts). - On a missing/invalid nonce the request is rejected with HTTP 403 and an Inertia-aware JSON envelope (deliberately not 419, which the Inertia client treats as a session-expired auto-reload).
- The REST/JWT bearer-token surface is untouched: WordPress already
nonce-protects
wp-jsonviaX-WP-Nonceand bearer tokens are not cookie-bound.
Decision/extraction/payload methods are pure and unit-tested; enforce()
is the thin superglobal-reading exit path (mirrors InertiaAdapter's
untested exit branches).
Table of Contents
Constants
- STATUS_FORBIDDEN : int = 403
- HTTP status for a missing/invalid nonce (WordPress-native, not Laravel's 419).
- BODY_KEY : string = '_wpnonce'
- Body field name for the classic-form nonce fallback.
- HEADER_KEY : string = 'HTTP_X_WP_NONCE'
- Superglobal key for the `X-WP-Nonce` request header.
- UNSAFE_METHODS : array<string|int, mixed> = ['POST', 'PUT', 'PATCH', 'DELETE']
Methods
- enforce() : void
- Guard the current request, reading method/nonce from PHP superglobals.
- extractNonce() : string|null
- Pull the nonce from the request: `X-WP-Nonce` header first, then the `_wpnonce` body field. Returns null when neither carries a usable value.
- failurePayload() : array{message: string, error: string}
- The Inertia-aware rejection envelope sent with a 403.
- isMutating() : bool
- Whether the HTTP method changes server state and therefore requires a nonce.
- isValidRequest() : bool
- Decide whether a request may proceed: safe verbs always pass; mutating verbs require a nonce valid for the component's nonce action (see {@see nonceAction()}).
- nonceAction() : string
- The WordPress nonce action for a host component's admin SPA. Derived from the component name so two plugins in the same request never share a nonce.
- reject() : never
- Emit the 403 envelope and terminate, through the injected emitter (which guards headers_sent() and, in tests, throws instead of exiting).
Constants
STATUS_FORBIDDEN
HTTP status for a missing/invalid nonce (WordPress-native, not Laravel's 419).
public
int
STATUS_FORBIDDEN
= 403
BODY_KEY
Body field name for the classic-form nonce fallback.
private
string
BODY_KEY
= '_wpnonce'
HEADER_KEY
Superglobal key for the `X-WP-Nonce` request header.
private
string
HEADER_KEY
= 'HTTP_X_WP_NONCE'
UNSAFE_METHODS
private
array<string|int, mixed>
UNSAFE_METHODS
= ['POST', 'PUT', 'PATCH', 'DELETE']
Methods
enforce()
Guard the current request, reading method/nonce from PHP superglobals.
public
static enforce(string $nonceAction[, ResponseEmitterInterface|null $emitter = null ]) : void
No-op for safe verbs and valid mutating requests; otherwise emits the 403 envelope and terminates. The host passes the component's nonce action (see nonceAction()), the same value the SPA received via InertiaAdapter. Response side-effects go through the injected ResponseEmitterInterface (default PhpSapiEmitter); tests pass a recording emitter to assert the reject path in-process.
Parameters
- $nonceAction : string
- $emitter : ResponseEmitterInterface|null = null
extractNonce()
Pull the nonce from the request: `X-WP-Nonce` header first, then the `_wpnonce` body field. Returns null when neither carries a usable value.
public
static extractNonce(array<string, mixed> $server, array<string, mixed> $body) : string|null
Parameters
- $server : array<string, mixed>
-
superglobal-shaped server vars (e.g. $_SERVER)
- $body : array<string, mixed>
-
parsed request body (e.g. $_POST)
Return values
string|nullfailurePayload()
The Inertia-aware rejection envelope sent with a 403.
public
static failurePayload() : array{message: string, error: string}
Return values
array{message: string, error: string}isMutating()
Whether the HTTP method changes server state and therefore requires a nonce.
public
static isMutating(string $method) : bool
Parameters
- $method : string
Return values
boolisValidRequest()
Decide whether a request may proceed: safe verbs always pass; mutating verbs require a nonce valid for the component's nonce action (see {@see nonceAction()}).
public
static isValidRequest(string $method, string|null $nonce, string $nonceAction) : bool
Parameters
- $method : string
- $nonce : string|null
- $nonceAction : string
Return values
boolnonceAction()
The WordPress nonce action for a host component's admin SPA. Derived from the component name so two plugins in the same request never share a nonce.
public
static nonceAction(string $component) : string
InertiaAdapter::nonceAction() builds the same value on the emit side.
Parameters
- $component : string
Return values
stringreject()
Emit the 403 envelope and terminate, through the injected emitter (which guards headers_sent() and, in tests, throws instead of exiting).
private
static reject(ResponseEmitterInterface $emitter) : never
Parameters
- $emitter : ResponseEmitterInterface