From fb4bf17ec45d051d80ee8f5bd0f9e2a179372aca Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:23:57 +0900 Subject: [PATCH 1/6] fix: use isArrayBuffer instead of isAnyArrayBuffer --- lib/fetch/file.js | 10 ++-------- lib/fetch/response.js | 2 +- lib/fetch/webidl.js | 2 +- lib/websocket/websocket.js | 2 +- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/fetch/file.js b/lib/fetch/file.js index 3133d255ecd..9d49d919906 100644 --- a/lib/fetch/file.js +++ b/lib/fetch/file.js @@ -211,10 +211,7 @@ webidl.converters.BlobPart = function (V, opts) { return webidl.converters.Blob(V, { strict: false }) } - if ( - ArrayBuffer.isView(V) || - types.isAnyArrayBuffer(V) - ) { + if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) { return webidl.converters.BufferSource(V, opts) } } @@ -282,10 +279,7 @@ function processBlobParts (parts, options) { // 3. Append the result of UTF-8 encoding s to bytes. bytes.push(encoder.encode(s)) - } else if ( - types.isAnyArrayBuffer(element) || - types.isTypedArray(element) - ) { + } else if (types.isArrayBuffer(element) || types.isTypedArray(element)) { // 2. If element is a BufferSource, get a copy of the // bytes held by the buffer source, and append those // bytes to bytes. diff --git a/lib/fetch/response.js b/lib/fetch/response.js index f8894b692eb..5be1f438a12 100644 --- a/lib/fetch/response.js +++ b/lib/fetch/response.js @@ -524,7 +524,7 @@ webidl.converters.XMLHttpRequestBodyInit = function (V) { return webidl.converters.Blob(V, { strict: false }) } - if (types.isArrayBuffer(V) || types.isTypedArray(V) || types.isDataView(V)) { + if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) { return webidl.converters.BufferSource(V) } diff --git a/lib/fetch/webidl.js b/lib/fetch/webidl.js index ca1019221d9..521e50892cf 100644 --- a/lib/fetch/webidl.js +++ b/lib/fetch/webidl.js @@ -613,7 +613,7 @@ webidl.converters.DataView = function (V, opts = {}) { // https://webidl.spec.whatwg.org/#BufferSource webidl.converters.BufferSource = function (V, opts = {}) { - if (types.isAnyArrayBuffer(V)) { + if (types.isArrayBuffer(V)) { return webidl.converters.ArrayBuffer(V, opts) } diff --git a/lib/websocket/websocket.js b/lib/websocket/websocket.js index 1eac67b7828..77a12396dcd 100644 --- a/lib/websocket/websocket.js +++ b/lib/websocket/websocket.js @@ -627,7 +627,7 @@ webidl.converters.WebSocketSendData = function (V) { return webidl.converters.Blob(V, { strict: false }) } - if (ArrayBuffer.isView(V) || types.isAnyArrayBuffer(V)) { + if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) { return webidl.converters.BufferSource(V) } } From 78d7a0afbd2fe24ba74393319aa426472c76f2f8 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 4 Jan 2024 06:59:23 +0900 Subject: [PATCH 2/6] test: add --- test/webidl/converters.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/webidl/converters.js b/test/webidl/converters.js index cabd9185468..14fc8368b5b 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -167,6 +167,10 @@ test('BufferSource', () => { assert.throws(() => { webidl.converters.BufferSource(3) }, TypeError) + + assert.throws(() => { + webidl.converters.BufferSource(new SharedArrayBuffer(0)) + }, TypeError) }) test('ByteString', () => { From b357bcd64ae81ba128bb9f9c96e8517939578ee1 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 4 Jan 2024 10:09:14 +0900 Subject: [PATCH 3/6] fixup & test --- lib/fetch/webidl.js | 6 +++--- test/fetch/file.js | 13 +++++++++++++ test/webidl/converters.js | 4 ---- test/websocket/send.js | 23 +++++++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/fetch/webidl.js b/lib/fetch/webidl.js index 521e50892cf..02ad4500634 100644 --- a/lib/fetch/webidl.js +++ b/lib/fetch/webidl.js @@ -614,15 +614,15 @@ webidl.converters.DataView = function (V, opts = {}) { // https://webidl.spec.whatwg.org/#BufferSource webidl.converters.BufferSource = function (V, opts = {}) { if (types.isArrayBuffer(V)) { - return webidl.converters.ArrayBuffer(V, opts) + return webidl.converters.ArrayBuffer(V, { ...opts, allowShared: false }) } if (types.isTypedArray(V)) { - return webidl.converters.TypedArray(V, V.constructor) + return webidl.converters.TypedArray(V, V.constructor, { ...opts, allowShared: false }) } if (types.isDataView(V)) { - return webidl.converters.DataView(V, opts) + return webidl.converters.DataView(V, opts, { ...opts, allowShared: false }) } throw new TypeError(`Could not convert ${V} to a BufferSource.`) diff --git a/test/fetch/file.js b/test/fetch/file.js index 0e2cac2f10c..877ce190469 100644 --- a/test/fetch/file.js +++ b/test/fetch/file.js @@ -175,3 +175,16 @@ test('endings=native', async () => { assert.strictEqual(text, 'Hello\nWorld', `on ${process.platform} LF stays LF`) } }) + +test('not allow SharedArrayBuffer', () => { + const buffer = new SharedArrayBuffer(0) + assert.throws(() => { + // eslint-disable-next-line no-new + new File([buffer], 'text.txt') + }, TypeError) + + assert.throws(() => { + // eslint-disable-next-line no-new + new File([new Uint8Array(buffer)], 'text.txt') + }, TypeError) +}) diff --git a/test/webidl/converters.js b/test/webidl/converters.js index 14fc8368b5b..cabd9185468 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -167,10 +167,6 @@ test('BufferSource', () => { assert.throws(() => { webidl.converters.BufferSource(3) }, TypeError) - - assert.throws(() => { - webidl.converters.BufferSource(new SharedArrayBuffer(0)) - }, TypeError) }) test('ByteString', () => { diff --git a/test/websocket/send.js b/test/websocket/send.js index 56f48a6ec85..bd49ce95766 100644 --- a/test/websocket/send.js +++ b/test/websocket/send.js @@ -211,4 +211,27 @@ describe('Sending data to a server', () => { }) }) }) + + test('Cannot send with SharedArrayBuffer', () => { + const sab = new SharedArrayBuffer(0) + const server = new WebSocketServer({ port: 0 }) + + const ws = new WebSocket(`ws://localhost:${server.address().port}`) + + ws.addEventListener('open', () => { + ws.send(sab) + }) + + return new Promise((resolve) => { + server.on('connection', (ws) => { + ws.on('message', (data, isBinary) => { + assert.ok(!isBinary) + assert.deepStrictEqual(data, Buffer.from('[object SharedArrayBuffer]')) + ws.close(1000) + server.close() + resolve() + }) + }) + }) + }) }) From 588a626af571286a604bf874aa490e5164606c32 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 4 Jan 2024 10:11:22 +0900 Subject: [PATCH 4/6] fixup --- lib/fetch/webidl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fetch/webidl.js b/lib/fetch/webidl.js index 02ad4500634..e43cdf6aecc 100644 --- a/lib/fetch/webidl.js +++ b/lib/fetch/webidl.js @@ -613,7 +613,7 @@ webidl.converters.DataView = function (V, opts = {}) { // https://webidl.spec.whatwg.org/#BufferSource webidl.converters.BufferSource = function (V, opts = {}) { - if (types.isArrayBuffer(V)) { + if (types.isAnyArrayBuffer(V)) { return webidl.converters.ArrayBuffer(V, { ...opts, allowShared: false }) } From 863b7e8389b28b34d3c89ce295b894bf85b2532d Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 4 Jan 2024 10:17:07 +0900 Subject: [PATCH 5/6] fixup --- lib/fetch/file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fetch/file.js b/lib/fetch/file.js index 9d49d919906..55ca30fa25a 100644 --- a/lib/fetch/file.js +++ b/lib/fetch/file.js @@ -211,7 +211,7 @@ webidl.converters.BlobPart = function (V, opts) { return webidl.converters.Blob(V, { strict: false }) } - if (ArrayBuffer.isView(V) || types.isArrayBuffer(V)) { + if (ArrayBuffer.isView(V) || types.isAnyArrayBuffer(V)) { return webidl.converters.BufferSource(V, opts) } } From 333929e25b30f8113b5e5ab7a3be6b6aeb8460d9 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:02:46 +0900 Subject: [PATCH 6/6] fix: use ArrayBuffer.isView --- lib/fetch/file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fetch/file.js b/lib/fetch/file.js index 55ca30fa25a..397994c0d93 100644 --- a/lib/fetch/file.js +++ b/lib/fetch/file.js @@ -279,7 +279,7 @@ function processBlobParts (parts, options) { // 3. Append the result of UTF-8 encoding s to bytes. bytes.push(encoder.encode(s)) - } else if (types.isArrayBuffer(element) || types.isTypedArray(element)) { + } else if (ArrayBuffer.isView(element) || types.isArrayBuffer(element)) { // 2. If element is a BufferSource, get a copy of the // bytes held by the buffer source, and append those // bytes to bytes.