Skip to content

Commit 2af0faf

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 07c60d9 commit 2af0faf

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎lib/web/websocket/connection.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function establishWebSocketConnection (url, protocols, client, ws, onEstablish,
192192
// is specified, the server needs to include the same field and one of
193193
// the selected subprotocol values in its response for the connection to
194194
// be established.
195-
if (!requestProtocols.includes(secProtocol)) {
195+
if (requestProtocols === null || !requestProtocols.includes(secProtocol)) {
196196
failWebsocketConnection(ws, 'Protocol was not set in the opening handshake.')
197197
return
198198
}

‎test/websocket/opening-handshake.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,37 @@ test('Server sends invalid Sec-WebSocket-Extensions header', () => {
215215
})
216216
})
217217
})
218+
219+
test('Server sends Sec-WebSocket-Protocol header when no protocols were requested', () => {
220+
const { createHash } = require('node:crypto')
221+
const uid = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
222+
223+
return new Promise((resolve, reject) => {
224+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
225+
const key = req.headers['sec-websocket-key']
226+
assert.ok(key)
227+
assert.strictEqual(req.headers['sec-websocket-protocol'], undefined)
228+
229+
const accept = createHash('sha1').update(key + uid).digest('base64')
230+
231+
res.setHeader('Upgrade', 'websocket')
232+
res.setHeader('Connection', 'upgrade')
233+
res.setHeader('Sec-WebSocket-Accept', accept)
234+
res.setHeader('Sec-WebSocket-Protocol', 'chat') // <-- never requested
235+
res.statusCode = 101
236+
237+
res.end()
238+
}).listen(0, () => {
239+
// No subprotocols requested, so the request omits Sec-WebSocket-Protocol.
240+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
241+
242+
ws.onopen = reject
243+
244+
ws.addEventListener('error', ({ error }) => {
245+
assert.ok(error)
246+
server.close()
247+
resolve()
248+
})
249+
})
250+
})
251+
})

0 commit comments

Comments
 (0)