From 00c425ec77993773d823f018f64a5c44e17023ff Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Tue, 25 May 2021 11:00:58 +0200 Subject: [PATCH] [security] Fix ReDoS vulnerability A specially crafted value of the `Sec-Websocket-Protocol` header could be used to significantly slow down a ws server. PoC and fix were sent privately by Robert McLaughlin from University of California, Santa Barbara. --- lib/websocket-server.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/websocket-server.js b/lib/websocket-server.js index b99ad050a..3c3bbe0b0 100644 --- a/lib/websocket-server.js +++ b/lib/websocket-server.js @@ -286,7 +286,7 @@ class WebSocketServer extends EventEmitter { let protocol = req.headers['sec-websocket-protocol']; if (protocol) { - protocol = protocol.trim().split(/ *, */); + protocol = protocol.split(',').map(trim); // // Optionally call external protocol selection handler. @@ -404,3 +404,15 @@ function abortHandshake(socket, code, message, headers) { socket.removeListener('error', socketOnError); socket.destroy(); } + +/** + * Remove whitespace characters from both ends of a string. + * + * @param {String} str The string + * @return {String} A new string representing `str` stripped of whitespace + * characters from both its beginning and end + * @private + */ +function trim(str) { + return str.trim(); +}