Files
evig/scripts/db/migrations/072_user_token_version.sql
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

21 lines
1.1 KiB
SQL

-- 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.