From 63e5d4b493b30dc2f4259ba6c30bf65a3bfc2abc Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 27 Sep 2024 17:25:07 -0400 Subject: [PATCH 1/3] fail websocket connection in error cases --- lib/web/websocket/receiver.js | 3 +-- lib/web/websocket/stream/websocketstream.js | 2 +- lib/web/websocket/websocket.js | 12 ++---------- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/web/websocket/receiver.js b/lib/web/websocket/receiver.js index 2638abe3bfd..dac9122a408 100644 --- a/lib/web/websocket/receiver.js +++ b/lib/web/websocket/receiver.js @@ -15,7 +15,6 @@ const { isContinuationFrame } = require('./util') const { WebsocketFrameSend } = require('./frame') -const { closeWebSocketConnection } = require('./connection') const { PerMessageDeflate } = require('./permessage-deflate') // This code was influenced by ws released under the MIT license. @@ -225,7 +224,7 @@ class ByteParser extends Writable { } else { this.#extensions.get('permessage-deflate').decompress(body, this.#info.fin, (error, data) => { if (error) { - closeWebSocketConnection(this.#handler, 1007, error.message) + failWebsocketConnection(this.#handler, 1007, error.message) return } diff --git a/lib/web/websocket/stream/websocketstream.js b/lib/web/websocket/stream/websocketstream.js index bd426f6af7a..1e956740c90 100644 --- a/lib/web/websocket/stream/websocketstream.js +++ b/lib/web/websocket/stream/websocketstream.js @@ -50,7 +50,7 @@ class WebSocketStream { onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions), onFail: (reason) => this.#onFail(reason), onMessage: (opcode, data) => this.#onMessage(opcode, data), - onParserError: (err) => {}, // eslint-disable-line + onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message), // eslint-disable-line onParserDrain: () => this.#handler.socket.resume(), onSocketData: (chunk) => { if (!this.#parser.write(chunk)) { diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index e04c6b6f921..05f8fa2e888 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -530,19 +530,11 @@ class WebSocket extends EventTarget { } #onParserError (err) { - let message - let code - - if (err instanceof CloseEvent) { - message = err.reason - code = err.code - } else { - message = err.message - } + const message = err.message fireEvent('error', this, () => new ErrorEvent('error', { error: err, message })) - closeWebSocketConnection(this.#handler, code, null) + failWebsocketConnection(this.#handler, null, message) } #onParserDrain () { From 0240ad6738e7212bca4d372e3abffc221a458bbf Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 27 Sep 2024 17:25:44 -0400 Subject: [PATCH 2/3] fail websocket connection in error cases --- lib/web/websocket/stream/websocketstream.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/websocket/stream/websocketstream.js b/lib/web/websocket/stream/websocketstream.js index 1e956740c90..fe6a6fba551 100644 --- a/lib/web/websocket/stream/websocketstream.js +++ b/lib/web/websocket/stream/websocketstream.js @@ -50,7 +50,7 @@ class WebSocketStream { onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions), onFail: (reason) => this.#onFail(reason), onMessage: (opcode, data) => this.#onMessage(opcode, data), - onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message), // eslint-disable-line + onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message), onParserDrain: () => this.#handler.socket.resume(), onSocketData: (chunk) => { if (!this.#parser.write(chunk)) { From bfbce5b5d2570919f1fb795c6dfddfd00b3a8f33 Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 27 Sep 2024 17:33:00 -0400 Subject: [PATCH 3/3] remove dupe error event emits --- lib/web/websocket/websocket.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index 05f8fa2e888..584ead55a5d 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -61,7 +61,7 @@ class WebSocket extends EventTarget { onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions), onFail: (code, reason) => this.#onFail(code, reason), onMessage: (opcode, data) => this.#onMessage(opcode, data), - onParserError: (err) => this.#onParserError(err), + onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message), onParserDrain: () => this.#onParserDrain(), onSocketData: (chunk) => { if (!this.#parser.write(chunk)) { @@ -529,14 +529,6 @@ class WebSocket extends EventTarget { }) } - #onParserError (err) { - const message = err.message - - fireEvent('error', this, () => new ErrorEvent('error', { error: err, message })) - - failWebsocketConnection(this.#handler, null, message) - } - #onParserDrain () { this.#handler.socket.resume() }