Skip to content

Add WebSocket authentication guard to CollaborationGateway using JWT validation #887

Description

@RUKAYAT-CODER

Overview

src/collaboration/collaboration.gateway.ts has @WebSocketGateway with no guards applied. Any WebSocket client that can reach the server can join any collaboration session by knowing the session ID, without any identity or authorization check. This is effectively unauthenticated write access to shared document state.

Specifications

Features:

  • Validate a JWT passed in the WebSocket handshake query string or Authorization header before the connection is accepted.
  • Reject connections with invalid or missing tokens.

Tasks:

  • Create WsJwtAuthGuard that reads socket.handshake.auth.token or query ?token=.
  • Verify the JWT using the same JwtService used for REST routes.
  • Apply the guard via @UseGuards(WsJwtAuthGuard) on the gateway class.
  • Store the verified user in socket.data.user for downstream use.
  • Add integration tests for rejected and accepted WebSocket connections.

Impacted Files:

  • src/collaboration/collaboration.gateway.ts
  • New src/collaboration/guards/ws-jwt-auth.guard.ts

Acceptance Criteria

  • WebSocket connection without valid JWT is rejected during handshake.
  • Authenticated user identity is available in all message handlers via socket.data.user.
  • Integration test covers both anonymous and authenticated connection scenarios.

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbugSomething isn't workingsecurity

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions