Revocation races and the epoch fence
In short
- The threat
Revoking by deleting sessions misses the one a racing login is still minting, and every one a crash leaves behind.
- The rule
Each user has a counter, the epoch: revoke-all bumps it first, every session carries the value its login read, and a mismatch is refused.
- In the package
session/session-manager.ts, behind every revoke-all.
What goes wrong
Section titled “What goes wrong”Changing a password should end every other session. The obvious code lists the user’s sessions and deletes them. That has two holes:
- A concurrent login. A login reads the stored password hash, then verifies the credential, then mints a session. If a password change commits and revokes in the middle, the login can finish after the cleanup ran, and the attacker who knew the old password ends up with a live session.
- A crash. If the process dies after the password changed but before every session was deleted, the leftovers stay valid.
Deleting what you can find is not enough, because the session you cannot find yet (still being minted) or cannot reach (the cleanup never ran) survives.
Time runs from left to right, not to scale. Someone who knew the old password starts a login: it reads the user's epoch, n, then checks the old password. Meanwhile the password change writes the new hash, bumps the epoch to n + 1 and deletes the sessions it can find, which cannot include a session not minted yet. The login then mints its session stamped n, re-reads the epoch, finds n + 1 and rolls the session back. Had the mint come first, the session would carry n and validation would refuse it at its first request. A crash after the bump leaves old sessions in the store, but each carries a stale epoch, so validation refuses them too.
Text version
The rule: an epoch fence
Section titled “The rule: an epoch fence”Each user has a counter, the epoch.
Read before checking
Section titled “Read before checking”A login reads the user’s epoch before it checks the credential and stamps that value into the session it creates.
Increment first
Section titled “Increment first”Revoke-all increments the epoch first, and only then deletes the sessions it can find.
Validate against the counter
Section titled “Validate against the counter”Validation rejects any session whose stamped epoch differs from the user’s current epoch.
Re-read at mint
Section titled “Re-read at mint”When the login mints its session it re-reads the epoch. On a mismatch it rolls the new session back.
Why it holds
Section titled “Why it holds”No order of events yields a live session. A login that read the old password hash while a password change was committing read the epoch before the increment. Either the mint sees the new epoch and rolls back, or the session is stamped with the old epoch and validation rejects it.
A crash leaves only invalid sessions. A crash between the increment and the cleanup leaves old sessions in the store, but each carries a stale epoch, so they are invalid rather than valid. Cleanup becomes housekeeping, not a security step.
The counter is the source of truth; deleting sessions only reclaims space.
In code
Section titled “In code”“Sign out everywhere” through the public API; a password change and a password reset run the same revoke-all after they write the new hash.
import { serializeCookie, type RequestMeta, type StoredSession } from '@noctcore/auth';import type { AppAuth } from '../build/node-server/auth';
type Session = StoredSession<Record<string, never>>;
/** * "Sign out everywhere". The epoch bump inside revokeAll is the revocation: * a login racing it is rolled back at mint or refused at its first request. * The sessions it then deletes only free space. */export async function signOutEverywhere( auth: AppAuth, input: { current: Session; meta: RequestMeta },) { const { cookie } = await auth.sessions.revokeAll({ session: input.current, meta: input.meta, });
// The caller's own cookie is cleared too. return { setCookie: serializeCookie(cookie) };}Attack tests
Section titled “Attack tests”| Test | Attack | Stopped by |
|---|---|---|
| A006 | Replaying a revoked session | session/session-manager.ts |
| A067 | Keeping a role, or a scope, after it was taken away | plugins/membership/guards.ts, session/session-manager.ts |
| A097 | Bringing back a revoked session through the legacy store an app adopts sessions from | session/legacy-sessions.ts |
Further reading
Section titled “Further reading”Auth sessions in Pilcrow’s auth guide, on tying a session to one authentication event.
Related
Section titled “Related”Sessions and expiry covers the token and its expiry.