You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(csrf): require the CSRF token on the session-authenticated admin routes - #204
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.
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.
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.
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: 4
Check name
Status
Explanation
Title check
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Description Check
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.
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.
…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.
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
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.
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 tomainonce #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 (prefixadmin/api/v1) authenticate with the session cookie (SameSite=Nonein stage and prod) and none checked a CSRF token. A form on another site using_method=PUT/DELETEcould act as a signed-in administrator. Onmainthat request answers 201 and the user lands in the group.Changes
routes/web.php: addcsrfto 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 byVerifyCsrfToken.tests/AdminApiCsrfTest.php(9 tests), running the full middleware stack with nowithoutMiddleware().VerifyCsrfTokenskips itself underAPP_ENV=testing, so the test binds a subclass that always validates; nothing depends on the environment.admin/*route: all carrycsrf.PUT groups/{id}/users/{user_id},DELETE users/{id},PUT clients/{id}/secret: without a token (form-stylePOST+_method) answers 419 and the database is unchanged; with the header it succeeds (201 / 204 / 201). A wrong token also answers 419.Testing
phpunit tests/AdminApiCsrfTest.php: 9 tests, 18 assertions, OK.routes/web.phpfrommain, the route sweep fails and the "without token" case answers 201 instead of 419, so the tests fail onmainand pass with the change.withoutMiddleware()or run withAPP_ENV=testing, so they are not affected. The full suite was not run (it resets the shared dev DB).Before releasing
X-CSRF-TOKEN.Route::anyon/accounts/user/logoutand/accounts/user/profile/trusted_site/delete/{id}accept GET and change state.Summary by CodeRabbit