Skip to content

Commit 718c33c

Browse files
committed
[security] Limit automatically sent pending pongs
A malicious peer could flood the other peer with pings while stopping reads, forcing it to buffer response pongs until the process crashes due to OOM. Add a limit on the number of automatically sent pending pongs and forcibly close the connection if the threshold is exceeded. The limit can be customized via the new `maxPendingAutoPongs` option.
1 parent 1680801 commit 718c33c

5 files changed

Lines changed: 138 additions & 3 deletions

File tree

‎doc/ws.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ This class represents a WebSocket server. It extends the `EventEmitter`.
8383
- `maxFragments` {Number} The maximum number of fragments in a message.
8484
Defaults to 16384. Set to 0 to disable the limit.
8585
- `maxPayload` {Number} The maximum allowed message size in bytes.
86+
- `maxPendingAutoPongs` {Number} The maximum number of pending automatic
87+
pongs. The value is coerced to a 32-bit signed integer. If the limit is
88+
exceeded, the connection is forcibly terminated. Defaults to 60. Set to 0 to
89+
disable the limit.
8690
- `callback` {Function}
8791

8892
Create a new server instance. One and only one of `port`, `server` or `noServer`
@@ -277,6 +281,10 @@ This class represents a WebSocket. It extends the `EventEmitter`.
277281
- `maxFragments` {Number} The maximum number of fragments in a message.
278282
Defaults to 16384. Set to 0 to disable the limit.
279283
- `maxPayload` {Number} The maximum allowed message size in bytes.
284+
- `maxPendingAutoPongs` {Number} The maximum number of pending automatic
285+
pongs. The value is coerced to a 32-bit signed integer. If the limit is
286+
exceeded, the connection is forcibly terminated. Defaults to 60. Set to 0 to
287+
disable the limit.
280288
- Any other option allowed in [http.request()][] or [https.request()][].
281289
Options given do not have any effect if parsed from the URL given with the
282290
`address` parameter.

‎lib/websocket-server.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class WebSocketServer extends EventEmitter {
4242
* fragments
4343
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
4444
* size
45+
* @param {Number} [options.maxPendingAutoPongs=60] The maximum number of
46+
* pending automatic pongs
4547
* @param {Boolean} [options.noServer=false] Enable no server mode
4648
* @param {String} [options.path] Accept only connections matching this path
4749
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
@@ -59,6 +61,7 @@ class WebSocketServer extends EventEmitter {
5961
maxBufferedChunks: 256 * 1024,
6062
maxFragments: 16 * 1024,
6163
maxPayload: 100 * 1024 * 1024,
64+
maxPendingAutoPongs: 60,
6265
perMessageDeflate: false,
6366
handleProtocols: null,
6467
clientTracking: true,
@@ -318,7 +321,7 @@ class WebSocketServer extends EventEmitter {
318321
`Sec-WebSocket-Accept: ${digest}`
319322
];
320323

321-
const ws = new WebSocket(null);
324+
const ws = new WebSocket(null, undefined, this.options);
322325
let protocol = req.headers['sec-websocket-protocol'];
323326

324327
if (protocol) {

‎lib/websocket.js‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ class WebSocket extends EventEmitter {
7171
protocols = undefined;
7272
}
7373

74-
initAsClient(this, address, protocols, options);
74+
options = initAsClient(this, address, protocols, options);
7575
} else {
7676
this._isServer = true;
7777
}
78+
79+
this._autoPongComplete = () => autoPongComplete(this);
80+
this._pendingAutoPongs = 0;
81+
this._maxPendingAutoPongs = options.maxPendingAutoPongs | 0;
7882
}
7983

8084
/**
@@ -583,10 +587,13 @@ module.exports = WebSocket;
583587
* fragments
584588
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
585589
* size
590+
* @param {Number} [options.maxPendingAutoPongs=60] The maximum number of
591+
* pending automatic pongs
586592
* @param {Boolean} [options.followRedirects=false] Whether or not to follow
587593
* redirects
588594
* @param {Number} [options.maxRedirects=10] The maximum number of redirects
589595
* allowed
596+
* @return {Object} The resolved options object used for initialization
590597
* @private
591598
*/
592599
function initAsClient(websocket, address, protocols, options) {
@@ -595,6 +602,7 @@ function initAsClient(websocket, address, protocols, options) {
595602
maxBufferedChunks: 256 * 1024,
596603
maxFragments: 16 * 1024,
597604
maxPayload: 100 * 1024 * 1024,
605+
maxPendingAutoPongs: 60,
598606
perMessageDeflate: true,
599607
followRedirects: false,
600608
maxRedirects: 10,
@@ -901,6 +909,8 @@ function initAsClient(websocket, address, protocols, options) {
901909
opts.maxFragments
902910
);
903911
});
912+
913+
return opts;
904914
}
905915

