lib: reject SharedArrayBuffer in web APIs per spec by thisalihassan · Pull Request #62632 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions lib/internal/crypto/webidl.js
49 changes: 49 additions & 0 deletions lib/internal/webidl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const {
ArrayBufferIsView,
ArrayPrototypePush,
ArrayPrototypeToSorted,
DataViewPrototypeGetBuffer,
MathAbs,
MathMax,
MathMin,
Expand All @@ -19,6 +21,7 @@ const {
Symbol,
SymbolIterator,
TypeError,
TypedArrayPrototypeGetBuffer,
} = primordials;

const {
Expand All @@ -28,6 +31,11 @@ const {
},
} = require('internal/errors');
const { kEmptyObject } = require('internal/util');
const {
isArrayBuffer,
isDataView,
isSharedArrayBuffer,
} = require('internal/util/types');

const converters = { __proto__: null };

Expand Down Expand Up @@ -382,6 +390,47 @@ function createInterfaceConverter(name, I) {
};
}

function getDataViewOrTypedArrayBuffer(V) {
return isDataView(V) ?
DataViewPrototypeGetBuffer(V) : TypedArrayPrototypeGetBuffer(V);
}

// https://webidl.spec.whatwg.org/#ArrayBufferView
converters.ArrayBufferView = (V, opts = kEmptyObject) => {
if (!ArrayBufferIsView(V)) {
throw makeException(
'is not an ArrayBufferView.',
opts);
}
Comment thread
thisalihassan marked this conversation as resolved.
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
throw makeException(
'is a view on a SharedArrayBuffer, which is not allowed.',
opts);
}

return V;
};

// https://webidl.spec.whatwg.org/#BufferSource
converters.BufferSource = (V, opts = kEmptyObject) => {
if (ArrayBufferIsView(V)) {
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
throw makeException(
'is a view on a SharedArrayBuffer, which is not allowed.',
opts);
}

return V;
}

if (!isArrayBuffer(V)) {
throw makeException(
'is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.',
opts);
}

return V;
};

module.exports = {
type,
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {
const {
isArrayBufferView,
isDataView,
isSharedArrayBuffer,
} = require('internal/util/types');

const {
Expand Down Expand Up @@ -988,6 +989,15 @@ class ReadableStreamBYOBReader {

const viewByteLength = ArrayBufferViewGetByteLength(view);
const viewBuffer = ArrayBufferViewGetBuffer(view);

if (isSharedArrayBuffer(viewBuffer)) {
throw new ERR_INVALID_ARG_VALUE(
'view',
view,
'must not be backed by a SharedArrayBuffer',
);
}

const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer);

if (viewByteLength === 0 || viewBufferByteLength === 0) {
Expand Down Expand Up @@ -1197,6 +1207,15 @@ class ReadableByteStreamController {
validateBuffer(chunk);
const chunkByteLength = ArrayBufferViewGetByteLength(chunk);
const chunkBuffer = ArrayBufferViewGetBuffer(chunk);

if (isSharedArrayBuffer(chunkBuffer)) {
throw new ERR_INVALID_ARG_VALUE(
'chunk',
chunk,
'must not be backed by a SharedArrayBuffer',
);
}

const chunkBufferByteLength = ArrayBufferPrototypeGetByteLength(chunkBuffer);
if (chunkByteLength === 0 || chunkBufferByteLength === 0) {
throw new ERR_INVALID_STATE.TypeError(
Expand Down
165 changes: 165 additions & 0 deletions test/parallel/test-webapi-sharedarraybuffer-rejection.js
Loading