Skip to content

Commit a33007b

Browse files
committed
Ensure userId in payload and socket match
1 parent 6f44ddf commit a33007b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

backend/gateway-service/src/modules/match/match.controller.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ export class MatchGateway implements OnGatewayInit {
6767
return;
6868
}
6969

70-
// Retrieve the userId associated with the current socket
71-
const storedUserId = [...this.userSockets.entries()].find(
72-
([, socketId]) => socketId === client.id,
73-
)?.[0];
74-
if (!storedUserId || storedUserId !== payload.userId) {
75-
client.emit(EXCEPTION, 'UserId does not match the current socket.');
70+
if (!this.validateUserId(client, payload.userId)) {
7671
return;
7772
}
7873

@@ -107,6 +102,10 @@ export class MatchGateway implements OnGatewayInit {
107102
return;
108103
}
109104

105+
if (!this.validateUserId(client, payload.userId)) {
106+
return;
107+
}
108+
110109
try {
111110
const result = await firstValueFrom(
112111
this.matchingClient.send('match-cancel', { userId: payload.userId }),
@@ -239,4 +238,16 @@ export class MatchGateway implements OnGatewayInit {
239238
client.emit(EXCEPTION, `Error connecting to /match socket: ${message}`);
240239
client.disconnect();
241240
}
241+
242+
// Method to validate the userId associated with the current socket
243+
private validateUserId(client: Socket, userId: string): boolean {
244+
const storedUserId = [...this.userSockets.entries()].find(
245+
([, socketId]) => socketId === client.id,
246+
)?.[0];
247+
if (!storedUserId || storedUserId !== userId) {
248+
client.emit(EXCEPTION, 'UserId does not match the current socket.');
249+
return false;
250+
}
251+
return true;
252+
}
242253
}

0 commit comments

Comments
 (0)