summaryrefslogtreecommitdiff
path: root/cli
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 /cli
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 'cli')
-rw-r--r--cli/js/40_testing.js4
-rw-r--r--cli/tests/integration/node_unit_tests.rs1
-rw-r--r--cli/tests/unit/console_test.ts35
-rw-r--r--cli/tests/unit_node/console_test.ts28
-rw-r--r--cli/tests/unit_node/util_test.ts45
5 files changed, 98 insertions, 15 deletions
diff --git a/cli/js/40_testing.js b/cli/js/40_testing.js
index a34eb5aff..87567a17e 100644
--- a/cli/js/40_testing.js
+++ b/cli/js/40_testing.js
@@ -16,14 +16,12 @@ const {
ArrayPrototypeShift,
DateNow,
Error,
- FunctionPrototype,
Map,
MapPrototypeGet,
MapPrototypeHas,
MapPrototypeSet,
MathCeil,
ObjectKeys,
- ObjectPrototypeIsPrototypeOf,
Promise,
SafeArrayIterator,
Set,
@@ -1294,7 +1292,7 @@ function createTestContext(desc) {
let stepDesc;
if (typeof nameOrFnOrOptions === "string") {
- if (!(ObjectPrototypeIsPrototypeOf(FunctionPrototype, maybeFn))) {
+ if (typeof maybeFn !== "function") {
throw new TypeError("Expected function for second argument.");
}
stepDesc = {
diff --git a/cli/tests/integration/node_unit_tests.rs b/cli/tests/integration/node_unit_tests.rs
index 1508ad9ac..273066b09 100644
--- a/cli/tests/integration/node_unit_tests.rs
+++ b/cli/tests/integration/node_unit_tests.rs
@@ -52,6 +52,7 @@ util::unit_test_factory!(
assertion_error_test,
buffer_test,
child_process_test,
+ console_test,
crypto_cipher_test = crypto / crypto_cipher_test,
crypto_cipher_gcm_test = crypto / crypto_cipher_gcm_test,
crypto_hash_test = crypto / crypto_hash_test,
diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts
index 7d4675976..031512f79 100644
--- a/cli/tests/unit/console_test.ts
+++ b/cli/tests/unit/console_test.ts
@@ -1845,18 +1845,36 @@ Deno.test(function consoleLogShouldNotThrowErrorWhenInvalidDateIsPassed() {
// console.log(new Proxy(new Set(), {}))
Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedSet() {
mockConsole((console, out) => {
- const proxiedSet = new Proxy(new Set(), {});
+ const proxiedSet = new Proxy(new Set([1, 2]), {});
console.log(proxiedSet);
- assertEquals(stripColor(out.toString()), "Set {}\n");
+ assertEquals(stripColor(out.toString()), "Set(2) { 1, 2 }\n");
});
});
// console.log(new Proxy(new Map(), {}))
Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedMap() {
mockConsole((console, out) => {
- const proxiedMap = new Proxy(new Map(), {});
+ const proxiedMap = new Proxy(new Map([[1, 1], [2, 2]]), {});
console.log(proxiedMap);
- assertEquals(stripColor(out.toString()), "Map {}\n");
+ assertEquals(stripColor(out.toString()), "Map(2) { 1 => 1, 2 => 2 }\n");
+ });
+});
+
+// console.log(new Proxy(new Uint8Array(), {}))
+Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedTypedArray() {
+ mockConsole((console, out) => {
+ const proxiedUint8Array = new Proxy(new Uint8Array([1, 2]), {});
+ console.log(proxiedUint8Array);
+ assertEquals(stripColor(out.toString()), "Uint8Array(2) [ 1, 2 ]\n");
+ });
+});
+
+// console.log(new Proxy(new RegExp(), {}))
+Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedRegExp() {
+ mockConsole((console, out) => {
+ const proxiedRegExp = new Proxy(/aaaa/, {});
+ console.log(proxiedRegExp);
+ assertEquals(stripColor(out.toString()), "/aaaa/\n");
});
});
@@ -1869,6 +1887,15 @@ Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedDate() {
});
});
+// console.log(new Proxy(new Error(), {}))
+Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedError() {
+ mockConsole((console, out) => {
+ const proxiedError = new Proxy(new Error("message"), {});
+ console.log(proxiedError);
+ assertStringIncludes(stripColor(out.toString()), "Error: message\n");
+ });
+});
+
// console.dir test
Deno.test(function consoleDir() {
mockConsole((console, out) => {
diff --git a/cli/tests/unit_node/console_test.ts b/cli/tests/unit_node/console_test.ts
new file mode 100644
index 000000000..583e98e22
--- /dev/null
+++ b/cli/tests/unit_node/console_test.ts
@@ -0,0 +1,28 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+import vm from "node:vm";
+import { stripColor } from "../../../test_util/std/fmt/colors.ts";
+import { assertStringIncludes } from "../../../test_util/std/assert/mod.ts";
+
+Deno.test(function inspectCrossRealmObjects() {
+ assertStringIncludes(
+ stripColor(
+ Deno.inspect(vm.runInNewContext(`new Error("This is an error")`)),
+ ),
+ "Error: This is an error",
+ );
+ assertStringIncludes(
+ stripColor(
+ Deno.inspect(
+ vm.runInNewContext(`new AggregateError([], "This is an error")`),
+ ),
+ ),
+ "AggregateError: This is an error",
+ );
+ assertStringIncludes(
+ stripColor(
+ Deno.inspect(vm.runInNewContext(`new Date("2018-12-10T02:26:59.002Z")`)),
+ ),
+ "2018-12-10T02:26:59.002Z",
+ );
+});
diff --git a/cli/tests/unit_node/util_test.ts b/cli/tests/unit_node/util_test.ts
index 3cb5e4366..2e2bb0021 100644
--- a/cli/tests/unit_node/util_test.ts
+++ b/cli/tests/unit_node/util_test.ts
@@ -8,6 +8,7 @@ import {
} from "../../../test_util/std/assert/mod.ts";
import { stripColor } from "../../../test_util/std/fmt/colors.ts";
import * as util from "node:util";
+import { Buffer } from "node:buffer";
Deno.test({
name: "[util] format",
@@ -130,9 +131,11 @@ Deno.test({
fn() {
const java = new Error();
const nodejs = Reflect.construct(Error, [], Object);
+ const bun = new DOMException();
const deno = "Future";
assert(util.isError(java));
assert(util.isError(nodejs));
+ assert(util.isError(bun));
assert(!util.isError(deno));
},
});
@@ -191,6 +194,40 @@ Deno.test({
});
Deno.test({
+ name: "[util] isDate",
+ fn() {
+ // Test verifies the method is exposed. See _util/_util_types_test for details
+ assert(util.isDate(new Date()));
+ },
+});
+
+Deno.test({
+ name: "[util] isBuffer",
+ fn() {
+ assert(util.isBuffer(new Buffer(4)));
+ assert(!util.isBuffer(new Uint8Array(4)));
+ },
+});
+
+Deno.test({
+ name: "[util] types.isTypedArray",
+ fn() {
+ assert(util.types.isTypedArray(new Buffer(4)));
+ assert(util.types.isTypedArray(new Uint8Array(4)));
+ assert(!util.types.isTypedArray(new DataView(new ArrayBuffer(4))));
+ },
+});
+
+Deno.test({
+ name: "[util] types.isNativeError",
+ fn() {
+ assert(util.types.isNativeError(new Error()));
+ assert(util.types.isNativeError(new TypeError()));
+ assert(!util.types.isNativeError(new DOMException()));
+ },
+});
+
+Deno.test({
name: "[util] TextDecoder",
fn() {
assert(util.TextDecoder === TextDecoder);
@@ -217,14 +254,6 @@ Deno.test({
});
Deno.test({
- name: "[util] isDate",
- fn() {
- // Test verifies the method is exposed. See _util/_util_types_test for details
- assert(util.types.isDate(new Date()));
- },
-});
-
-Deno.test({
name: "[util] getSystemErrorName()",
fn() {
type FnTestInvalidArg = (code?: unknown) => void;