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>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-26 09:35:16 +02:00
1 parent d85e276470
commit ae52f73960
2 files changed
+27

No files matched your search

@@ -0,0 +1,20 @@
-- Migration 072: Add token_version to users for JWT-stale-permissions enforcement
--
-- Auth.js v5 with JWT session strategy holds is_staff / staff_permissions /
-- is_super_admin in the signed token. The token is populated on initial sign-in
-- and refreshed every 24h (updateAge); the 30-day maxAge defines when it
-- finally expires. Until then, the jwt callback never re-fetches the user from
-- the DB, so an admin who demotes a colleague's staff permissions keeps seeing
-- a valid token for the demoted user with the OLD permissions.
--
-- token_version is bumped by the admin permission-change routes; the jwt
-- callback compares token.tokenVersion against users.token_version on each
-- refresh and re-fetches permissions when they diverge. Default 0 so existing
-- rows match a freshly-issued token's "no tokenVersion seen yet → treat as 0"
-- baseline; the first demotion bumps to 1 and the next token refresh picks it up.
ALTER TABLE users
ADD COLUMN IF NOT EXISTS token_version INTEGER NOT NULL DEFAULT 0;
-- No index needed: lookups happen by user id (already indexed via PK) and
-- token_version is read as a single column, not filtered on.
+7
View File
@@ -20,6 +20,13 @@ export const users = pgTable('users', {
staffPermissions: text('staff_permissions').array().default([]),
// Super admin management (004)
isSuperAdmin: boolean('is_super_admin').default(false),
// JWT staleness counter (072). Bumped by admin permission-change routes
// to force the Auth.js jwt callback to re-fetch staff_permissions /
// is_staff / is_super_admin from the DB on the user's next token
// refresh (~24h via Auth.js updateAge). Without this, a demoted admin
// retained their old token's permissions until the 30-day maxAge
// expired or they manually re-logged-in.
tokenVersion: integer('token_version').notNull().default(0),
// Dashboard layout preference (Phase 6)
dashboardMode: text('dashboard_mode').notNull().default('coordinator'),
// Verein membership (062)