You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Throws TypeError when there are too many HTTP headersΒ #2230
I've searched for any related issues and avoided creating a duplicate issue.
Description
I use this package for a websocket server. In the process of changing some options to try to make my websocket server slightly more resistant to DoS, I lowered the maxHeadersCount field on the HTTP server from its default of 2000 to a lower value. I then tested to see whether the server would reject requests with too many headers. Instead, the server crashed due to a TypeError from websocket-server.js line 246. This seems like a DoS issue.
I expected the websocket server to refuse the handshake. Maybe respond with a 400 Bad Request. I did not expect my server app to crash from a TypeError.
Actual result
webpack://somewhere/node_modules/ws/lib/websocket-server.js:246
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
^
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
at handleUpgrade (webpack://somewhere/node_modules/ws/lib/websocket-server.js:246:29)
at Server.upgrade (webpack://somewhere/node_modules/ws/lib/websocket-server.js:119:16)
at Server.emit (node:events:519:28)
at onParserExecuteCommon (node:_http_server:942:14)
at onParserExecute (node:_http_server:836:3)
Node.js v21.5.0
Here is a test case to reproduce the issue using only Node.js core modules.
consthttp=require('http');constserver=http.createServer();server.maxHeadersCount=1;server.on('upgrade',function(request){console.log(request.headers);});server.listen(function(){const{ port }=server.address();constrequest=http.request({headers: {foo: 'foo',bar: 'bar',baz: 'baz',qux: 'qux',connection: 'Upgrade',upgrade: 'protocol'},host: '127.0.0.1',
port
});request.end();});
The 'upgrade' event is emitted even if server.maxHeadersCount is set to 1. It seems that setting the property does not abort the request. If it is the expected behavior, I wonder what is the purpose of setting that limit.
Activity
lpinca commentedon Jun 15, 2024
Please create a minimal reproducible test case using only
ws. As per #1838 (comment), #1838 (comment) and #1838 (comment) it cannot happen unlesswsis incorrectly used.If the server
'upgrade'event is emitted without theUpgradeheader then the bug is in Node.js core.lpinca commentedon Jun 15, 2024
Here is a test case to reproduce the issue using only Node.js core modules.
The
'upgrade'event is emitted even ifserver.maxHeadersCountis set to1. It seems that setting the property does not abort the request. If it is the expected behavior, I wonder what is the purpose of setting that limit.[fix] Abort the handshake if the Upgrade header cannot be validated
lpinca commentedon Jun 15, 2024
See #2231.
[security] Fix crash when the Upgrade header cannot be read
[security] Fix crash when the Upgrade header cannot be read (#2231)
[security] Fix crash when the Upgrade header cannot be read (#2231)
[security] Fix crash when the Upgrade header cannot be read (#2231)
[security] Fix crash when the Upgrade header cannot be read (#2231)
[security] Fix crash when the Upgrade header cannot be read (#2231)
Update dependency ws to v8.17.1 (#1080)
Update dependency ws to v8.17.1 (#2122)
fix(deps): update all non-major dependencies (#436)
Update ws to v7.5.10 (websockets/ws#2230)
Update ws to v7.5.10 (websockets/ws#2230)
build: Update dependencies to fix warning
Bump ws to 8.17.1 to mitigate security issue - a DoS when handling a β¦
Update dependency ws to v8.18.0 (#2232)
chore: Added resolutions in package.json file to resolve CVE-2024-378β¦ (
Update dependency ws to v7.5.10 [SECURITY] (#259)
deps: Update dependency ws to v8.17.1 [SECURITY] (main) (#20945)