From 76b8ecbb6d8c07d29c34fb0b301cc3bf3351e3aa Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 19 Jul 2024 12:37:08 +0900 Subject: [PATCH] fix(ext/node): do not expose `self` global in node (#24637) closes #23727 --- ext/node/global.rs | 5 +++-- ext/node/polyfills/worker_threads.ts | 2 +- tests/registry/npm/@denotest/globals/1.0.0/index.d.ts | 3 ++- tests/registry/npm/@denotest/globals/1.0.0/index.js | 7 ++++++- tests/testdata/npm/compare_globals/main.out | 4 ++++ tests/testdata/npm/compare_globals/main.ts | 7 ++++++- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ext/node/global.rs b/ext/node/global.rs index 2367814f93b728..7f901fd0358521 100644 --- a/ext/node/global.rs +++ b/ext/node/global.rs @@ -67,7 +67,7 @@ const fn str_to_utf16(s: &str) -> [u16; N] { // UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED. #[rustfmt::skip] -const MANAGED_GLOBALS: [&[u16]; 12] = [ +const MANAGED_GLOBALS: [&[u16]; 13] = [ &str_to_utf16::<6>("Buffer"), &str_to_utf16::<14>("clearImmediate"), &str_to_utf16::<13>("clearInterval"), @@ -76,13 +76,14 @@ const MANAGED_GLOBALS: [&[u16]; 12] = [ &str_to_utf16::<6>("global"), &str_to_utf16::<11>("performance"), &str_to_utf16::<7>("process"), + &str_to_utf16::<4>("self"), &str_to_utf16::<12>("setImmediate"), &str_to_utf16::<11>("setInterval"), &str_to_utf16::<10>("setTimeout"), &str_to_utf16::<6>("window"), ]; -const SHORTEST_MANAGED_GLOBAL: usize = 6; +const SHORTEST_MANAGED_GLOBAL: usize = 4; const LONGEST_MANAGED_GLOBAL: usize = 14; #[derive(Debug, Clone, Copy)] diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index 8bbd0e9296a7c3..b51049af5c8cb9 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -356,7 +356,7 @@ internals.__initWorkerThreads = ( (ev: any) => any >(); - parentPort = self as ParentPort; + parentPort = globalThis as ParentPort; threadId = workerId; if (maybeWorkerMetadata) { const { 0: metadata, 1: _ } = maybeWorkerMetadata; diff --git a/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts b/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts index 1bbb8204703453..76dd781db6dc5b 100644 --- a/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts +++ b/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts @@ -17,5 +17,6 @@ export function getSetTimeout(): typeof setTimeout; export function checkProcessGlobal(): void; export function checkWindowGlobal(): void; +export function checkSelfGlobal(): void; -export function getFoo(): string; \ No newline at end of file +export function getFoo(): string; diff --git a/tests/registry/npm/@denotest/globals/1.0.0/index.js b/tests/registry/npm/@denotest/globals/1.0.0/index.js index b946bbd2aac54c..64f913b37a6036 100644 --- a/tests/registry/npm/@denotest/globals/1.0.0/index.js +++ b/tests/registry/npm/@denotest/globals/1.0.0/index.js @@ -20,6 +20,11 @@ exports.checkWindowGlobal = function () { console.log(Object.getOwnPropertyDescriptor(globalThis, "window") !== undefined); } +exports.checkSelfGlobal = function () { + console.log("self" in globalThis); + console.log(Object.getOwnPropertyDescriptor(globalThis, "self") !== undefined); +} + exports.getFoo = function () { return globalThis.foo; -} \ No newline at end of file +} diff --git a/tests/testdata/npm/compare_globals/main.out b/tests/testdata/npm/compare_globals/main.out index 5af5366578061c..9c9c2203aa188d 100644 --- a/tests/testdata/npm/compare_globals/main.out +++ b/tests/testdata/npm/compare_globals/main.out @@ -21,6 +21,10 @@ true true true true +true +true +false +false false false bar diff --git a/tests/testdata/npm/compare_globals/main.ts b/tests/testdata/npm/compare_globals/main.ts index 6f7b9ef8e8f429..5d082386fe2e0c 100644 --- a/tests/testdata/npm/compare_globals/main.ts +++ b/tests/testdata/npm/compare_globals/main.ts @@ -37,12 +37,17 @@ console.log( ); globals.checkProcessGlobal(); -// In Deno, the window global is defined, but in Node it is not. +// In Deno, the window and self globals are defined, but in Node they are not. console.log("window" in globalThis); +console.log("self" in globalThis); console.log( Object.getOwnPropertyDescriptor(globalThis, "window") !== undefined, ); +console.log( + Object.getOwnPropertyDescriptor(globalThis, "self") !== undefined, +); globals.checkWindowGlobal(); +globals.checkSelfGlobal(); // "Non-managed" globals are shared between Node and Deno. (globalThis as any).foo = "bar";