Skip to content

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.

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.

The epoch, not the delete, is the revocation: a session minted on the old epoch is rolled back at mint or refused at its first request, whatever the cleanup found.

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

Each user has a counter, the epoch.

A login reads the user’s epoch before it checks the credential and stamps that value into the session it creates.

Revoke-all increments the epoch first, and only then deletes the sessions it can find.

Validation rejects any session whose stamped epoch differs from the user’s current epoch.

When the login mints its session it re-reads the epoch. On a mismatch it rolls the new session back.

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.

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

revocation.ts
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) };
}
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

Auth sessions in Pilcrow’s auth guide, on tying a session to one authentication event.

Sessions and expiry covers the token and its expiry.