From 42e38d8e3580adcbfae5e0a579bcebe6df695262 Mon Sep 17 00:00:00 2001 From: Patrick Schiller
Date: Sat, 5 Dec 2026 11:44:48 +0100 Subject: [PATCH] fix(security): make realtime invalidations opaque and session-bound Signed-off-by: Patrick Schiller
---
.../api/realtime-session-boundary.e2e.spec.ts | 53 +++++++++++++++----
apps/api/src/app/events/events.gateway.ts | 51 ++++++++++++++++--
2 files changed, 90 insertions(+), 14 deletions(-)
diff --git a/apps/api-e2e/src/api/realtime-session-boundary.e2e.spec.ts b/apps/api-e2e/src/api/realtime-session-boundary.e2e.spec.ts
index 9a3e3aa..971a070 100644
--- a/apps/api-e2e/src/api/realtime-session-boundary.e2e.spec.ts
+++ b/apps/api-e2e/src/api/realtime-session-boundary.e2e.spec.ts
@@ -105,9 +105,7 @@ describe('Realtime session boundary against persisted identity and installation
await gateway.handleConnection(client);
expect(client.data.user.role).toBe('Manager');
await gateway.broadcast('project:changed', { projectId: 'example' });
- expect(client.emit).toHaveBeenCalledWith('project:changed', {
- projectId: 'example',
- });
+ expect(client.emit).toHaveBeenCalledWith('project:changed');
jest.mocked(client.emit).mockClear();
await ctx.prisma.employee.update({
where: { id: owner.id },
@@ -132,9 +130,7 @@ describe('Realtime session boundary against persisted identity and installation
update: { mode: 'Solo', ownerEmployeeId: owner.id },
});
await gateway.broadcast('time-entry:created', { employeeId: owner.id });
- expect(ownerClient.emit).toHaveBeenCalledWith('time-entry:created', {
- employeeId: owner.id,
- });
+ expect(ownerClient.emit).toHaveBeenCalledWith('time-entry:created');
expect(otherClient.emit).not.toHaveBeenCalled();
expect(otherClient.disconnect).toHaveBeenCalledWith(true);
jest.mocked(ownerClient.emit).mockClear();
@@ -161,9 +157,7 @@ describe('Realtime session boundary against persisted identity and installation
});
expect(oldClient.disconnect).toHaveBeenCalledWith(true);
expect(oldClient.emit).not.toHaveBeenCalled();
- expect(newClient.emit).toHaveBeenCalledWith('project:changed', {
- projectId: 'after-password-change',
- });
+ expect(newClient.emit).toHaveBeenCalledWith('project:changed');
const denied = socket(oldToken);
await gateway.handleConnection(denied);
expect(denied.disconnect).toHaveBeenCalledWith(true);
@@ -180,4 +174,45 @@ describe('Realtime session boundary against persisted identity and installation
expect(client.disconnect).toHaveBeenCalledWith(true);
expect(client.emit).not.toHaveBeenCalled();
});
+
+ it('disconnects a client whose server-side login session was revoked', async () => {
+ const { owner } = await fixture();
+ const client = socket(await login(ctx.http, owner.email));
+ await gateway.handleConnection(client);
+ const session = await ctx.prisma.authSession.findFirstOrThrow({
+ where: { employeeId: owner.id },
+ });
+ await ctx.prisma.authSession.update({
+ where: { id: session.id },
+ data: { revokedAt: new Date() },
+ });
+
+ await gateway.broadcast('project:changed', { projectId: 'revoked' });
+
+ expect(client.disconnect).toHaveBeenCalledWith(true);
+ expect(client.emit).not.toHaveBeenCalled();
+ });
+
+ it('emits opaque Team invalidations without cross-user payload metadata', async () => {
+ const { owner, other } = await fixture();
+ const ownerClient = socket(await login(ctx.http, owner.email));
+ const otherClient = socket(await login(ctx.http, other.email));
+ await gateway.handleConnection(ownerClient);
+ await gateway.handleConnection(otherClient);
+
+ await gateway.broadcast('request:transitioned', {
+ id: 'private-request',
+ employeeId: owner.id,
+ workflowState: 'PendingManager',
+ actorId: owner.id,
+ });
+
+ for (const client of [ownerClient, otherClient]) {
+ expect(client.emit).toHaveBeenCalledWith('request:transitioned');
+ expect(client.emit).not.toHaveBeenCalledWith(
+ 'request:transitioned',
+ expect.anything(),
+ );
+ }
+ });
});
diff --git a/apps/api/src/app/events/events.gateway.ts b/apps/api/src/app/events/events.gateway.ts
index 2b5d19a..64af52c 100644
--- a/apps/api/src/app/events/events.gateway.ts
+++ b/apps/api/src/app/events/events.gateway.ts
@@ -14,6 +14,7 @@ import { InstallationService } from '../installation/installation.service';
interface AuthorizedSocket {
client: Socket;
employeeId: string;
+ sessionId: string | null;
authVersion: number;
expiresAt: number;
expiryTimer: ReturnType