Skip to content

Commit 1858656

Browse files
committed
fix(websocket): error locked writable on unclean close
Propagate connection-close failures through the retained writable stream controller instead of calling abort() on a potentially locked stream. This prevents an unobserved rejected promise from terminating the process. Fixes: GHSA-rx4f-c7p8-82vq CVE: CVE-2026-85014 Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit 78bd61a82a7185011511577c9026cba8864d348d) (cherry picked from commit 7c793b69176b5665ddd0752010ae8909d4179d87)
1 parent 63cf698 commit 1858656

2 files changed

Lines changed: 124 additions & 10 deletions

File tree

‎lib/web/websocket/stream/websocketstream.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class WebSocketStream {
3434
/** @type {ReadableStreamDefaultController} */
3535
#readableStreamController
3636

37-
// Each WebSocketStream object has an associated writable stream , which is a WritableStream .
38-
/** @type {WritableStream} */
39-
#writableStream
37+
// Retain the controller so the writable stream can be errored while locked.
38+
/** @type {WritableStreamDefaultController} */
39+
#writableStreamController
4040

4141
// Each WebSocketStream object has an associated boolean handshake aborted , which is initially false.
4242
#handshakeAborted = false
@@ -300,6 +300,9 @@ class WebSocketStream {
300300
// 12. Let writable be a new WritableStream .
301301
// 13. Set up writable with writeAlgorithm , closeAlgorithm , and abortAlgorithm .
302302
const writable = new WritableStream({
303+
start: (controller) => {
304+
this.#writableStreamController = controller
305+
},
303306
write: (chunk) => this.#write(chunk),
304307
close: () => closeWebSocketConnection(this.#handler, null, null),
305308
abort: (reason) => this.#closeUsingReason(reason)
@@ -308,9 +311,6 @@ class WebSocketStream {
308311
// Set stream ’s readable stream to readable .
309312
this.#readableStream = readable
310313

311-
// Set stream ’s writable stream to writable .
312-
this.#writableStream = writable
313-
314314
// Resolve stream ’s opened promise with WebSocketOpenInfo «[ " extensions " → extensions , " protocol " → protocol , " readable " → readable , " writable " → writable ]».
315315
this.#openedPromise.resolve({
316316
extensions,
@@ -396,9 +396,7 @@ class WebSocketStream {
396396
this.#readableStreamController.close()
397397

398398
// 6.2. Error stream ’s writable stream with an " InvalidStateError " DOMException indicating that a closed WebSocketStream cannot be written to.
399-
if (!this.#writableStream.locked) {
400-
this.#writableStream.abort(new DOMException('A closed WebSocketStream cannot be written to', 'InvalidStateError'))
401-
}
399+
this.#writableStreamController.error(new DOMException('A closed WebSocketStream cannot be written to', 'InvalidStateError'))
402400

403401
// 6.3. Resolve stream ’s closed promise with WebSocketCloseInfo «[ " closeCode " → code , " reason " → reason ]».
404402
this.#closedPromise.resolve({
@@ -415,7 +413,7 @@ class WebSocketStream {
415413
this.#readableStreamController?.error(error)
416414

417415
// 7.3. Error stream ’s writable stream with error .
418-
this.#writableStream?.abort(error)
416+
this.#writableStreamController?.error(error)
419417

420418
// 7.4. Reject stream ’s closed promise with error .
421419
this.#closedPromise.reject(error)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict'
2+
3+
const { test } = require('node:test')
4+
const { spawn } = require('node:child_process')
5+
const { once } = require('node:events')
6+
const { join } = require('node:path')
7+
const { WebSocketServer } = require('ws')
8+
9+
function waitForExit (child, timeout) {
10+
return new Promise((resolve, reject) => {
11+
const timer = setTimeout(() => {
12+
reject(new Error('Child process did not exit after the WebSocket closed'))
13+
}, timeout)
14+
15+
child.once('error', (error) => {
16+
clearTimeout(timer)
17+
reject(error)
18+
})
19+
20+
child.once('exit', (code, signal) => {
21+
clearTimeout(timer)
22+
resolve({ code, signal })
23+
})
24+
})
25+
}
26+
27+
async function testLockedWritableClose (t, clean) {
28+
const server = new WebSocketServer({ port: 0 })
29+
await once(server, 'listening')
30+
31+
const connection = new Promise((resolve) => {
32+
server.once('connection', resolve)
33+
})
34+
35+
const undici = join(__dirname, '../../..')
36+
const url = `ws://127.0.0.1:${server.address().port}`
37+
const child = spawn(process.execPath, ['--unhandled-rejections=strict', '-e', `
38+
const assert = require('node:assert')
39+
const { WebSocketStream } = require(${JSON.stringify(undici)})
40+
41+
;(async () => {
42+
const stream = new WebSocketStream(${JSON.stringify(url)})
43+
const { writable } = await stream.opened
44+
const writer = writable.getWriter()
45+
const writerClosed = writer.closed.catch((error) => error)
46+
process.stdout.write('locked\\n')
47+
48+
const closeResult = await stream.closed.then(
49+
(value) => ({ value }),
50+
(error) => ({ error })
51+
)
52+
const writerError = await writerClosed
53+
54+
if (${clean}) {
55+
assert.deepStrictEqual(closeResult.value, { closeCode: 1000, reason: '' })
56+
assert.strictEqual(writerError.name, 'InvalidStateError')
57+
} else {
58+
assert.strictEqual(writerError, closeResult.error)
59+
assert.strictEqual(closeResult.error.name, 'WebSocketError')
60+
assert.strictEqual(closeResult.error.closeCode, 1006)
61+
}
62+
63+
writer.releaseLock()
64+
await new Promise((resolve) => setTimeout(resolve, 50))
65+
process.stdout.write('survived\\n')
66+
})().catch((error) => {
67+
console.error(error)
68+
process.exitCode = 2
69+
})
70+
`], {
71+
stdio: ['ignore', 'pipe', 'pipe']
72+
})
73+
74+
t.after(() => {
75+
child.kill()
76+
77+
for (const client of server.clients) {
78+
client.terminate()
79+
}
80+
81+
server.close()
82+
})
83+
84+
let stdout = ''
85+
let stderr = ''
86+
let terminated = false
87+
88+
child.stdout.setEncoding('utf8')
89+
child.stdout.on('data', (chunk) => {
90+
stdout += chunk
91+
92+
if (!terminated && stdout.includes('locked\n')) {
93+
terminated = true
94+
connection.then((socket) => clean ? socket.close(1000) : socket.terminate())
95+
}
96+
})
97+
98+
child.stderr.setEncoding('utf8')
99+
child.stderr.on('data', (chunk) => {
100+
stderr += chunk
101+
})
102+
103+
const { code, signal } = await waitForExit(child, 5000)
104+
105+
t.assert.strictEqual(code, 0, stderr)
106+
t.assert.strictEqual(signal, null)
107+
t.assert.strictEqual(stdout, 'locked\nsurvived\n')
108+
}
109+
110+
test('unclean close errors a locked writable without terminating the process', async (t) => {
111+
await testLockedWritableClose(t, false)
112+
})
113+
114+
test('clean close errors a locked writable without terminating the process', async (t) => {
115+
await testLockedWritableClose(t, true)
116+
})

0 commit comments

Comments
 (0)