Skip to content

fix(csrf): send the CSRF token from every admin/profile frontend call - #203

Open
romanetar wants to merge 3 commits into
mainfrom
fix/csrf-frontend-token
Open

romanetar wants to merge 3 commits into
mainfrom
fix/csrf-frontend-token

Conversation

@romanetar

@romanetar romanetar commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

ref: https://app.clickup.com/t/9014802374/1k5eh578t2z

Context

PR 1 of 2 for ClickUp 1k5eh578t2z (CSRF protection for the session-authenticated admin API). The frontend has to send the token before the backend starts enforcing it, so this PR is safe to deploy on its own: it only sends an extra header to routes that do not check it yet. PR 2 (the csrf middleware on the routes, plus tests with the middleware enabled) is stacked on top of this one and must not ship before it.

Changes

  • layout.blade.php: expose <meta name="csrf-token"> to the 15 legacy jQuery views (those on reactapp_layout already declare it).

  • public/assets/js/ajax.utils.js: global jQuery ajaxPrefilter that adds X-CSRF-TOKEN to same-origin state-changing requests. It is never added to GET/HEAD/OPTIONS or cross-origin requests.

  • resources/js/profile/actions.js: save() passed the token as a query parameter instead of a header (putRawRequest takes payload, params, headers). With csrf enforced this would have returned 419.

  • server-config.blade.php: add @csrf to the native POST form.

  • tests/js/csrf_token.test.js: 6 Jest tests (header position, prefilter on POST/PUT/DELETE, not on GET, not cross-origin). The 4 that cover the new behavior fail without the change.

  • Token passed in the wrong argument of the base_actions helpers, so it never reached the X-CSRF-TOKEN header (found in review of fix(csrf): require the CSRF token on the session-authenticated admin routes #204):

    • edit_client regenerateClientSecret: sent as the JSON payload of putRawRequest.
    • clients addClient and edit_client addPublicKey: postRawRequest takes (params, headers); the extra {} became the headers and the token was dropped.
    • profile and admin/edit_user picture upload: sent as fileMetadata of putFile, i.e. a multipart field.
  • profile revokeToken / revokeAllTokens read window.CSFR_TOKEN, which the admin Edit User page (which reuses revokeToken) never sets, so it sent X-CSRF-TOKEN: undefined. Both now take the token from the caller (csrfToken prop).

  • tests/js/csrf_actions.test.js: mocks superagent and checks, for every mutating admin/profile React action (15), that the token goes out as a header and never in the body, URL or multipart fields. Fails on 5 actions before the fix, plus a case with window.CSFR_TOKEN unset for the revocation actions.

Testing

  • npx jest tests/js/csrf_token.test.js tests/js/csrf_actions.test.js: 38 passed.
  • Not yet checked in a browser; the end-to-end smoke test of the admin UI and profile is planned for dev with both PRs applied.
  • tests/js/turnstile_* suites fail to run locally because @testing-library/react is missing from node_modules (unrelated to this change).

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a2e21a2c-32fc-4eda-9eb8-6bc92bedb104
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/openstackid/openapi/pr-203/

This page is automatically updated on each push to this PR.

@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/openstackid/openapi/pr-203/

This page is automatically updated on each push to this PR.

@romanetar
romanetar added this pull request to stack #205 November 24, 2026 15:18
@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/openstackid/openapi/pr-203/

This page is automatically updated on each push to this PR.

First step of adding CSRF protection to the session-authenticated admin
API. The frontend must send the token before the routes enforce it.

- layout.blade.php: expose <meta name="csrf-token"> to the legacy jQuery pages
- ajax.utils.js: jQuery ajaxPrefilter adding X-CSRF-TOKEN to same-origin
  state-changing requests (never cross-origin, never GET/HEAD/OPTIONS)
- profile/actions.js: save() passed the token as a query parameter instead
  of a header (putRawRequest args are payload, params, headers)
- server-config.blade.php: add the _token field to the native POST form
- tests/js/csrf_token.test.js: cover the header position and the prefilter
Several actions passed the token in an argument that is not the headers
of the base_actions helper, so it never reached X-CSRF-TOKEN and the call
would answer 419 once the admin routes enforce csrf:

- regenerateClientSecret: putRawRequest(payload, params, headers) got the
  token as payload, so it went out as a JSON body field
- addClient, addPublicKey: postRawRequest takes (params, headers); the
  extra {} became the headers and the token was dropped
- profile and admin edit-user picture upload: putFile(file, name,
  fileMetadata, headers) got the token as fileMetadata, a multipart field

tests/js/csrf_actions.test.js mocks superagent and checks, for every
mutating admin/profile action, that X-CSRF-TOKEN is sent as a header and
never in the body, URL or multipart fields. It fails on 5 actions before
this change.
profile/actions revokeToken and revokeAllTokens read window.CSFR_TOKEN,
which only profile.blade.php and edit-client.blade.php set. The admin
Edit User page reuses revokeToken but never sets that global, so it sent
"X-CSRF-TOKEN: undefined" and revoking a user's access token would answer
419 once admin/api/v1 enforces csrf.

Both actions now take the token from the caller; the profile and admin
edit-user pages pass their csrfToken prop. The Jest test covers both
actions with window.CSFR_TOKEN unset.
@romanetar
romanetar force-pushed the fix/csrf-frontend-token branch from c8b1cd0 to a413ad0 Compare November 24, 2026 15:27
@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/openstackid/openapi/pr-203/

This page is automatically updated on each push to this PR.

@romanetar
romanetar requested a review from smarcet November 24, 2026 15:40

This branch has not been deployed

No deployments
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.

1 participant