Skip to content

Commit 27b2589

Browse files
authored
websocket: don't use pooled buffer in mask pool (#3357)
* websocket: don't use pooled buffer in mask pool * add test * fixup * fixup
1 parent 02c3a67 commit 27b2589

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/web/websocket/frame.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ try {
2727
function generateMask () {
2828
if (bufIdx === BUFFER_SIZE) {
2929
bufIdx = 0
30-
crypto.randomFillSync((buffer ??= Buffer.allocUnsafe(BUFFER_SIZE)), 0, BUFFER_SIZE)
30+
crypto.randomFillSync((buffer ??= Buffer.allocUnsafeSlow(BUFFER_SIZE)), 0, BUFFER_SIZE)
3131
}
3232
return [buffer[bufIdx++], buffer[bufIdx++], buffer[bufIdx++], buffer[bufIdx++]]
3333
}

test/websocket/frame.js

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ const assert = require('node:assert')
55
const { WebsocketFrameSend } = require('../../lib/web/websocket/frame')
66
const { opcodes } = require('../../lib/web/websocket/constants')
77

8+
// Always be above all tests.
9+
test('Don not use pooled buffer in mask pool', () => {
10+
const allocUnsafe = Buffer.allocUnsafe
11+
let counter = 0
12+
try {
13+
Buffer.allocUnsafe = (n) => {
14+
counter++
15+
return allocUnsafe(n)
16+
}
17+
// create mask pool
18+
new WebsocketFrameSend(Buffer.alloc(0)).createFrame(opcodes.BINARY)
19+
assert.strictEqual(counter, 1)
20+
} finally {
21+
Buffer.allocUnsafe = allocUnsafe
22+
}
23+
})
24+
825
test('Writing 16-bit frame length value at correct offset when buffer has a non-zero byteOffset', () => {
926
/*
1027
When writing 16-bit frame lengths, a `DataView` was being used without setting a `byteOffset` into the buffer:

0 commit comments

Comments
 (0)