Commit Graph
1 Commits
Author SHA1 Message Date
g-butandClaude Opus 4.7 ae52f73960 feat(auth): add users.token_version column for JWT-stale-permissions enforcement (1/3)
User-approved scope-expansion for the JWT-stale-permissions security
fix. Auth.js v5 with JWT session strategy holds is_staff /
staff_permissions / is_super_admin in the signed token. The jwt
callback populates these on initial sign-in (auth.ts:233 `if (user)`
guard) and never re-fetches from the DB until the token's 30-day
maxAge expires.

Effect: an admin demotes a colleague's staff permissions via
/admin/users/[id] PATCH or /api/admin/users/[id]/permissions, but the
demoted user keeps their old token's staff_permissions until they
log out + back in. Real security gap — the platform thinks the user
is still an admin for up to 30 days.

Fix design (3 commits):
  1/3 (this commit): Schema + migration. Add token_version column
      to users with default 0. The default-0 baseline matches a
      freshly-issued token's "no tokenVersion seen yet → treat as 0"
      semantics, so existing rows are equivalent to tokenVersion=0.
  2/3 (next commit): Admin permission-change routes bump
      users.token_version when relevant fields change.
  3/3 (next commit): jwt callback compares token.tokenVersion against
      users.token_version on each refresh; re-fetches permissions
      when they diverge. Token is otherwise reused unchanged so
      perf impact is one indexed-PK lookup per token refresh (~24h
      cycle per Auth.js updateAge).

Migration is non-breaking: ADD COLUMN with NOT NULL DEFAULT 0
backfills atomically. Drizzle schema declaration matches. No code
yet reads or writes token_version — that arrives in commits 2/3
and 3/3. Splitting the commits this way means each individually
deploys safely:
  - This commit alone: column exists but unused → no behavior change.
  - + bump commit: token_version increments on admin change → still
    no behavior change because jwt doesn't check it yet.
  - + jwt commit: full enforcement → demoted users get fresh
    permissions on next token refresh.

tsc clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 09:35:16 +02:00