Quick answer: leave Laravel’s request-forgery middleware on for every cookie-authenticated, state-changing route. Put a CSRF secret in forms (@csrf / _token) or AJAX headers (X-CSRF-TOKEN or X-XSRF-TOKEN from the XSRF-TOKEN cookie). Exclude only endpoints that truly cannot send a token (payment webhooks). For a first-party SPA with Sanctum, call /sanctum/csrf-cookie first and enable axios.defaults.withCredentials and withXSRFToken.
What CSRF is
Cross-Site Request Forgery (CSRF) is when a malicious site, email, or page tricks a victim’s browser into sending an unwanted request to a site where that victim is already logged in. Browsers attach session cookies automatically, so the target app can mistake the forged request for a genuine one unless it checks something the attacker cannot read — typically a synchronizer token or modern Fetch Metadata (Sec-Fetch-Site). OWASP’s CSRF Prevention Cheat Sheet is the reference definition and defence catalogue.
How Laravel’s forgery middleware works
Current Laravel docs protect the web middleware group with Illuminate\Foundation\Http\Middleware\PreventRequestForgery (older releases exposed the same idea as VerifyCsrfToken / ValidateCsrfToken). It uses a two-layer approach:
- Origin verification — if the browser sends
Sec-Fetch-Siteand the request is same-origin, Laravel allows it without a token check. - CSRF token fallback — when origin verification does not pass (older browsers, missing header, non-HTTPS), Laravel requires a secret that matches the session token.
The session CSRF token is available as $request->session()->token() or csrf_token(). On protected methods (POST, PUT, PATCH, DELETE, and related), the middleware accepts the token from any of:
- the
_tokenform field - the
X-CSRF-TOKENrequest header - the
X-XSRF-TOKENrequest header (value taken from Laravel’s encryptedXSRF-TOKENcookie)
A mismatch typically yields HTTP 419. You can also opt into origin-only mode with preventRequestForgery(originOnly: true) in bootstrap/app.php (failures then return 403 instead of 419).
Blade @csrf
For every HTML form that changes state, include the hidden field:
<form method="POST" action="/profile">
@csrf
{{-- Equivalent to: --}}
{{-- <input type="hidden" name="_token" value="{{ csrf_token() }}" /> --}}
<button type="submit">Save</button>
</form>
For AJAX from Blade pages, a common pattern is a meta tag plus the X-CSRF-TOKEN header:
<meta name="csrf-token" content="{{ csrf_token() }}">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Laravel also sets an encrypted XSRF-TOKEN cookie on responses so libraries such as Axios or Angular can send X-XSRF-TOKEN automatically on same-origin requests.
Excluding routes carefully
Some callers (Stripe webhooks, payment callbacks) cannot know your session token. Prefer placing those routes outside the web middleware group. If you must stay on web, exclude specific URIs in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware): void {
$middleware->preventRequestForgery(except: [
'stripe/*',
]);
})
Do not blanket-exclude your entire API just because it is JSON. If those routes still use cookie/session authentication (Sanctum SPA, web guard, auth session), excluding CSRF turns them into classic CSRF targets: any malicious page can POST with the victim’s cookies. Token-based Bearer APIs that never rely on browser cookies are a different model — they are not protected by this middleware in the first place, and should not share a cookie session with a first-party SPA without CSRF.
CSRF verification is automatically disabled while running tests.
SPA / Axios and Sanctum
Laravel’s CSRF docs point SPA backends at Sanctum. For cookie-based SPA auth:
- SPA and API must share the same top-level domain (subdomains are fine).
- Enable Sanctum’s stateful API middleware (
statefulApi()), configuresanctum.statefuldomains, and allow credentials in CORS (supports_credentials=>true). - On the client:
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;
- Before login, initialize CSRF protection:
axios.get('/sanctum/csrf-cookie').then(() => {
// then POST /login
});
That route sets the XSRF-TOKEN cookie. Subsequent state-changing requests must send its URL-decoded value as X-XSRF-TOKEN (Axios does this when withXSRFToken is on). Session expiry often surfaces as 401 or 419 — send the user back to login.
Minimal PHP demo (no Laravel)
This CLI script shows why a state-changing POST needs a secret the attacker cannot forge. It is conceptual — not a web server:
<?php
declare(strict_types=1);
// Victim's session (server-side only)
$sessionToken = bin2hex(random_bytes(16));
function acceptPost(array $post, string $sessionToken): string
{
$incoming = $post['_token'] ?? '';
if (! is_string($incoming) || $incoming === '' || ! hash_equals($sessionToken, $incoming)) {
return "REJECTED (419-style): missing or mismatched CSRF token\n";
}
$email = $post['email'] ?? '';
return "ACCEPTED: would change email to {$email}\n";
}
// Attacker site can auto-submit email=… but cannot read the session token
$forged = ['email' => 'attacker@evil.example'];
echo acceptPost($forged, $sessionToken);
// Same-origin form includes the hidden token
$legit = ['email' => 'me@example.com', '_token' => $sessionToken];
echo acceptPost($legit, $sessionToken);
Run it with php demo.php. The forged body is rejected; the matching token is accepted. hash_equals() avoids timing leaks when comparing secrets (php.net hash_equals).
Practical checklist
- Keep forgery middleware on every cookie-auth, state-changing route.
- Always use
@csrf(or an equivalent header) on mutating forms and AJAX. - Exclude only third-party webhooks you cannot tokenise; never “turn off CSRF for
/api/*” if those routes still use cookies. - For Sanctum SPAs:
/sanctum/csrf-cookie→ credentials + XSRF header → then login. - Prefer HTTPS so
Sec-Fetch-Siteorigin checks can apply; treat tokens as the fallback for older clients. - Remember OWASP’s warning: XSS can defeat CSRF defences — fix XSS too.
Want a second pair of eyes on Laravel auth, Sanctum, or session cookie settings? See PHP Hub services (contact for quote) or run a quick pass with the PHP Security Checker.
This lesson was drafted with AI assistance and reviewed for accuracy against the official Laravel CSRF and Sanctum documentation and the OWASP CSRF Prevention Cheat Sheet linked below.
Sources
- Laravel CSRF Protection —
PreventRequestForgery,@csrf, exclusions,X-CSRF-TOKEN,X-XSRF-TOKEN - Laravel Sanctum — SPA Authentication —
/sanctum/csrf-cookie,withCredentials,withXSRFToken - OWASP Cross-Site Request Forgery Prevention Cheat Sheet
- PHP hash_equals



