Skip to content

fix(sessions): close refresh rotation gaps and add a replay window - #613

Merged
Bccorb merged 2 commits into
mainfrom
fix/refresh-rotation-chain
Nov 19, 2026
Merged

Bccorb merged 2 commits into
mainfrom
fix/refresh-rotation-chain

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #592, closes #593, closes #598. All three touch refresh rotation in sessionService.ts and /refresh, so they land in one PR.

Changes

#592: revoke by id ends the chain head. DELETE /sessions/:id and DELETE /admin/sessions/by-id/:id call revokeSessionChain instead of hardRevokeSession. The walk starts at the row given and follows replacedBySessionId to the head, so a session that rotated after it was listed is revoked. Two more changes to revokeSessionChain:

  • Rows that were already revoked keep their original revokedReason.
  • It reloads the head before stopping, so it follows a link that a concurrent rotation wrote between the read and the save.

#593: reuse detection no longer expires. findRefreshSessionByToken looks the row up by its fingerprint only and no longer filters on revoked, expired or idle. /refresh checks any row with replacedBySessionId set first: it is either answered from the replay window or treated as reuse (401 refresh_token_reused, chain revoked). Revocation and expiry are checked after that, for rows that never rotated, using classifyRefreshRefusal (src/lib/refreshRefusal.ts). That function replaces classifyExpiredRefreshToken.

#598: refresh replay window.

  • For REFRESH_REPLAY_WINDOW_SECONDS after a rotation (default 5, capped at 60, 0 disables; 5 matches the Node adapter's completed-refresh cache and the conformance harness), the rotated token gets the same successor back: the same refreshToken and sessionId, and a freshly signed access token for that session. This works up to three times per rotation.
  • Only the immediate predecessor of a live head qualifies. The successor must pass validateSessionRecord, which rejects one that is revoked, rotated, expired, cut off or orphaned, and its owner must not be revoked.
  • The replay count is incremented in one conditional UPDATE, so concurrent replays cannot all use up the last one.
  • The request that loses the rotation race revokes the replacement it made, as before, and is then answered from the window.
  • The successor token is stored on the rotated row, sealed with AES-256-GCM (16-byte tag pinned). The key is HMAC(REFRESH_TOKEN_LOOKUP_SECRET, domain tag + rotated token), and the AAD binds both session ids. A database copy alone cannot open it. After opening, the plaintext's lookup is checked against the successor row.
  • The sealed copy is cleared when the window lapses or the chain is revoked.
  • I chose this over deriving the successor deterministically from its predecessor. Deriving it would need no storage, but every later token in a chain would then be computable from any one old token plus the secret.
  • The window is an env var, not a system_config key, because SystemConfigSchema lives in @seamless-auth/types. This follows the same reasoning as LOGIN_RESPONSE_FLOOR_MS.

Migration: 20261118120000-add-refresh-replay.cjs adds sessions."rotatedAt", "replayTokenCiphertext" and "replayCount" (integer, not null, default 0).

Docs: docs/security-posture.md (new sections: reuse detection does not expire, refresh replay window) and docs/configuration.md.

Contract change

No routes, request or response schemas, token claims, JWKS entries or status codes are added or removed. npm run generate:api leaves openapi.json and adapter-manifest.json unchanged. Two /refresh outcomes change:

  • Concurrent refreshes of one token inside the window now all get 200 with the same refreshToken and sessionId. Before, the losing call got 401 refresh_token_reused and the user was signed out.
  • A rotated token presented after its row's idle window is now 401 refresh_token_reused with the chain revoked. Before, it was 401 invalid_refresh_token and nothing was revoked.

Affected repos:

Each adapter keeps working unchanged. Its issue can be closed with a test that two parallel refreshes both succeed against an API that includes this change. Adapters should keep treating refresh_token_reused as terminal.

Verification

  • npm run typecheck: clean.
  • npm run lint: clean.
  • npm run format:check: clean.
  • npm run test:run: 195 files, 3441 tests passed, 1 todo.
  • npm run coverage: 99.07 / 95.81 / 99.33 / 99.28 (lines / branches / functions / statements). The touched files are at 100% line coverage.
  • TEST_DB=postgres on a scratch database: 19 files, 283 tests passed. This includes the new tests/postgres/refreshRotation.spec.ts:
    • two concurrent refreshes return the same successor and leave one live chain;
    • three sequential replays succeed and a fourth is reuse;
    • a replay after the window is reuse;
    • an older ancestor is reuse inside the window;
    • reuse after the idle window revokes the chain;
    • user and admin revoke of a superseded row end the head.
  • Two sessionExchange reuse tests now age the rotation past the window before presenting reuse.
  • The migration was run up, down and up on Postgres, with the columns checked after each step.

Revoking a session by id now walks the rotation chain to its head, for
users and admins, so a session that rotated after it was listed is ended
rather than left live (#592).

The refresh lookup finds a row by its fingerprint alone, so a rotated
token is reuse however long after its rotation it returns, instead of an
unknown token once the rotated row's idle window passed (#593).

A rotated token presented again within REFRESH_REPLAY_WINDOW_SECONDS
(default 10) is answered with the same successor, up to three times, so
concurrent refreshes from separate adapter instances no longer read as
reuse. Only the immediate predecessor of a live head qualifies. The
successor token is stored AES-256-GCM sealed under a key derived from the
lookup secret and the rotated token, and cleared when the window lapses
or the chain is revoked (#598).

Closes #592
Closes #593
Closes #598
Matches the five second refresh dedupe window the Node adapter keeps and
the conformance harness expects, so a replay presented after it is reuse.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant