fix: do not serve data to an unauthenticated websocket client

This commit is contained in:
Carlos Valente
2026-09-15 18:20:37 +02:00
committed by Carlos Valente
parent 319293ab4c
commit 1a9950d658
2 changed files with 93 additions and 2 deletions
+11 -2
View File
@@ -62,11 +62,22 @@ class SocketServer implements IAdapter {
this.wss = new WebSocketServer({ path: `${prefix}/ws`, server, maxPayload: this.MAX_PAYLOAD });
this.wss.on('connection', (ws, req) => {
// Rejected sockets can emit an error while their close handshake is in progress.
ws.on('error', console.error);
let isAuthenticated = false;
authenticateSocket(ws, req, (error) => {
if (error) {
ws.close(1008, 'Unauthorized');
return;
}
isAuthenticated = true;
});
if (!isAuthenticated) {
return;
}
const clientId = generateId();
const clientName = getRandomName();
function sendPacket<T extends MessageTag>(
@@ -94,8 +105,6 @@ class SocketServer implements IAdapter {
// send store payload on connect
sendPacket(MessageTag.RuntimeData, eventStore.poll());
ws.on('error', console.error);
ws.on('close', () => {
this.clients.delete(clientId);
logger.info(LogOrigin.Client, `${this.clients.size} Connections with disconnected: ${clientName}`);