Skip to content

[6.x] Fix CSRF token mismatch on statically cached pages when using Precognition - #15939

Merged
duncanmcclean merged 1 commit into
6.xfrom
10801
Nov 16, 2026
Merged

duncanmcclean merged 1 commit into
6.xfrom
10801

Conversation

@duncanmcclean

Copy link
Copy Markdown
Member

This pull request fixes an issue where forms submitted with Laravel Precognition (or anything else that relies on axios' X-XSRF-TOKEN header) fail with a 419 CSRF token mismatch on pages served by full-measure static caching.

This was happening because the XSRF-TOKEN cookie is only set by Laravel's CSRF middleware. A cached page never hits Laravel, and the only requests that do, /!/csrf and /!/nocache, removed the CSRF middleware entirely with withoutMiddleware(). The form's _token input was replaced, but the cookie never was, so requests that only send X-XSRF-TOKEN had no token. Precognition's form.submit() is one of these. This only affects Laravel 12, since Laravel 13's PreventRequestForgery accepts same-origin browser requests without a token.

This PR fixes it by excluding those routes from CSRF verification with VerifyCsrfToken::except() instead of removing the middleware. The middleware still runs, skips verification, and sets the XSRF-TOKEN cookie the same way it does for uncached pages. This is the same mechanism as Laravel's validateCsrfTokens(except: [...]), and it respects Laravel 13's origin-only mode.

Fixes #10801

Amp thread: https://ampcode.com/threads/T-01a1e4c3-76e6-7338-a31c-50f85c0c8d62

@duncanmcclean
duncanmcclean merged commit a3f7ae0 into 6.x Nov 16, 2026
64 checks passed
@duncanmcclean
duncanmcclean deleted the 10801 branch November 16, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSRF token mismatch full cache strategy

2 participants