From 7ffca8edb7555c2e83f3c352f37946c9630b6a84 Mon Sep 17 00:00:00 2001 From: "wind-stream-staging[bot]" <280913783+wind-stream-staging[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:35:45 +0000 Subject: [PATCH] [REPL] Builtin objects cannot be accessed after delete globalThis Duplicate of https://github.com/nodejs/node/issues/67702 WS: session-iCXlj-X5OfXAjOex7EzHc Factory-Run: srun_1QkE87DNomQcLgLHLOK7 Factory-Attempt: satt_dUw4mVE8jchA4kJvuP8h Candidate-SHA256: 38d114834c8008de1723baa07f2c4c098f4733b14263fb4584a2a7d2a2844271 --- lib/internal/repl/inspector.js | 5 ++- .../parallel/test-repl-delete-global-this.mjs | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-repl-delete-global-this.mjs diff --git a/lib/internal/repl/inspector.js b/lib/internal/repl/inspector.js index 371255eac28d..76c083b0e883 100644 --- a/lib/internal/repl/inspector.js +++ b/lib/internal/repl/inspector.js @@ -2,7 +2,6 @@ const { BigInt, - JSONStringify, ObjectDefineProperty, Promise, PromisePrototypeThen, @@ -29,8 +28,10 @@ let nextObjectGroupId = 0; // Property used to ferry a live value from the inspector world back to ours. const kStashKey = '__nodeREPLStash'; +// Resolve the stash directly so deleting or shadowing globalThis in the REPL +// does not prevent transferring results and exceptions back to the host. const kStashFn = `function() { - globalThis[${JSONStringify(kStashKey)}].value = this; + ${kStashKey}.value = this; }`; // Cached channel, attached to the REPL server instance. diff --git a/test/parallel/test-repl-delete-global-this.mjs b/test/parallel/test-repl-delete-global-this.mjs new file mode 100644 index 000000000000..d5f80b9cc860 --- /dev/null +++ b/test/parallel/test-repl-delete-global-this.mjs @@ -0,0 +1,40 @@ +import '../common/index.mjs'; +import assert from 'node:assert/strict'; +import { startNewREPLServer } from '../common/repl.js'; + +const globalObject = globalThis; +const globalThisDescriptor = Object.getOwnPropertyDescriptor(globalObject, 'globalThis'); + +for (const useGlobal of [false, true]) { + const { replServer, output, run } = startNewREPLServer({ + terminal: false, + useColors: false, + useGlobal, + }); + + async function check(input, expected) { + output.accumulator = ''; + await run([input]); + assert.match(output.accumulator, expected); + } + + try { + await check('delete globalThis', /^true\n$/); + await check('typeof globalThis', /^'undefined'\n$/); + await check('Math', /\[Math\]/); + await check('JSON', /\[JSON\]/); + await check('Object', /\[Function: Object\]/); + await check('JSON.stringify({ pi: Math.PI })', /3\.141592653589793/); + + await check('a', /^Uncaught ReferenceError: a is not defined\n/); + await check('21 * 2', /^42\n$/); + await check('!@#$%^&[]', /^Uncaught SyntaxError:/); + await check('21 * 2', /^42\n$/); + + await check('({', /^\| $/); + await check('answer: 42 })', /^\{ answer: 42 \}\n$/); + } finally { + Object.defineProperty(globalObject, 'globalThis', globalThisDescriptor); + replServer.close(); + } +}