summaryrefslogtreecommitdiff
path: root/ext/webidl/00_webidl.js
diff options
context:
space:
mode:
authorKenta Moriuchi <moriken@kimamass.com>2024-01-04 13:12:38 +0900
committerGitHub <noreply@github.com>2024-01-04 09:42:38 +0530
commitb2cd254c35b6b1b128beea0eacdb8e814d91e003 (patch)
treed55fa5910e32d8a664aff5b680e07debea93181e /ext/webidl/00_webidl.js
parent48556748577ba46db5f9212d14a0fcaa90d632f6 (diff)
fix: strict type check for cross realms (#21669)
Deno v1.39 introduces `vm.runInNewContext`. This may cause problems when using `Object.prototype.isPrototypeOf` to check built-in types. ```js import vm from "node:vm"; const err = new Error(); const crossErr = vm.runInNewContext(`new Error()`); console.assert( !(crossErr instanceof Error) ); console.assert( Object.getPrototypeOf(err) !== Object.getPrototypeOf(crossErr) ); ``` This PR changes to check using internal slots solves them. --- current: ``` > import vm from "node:vm"; undefined > vm.runInNewContext(`new Error("message")`) Error {} > vm.runInNewContext(`new Date("2018-12-10T02:26:59.002Z")`) Date {} ``` this PR: ``` > import vm from "node:vm"; undefined > vm.runInNewContext(`new Error("message")`) Error: message at <anonymous>:1:1 > vm.runInNewContext(`new Date("2018-12-10T02:26:59.002Z")`) 2018-12-10T02:26:59.002Z ``` --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/webidl/00_webidl.js')
-rw-r--r--ext/webidl/00_webidl.js38
1 files changed, 16 insertions, 22 deletions
diff --git a/ext/webidl/00_webidl.js b/ext/webidl/00_webidl.js
index 3713c7d09..a702e7324 100644
--- a/ext/webidl/00_webidl.js
+++ b/ext/webidl/00_webidl.js
@@ -8,7 +8,6 @@
import { core, primordials } from "ext:core/mod.js";
const {
- ArrayBufferPrototype,
ArrayBufferIsView,
ArrayPrototypeForEach,
ArrayPrototypePush,
@@ -67,7 +66,7 @@ const {
SetPrototypeDelete,
SetPrototypeAdd,
// TODO(lucacasonato): add SharedArrayBuffer to primordials
- // SharedArrayBufferPrototype
+ // SharedArrayBufferPrototype,
String,
StringPrototypeCharCodeAt,
StringPrototypeToWellFormed,
@@ -82,6 +81,12 @@ const {
Uint8Array,
Uint8ClampedArray,
} = primordials;
+const {
+ isArrayBuffer,
+ isDataView,
+ isSharedArrayBuffer,
+ isTypedArray,
+} = core;
function makeException(ErrorType, message, prefix, context) {
return new ErrorType(
@@ -456,27 +461,13 @@ function convertCallbackFunction(V, prefix, context, _opts) {
return V;
}
-function isDataView(V) {
- return ArrayBufferIsView(V) &&
- TypedArrayPrototypeGetSymbolToStringTag(V) === undefined;
-}
-
-function isNonSharedArrayBuffer(V) {
- return ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V);
-}
-
-function isSharedArrayBuffer(V) {
- // deno-lint-ignore prefer-primordials
- return ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V);
-}
-
converters.ArrayBuffer = (
V,
prefix = undefined,
context = undefined,
opts = {},
) => {
- if (!isNonSharedArrayBuffer(V)) {
+ if (!isArrayBuffer(V)) {
if (opts.allowShared && !isSharedArrayBuffer(V)) {
throw makeException(
TypeError,
@@ -511,7 +502,10 @@ converters.DataView = (
);
}
- if (!opts.allowShared && isSharedArrayBuffer(DataViewPrototypeGetBuffer(V))) {
+ if (
+ !opts.allowShared &&
+ isSharedArrayBuffer(DataViewPrototypeGetBuffer(V))
+ ) {
throw makeException(
TypeError,
"is backed by a SharedArrayBuffer, which is not allowed",
@@ -588,7 +582,7 @@ converters.ArrayBufferView = (
);
}
let buffer;
- if (TypedArrayPrototypeGetSymbolToStringTag(V) !== undefined) {
+ if (isTypedArray(V)) {
buffer = TypedArrayPrototypeGetBuffer(V);
} else {
buffer = DataViewPrototypeGetBuffer(V);
@@ -613,7 +607,7 @@ converters.BufferSource = (
) => {
if (ArrayBufferIsView(V)) {
let buffer;
- if (TypedArrayPrototypeGetSymbolToStringTag(V) !== undefined) {
+ if (isTypedArray(V)) {
buffer = TypedArrayPrototypeGetBuffer(V);
} else {
buffer = DataViewPrototypeGetBuffer(V);
@@ -630,7 +624,7 @@ converters.BufferSource = (
return V;
}
- if (!opts.allowShared && !isNonSharedArrayBuffer(V)) {
+ if (!opts.allowShared && !isArrayBuffer(V)) {
throw makeException(
TypeError,
"is not an ArrayBuffer or a view on one",
@@ -641,7 +635,7 @@ converters.BufferSource = (
if (
opts.allowShared &&
!isSharedArrayBuffer(V) &&
- !isNonSharedArrayBuffer(V)
+ !isArrayBuffer(V)
) {
throw makeException(
TypeError,