Files
Tom MoorandClaude Fable 5 999004f959 fix: Stale CSRF token sent on request retries and form submissions (#12888)
* fix: Stale CSRF token sent on request retries and form submissions

The CSRF token cookie is rotated by the server on every safe request, but
ApiClient read the cookie once when a request was prepared and fetch-retry
reused the same headers for every attempt. Any rotation between preparation
and a retry (e.g. a concurrent GET in another tab) caused the double-submit
check to fail with csrf_error.

- ApiClient now re-reads the token cookie immediately before each network
  attempt, so retries always carry the current token
- Form now reads the token at render and refreshes the hidden _csrf input
  from the cookie during submit, replacing the useCsrfToken 1s polling hook

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

* chore: Remove ApiClient test file

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

* feat: Allow routes to opt out of CSRF token rotation

The CSRF token cookie is rotated on every safe request, which causes
in-flight mutating requests prepared with the previous token to fail the
double-submit check. High-frequency endpoints such as attachments.redirect,
hit for every inline image while reading a document, rotate the token
constantly for no benefit.

attachCSRFToken now sets the cookie after downstream middleware has run,
allowing routes to opt out of rotation via the new suppressCsrfTokenRotation
middleware. Suppressed routes still issue a token when the client does not
already hold a valid one. Applied to the attachments.redirect GET route.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

* feat: Suppress CSRF token rotation on files.get

Like attachments.redirect, files.get is requested for every inline image
stored locally and has no need to rotate the CSRF token cookie.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

* docs: Correct suppressCsrfTokenRotation comment

Requests already dispatched carry the previous cookie and header together
and still pass the double-submit check; rotation only races requests that
read the token before rotation but are sent after it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

* docs: Simplify comments

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

* revert: Remove CSRF token rotation suppression

The api router is mounted before attachCSRFToken in the middleware chain,
so API responses never rotate the token cookie and the suppression on
attachments.redirect and files.get had no effect.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fGhLu7zQyLdkRCx5EkYC5

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-07-06 17:04:24 -04:00
..