906916
/**
@@ -1097,10 +1107,31 @@ function receiverOnMessage(data) {
10971107
function receiverOnPing(data) {
10981108
const websocket = this[kWebSocket];
10991109

1100-
websocket.pong(data, !websocket._isServer, NOOP);
1110+
if (websocket.readyState === WebSocket.OPEN) {
1111+
if (
1112+
websocket._maxPendingAutoPongs > 0 &&
1113+
websocket._pendingAutoPongs >= websocket._maxPendingAutoPongs
1114+
) {
1115+
websocket.terminate();
1116+
} else {
1117+
websocket._pendingAutoPongs++;
1118+
websocket.pong(data, !websocket._isServer, websocket._autoPongComplete);
1119+
}
1120+
}
1121+
11011122
websocket.emit('ping', data);
11021123
}
11031124

1125+
/**
1126+
* The callback for automatic pong responses.
1127+
*
1128+
* @param {WebSocket} websocket The WebSocket instance
1129+
* @private
1130+
*/
1131+
function autoPongComplete(websocket) {
1132+
websocket._pendingAutoPongs--;
1133+
}
1134+
11041135
/**
11051136
* The listener of the `Receiver` `'pong'` event.
11061137
*

‎test/websocket-server.test.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,54 @@ describe('WebSocketServer', () => {
9696
wss.close(done);
9797
});
9898
});
99+
100+
it('honors the `maxPendingAutoPongs` option', (done) => {
101+
const maxPendingAutoPongs = 30;
102+
let serverClientTerminateCalled = false;
103+
104+
const wss = new WebSocket.Server(
105+
{ port: 0, maxPendingAutoPongs },
106+
() => {
107+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
108+
109+
ws.on('open', () => {
110+
ws._socket.pause();
111+
112+
const data = Buffer.alloc(125);
113+
114+
function ping(err) {
115+
if (err) {
116+
ws._socket.resume();
117+
return;
118+
}
119+
120+
ws.ping(data, ping);
121+
}
122+
123+
ping();
124+
});
125+
126+
ws.on('close', (code) => {
127+
assert.ok(serverClientTerminateCalled);
128+
assert.strictEqual(code, 1006);
129+
wss.close(done);
130+
});
131+
}
132+
);
133+
134+
wss.on('connection', (ws) => {
135+
assert.strictEqual(ws._maxPendingAutoPongs, maxPendingAutoPongs);
136+
137+
const original = ws.terminate;
138+
139+
ws.terminate = () => {
140+
assert.strictEqual(ws._pendingAutoPongs, maxPendingAutoPongs);
141+
serverClientTerminateCalled = true;
142+
ws.terminate = original;
143+
ws.terminate();
144+
};
145+
});
146+
});
99147
});
100148

101149
it('emits an error if http server bind fails', (done) => {

‎test/websocket.test.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,51 @@ describe('WebSocket', () => {
9999
/^RangeError: Unsupported protocol version: 1000 \(supported versions: 8, 13\)$/
100100
);
101101
});
102+
103+
it('honors the `maxPendingAutoPongs` option', (done) => {
104+
const maxPendingAutoPongs = 30;
105+
let clientTerminateCalled = false;
106+
107+
const wss = new WebSocket.Server({ port: 0 }, () => {
108+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {
109+
maxPendingAutoPongs
110+
});
111+
112+
assert.strictEqual(ws._maxPendingAutoPongs, maxPendingAutoPongs);
113+
114+
const original = ws.terminate;
115+
116+
ws.terminate = () => {
117+
assert.strictEqual(ws._pendingAutoPongs, maxPendingAutoPongs);
118+
clientTerminateCalled = true;
119+
ws.terminate = original;
120+
ws.terminate();
121+
};
122+
});
123+
124+
wss.on('connection', (ws) => {
125+
ws._socket.pause();
126+
127+
const data = Buffer.alloc(125);
128+
129+
function ping(err) {
130+
if (err) {
131+
ws._socket.resume();
132+
return;
133+
}
134+
135+
ws.ping(data, ping);
136+
}
137+
138+
ping();
139+
140+
ws.on('close', (code) => {
141+
assert.ok(clientTerminateCalled);
142+
assert.strictEqual(code, 1006);
143+
wss.close(done);
144+
});
145+
});
146+
});
102147
});
103148
});
104149

0 commit comments

Comments
 (0)