lib: reject SharedArrayBuffer in web APIs per spec · nodejs/node@3d18162 · GitHub
Skip to content

Commit 3d18162

Browse files
thisalihassanaduh95
authored andcommitted
lib: reject SharedArrayBuffer in web APIs per spec
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com> PR-URL: #62632 Refs: #59688 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent ce47ea3 commit 3d18162

4 files changed

Lines changed: 236 additions & 23 deletions

File tree

lib/internal/crypto/webidl.js

Lines changed: 3 additions & 23 deletions

lib/internal/webidl.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

33
const {
4+
ArrayBufferIsView,
45
ArrayPrototypePush,
56
ArrayPrototypeToSorted,
7+
DataViewPrototypeGetBuffer,
68
MathAbs,
79
MathMax,
810
MathMin,
@@ -19,6 +21,7 @@ const {
1921
Symbol,
2022
SymbolIterator,
2123
TypeError,
24+
TypedArrayPrototypeGetBuffer,
2225
} = primordials;
2326

2427
const {
@@ -28,6 +31,11 @@ const {
2831
},
2932
} = require('internal/errors');
3033
const { kEmptyObject } = require('internal/util');
34+
const {
35+
isArrayBuffer,
36+
isDataView,
37+
isSharedArrayBuffer,
38+
} = require('internal/util/types');
3139

3240
const converters = { __proto__: null };
3341

@@ -382,6 +390,47 @@ function createInterfaceConverter(name, I) {
382390
};
383391
}
384392

393+
function getDataViewOrTypedArrayBuffer(V) {
394+
return isDataView(V) ?
395+
DataViewPrototypeGetBuffer(V) : TypedArrayPrototypeGetBuffer(V);
396+
}
397+
398+
// https://webidl.spec.whatwg.org/#ArrayBufferView
399+
converters.ArrayBufferView = (V, opts = kEmptyObject) => {
400+
if (!ArrayBufferIsView(V)) {
401+
throw makeException(
402+
'is not an ArrayBufferView.',
403+
opts);
404+
}
405+
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
406+
throw makeException(
407+
'is a view on a SharedArrayBuffer, which is not allowed.',
408+
opts);
409+
}
410+
411+
return V;
412+
};
413+
414+
// https://webidl.spec.whatwg.org/#BufferSource
415+
converters.BufferSource = (V, opts = kEmptyObject) => {
416+
if (ArrayBufferIsView(V)) {
417+
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
418+
throw makeException(
419+
'is a view on a SharedArrayBuffer, which is not allowed.',
420+
opts);
421+
}
422+
423+
return V;
424+
}
425+
426+
if (!isArrayBuffer(V)) {
427+
throw makeException(
428+
'is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.',
429+
opts);
430+
}
431+
432+
return V;
433+
};
385434

386435
module.exports = {
387436
type,

lib/internal/webstreams/readablestream.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const {
4848
const {
4949
isArrayBufferView,
5050
isDataView,
51+
isSharedArrayBuffer,
5152
} = require('internal/util/types');
5253

5354
const {
@@ -988,6 +989,15 @@ class ReadableStreamBYOBReader {
988989

989990
const viewByteLength = ArrayBufferViewGetByteLength(view);
990991
const viewBuffer = ArrayBufferViewGetBuffer(view);
992+
993+
if (isSharedArrayBuffer(viewBuffer)) {
994+
throw new ERR_INVALID_ARG_VALUE(
995+
'view',
996+
view,
997+
'must not be backed by a SharedArrayBuffer',
998+
);
999+
}
1000+
9911001
const viewBufferByteLength = ArrayBufferPrototypeGetByteLength(viewBuffer);
9921002

9931003
if (viewByteLength === 0 || viewBufferByteLength === 0) {
@@ -1197,6 +1207,15 @@ class ReadableByteStreamController {
11971207
validateBuffer(chunk);
11981208
const chunkByteLength = ArrayBufferViewGetByteLength(chunk);
11991209
const chunkBuffer = ArrayBufferViewGetBuffer(chunk);
1210+
1211+
if (isSharedArrayBuffer(chunkBuffer)) {
1212+
throw new ERR_INVALID_ARG_VALUE(
1213+
'chunk',
1214+
chunk,
1215+
'must not be backed by a SharedArrayBuffer',
1216+
);
1217+
}
1218+
12001219
const chunkBufferByteLength = ArrayBufferPrototypeGetByteLength(chunkBuffer);
12011220
if (chunkByteLength === 0 || chunkBufferByteLength === 0) {
12021221
throw new ERR_INVALID_STATE.TypeError(
Lines changed: 165 additions & 0 deletions

0 commit comments

Comments
 (0)