fix(sessions): keep an ended session's row until it expires

A session renewed from a token issued before sessions were tracked has
an id derived from that token, and the renewal inserts it only when no
row by that id exists. The nightly purge deleted revoked rows straight
away, so the next request with the still valid old token inserted the
session again as an active one and got a fresh tracked token with a new
lifetime. A logout, an ended session or a sign out of the others held
only until the purge ran.

The purge now deletes expired rows only, and a revoked row stays until
its own expiry. A derived row is recorded with an expiry no earlier
than the old token's, so by the time it goes the old token is refused
by its signature check anyway.
This commit is contained in:
Maurice
2026-10-08 17:55:20 +02:00
parent e94525afd3
commit 648f63c626
9 changed files with 104 additions and 24 deletions
@@ -217,11 +217,16 @@ export class UserSessionsRepository extends TrekRepository<UserSessions> {
}
/**
* The nightly purge: `DELETE FROM user_sessions WHERE expires_at <= ? OR revoked_at IS NOT NULL`.
* A row that is gone refuses its token just as a revoked one does, so
* nothing depends on keeping them.
* The nightly purge: `DELETE FROM user_sessions WHERE expires_at <= ?`.
*
* A revoked row stays until its own expiry. A session renewed from a token
* issued before sessions were tracked has an id derived from that token,
* and only its revoked row stops the still valid old token from inserting
* it again as a fresh, active session. Its `expires_at` is never earlier
* than the old token's expiry, so once it has passed, the old token is dead
* too and the row can go.
*/
async deleteInactive(now: string): Promise<number> {
return await this.nativeDelete({ $or: [{ expires_at: { $lte: now } }, { revoked_at: { $ne: null } }] });
return await this.nativeDelete({ expires_at: { $lte: now } });
}
}