Skip to content

fix: admin checks use the account's current role - #483

Merged
kikootwo merged 2 commits into
mainfrom
fix/admin-role-current
Oct 27, 2026
Merged

kikootwo merged 2 commits into
mainfrom
fix/admin-role-current

Conversation

@kikootwo

Copy link
Copy Markdown
Owner

What changed

  • requireAuth (src/lib/middleware/auth.ts), access-token path: the user lookup it already makes now also selects role, and request.user.role is the account's current role instead of the role recorded when the token was issued. requireAdmin, requireLocalAdmin and every route that reads request.user.role therefore 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.
  • API tokens (authenticateApiToken, used by requireAuth and getCurrentUserAsync): 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 as user stays user if 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.
  • Test fixtures: four route tests that run the real 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 a role, matching the role in their tokens.
  • Docs: 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 same requireAuth -> requireAdmin chain admin routes use:
    • access token issued as admin, account now user: 403 Admin access required, handler not reached; requireLocalAdmin also refuses.
    • access token issued as user, account now admin: 200.
    • request.user keeps every other token field and carries the current role.
    • API token: admin token for an account now user gets 403; admin token for an admin account still works; user token for an account now admin stays at 403; getCurrentUserAsync reports the current role.
    • refresh issues the current role for user and admin.
    • each path makes exactly one lookup, so no query is added.
  • With main's source (633fbde) and this branch's tests: 7 failed, 4 passed, all 7 on an assertion. The 4 that pass on main are the ones main already 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

  • Unit tests for the changed code (vitest related on the changed source file plus the five changed test files): PASS, 74 files, 937/937
  • Type check and lint: PASS (tsc --noEmit clean; ESLint reports no errors in src/lib/middleware/auth.ts or the new test file; the 23 no-explicit-any errors in tests/api/admin-jobs.routes.test.ts and tests/api/admin-job-status.routes.test.ts are the same count main has)
  • Image build + smoke E2E (3cadda3): PASS, 1/1
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • Who is affected: only accounts whose role changed after their access token or API token was issued. Their next request uses the new role.
  • API tokens never gain access: the token's own role is still the upper bound.
  • No extra database cost: the column is added to selects that already run.
  • Rollback: revert this PR. No data is written or migrated.

Release note

  • Admin checks use the account's current role: a role change applies on the next request, without signing in again, and API tokens follow a role removal right away.

🤖 Generated with Claude Code

kikootwo and others added 2 commits October 27, 2026 04:37
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
@kikootwo
kikootwo merged commit 0d10fa8 into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/admin-role-current branch October 27, 2026 09:11
@kikootwo kikootwo mentioned this pull request Oct 27, 2026
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