Repository navigation
fix: admin checks use the account's current role - #483
Merged
Merged
Conversation
requireAuth now selects role in the user lookup it already makes and puts the account's current role on request.user, so requireAdmin, requireLocalAdmin and route role checks follow a role change on the next request. An API token's role is capped by its owner's current role. The refresh route already issued the current role; it is now covered by a test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # documentation/backend/services/auth.md
Merged
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.
What changed
requireAuth(src/lib/middleware/auth.ts), access-token path: the user lookup it already makes now also selectsrole, andrequest.user.roleis the account's current role instead of the role recorded when the token was issued.requireAdmin,requireLocalAdminand every route that readsrequest.user.roletherefore use the current role. A role change applies on the account's next request, both ways: an account made a regular user gets 403 from admin routes, and an account made an admin gets admin access, without signing in again.authenticateApiToken, used byrequireAuthandgetCurrentUserAsync): the token's role is now capped by the owner's current role, read from the token lookup that already loads the owner. An admin token stops acting as admin when its owner is no longer an admin. A token created asuserstaysuserif the owner is later made an admin: a token handed to another tool keeps the access it was created with, and the owner can create a new token.POST /api/auth/refresh: already issued the current role from the database. Unchanged; now covered by a test.requireAuth(tests/api/admin-jobs.routes.test.ts,admin-job-status.routes.test.ts,admin-job-routes.access.test.ts,bookdate-test-connection.access.test.ts) now give their mocked user rows arole, matching the role in their tokens.documentation/backend/services/auth.md(one Fixed Issues line),documentation/backend/services/api-tokens.md(role binding line).Why
Admin checks used the role stored in the access token or API token, so a role change applied only to tokens issued after it. Admin checks now use the account's current role.
Evidence
tests/middleware/session-current-role.test.ts, through the samerequireAuth->requireAdminchain admin routes use:user: 403Admin access required, handler not reached;requireLocalAdminalso refuses.admin: 200.request.userkeeps every other token field and carries the current role.usergets 403; admin token for an admin account still works; user token for an account nowadminstays at 403;getCurrentUserAsyncreports the current role.userandadmin.main's source (633fbde) and this branch's tests: 7 failed, 4 passed, all 7 on an assertion. The 4 that pass onmainare the onesmainalready gets right (refresh for both roles, an admin token for an admin account, a user token for a promoted account). On this branch: 11/11 pass.Checks
vitest relatedon the changed source file plus the five changed test files): PASS, 74 files, 937/937tsc --noEmitclean; ESLint reports no errors insrc/lib/middleware/auth.tsor the new test file; the 23no-explicit-anyerrors intests/api/admin-jobs.routes.test.tsandtests/api/admin-job-status.routes.test.tsare the same countmainhas)Risk and rollback
Release note
🤖 Generated with Claude Code