Skip to content

Commit 6615e01

Browse files
committed
fix(websocket): reject unrequested subprotocols
Guard the opening-handshake protocol check when the client did not request any subprotocols. Fail the WebSocket connection with protocol error 1002 instead of allowing a TypeError to escape from the response microtask and terminate the process. Fixes: GHSA-rfgv-xxqx-mfg5 CVE: CVE-2026-19534 Signed-off-by: Matteo Collina <[email protected]> (cherry picked from commit 3e2c9c7210ce437636e58df4804fa7d689d5ca7a)
1 parent 2c7d7e1 commit 6615e01

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎lib/web/websocket/connection.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
200200
// is specified, the server needs to include the same field and one of
201201
// the selected subprotocol values in its response for the connection to
202202
// be established.
203-
if (!requestProtocols.includes(secProtocol)) {
203+
if (requestProtocols === null || !requestProtocols.includes(secProtocol)) {
204204
failWebsocketConnection(handler, 1002, 'Protocol was not set in the opening handshake.')
205205
return
206206
}

‎test/websocket/opening-handshake.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,36 @@ test('Server sends invalid Sec-WebSocket-Extensions header', { skip: runtimeFeat
364364
})
365365
})
366366
})
367+
368+
test('Server sends Sec-WebSocket-Protocol header when no protocols were requested', { skip: runtimeFeatures.has('crypto') === false }, (t) => {
369+
const uid = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
370+
371+
return new Promise((resolve, reject) => {
372+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
373+
const key = req.headers['sec-websocket-key']
374+
t.assert.ok(key)
375+
t.assert.strictEqual(req.headers['sec-websocket-protocol'], undefined)
376+
377+
const accept = require('node:crypto').hash('sha1', key + uid, 'base64')
378+
379+
res.setHeader('Upgrade', 'websocket')
380+
res.setHeader('Connection', 'upgrade')
381+
res.setHeader('Sec-WebSocket-Accept', accept)
382+
res.setHeader('Sec-WebSocket-Protocol', 'chat') // <-- never requested
383+
res.statusCode = 101
384+
385+
res.end()
386+
}).listen(0, () => {
387+
// No subprotocols requested, so the request omits Sec-WebSocket-Protocol.
388+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
389+
390+
ws.onopen = reject
391+
392+
ws.addEventListener('error', ({ error }) => {
393+
t.assert.ok(error)
394+
server.close()
395+
resolve()
396+
})
397+
})
398+
})
399+
})

0 commit comments

Comments
 (0)