Csrf
Csrf
CSRF protection for POST, PUT, PATCH and DELETE requests.
class Csrf implements \Hazaar\Middleware\Interface\MiddlewareImplements: Hazaar\Middleware\Interface\Middleware
A request passes if any of these hold:
- it uses a safe method (GET, HEAD, OPTIONS);
- it carries an
Authorization: Bearerheader, which a cross-site page can't attach, so there are no ambient credentials to abuse; - its path matches the
$exceptallow-list (fnmatchpatterns, for API routes); - it carries a valid token in the
_csrffield or theX-CSRF-Tokenheader.
Before the token check, Sec-Fetch-Site: cross-site is rejected, and so is an Origin header whose host differs from the request's Host unless listed in $trustedOrigins. Otherwise Hazaar\Controller\Exception\CsrfMismatch (403) is thrown.
Register this after the auth session middleware so tokens are bound to the signed-in identity.
Constants
SAFE
private const SAFE = array (
0 => 'GET',
1 => 'HEAD',
2 => 'OPTIONS',
)Properties
except
path patterns that skip the check
private array $exceptType: array
trustedOrigins
extra origins (https://host) accepted by the origin check
private array $trustedOriginsType: array
key
server-side secret mixed into the token HMAC
private string $keyType: string
Methods
__construct
public __construct(array $except, array $trustedOrigins, string $key): voidParameters
| Parameter | Type | Description |
|---|---|---|
$except | array | path patterns that skip the check |
$trustedOrigins | array | extra origins (https://host) accepted by the origin check |
$key | string | server-side secret mixed into the token HMAC |
handle
public handle(Request $request, callable $next, mixed $args): ResponseReturns: Hazaar\Controller\Response
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request | |
$next | callable | |
$args | mixed |
requiresCheck
private requiresCheck(Request $request): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request |
checkOrigin
private checkOrigin(Request $request): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request |
Generated by Hazaar API Doc Generator on Sun, 08 Nov 2026 03:36:14 +0000