Skip to content

Commit 63cf698

Browse files
committed
fix(websocket): destroy inflater after decompression limit
Stop the permessage-deflate inflater after removing its listeners so a late zlib error cannot become an uncaught exception. Add regression coverage for malformed compressed payloads that exceed the configured limit. Fixes: GHSA-3wwx-pv8p-q78v CVE: CVE-2026-85024 Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit fa45246280698732d8822571da3ae650a8cdfa10) (cherry picked from commit 1ccbedd3efc971e3ce2ea9f33dcc6644d4268e09)
1 parent a03b46a commit 63cf698

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎lib/web/websocket/permessage-deflate.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ class PerMessageDeflate {
6363

6464
if (this.#maxPayloadSize > 0 && this.#inflate[kLength] > this.#maxPayloadSize) {
6565
callback(new MessageSizeExceededError())
66+
// The inflater may still hold buffered input that can emit a late
67+
// zlib error. Remove the data listener, then deterministically stop
68+
// the stream so a subsequent 'error' cannot fire without a listener
69+
// (which would terminate the process as an unhandled error event).
6670
this.#inflate.removeAllListeners()
71+
this.#inflate.destroy()
6772
this.#inflate = null
6873
return
6974
}

‎test/websocket/permessage-deflate-limit.js‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { test } = require('node:test')
44
const { once } = require('node:events')
55
const { randomFillSync } = require('node:crypto')
6-
const { deflateRawSync } = require('node:zlib')
6+
const { deflateRawSync, createDeflateRaw, constants } = require('node:zlib')
77
const { setTimeout: sleep } = require('node:timers/promises')
88
const { WebSocketServer } = require('ws')
99
const { WebSocket, Agent } = require('../..')
@@ -465,3 +465,62 @@ test('cumulative payload size', (t, done) => {
465465
done()
466466
})
467467
})
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

Comments
 (0)