Introduction
Cross-site request forgery (CSRF) is an attack where a malicious site causes a logged-in user to send unintended requests to your app. For example, if your app has aPOST /user/email route, an attacker can host a page that auto-submits a hidden form to that endpoint. If the victim is already authenticated in your app, their email could be changed without their intent.
Laravel 13 protects against this by default in the web middleware group.
Preventing CSRF requests
Laravel’sPreventRequestForgery middleware uses a two-layer strategy:
- Origin verification using the browser’s
Sec-Fetch-Siteheader. - Token verification using a per-session CSRF token when origin verification is unavailable or fails.
Origin verification
Laravel first checksSec-Fetch-Site to determine whether a request is from the same origin. This works best on secure HTTPS connections.
If origin verification passes, the request is accepted immediately. If it does not pass, Laravel falls back to CSRF token validation.
Origin-only mode
If you want to rely only on origin verification, enableoriginOnly.
403 instead of the usual CSRF token mismatch status 419.
If you must accept same-site requests (such as subdomain-to-root requests), you can allow that explicitly:
Token verification
Laravel generates a CSRF token for each active session. You can access it viacsrf_token() or the session:
POST, PUT, PATCH, or DELETE form in web routes, include @csrf:
Excluding URIs from CSRF protection
For third-party webhooks (for example Stripe), you may need to exclude specific URIs:Prefer placing webhook endpoints outside the
web middleware group when possible. Exclusions should be explicit and minimal.X-CSRF-TOKEN header
Besides form_token, Laravel also checks the X-CSRF-TOKEN request header.
You can expose the token in a meta tag and send it in AJAX requests:
X-XSRF-TOKEN header
Laravel also sends an encryptedXSRF-TOKEN cookie. Frameworks such as Axios and Angular automatically read this cookie and send it as X-XSRF-TOKEN on same-origin requests.
That means many SPA and AJAX setups get CSRF header handling with little or no manual code.