-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix websocket receiving an invalid utf-8 in close frame (#3206)
* fix websocket receiving an invalid utf-8 in close frame * fixup * fixup * fail if receiving masked frame * fail on invalid status code in close frame
- Loading branch information
Showing
7 changed files
with
243 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ const { kReadyState, kSentClose, kResponse, kReceivedClose } = require('./symbol | |
const { channels } = require('../../core/diagnostics') | ||
const { isValidStatusCode, failWebsocketConnection, websocketMessageReceived, utf8Decode } = require('./util') | ||
const { WebsocketFrameSend } = require('./frame') | ||
const { CloseEvent } = require('./events') | ||
|
||
// This code was influenced by ws released under the MIT license. | ||
// Copyright (c) 2011 Einar Otto Stangvik <[email protected]> | ||
|
@@ -55,6 +56,12 @@ class ByteParser extends Writable { | |
|
||
this.#info.fin = (buffer[0] & 0x80) !== 0 | ||
this.#info.opcode = buffer[0] & 0x0F | ||
this.#info.masked = (buffer[1] & 0x80) === 0x80 | ||
|
||
if (this.#info.masked) { | ||
failWebsocketConnection(this.ws, 'Frame cannot be masked') | ||
return callback() | ||
} | ||
|
||
// If we receive a fragmented message, we use the type of the first | ||
// frame to parse the full message as binary/text, when it's terminated | ||
|
@@ -102,6 +109,13 @@ class ByteParser extends Writable { | |
|
||
this.#info.closeInfo = this.parseCloseBody(body) | ||
|
||
if (this.#info.closeInfo.error) { | ||
const { code, reason } = this.#info.closeInfo | ||
|
||
callback(new CloseEvent('close', { wasClean: false, reason, code })) | ||
return | ||
} | ||
|
||
if (this.ws[kSentClose] !== sentCloseFrameState.SENT) { | ||
// If an endpoint receives a Close frame and did not previously send a | ||
// Close frame, the endpoint MUST send a Close frame in response. (When | ||
|
@@ -310,16 +324,16 @@ class ByteParser extends Writable { | |
} | ||
|
||
if (code !== undefined && !isValidStatusCode(code)) { | ||
return null | ||
return { code: 1002, reason: 'Invalid status code', error: true } | ||
} | ||
|
||
try { | ||
reason = utf8Decode(reason) | ||
} catch { | ||
return null | ||
return { code: 1007, reason: 'Invalid UTF-8', error: true } | ||
} | ||
|
||
return { code, reason } | ||
return { code, reason, error: false } | ||
} | ||
|
||
get closingInfo () { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { once } = require('node:events') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
const { tspl } = require('@matteo.collina/tspl') | ||
const { WebsocketFrameSend } = require('../../lib/web/websocket/frame') | ||
|
||
test('Client fails the connection if receiving a masked frame', async (t) => { | ||
const assert = tspl(t, { plan: 2 }) | ||
|
||
const body = Buffer.allocUnsafe(2) | ||
body.writeUInt16BE(1006, 0) | ||
|
||
const frame = new WebsocketFrameSend(body) | ||
const buffer = frame.createFrame(0x8) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
const socket = ws._socket | ||
|
||
socket.write(buffer, () => ws.close()) | ||
}) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
ws.addEventListener('close', (e) => { | ||
assert.deepStrictEqual(e.code, 1006) | ||
}) | ||
|
||
ws.addEventListener('error', () => { | ||
assert.ok(true) | ||
}) | ||
|
||
t.after(() => { | ||
server.close() | ||
ws.close() | ||
}) | ||
|
||
await once(ws, 'close') | ||
|
||
await assert.completed | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { once } = require('node:events') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
const { tspl } = require('@matteo.collina/tspl') | ||
|
||
test('Client fails the connection if receiving a masked frame', async (t) => { | ||
const assert = tspl(t, { plan: 2 }) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
const socket = ws._socket | ||
|
||
// 1006 status code | ||
socket.write(Buffer.from([0x88, 0x02, 0x03, 0xee]), () => ws.close()) | ||
}) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
ws.addEventListener('close', (e) => { | ||
assert.deepStrictEqual(e.code, 1006) | ||
}) | ||
|
||
ws.addEventListener('error', () => { | ||
assert.ok(true) | ||
}) | ||
|
||
t.after(() => { | ||
server.close() | ||
ws.close() | ||
}) | ||
|
||
await once(ws, 'close') | ||
|
||
await assert.completed | ||
}) |
Oops, something went wrong.