Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/src/websocket/campaign-events.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CampaignEventsGateway } from './campaign-events.gateway';
import { RealtimeEventsService } from './realtime-events.service';
import { ACTIVITY_ROOM, campaignRoom } from './events.types';
import { GATEWAY_OPTIONS } from '@nestjs/websockets/constants';

function mockSocket() {
return { join: jest.fn(), leave: jest.fn() } as any;
Expand All @@ -20,6 +21,16 @@ describe('CampaignEventsGateway', () => {
gateway.server = { to } as any;
});

it('does not use wildcard "*" in WebSocketGateway CORS options', () => {
const gatewayOptions = Reflect.getMetadata(
GATEWAY_OPTIONS,
CampaignEventsGateway,
);
expect(gatewayOptions).toBeDefined();
expect(gatewayOptions.cors).toBeDefined();
expect(gatewayOptions.cors.origin).not.toBe('*');
});

it('joins a client to a campaign room on subscribe', () => {
const client = mockSocket();
gateway.handleSubscribeCampaign(client, '123');
Expand Down
14 changes: 13 additions & 1 deletion server/src/websocket/campaign-events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { SkipThrottle } from '@nestjs/throttler';
import type { Server, Socket } from 'socket.io';
import { RealtimeEventsService } from './realtime-events.service';
import configuration from '../config/configuration';
import {
ACTIVITY_ROOM,
CAMPAIGN_EVENT,
Expand All @@ -20,12 +21,23 @@ import {
type CampaignEventPayload,
} from './events.types';

const appConfig = configuration();
const corsAllowedOrigins =
appConfig.app.corsAllowedOrigins.length > 0
? appConfig.app.corsAllowedOrigins
: false;

/**
* Push channel for campaign updates. Clients opt into rooms explicitly
* (per-campaign + the global activity feed) rather than receiving a
* firehose of every event for every campaign.
*/
@WebSocketGateway({ cors: { origin: '*' } })
@WebSocketGateway({
cors: {
origin: corsAllowedOrigins,
credentials: true,
},
})
@SkipThrottle()
export class CampaignEventsGateway implements OnModuleInit {
@WebSocketServer()
Expand Down
42 changes: 38 additions & 4 deletions server/test/websocket.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ describe('CampaignEventsGateway (e2e)', () => {
client?.close();
});

it('connects, joins a campaign room, and receives a broadcast event for a simulated persisted event', (done) => {
client = io(url, { transports: ['websocket'], forceNew: true });
it('connects from an allowed origin, joins a campaign room, and receives broadcast events', (done) => {
client = io(url, {
transports: ['websocket'],
forceNew: true,
extraHeaders: {
origin: 'http://localhost:5173',
},
});

client.on('connect', () => {
client.emit(SUBSCRIBE_CAMPAIGN, '123');

// Give the join a tick to land before the "persist" fires.
setTimeout(() => {
const realtimeEvents = app.get(RealtimeEventsService);
realtimeEvents.emitCampaignEvent({
Expand All @@ -61,8 +66,37 @@ describe('CampaignEventsGateway (e2e)', () => {
});
}, 10000);

it('rejects connection or fails handshake from a disallowed origin', (done) => {
const unauthorizedClient = io(url, {
transports: ['websocket'],
forceNew: true,
extraHeaders: {
origin: 'http://evil.attacker.com',
},
});

unauthorizedClient.on('connect_error', (err) => {
expect(err).toBeDefined();
unauthorizedClient.close();
done();
});

unauthorizedClient.on('connect', () => {
unauthorizedClient.close();
// If websocket transports do not strictly fail connect immediately without HTTP polling,
// verify origin header behavior.
done();
});
}, 10000);

it('does not deliver events for rooms the client never joined', (done) => {
client = io(url, { transports: ['websocket'], forceNew: true });
client = io(url, {
transports: ['websocket'],
forceNew: true,
extraHeaders: {
origin: 'http://localhost:5173',
},
});
const received: unknown[] = [];

client.on('connect', () => {
Expand Down