Limit pings like messages in the tracker

ws answered every ping on its own, past the message rate limit and past the bound on bytes queued
for a peer: a client pinging without reading made the tracker buffer pongs until it ran out of
memory. Pongs are now sent by the tracker, pings and pongs count as messages, and a pong is queued
only within the same bound as any message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-25 15:54:03 +01:00
1 parent 22d794965e
commit 4725b14a40
2 files changed
+28 -4

No files matched your search

+12 -4
View File
@@ -57,10 +57,14 @@ function refuse(socket, status) {
// MAX_FORWARDED limits what each sender sends, this what a peer gets from all of them, e.g. from // MAX_FORWARDED limits what each sender sends, this what a peer gets from all of them, e.g. from
// receivers that reconnect over and over. A peer that stopped reading is dropped at once, close would // receivers that reconnect over and over. A peer that stopped reading is dropped at once, close would
// wait for its close frame. // wait for its close frame.
function fits(peer, bytes) {
if (peer.ws.bufferedAmount + bytes <= MAX_QUEUED) return true;
peer.ws.terminate();
}
function send(peer, message) { function send(peer, message) {
const data = JSON.stringify(message); const data = JSON.stringify(message);
if (peer.ws.bufferedAmount + Buffer.byteLength(data) > MAX_QUEUED) return peer.ws.terminate(); if (fits(peer, Buffer.byteLength(data))) peer.ws.send(data);
peer.ws.send(data);
} }
// HellCon shows the reason, the connection closes. // HellCon shows the reason, the connection closes.
@@ -133,8 +137,12 @@ function open(ws) {
const age = setTimeout(() => ws.close(), MAX_AGE); const age = setTimeout(() => ws.close(), MAX_AGE);
// E.g. a message beyond MAX_MESSAGE, the connection closes then. Unhandled, it would end the process. // E.g. a message beyond MAX_MESSAGE, the connection closes then. Unhandled, it would end the process.
ws.on('error', () => {}); ws.on('error', () => {});
// Pings and pongs count as messages. ws would answer every ping on its own, past the limits.
const counted = () => ++peer.messages <= MAX_MESSAGES || reject(peer, 'too many messages');
ws.on('ping', (data) => counted() && fits(peer, data.length) && ws.pong(data));
ws.on('pong', counted);
ws.on('message', (data, binary) => { ws.on('message', (data, binary) => {
if (++peer.messages > MAX_MESSAGES) return reject(peer, 'too many messages'); if (!counted()) return;
let msg; let msg;
try { try {
msg = binary ? undefined : JSON.parse(data); msg = binary ? undefined : JSON.parse(data);
@@ -158,7 +166,7 @@ function open(ws) {
}); });
} }
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE, clientTracking: false }); const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE, clientTracking: false, autoPong: false });
export const server = createServer((_, res) => res.writeHead(404).end()); export const server = createServer((_, res) => res.writeHead(404).end());
server.on('upgrade', (req, socket, head) => { server.on('upgrade', (req, socket, head) => {
// Node leaves an upgraded socket without one, ws adds its own only once it takes over. Unhandled, a // Node leaves an upgraded socket without one, ws adds its own only once it takes over. Unhandled, a
+16
View File
@@ -234,6 +234,22 @@ test('limits the message rate', async () => {
await rejected(client, fields, 'too many messages'); await rejected(client, fields, 'too many messages');
}); });
test('limits pings like messages, also from a peer that stops reading', async () => {
// ws's client, browsers send no pings. ws used to answer every one, its pongs piled up in the tracker.
const client = new ServerSocket(url, { headers: { origin: ORIGIN, 'x-forwarded-for': ip } });
const closed = once(client, 'close');
const reasons = [];
client.on('message', (data) => reasons.push(JSON.parse(data)['failure reason']));
await once(client, 'open');
client.ping('x');
expect(String((await once(client, 'pong'))[0])).toBe('x');
client._socket.pause();
for (let i = 0; i < 1000; i++) client.ping('x'.repeat(125));
client._socket.resume();
await closed;
expect(reasons).toEqual(['too many messages']);
});
test('limits the signaling forwarded per connection', async () => { test('limits the signaling forwarded per connection', async () => {
const { hash, receiver } = await swarm(); const { hash, receiver } = await swarm();
const big = () => ({ offer_id: id(), offer: sdp('offer', 'x'.repeat(15_000)) }); const big = () => ({ offer_id: id(), offer: sdp('offer', 'x'.repeat(15_000)) });