Skip to content

fix(csrf): require the CSRF token on the session-authenticated admin routes - #204

Open
romanetar wants to merge 1 commit into
fix/csrf-frontend-tokenfrom
fix/csrf-backend-routes
Open

romanetar wants to merge 1 commit into
fix/csrf-frontend-tokenfrom
fix/csrf-backend-routes

Conversation

@romanetar

@romanetar romanetar commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

Context

PR 2 of 2 for ClickUp 1k5eh578t2z. Stacked on #203 (base branch is fix/csrf-frontend-token; retarget to main once #203 is merged). Must not be deployed before #203: without the frontend sending the token, the admin UI would answer 419.

The 68 POST/PUT/DELETE routes of the admin pages (prefix admin) and the admin API (prefix admin/api/v1) authenticate with the session cookie (SameSite=None in stage and prod) and none checked a CSRF token. A form on another site using _method=PUT/DELETE could act as a signed-in administrator. On main that request answers 201 and the user lands in the group.

Changes

  • routes/web.php: add csrf to the middleware of both session-authenticated groups. Group-level, so every current mutating route and any added later is covered. GET/HEAD/OPTIONS are not checked by VerifyCsrfToken.
  • tests/AdminApiCsrfTest.php (9 tests), running the full middleware stack with no withoutMiddleware(). VerifyCsrfToken skips itself under APP_ENV=testing, so the test binds a subclass that always validates; nothing depends on the environment.
    • Sweep of every mutating admin/* route: all carry csrf.
    • PUT groups/{id}/users/{user_id}, DELETE users/{id}, PUT clients/{id}/secret: without a token (form-style POST + _method) answers 419 and the database is unchanged; with the header it succeeds (201 / 204 / 201). A wrong token also answers 419.
    • A GET needs no token.

Testing

  • phpunit tests/AdminApiCsrfTest.php: 9 tests, 18 assertions, OK.
  • With routes/web.php from main, the route sweep fails and the "without token" case answers 201 instead of 419, so the tests fail on main and pass with the change.
  • Existing API tests use withoutMiddleware() or run with APP_ENV=testing, so they are not affected. The full suite was not run (it resets the shared dev DB).

Before releasing

  • Smoke test the admin UI and profile on dev with both PRs: create/edit/delete a user, group membership, OAuth2 client edition, profile with picture, server config. Each request should carry X-CSRF-TOKEN.
  • Check the access logs for callers of these routes other than the IdP's own pages; they would start getting 419.
  • Out of scope, noted for a follow-up: Route::any on /accounts/user/logout and /accounts/user/profile/trusted_site/delete/{id} accept GET and change state.

Summary by CodeRabbit

  • Security
    • Administrative pages and API actions that change data now require a valid CSRF token, helping prevent unauthorized changes.
    • Read-only administrative requests continue to work without a CSRF token.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

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: a9a5a073-f1b7-43ac-b113-b9728ae0ca4c

No actionable comments were generated in the recent review.

Walkthrough

The admin web and API route groups now include CSRF middleware. Integration tests verify CSRF enforcement across mutating admin routes and confirm that read-only admin requests succeed without a token.

Changes: Admin CSRF protection

Layer / File(s) Summary
Protect admin route groups
routes/web.php
The /admin and /admin/api/v1 route groups add csrf alongside ssl and auth.
Verify CSRF enforcement
tests/AdminApiCsrfTest.php
Tests check that more than 60 mutating admin routes declare CSRF middleware. They verify rejection of missing or incorrect tokens, successful group membership, user deletion, and client-secret regeneration with a valid token, and successful reads without a token.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant AdminClient
  participant VerifyCsrfToken
  participant AdminRoute
  participant AdminState
  AdminClient->>VerifyCsrfToken: Send admin mutation with CSRF token
  alt Token missing or invalid
    VerifyCsrfToken-->>AdminClient: Return 419
  else Token valid
    VerifyCsrfToken->>AdminRoute: Forward request
    AdminRoute->>AdminState: Apply mutation
    AdminState-->>AdminClient: Return mutation response
  end
Loading

Possibly related PRs

  • OpenStackweb/openstackid#187: Adds an admin API endpoint within the route group protected by this change and includes related CSRF testing.

Suggested labels: security

Suggested reviewers: smarcet

Merge Risk: Minimal · previous review 1c864

Admin routes now require CSRF tokens on mutating requests, with tests covering enforcement. Merge only together with, or after, the companion frontend change, since otherwise the admin UI would get 419 responses.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: the CSRF middleware now protects the session-authenticated admin routes. The raw summary confirms that the /admin and /admin/api/v1 route groups now include `csrf…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title states the main change: the CSRF middleware now protects the session-authenticated admin routes. The raw summary confirms that the /admin and /admin/api/v1 route groups now include csrf middleware, and that a new test class checks CSRF rejection on mutating admin routes. The fix(csrf) prefix matches the security-fix intent in the objectives. The title is 75 characters and fits the single-sentence format.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

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: a952cadf-2719-4ee1-83bd-577fc56dab3d
Commits

Reviewing files that changed from the base of the PR and between 1dae2fd and 1c864c2.

Files selected for processing: 2

  • routes/web.php
  • tests/AdminApiCsrfTest.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

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

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-204/

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
@romanetar
romanetar force-pushed the fix/csrf-backend-routes branch from 754152f to fd10c18 Compare November 24, 2026 15:20
@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

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

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

…routes

Every POST, PUT and DELETE in the admin pages (prefix admin) and the admin
API (prefix admin/api/v1) authenticates with the browser session cookie,
which is SameSite=None in stage and prod, and Laravel reads the verb from
_method. A form on any other site could therefore act as a signed-in
administrator (e.g. PUT groups/{id}/users/{user_id}, DELETE users/{id},
PUT clients/{id}/secret).

Add the csrf middleware to the middleware of both groups, so the 68 existing
mutating routes and any added later are covered. Reads are unaffected.

tests/AdminApiCsrfTest.php runs the full middleware stack, with no
withoutMiddleware() and no reliance on APP_ENV=testing (VerifyCsrfToken
skips itself there, so the test binds a subclass that always validates).
It covers a route sweep plus, for the three acceptance cases, rejection
without a token (419, nothing changes) and success with one.
@romanetar
romanetar force-pushed the fix/csrf-backend-routes branch from fd10c18 to e9203e3 Compare November 24, 2026 15:27
@github-actions

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

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

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