|
3 | 3 | const { test } = require('node:test') |
4 | 4 | const { once } = require('node:events') |
5 | 5 | const { randomFillSync } = require('node:crypto') |
6 | | -const { deflateRawSync } = require('node:zlib') |
| 6 | +const { deflateRawSync, createDeflateRaw, constants } = require('node:zlib') |
7 | 7 | const { setTimeout: sleep } = require('node:timers/promises') |
8 | 8 | const { WebSocketServer } = require('ws') |
9 | 9 | const { WebSocket, Agent } = require('../..') |
@@ -465,3 +465,62 @@ test('cumulative payload size', (t, done) => { |
465 | 465 | done() |
466 | 466 | }) |
467 | 467 | }) |
| 468 | + |
| 469 | +test('Malformed over-limit compressed payload closes connection without unhandled error', async (t) => { |
| 470 | + const limit = 1 * 1024 * 1024 // 1 MB |
| 471 | + const server = new WebSocketServer({ |
| 472 | + port: 0, |
| 473 | + perMessageDeflate: true |
| 474 | + }) |
| 475 | + |
| 476 | + t.after(() => server.close()) |
| 477 | + await once(server, 'listening') |
| 478 | + |
| 479 | + server.on('connection', (ws) => { |
| 480 | + const socket = ws._socket |
| 481 | + |
| 482 | + // Craft a non-final DEFLATE stream that decompresses beyond the size limit, |
| 483 | + // followed by a malformed stored block (BTYPE=00 stored with LEN != ~NLEN) so |
| 484 | + // the inflater would emit Z_DATA_ERROR after the size-limit cleanup runs. |
| 485 | + const def = createDeflateRaw() |
| 486 | + const out = [] |
| 487 | + def.on('data', (data) => out.push(data)) |
| 488 | + def.write(Buffer.alloc(limit + 64 * 1024)) |
| 489 | + def.flush(constants.Z_SYNC_FLUSH, () => { |
| 490 | + const stream = Buffer.concat(out) |
| 491 | + const bad = Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff]) |
| 492 | + const payload = Buffer.concat([stream, bad]) |
| 493 | + |
| 494 | + // Server -> client binary frame, FIN=1, RSV1=1 (compressed), 64-bit length |
| 495 | + const header = Buffer.alloc(10) |
| 496 | + header[0] = 0xC2 |
| 497 | + header[1] = 0x7f |
| 498 | + header.writeUInt32BE(0, 2) |
| 499 | + header.writeUInt32BE(payload.length, 6) |
| 500 | + socket.write(Buffer.concat([header, payload])) |
| 501 | + }) |
| 502 | + }) |
| 503 | + |
| 504 | + const agent = new Agent({ |
| 505 | + webSocket: { |
| 506 | + maxPayloadSize: limit |
| 507 | + } |
| 508 | + }) |
| 509 | + |
| 510 | + t.after(() => agent.close()) |
| 511 | + |
| 512 | + const client = new WebSocket(`ws://127.0.0.1:${server.address().port}`, { dispatcher: agent }) |
| 513 | + |
| 514 | + let messageReceived = false |
| 515 | + client.addEventListener('message', () => { |
| 516 | + messageReceived = true |
| 517 | + }) |
| 518 | + |
| 519 | + const closePromise = once(client, 'close') |
| 520 | + const timeoutPromise = sleep(5000) |
| 521 | + |
| 522 | + await Promise.race([closePromise, timeoutPromise]) |
| 523 | + |
| 524 | + t.assert.strictEqual(messageReceived, false, 'Malformed over-limit payload should be rejected') |
| 525 | + t.assert.strictEqual(client.readyState, WebSocket.CLOSED, 'Connection should be closed without crashing the process') |
| 526 | +}) |
0 commit comments