-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
lib: reject SharedArrayBuffer in web APIs per spec #62632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 2 commits into
nodejs:main
from
thisalihassan:fix/reject-sharedarraybuffer-web-apis
Apr 16, 2026
+236
−23
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
165 changes: 165 additions & 0 deletions
165
test/parallel/test-webapi-sharedarraybuffer-rejection.js
This file contains hidden or 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,165 @@ | ||
| 'use strict'; | ||
| // Flags: --expose-internals | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const test = require('node:test'); | ||
| const { ReadableStream } = require('stream/web'); | ||
|
|
||
| const sab = new SharedArrayBuffer(8); | ||
| const sabView = new Uint8Array(sab); | ||
| const sabDataView = new DataView(sab); | ||
|
|
||
| // -- ReadableStreamBYOBReader.read() -- | ||
|
|
||
| test('ReadableStreamBYOBReader.read() rejects SAB-backed Uint8Array', async () => { | ||
| const rs = new ReadableStream({ | ||
| type: 'bytes', | ||
| pull(controller) { | ||
| controller.enqueue(new Uint8Array([1, 2, 3])); | ||
| }, | ||
| }); | ||
| const reader = rs.getReader({ mode: 'byob' }); | ||
| await assert.rejects( | ||
| reader.read(new Uint8Array(sab)), | ||
| { code: 'ERR_INVALID_ARG_VALUE' }, | ||
| ); | ||
| reader.releaseLock(); | ||
| }); | ||
|
|
||
| test('ReadableStreamBYOBReader.read() rejects SAB-backed DataView', async () => { | ||
| const rs = new ReadableStream({ | ||
| type: 'bytes', | ||
| pull(controller) { | ||
| controller.enqueue(new Uint8Array([1, 2, 3])); | ||
| }, | ||
| }); | ||
| const reader = rs.getReader({ mode: 'byob' }); | ||
| await assert.rejects( | ||
| reader.read(sabDataView), | ||
| { code: 'ERR_INVALID_ARG_VALUE' }, | ||
| ); | ||
| reader.releaseLock(); | ||
| }); | ||
|
|
||
| test('ReadableStreamBYOBReader.read() accepts regular view', async () => { | ||
| const rs = new ReadableStream({ | ||
| type: 'bytes', | ||
| pull(controller) { | ||
| controller.enqueue(new Uint8Array([1, 2, 3])); | ||
| }, | ||
| }); | ||
| const reader = rs.getReader({ mode: 'byob' }); | ||
| const { value, done } = await reader.read(new Uint8Array(3)); | ||
| assert.strictEqual(done, false); | ||
| assert.deepStrictEqual(value, new Uint8Array([1, 2, 3])); | ||
| reader.releaseLock(); | ||
| }); | ||
|
|
||
| // -- ReadableByteStreamController.enqueue() -- | ||
|
|
||
| test('ReadableByteStreamController.enqueue() rejects SAB-backed Uint8Array', async () => { | ||
| const sabForEnqueue = new SharedArrayBuffer(4); | ||
| const sabViewForEnqueue = new Uint8Array(sabForEnqueue); | ||
| sabViewForEnqueue[0] = 42; | ||
|
|
||
| const rs = new ReadableStream({ | ||
| type: 'bytes', | ||
| pull: common.mustCall((controller) => { | ||
| assert.throws( | ||
| () => controller.enqueue(sabViewForEnqueue), | ||
| { code: 'ERR_INVALID_ARG_VALUE' }, | ||
| ); | ||
| controller.enqueue(new Uint8Array([1])); | ||
| }), | ||
| }); | ||
| const reader = rs.getReader(); | ||
| const { value } = await reader.read(); | ||
| assert.deepStrictEqual(value, new Uint8Array([1])); | ||
| reader.releaseLock(); | ||
| }); | ||
|
|
||
| test('ReadableByteStreamController.enqueue() rejects SAB-backed DataView', async () => { | ||
| const sabForDv = new SharedArrayBuffer(4); | ||
| const dvForEnqueue = new DataView(sabForDv); | ||
|
|
||
| const rs = new ReadableStream({ | ||
| type: 'bytes', | ||
| pull: common.mustCall((controller) => { | ||
| assert.throws( | ||
| () => controller.enqueue(dvForEnqueue), | ||
| { code: 'ERR_INVALID_ARG_VALUE' }, | ||
| ); | ||
| controller.enqueue(new Uint8Array([2])); | ||
| }), | ||
| }); | ||
| const reader = rs.getReader(); | ||
| const { value } = await reader.read(); | ||
| assert.deepStrictEqual(value, new Uint8Array([2])); | ||
| reader.releaseLock(); | ||
| }); | ||
|
|
||
| // -- SharedWebIDL converters -- | ||
|
|
||
| const { converters } = require('internal/webidl'); | ||
|
|
||
| test('webidl converters.BufferSource rejects SharedArrayBuffer', () => { | ||
| assert.throws( | ||
| () => converters.BufferSource(sab), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }, | ||
| ); | ||
| }); | ||
|
|
||
| test('webidl converters.BufferSource rejects SAB-backed Uint8Array', () => { | ||
| assert.throws( | ||
| () => converters.BufferSource(sabView), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }, | ||
| ); | ||
| }); | ||
|
|
||
| test('webidl converters.BufferSource rejects SAB-backed DataView', () => { | ||
| assert.throws( | ||
| () => converters.BufferSource(sabDataView), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }, | ||
| ); | ||
| }); | ||
|
|
||
| test('webidl converters.BufferSource accepts ArrayBuffer', () => { | ||
| const ab = new ArrayBuffer(4); | ||
| assert.strictEqual(converters.BufferSource(ab), ab); | ||
| }); | ||
|
|
||
| test('webidl converters.BufferSource accepts regular TypedArray', () => { | ||
| const ta = new Uint8Array(4); | ||
| assert.strictEqual(converters.BufferSource(ta), ta); | ||
| }); | ||
|
|
||
| test('webidl converters.ArrayBufferView rejects SAB-backed Uint8Array', () => { | ||
| assert.throws( | ||
| () => converters.ArrayBufferView(sabView), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }, | ||
| ); | ||
| }); | ||
|
|
||
| test('webidl converters.ArrayBufferView rejects SAB-backed DataView', () => { | ||
| assert.throws( | ||
| () => converters.ArrayBufferView(sabDataView), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }, | ||
| ); | ||
| }); | ||
|
|
||
| test('webidl converters.ArrayBufferView rejects non-view', () => { | ||
| assert.throws( | ||
| () => converters.ArrayBufferView('not a view'), | ||
| { code: 'ERR_INVALID_ARG_TYPE' }, | ||
| ); | ||
| }); | ||
|
|
||
| test('webidl converters.ArrayBufferView accepts regular Uint8Array', () => { | ||
| const ta = new Uint8Array(4); | ||
| assert.strictEqual(converters.ArrayBufferView(ta), ta); | ||
| }); | ||
|
|
||
| test('webidl converters.ArrayBufferView accepts regular DataView', () => { | ||
| const dv = new DataView(new ArrayBuffer(4)); | ||
| assert.strictEqual(converters.ArrayBufferView(dv), dv); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.