fix(inertia): send csrf token with visits - #36
Merged
Conversation
Inertia v3 replaced axios with its own XHR client, which does not send Phoenix's CSRF token. Any non-GET Inertia visit would 403 on protect_from_forgery. Attach the token from the csrf-token meta tag via http.onRequest, ported from the same fix in NexPB/cloak. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Inertia v3 replaced axios with its own XHR client, which doesn't send Phoenix's CSRF token — so any non-GET Inertia visit (
useFormpost,router.delete, …) would 403 onprotect_from_forgery. Latent today only because the demo pages have no forms.Ports the fix from NexPB/cloak (
493c3d52/17e58336), minus the Clerk-specific parts:assets/src/lib/http.ts(new) —installHttpHeaderInterceptor()reads the csrf-token meta tag (already rendered inroot.html.heex) and attaches it asx-csrf-tokenon every Inertia request viahttp.onRequestassets/src/app.tsx— installs the interceptor beforecreateInertiaAppassets/e2e/demo.spec.ts— asserts the Inertia XHR on Demo A → B navigation carries thex-csrf-tokenheaderNot ported (already covered here): server-side
:inertiais at3.0.0-rc3, client at^3.0.0, no axios dependency.Testing
CI is Elixir-only, so JS was verified locally:
pnpm lintpnpm exec tsc --noEmitpnpm buildpnpm test:e2e— 6/6 passed, including the new CSRF header assertionmix precommit— compile, format, credo strict, 4 tests passed🤖 Generated with Claude Code