Skip to content

Commit 76d47c1

Browse files
committed
[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.
1 parent 5d55e52 commit 76d47c1

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/websocket-server.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class WebSocketServer extends EventEmitter {
251251
var protocol = req.headers['sec-websocket-protocol'];
252252

253253
if (protocol) {
254-
protocol = protocol.trim().split(/ *, */);
254+
protocol = protocol.split(',').map(trim);
255255

256256
//
257257
// Optionally call external protocol selection handler.
@@ -355,3 +355,15 @@ function abortHandshake (socket, code, message, headers) {
355355
socket.removeListener('error', socketOnError);
356356
socket.destroy();
357357
}
358+
359+
/**
360+
* Remove whitespace characters from both ends of a string.
361+
*
362+
* @param {String} str The string
363+
* @return {String} A new string representing `str` stripped of whitespace
364+
* characters from both its beginning and end
365+
* @private
366+
*/
367+
function trim(str) {
368+
return str.trim();
369+
}

0 commit comments

Comments
 (0)