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
@@ -321,7 +321,11 @@ describe('UserSessionsRepository', () => {
t.clear();
// Scoped to one account: only its sessions with that sid end.
expect(await sessions.revokeByOidcSid('sid-a', ISSUER, NOW, user.id)).toEqual([user.id, user.id]);
const ended = await sessions.revokeByOidcSid('sid-a', ISSUER, NOW, user.id);
expect(ended.sort((a, b) => a.id.localeCompare(b.id))).toEqual([
{ id: 'mine', userId: user.id },
{ id: 'mine-too', userId: user.id },
]);
expect(await row('mine')).toEqual(expect.objectContaining({ revoked_at: NOW }));
expect(await row('mine-too')).toEqual(expect.objectContaining({ revoked_at: NOW }));
expect(await row('other-sid')).toEqual(expect.objectContaining({ revoked_at: null }));
@@ -331,7 +335,7 @@ describe('UserSessionsRepository', () => {
expect(await row('ended')).toEqual(expect.objectContaining({ revoked_at: EARLIER }));
// Without an account the sid is matched across the issuer's accounts, never another issuer's.
expect(await sessions.revokeByOidcSid('sid-a', ISSUER, NOW)).toEqual([other.id]);
expect(await sessions.revokeByOidcSid('sid-a', ISSUER, NOW)).toEqual([{ id: 'theirs', userId: other.id }]);
expect(await row('old-provider')).toEqual(expect.objectContaining({ revoked_at: null }));
expect(await sessions.revokeByOidcSid('sid-a', ISSUER, NOW)).toEqual([]);
});
@@ -11,7 +11,7 @@ import {
} from '../../../../src/nest/sessions/sessions.service';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createUser } from '../../../helpers/factories';
import { countRows, findRow, findRows } from '../../../helpers/factories/rows';
import { countRows, findRow, findRows, updateRows } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { userSessionIdSchema } from '@trek/shared';
@@ -322,6 +322,24 @@ describe('SessionsService list and revoke', () => {
closeOne.mockRestore();
});
it('SESS-021: a back-channel logout by sid closes the sockets of every session it ended', async () => {
const close = vi.spyOn(RealtimeService.prototype, 'closeSessionSockets').mockImplementation(() => undefined);
const ISSUER = 'https://idp.example.test';
const { user } = createUser(testDb);
await updateRows(t, Users, { id: user.id }, { oidc_issuer: ISSUER });
const sso = claimsOf(await svc.issue({ id: user.id, pv: 0 }, undefined, { oidcSid: 'op-1' }));
const other = claimsOf(await svc.issue({ id: user.id, pv: 0 }, undefined, { oidcSid: 'op-2' }));
expect(await svc.revokeByOidcSid('op-1', ISSUER)).toEqual([user.id]);
expect(close).toHaveBeenCalledTimes(1);
expect(close).toHaveBeenLastCalledWith(user.id, sso.jti, 'Session ended');
expect(close).not.toHaveBeenCalledWith(user.id, other.jti, expect.anything());
expect(await svc.revokeByOidcSid('op-1', ISSUER, user.id)).toEqual([]);
expect(close).toHaveBeenCalledTimes(1);
close.mockRestore();
});
it('SESS-010: endSession ends the session a token names, and nothing for an untracked or missing token', async () => {
const { user } = createUser(testDb);
const mine = claimsOf(await svc.issue({ id: user.id, pv: 0 }));