From fd1675ae94e588e362190ebc53c02a666fcd9f86 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 5 May 2024 10:08:40 -0400 Subject: [PATCH 1/2] fix parsing when receiving empty body websocket --- lib/web/websocket/receiver.js | 2 +- test/websocket/issue-3202.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/websocket/issue-3202.js diff --git a/lib/web/websocket/receiver.js b/lib/web/websocket/receiver.js index 4b35ceb54a4..e0ebe6fc801 100644 --- a/lib/web/websocket/receiver.js +++ b/lib/web/websocket/receiver.js @@ -239,7 +239,7 @@ class ByteParser extends Writable { } } - if (this.#byteOffset === 0) { + if (this.#byteOffset === 0 && this.#info.payloadLength !== 0) { callback() break } diff --git a/test/websocket/issue-3202.js b/test/websocket/issue-3202.js new file mode 100644 index 00000000000..814f2eb542f --- /dev/null +++ b/test/websocket/issue-3202.js @@ -0,0 +1,35 @@ +'use strict' + +const { test } = require('node:test') +const { WebSocketServer } = require('ws') +const { WebSocket } = require('../..') +const { tspl } = require('@matteo.collina/tspl') + +test('Close without receiving code does not send an invalid payload', async (t) => { + const { ok, completed } = tspl(t, { plan: 1 }) + + const server = new WebSocketServer({ port: 0 }) + + server.on('connection', (socket) => { + socket.on('message', () => { + socket.send('') + }) + }) + + t.after(() => { + server.close() + ws.close() + }) + + const ws = new WebSocket(`ws://127.0.0.1:${server.address().port}`) + + ws.addEventListener('open', () => { + ws.send('Hi') + }) + + ws.addEventListener('message', () => { + ok(true) + }) + + await completed +}) From 71cc9860ab0ca77bce7ff838039392816060d973 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 5 May 2024 10:10:08 -0400 Subject: [PATCH 2/2] fixup --- test/websocket/issue-3202.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/websocket/issue-3202.js b/test/websocket/issue-3202.js index 814f2eb542f..3630a79f4d1 100644 --- a/test/websocket/issue-3202.js +++ b/test/websocket/issue-3202.js @@ -5,7 +5,7 @@ const { WebSocketServer } = require('ws') const { WebSocket } = require('../..') const { tspl } = require('@matteo.collina/tspl') -test('Close without receiving code does not send an invalid payload', async (t) => { +test('Receiving frame with payload length 0 works', async (t) => { const { ok, completed } = tspl(t, { plan: 1 }) const server = new WebSocketServer({ port: 0 })