⚠️ This is a backend issue — work is done inside the backend/ folder
Description
backend/src/modules/realtime/realtime.gateway.ts has a comment — // Extract userId from token if auth implemented — in handleConnection(). The gateway currently accepts connections from any client without verifying identity. This means any unauthenticated client can subscribe to any user's file upload and processing events.
Current State
backend/src/modules/realtime/realtime.gateway.ts — no auth on connection
backend/src/websocket/guards/ws-auth.guard.ts — a WebSocket auth guard exists in the websocket module but is not used in the realtime gateway
- CORS is set to
origin: '*' — acceptable for dev but must be env-driven
What Needs to Be Built
1. Apply WsAuthGuard to RealtimeGateway
@UseGuards(WsAuthGuard)
@WebSocketGateway({ namespace: 'files', cors: { origin: process.env.ALLOWED_ORIGINS } })
export class RealtimeGateway implements OnGatewayConnection, OnGatewayDisconnect {
2. Extract userId on Connection
handleConnection(client: Socket) {
const token = client.handshake.auth?.token ?? client.handshake.headers?.authorization?.split(' ')[1];
const payload = this.jwtService.verify(token);
client.data.userId = payload.sub;
client.join(`user:${payload.sub}`);
}
3. Scope Subscriptions to Authenticated User
subscribe:file — verify the requesting user owns the file before joining the room
subscribe:user — only allow joining own user room (prevent subscribing to other users' events)
4. CORS Configuration
- Replace hardcoded
origin: '*' with ALLOWED_ORIGINS env variable
- Support comma-separated list of origins
5. Unit Tests
- Test that unauthenticated connections are rejected
- Test that users cannot subscribe to another user's room
Acceptance Criteria
Files to Modify
backend/src/modules/realtime/realtime.gateway.ts
backend/src/modules/realtime/realtime.module.ts (import JwtModule)
Priority
High — unauthenticated access to real-time events is a security vulnerability
Estimated Effort
1–2 days
backend/folderDescription
backend/src/modules/realtime/realtime.gateway.tshas a comment —// Extract userId from token if auth implemented— inhandleConnection(). The gateway currently accepts connections from any client without verifying identity. This means any unauthenticated client can subscribe to any user's file upload and processing events.Current State
backend/src/modules/realtime/realtime.gateway.ts— no auth on connectionbackend/src/websocket/guards/ws-auth.guard.ts— a WebSocket auth guard exists in the websocket module but is not used in the realtime gatewayorigin: '*'— acceptable for dev but must be env-drivenWhat Needs to Be Built
1. Apply WsAuthGuard to RealtimeGateway
2. Extract userId on Connection
3. Scope Subscriptions to Authenticated User
subscribe:file— verify the requesting user owns the file before joining the roomsubscribe:user— only allow joining own user room (prevent subscribing to other users' events)4. CORS Configuration
origin: '*'withALLOWED_ORIGINSenv variable5. Unit Tests
Acceptance Criteria
401disconnectclient.data.userIdis populated from the JWT on every connectionFiles to Modify
backend/src/modules/realtime/realtime.gateway.tsbackend/src/modules/realtime/realtime.module.ts(import JwtModule)Priority
High — unauthenticated access to real-time events is a security vulnerability
Estimated Effort
1–2 days