Skip to content

Commit

Permalink
fix(ext/node): do not expose self global in node (#24637)
Browse files Browse the repository at this point in the history
closes #23727
  • Loading branch information
kt3k authored and bartlomieju committed Jul 22, 2024
1 parent 1469d61 commit 600b32f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions ext/node/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const fn str_to_utf16<const N: usize>(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"),
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion ext/node/polyfills/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/registry/npm/@denotest/globals/1.0.0/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
export function getFoo(): string;
7 changes: 6 additions & 1 deletion tests/registry/npm/@denotest/globals/1.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions tests/testdata/npm/compare_globals/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ true
true
true
true
true
true
false
false
false
false
bar
Expand Down
7 changes: 6 additions & 1 deletion tests/testdata/npm/compare_globals/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 600b32f

Please sign in to comment.