From b2cd254c35b6b1b128beea0eacdb8e814d91e003 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Thu, 4 Jan 2024 13:12:38 +0900 Subject: fix: strict type check for cross realms (#21669) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 :1:1 > vm.runInNewContext(`new Date("2018-12-10T02:26:59.002Z")`) 2018-12-10T02:26:59.002Z ``` --------- Co-authored-by: Bartek IwaƄczuk --- ext/websocket/01_websocket.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'ext/websocket/01_websocket.js') diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 57689d1ae..fdcb0be99 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -7,7 +7,7 @@ import { URL } from "ext:deno_url/00_url.js"; import * as webidl from "ext:deno_webidl/00_webidl.js"; import { createFilteredInspectProxy } from "ext:deno_console/01_console.js"; import { HTTP_TOKEN_CODE_POINT_RE } from "ext:deno_web/00_infra.js"; -import DOMException from "ext:deno_web/01_dom_exception.js"; +import { DOMException } from "ext:deno_web/01_dom_exception.js"; import { CloseEvent, defineEventHandler, @@ -21,7 +21,6 @@ import { import { Blob, BlobPrototype } from "ext:deno_web/09_file.js"; import { getLocationHref } from "ext:deno_web/12_location.js"; const { - ArrayBufferPrototype, ArrayBufferIsView, ArrayPrototypeJoin, ArrayPrototypeMap, @@ -29,21 +28,24 @@ const { ErrorPrototypeToString, ObjectDefineProperties, ObjectPrototypeIsPrototypeOf, + PromisePrototypeCatch, PromisePrototypeThen, RegExpPrototypeExec, SafeSet, SetPrototypeGetSize, - // TODO(lucacasonato): add SharedArrayBuffer to primordials - // SharedArrayBufferPrototype String, StringPrototypeEndsWith, StringPrototypeToLowerCase, Symbol, - SymbolIterator, - PromisePrototypeCatch, SymbolFor, + SymbolIterator, TypedArrayPrototypeGetByteLength, + Uint8Array, } = primordials; +const { + isAnyArrayBuffer, + isArrayBuffer, +} = core; import { op_ws_check_permission_and_cancel_handle, op_ws_close, @@ -80,11 +82,7 @@ webidl.converters["WebSocketSend"] = (V, prefix, context, opts) => { return webidl.converters["Blob"](V, prefix, context, opts); } if (typeof V === "object") { - if ( - ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) || - // deno-lint-ignore prefer-primordials - ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V) - ) { + if (isAnyArrayBuffer(V)) { return webidl.converters["ArrayBuffer"](V, prefix, context, opts); } if (ArrayBufferIsView(V)) { @@ -329,8 +327,7 @@ class WebSocket extends EventTarget { if (ArrayBufferIsView(data)) { op_ws_send_binary(this[_rid], data); - } else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) { - // deno-lint-ignore prefer-primordials + } else if (isArrayBuffer(data)) { op_ws_send_binary(this[_rid], new Uint8Array(data)); } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) { PromisePrototypeThen( -- cgit v1.2.3