diff --git a/server/src/db/repositories/UserSessions.repository.ts b/server/src/db/repositories/UserSessions.repository.ts index e94aff8e9..f3fdf23c9 100644 --- a/server/src/db/repositories/UserSessions.repository.ts +++ b/server/src/db/repositories/UserSessions.repository.ts @@ -180,11 +180,15 @@ export class UserSessionsRepository extends TrekRepository { * 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 { + async revokeByOidcSid( + sid: string, + issuer: string, + now: string, + userId?: number, + ): Promise> { const db = this.kysely>(); let update = db .updateTable('user_sessions') @@ -193,8 +197,8 @@ export class UserSessionsRepository extends TrekRepository { .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) })); } /** diff --git a/server/src/nest/oidc/oidc-backchannel.service.ts b/server/src/nest/oidc/oidc-backchannel.service.ts index 64616598c..70abf4f5c 100644 --- a/server/src/nest/oidc/oidc-backchannel.service.ts +++ b/server/src/nest/oidc/oidc-backchannel.service.ts @@ -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. diff --git a/server/src/nest/sessions/sessions.service.ts b/server/src/nest/sessions/sessions.service.ts index 816752b9d..4b709d994 100644 --- a/server/src/nest/sessions/sessions.service.ts +++ b/server/src/nest/sessions/sessions.service.ts @@ -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 { - 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); } /** diff --git a/server/tests/unit/db/repositories/UserSessions.repository.test.ts b/server/tests/unit/db/repositories/UserSessions.repository.test.ts index 6927f9ee3..63f126563 100644 --- a/server/tests/unit/db/repositories/UserSessions.repository.test.ts +++ b/server/tests/unit/db/repositories/UserSessions.repository.test.ts @@ -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([]); }); diff --git a/server/tests/unit/nest/sessions/sessions.service.test.ts b/server/tests/unit/nest/sessions/sessions.service.test.ts index b4429228c..3650ffa34 100644 --- a/server/tests/unit/nest/sessions/sessions.service.test.ts +++ b/server/tests/unit/nest/sessions/sessions.service.test.ts @@ -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 }));