From fe772c4e14fe9b0f73fa52bcd9ee071c8928f218 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 19 Jul 2020 17:37:10 +0200 Subject: [PATCH] WebSocketListener: make cookies+session optional --- package.json | 2 +- src/WebSocketListener.ts | 2 +- src/auth/magic_link/MagicLinkWebSocketListener.ts | 8 +++++++- src/components/WebSocketServerComponent.ts | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 74c879a..1d8e8bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.17.1", + "version": "0.17.2", "description": "Node web framework", "repository": "git@gitlab.com:ArisuOngaku/wms-core.git", "author": "Alice Gaudon ", diff --git a/src/WebSocketListener.ts b/src/WebSocketListener.ts index e0d60a3..3f822ff 100644 --- a/src/WebSocketListener.ts +++ b/src/WebSocketListener.ts @@ -15,5 +15,5 @@ export default abstract class WebSocketListener { public abstract path(): string; - public abstract async handle(socket: WebSocket, request: IncomingMessage, session: Express.SessionData): Promise; + public abstract async handle(socket: WebSocket, request: IncomingMessage, session: Express.SessionData | null): Promise; } \ No newline at end of file diff --git a/src/auth/magic_link/MagicLinkWebSocketListener.ts b/src/auth/magic_link/MagicLinkWebSocketListener.ts index 22aa198..4996966 100644 --- a/src/auth/magic_link/MagicLinkWebSocketListener.ts +++ b/src/auth/magic_link/MagicLinkWebSocketListener.ts @@ -13,7 +13,13 @@ export default class MagicLinkWebSocketListener extends WebSocketListener { } } - async handle(socket: WebSocket, request: IncomingMessage, session: Express.SessionData): Promise { + async handle(socket: WebSocket, request: IncomingMessage, session: Express.SessionData | null): Promise { + // Drop if requested without session + if (!session) { + socket.close(1002, 'Session is required for this request.'); + return; + } + // Refuse any incoming data socket.on('message', () => { socket.close(1003); diff --git a/src/components/WebSocketServerComponent.ts b/src/components/WebSocketServerComponent.ts index 52d22b0..5faf4e4 100644 --- a/src/components/WebSocketServerComponent.ts +++ b/src/components/WebSocketServerComponent.ts @@ -39,7 +39,9 @@ export default class WebSocketServerComponent extends ApplicationComponent socket.close(1002, `Path not found ${request.url}`); return; } else if (!request.headers.cookie) { - socket.close(1002, `Can't process request without cookies.`); + listener.handle(socket, request, null).catch(err => { + Logger.error(err, 'Error in websocket listener.'); + }); return; }