From 97b5ba2747afb2bcd0b2fb9a56f52c44cd789f96 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sat, 22 Jun 2024 16:06:51 +0900 Subject: [PATCH 1/4] websocket: don't use pooled buffer in mask pool --- lib/web/websocket/frame.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/websocket/frame.js b/lib/web/websocket/frame.js index b062ffde8ec..fe54646cc7d 100644 --- a/lib/web/websocket/frame.js +++ b/lib/web/websocket/frame.js @@ -27,7 +27,7 @@ try { function generateMask () { if (bufIdx === BUFFER_SIZE) { bufIdx = 0 - crypto.randomFillSync((buffer ??= Buffer.allocUnsafe(BUFFER_SIZE)), 0, BUFFER_SIZE) + crypto.randomFillSync((buffer ??= Buffer.allocUnsafeSlow(BUFFER_SIZE)), 0, BUFFER_SIZE) } return [buffer[bufIdx++], buffer[bufIdx++], buffer[bufIdx++], buffer[bufIdx++]] } From abc632335158c8005c99f686b03baf3672e1a9a5 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sun, 23 Jun 2024 06:10:20 +0900 Subject: [PATCH 2/4] add test --- test/websocket/frame.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/websocket/frame.js b/test/websocket/frame.js index 0f6a7d4a751..de436a72b2d 100644 --- a/test/websocket/frame.js +++ b/test/websocket/frame.js @@ -5,6 +5,19 @@ const assert = require('node:assert') const { WebsocketFrameSend } = require('../../lib/web/websocket/frame') const { opcodes } = require('../../lib/web/websocket/constants') +test('Don not use pooled buffer in mask pool', () => { + const allocUnsafe = Buffer.allocUnsafe + try { + Buffer.allocUnsafe = () => { + throw new Error('do not use!!!') + } + // create mask pool + new WebsocketFrameSend(Buffer.alloc(0)).createFrame(opcodes.BINARY) + } finally { + Buffer.allocUnsafe = allocUnsafe + } +}) + test('Writing 16-bit frame length value at correct offset when buffer has a non-zero byteOffset', () => { /* When writing 16-bit frame lengths, a `DataView` was being used without setting a `byteOffset` into the buffer: From cae53bc6f37469f5bb8bd457895d92c1913a6953 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sun, 23 Jun 2024 06:15:12 +0900 Subject: [PATCH 3/4] fixup --- test/websocket/frame.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/websocket/frame.js b/test/websocket/frame.js index de436a72b2d..96958100494 100644 --- a/test/websocket/frame.js +++ b/test/websocket/frame.js @@ -5,10 +5,13 @@ const assert = require('node:assert') const { WebsocketFrameSend } = require('../../lib/web/websocket/frame') const { opcodes } = require('../../lib/web/websocket/constants') +// Always be above all tests. test('Don not use pooled buffer in mask pool', () => { const allocUnsafe = Buffer.allocUnsafe + let counter = 0 try { - Buffer.allocUnsafe = () => { + Buffer.allocUnsafe = (n) => { + if (counter++ === 0) return allocUnsafe(n) throw new Error('do not use!!!') } // create mask pool From 071528e3e0d83502cad213cbb2dde987d1fd9a86 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sun, 23 Jun 2024 06:19:12 +0900 Subject: [PATCH 4/4] fixup --- test/websocket/frame.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/websocket/frame.js b/test/websocket/frame.js index 96958100494..38022f88bc1 100644 --- a/test/websocket/frame.js +++ b/test/websocket/frame.js @@ -11,11 +11,12 @@ test('Don not use pooled buffer in mask pool', () => { let counter = 0 try { Buffer.allocUnsafe = (n) => { - if (counter++ === 0) return allocUnsafe(n) - throw new Error('do not use!!!') + counter++ + return allocUnsafe(n) } // create mask pool new WebsocketFrameSend(Buffer.alloc(0)).createFrame(opcodes.BINARY) + assert.strictEqual(counter, 1) } finally { Buffer.allocUnsafe = allocUnsafe }