fix(oidc): close the sockets of the sessions a back-channel logout ends by sid

A logout token naming the provider's session ended the TREK sessions but left their websockets open, so a signed-out tab kept receiving trip and journey broadcasts. Each ended session now closes its sockets, as the other revoke paths do.
This commit is contained in:
Maurice
2026-11-28 00:33:50 +01:00
parent 96d8b5c664
commit a6328cef3b
5 changed files with 44 additions and 15 deletions
@@ -180,11 +180,15 @@ export class UserSessionsRepository extends TrekRepository<UserSessions> {
* A back-channel logout naming the provider's session:
* `UPDATE user_sessions SET revoked_at = ? WHERE oidc_sid = ? AND revoked_at IS NULL
* AND user_id IN (SELECT id FROM users WHERE oidc_issuer = ?) [AND user_id = ?]
* RETURNING user_id`. Only accounts linked to that issuer match, and with
* `userId` only that account. Answers the owner of every session it ended,
* one entry per session.
* RETURNING id, user_id`. Only accounts linked to that issuer match, and with
* `userId` only that account. Answers every session it ended with its owner.
*/
async revokeByOidcSid(sid: string, issuer: string, now: string, userId?: number): Promise<number[]> {
async revokeByOidcSid(
sid: string,
issuer: string,
now: string,
userId?: number,
): Promise<Array<{ id: string; userId: number }>> {
const db = this.kysely<Pick<DB, 'user_sessions' | 'users'>>();
let update = db
.updateTable('user_sessions')
@@ -193,8 +197,8 @@ export class UserSessionsRepository extends TrekRepository<UserSessions> {
.where('revoked_at', 'is', null)
.where('user_id', 'in', db.selectFrom('users').select('id').where('oidc_issuer', '=', issuer));
if (userId !== undefined) update = update.where('user_id', '=', userId);
const rows = await update.returning('user_id').execute();
return rows.map((row) => Number(row.user_id));
const rows = await update.returning(['id', 'user_id']).execute();
return rows.map((row) => ({ id: String(row.id), userId: Number(row.user_id) }));
}
/**
@@ -72,9 +72,9 @@ const nonEmptyString = (v: unknown): v is string => typeof v === 'string' && v.l
* session that does not exist is not an error: the provider gets the same 200
* either way, so the route never tells which subjects have an account here.
*
* Sessions are not announced over the websocket when they end, on any path
* (`SessionsService.revokeAll` callers included): the next request of a
* signed-out browser is refused by the session check, and so is this one's.
* The websockets of an ended session are closed with it, on both paths (by
* `SessionsService.revokeByOidcSid` and `revokeAll`), so a signed-out tab stops
* hearing its rooms at once; its next request is refused by the session check.
*
* Unlike the login, this works while SSO login is switched off: sessions made
* before the switch still came from the provider, and it may still end them.
+6 -3
View File
@@ -237,11 +237,14 @@ export class SessionsService {
/**
* A back-channel logout that names the identity provider's session: end the
* active sessions signed in with that `sid` at `issuer`, of the account
* `userId` only when the logout token also named its subject. Answers the
* owner of every session it ended, one entry per session.
* `userId` only when the logout token also named its subject, and close
* their sockets. Answers the owner of every session it ended, one entry per
* session.
*/
async revokeByOidcSid(sid: string, issuer: string, userId?: number): Promise<number[]> {
return this.sessions.revokeByOidcSid(sid, issuer, dbNow(), userId);
const revoked = await this.sessions.revokeByOidcSid(sid, issuer, dbNow(), userId);
for (const session of revoked) this.realtime.closeSessionSockets(session.userId, session.id, 'Session ended');
return revoked.map((session) => session.userId);
}
/**