From efb38ce21ced417fc6f3597aaabed723daf2ef39 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 23 Nov 2026 15:29:43 +0100 Subject: [PATCH 1/4] repl: do not depend on user-mutable `globalThis` Signed-off-by: Antoine du Hamel --- lib/internal/repl/eval.js | 7 ++- lib/internal/repl/inspector.js | 64 +++++++++++++++++++-------- test/parallel/test-repl-inspector.mjs | 5 +++ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/lib/internal/repl/eval.js b/lib/internal/repl/eval.js index f988dcc13230..72f33c7287ff 100644 --- a/lib/internal/repl/eval.js +++ b/lib/internal/repl/eval.js @@ -26,7 +26,7 @@ const { kDynamicImportName, transformModuleSyntax, } = require('internal/repl/transform'); -const { getReplInspector, prepareContext } = require('internal/repl/inspector'); +const { getReplInspector } = require('internal/repl/inspector'); let debug = require('internal/util/debuglog').debuglog('repl', (fn) => { debug = fn; @@ -117,7 +117,8 @@ function createReplEval(repl) { async function inspectorEval(rawCode, context, file, cb) { installDynamicImport(context); - prepareContext(context); + const contextId = repl.useGlobal ? undefined : repl[kContextId]; + await inspector.prepareContext(context, contextId); // Wrap bare object literals so `{ a: 1 }` is evaluated as an expression // rather than a block statement. @@ -142,8 +143,6 @@ function createReplEval(repl) { return cb(await null); } - const contextId = repl.useGlobal ? undefined : repl[kContextId]; - const evaluateParams = { expression: `${code}\n//# sourceURL=${file}`, objectGroup: inspector.objectGroup, diff --git a/lib/internal/repl/inspector.js b/lib/internal/repl/inspector.js index 371255eac28d..4e6954c2220c 100644 --- a/lib/internal/repl/inspector.js +++ b/lib/internal/repl/inspector.js @@ -2,11 +2,11 @@ const { BigInt, - JSONStringify, ObjectDefineProperty, Promise, PromisePrototypeThen, ReflectHas, + SafeWeakMap, StringPrototypeSlice, Symbol, } = primordials; @@ -28,26 +28,14 @@ let Session; let nextObjectGroupId = 0; // Property used to ferry a live value from the inspector world back to ours. +// The stash is passed by object id rather than looked up through a global, so +// user code deleting or shadowing `globalThis` (or anything else) can't break it. const kStashKey = '__nodeREPLStash'; -const kStashFn = `function() { - globalThis[${JSONStringify(kStashKey)}].value = this; -}`; +const kStashFn = 'function(stash) { stash.value = this; }'; // Cached channel, attached to the REPL server instance. const kChannel = Symbol('kReplInspectorChannel'); -function prepareContext(context) { - if (!ReflectHas(context, kStashKey)) { - ObjectDefineProperty(context, kStashKey, { - __proto__: null, - value: { __proto__: null }, - enumerable: false, - writable: false, - configurable: true, - }); - } -} - // Converts the `unserializableValue` field of a remote object back into the // real primitive it represents. function fromUnserializable(value) { @@ -79,6 +67,10 @@ class ReplInspectorChannel { this.session = null; // Exposed so callers can scope their own remote handles to the shared group. this.objectGroup = `node-repl-${nextObjectGroupId++}`; + // Remote handles to each context's stash, kept outside `objectGroup` so + // `releaseObjects()` does not drop them. + this.stashObjectGroup = `${this.objectGroup}-stash`; + this.stashes = new SafeWeakMap(); } // Lazily connects the shared session and arranges for it to be torn down on @@ -193,16 +185,22 @@ class ReplInspectorChannel { // it points at. async toLiveValue(remote, context) { if (remote.objectId !== undefined) { + const entry = this.stashes.get(context); + if (entry === undefined) { + throw new ERR_INSPECTOR_NOT_AVAILABLE(); + } const response = await this.post('Runtime.callFunctionOn', { functionDeclaration: kStashFn, objectId: remote.objectId, + arguments: [{ objectId: entry.objectId }], objectGroup: this.objectGroup, }); if (response.exceptionDetails) { throw new ERR_INSPECTOR_NOT_AVAILABLE(); } - const value = context[kStashKey].value; - context[kStashKey].value = undefined; + const { stash } = entry; + const value = stash.value; + stash.value = undefined; return { __proto__: null, value }; } if (remote.type === 'undefined') { @@ -217,6 +215,35 @@ class ReplInspectorChannel { return { __proto__: null, value: undefined }; } + // Installs the stash on `context` if needed and retains a remote handle to it. + // Runs before any user code is evaluated in a fresh context. + async prepareContext(context, contextId) { + if (this.stashes.has(context) && ReflectHas(context, kStashKey)) { + return; + } + if (!ReflectHas(context, kStashKey)) { + ObjectDefineProperty(context, kStashKey, { + __proto__: null, + value: { __proto__: null }, + enumerable: false, + writable: false, + configurable: true, + }); + } + const stash = context[kStashKey]; + const { result, exceptionDetails } = await this.post('Runtime.evaluate', { + expression: kStashKey, + objectGroup: this.stashObjectGroup, + silent: true, + ...(contextId !== undefined ? { contextId } : null), + }); + if (exceptionDetails || result.objectId === undefined) { + this.stashes.delete(context); + return; + } + this.stashes.set(context, { __proto__: null, stash, objectId: result.objectId }); + } + // Releases every remote handle created in the shared object group. releaseObjects() { if (this.session !== null) { @@ -268,5 +295,4 @@ function getReplInspector(repl) { module.exports = { getReplInspector, - prepareContext, }; diff --git a/test/parallel/test-repl-inspector.mjs b/test/parallel/test-repl-inspector.mjs index 203b376a3e4a..5ac11c644890 100644 --- a/test/parallel/test-repl-inspector.mjs +++ b/test/parallel/test-repl-inspector.mjs @@ -22,3 +22,8 @@ await run([ assert(output.accumulator.includes( "null { result: { type: 'number', value: 2, description: '2' } }")); + +// Ensures that we do not rely on user-mutable global identifiers: +await run(['delete global, delete globalThis, delete Function, delete eval, delete Object, delete Reflect; Math']); + +assert.match(output.accumulator, /^Object \[Math\] \{\}$/m); From 0c61a638de54d3f8270594d683d7cdad1e6cb7f9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 23 Nov 2026 20:13:57 +0100 Subject: [PATCH 2/4] fixup! repl: do not depend on user-mutable `globalThis` --- test/parallel/test-repl-uncaught-exception-after-input-ended.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl-uncaught-exception-after-input-ended.js b/test/parallel/test-repl-uncaught-exception-after-input-ended.js index e27f22303059..ce142a960b0b 100644 --- a/test/parallel/test-repl-uncaught-exception-after-input-ended.js +++ b/test/parallel/test-repl-uncaught-exception-after-input-ended.js @@ -20,6 +20,6 @@ start({ input.end('setImmediate(() => { throw new Error("test"); });\n'); -setImmediate(common.mustCall(() => { +process.on('beforeExit', common.mustCall(() => { assert.match(output.read(), /Uncaught Error: test/); })); From f7c982a05b6a3ff81d7e21a09d88ceb9833e76a8 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 24 Nov 2026 10:53:12 +0100 Subject: [PATCH 3/4] fixup! repl: do not depend on user-mutable `globalThis` --- lib/inspector.js | 35 ++++++++++++++++++++++++++- test/parallel/test-repl-inspector.mjs | 5 +++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/inspector.js b/lib/inspector.js index 1c440794f393..519a9e668f33 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -1,9 +1,14 @@ 'use strict'; const { + Array, + ArrayIsArray, JSONParse, JSONStringify, + ObjectEntries, + ObjectSetPrototypeOf, SafeMap, + SafeWeakMap, SymbolDispose, } = primordials; @@ -51,6 +56,34 @@ const { emitProtocolEvent, } = internalBinding('inspector'); +function toNullProtoTree(value, seen = new SafeWeakMap()) { + if (value === null || typeof value !== 'object') return value; + + if (seen.has(value)) { + // Escape infinite recursion. + return seen.get(value); + } + + if (ArrayIsArray(value)) { + const { length } = value; + const arr = ObjectSetPrototypeOf(new Array(length), null); + seen.set(value, arr); + for (let i = 0; i < length; i++) { + arr[i] = toNullProtoTree(value[i], seen); + } + return arr; + } + + const clean = { __proto__: null }; + seen.set(value, clean); + const entries = ObjectEntries(value); + for (let i = 0; i < entries.length; i++) { + const { 0: key, 1: value } = entries[i]; + clean[key] = toNullProtoTree(value, seen); + } + return clean; +} + class Session extends EventEmitter { #connection = null; #nextId = 1; @@ -136,7 +169,7 @@ class Session extends EventEmitter { if (callback) { this.#messageCallbacks.set(id, callback); } - this.#connection.dispatch(JSONStringify(message)); + this.#connection.dispatch(JSONStringify(toNullProtoTree(message))); } /** diff --git a/test/parallel/test-repl-inspector.mjs b/test/parallel/test-repl-inspector.mjs index 5ac11c644890..cc0a5cccc214 100644 --- a/test/parallel/test-repl-inspector.mjs +++ b/test/parallel/test-repl-inspector.mjs @@ -24,6 +24,9 @@ assert(output.accumulator.includes( "null { result: { type: 'number', value: 2, description: '2' } }")); // Ensures that we do not rely on user-mutable global identifiers: -await run(['delete global, delete globalThis, delete Function, delete eval, delete Object, delete Reflect; Math']); +await run([ + 'Object.prototype.toJSON = function() { throw new Error() }', + 'delete global, delete globalThis, delete Function, delete eval, delete Object, delete Reflect; Math' +]); assert.match(output.accumulator, /^Object \[Math\] \{\}$/m); From cf7baaa82a2a677964c2fe1a161726226b0ccea9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 24 Nov 2026 11:48:24 +0100 Subject: [PATCH 4/4] fixup! repl: do not depend on user-mutable `globalThis` --- test/parallel/test-repl-inspector.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl-inspector.mjs b/test/parallel/test-repl-inspector.mjs index cc0a5cccc214..9e0b5f95b1b2 100644 --- a/test/parallel/test-repl-inspector.mjs +++ b/test/parallel/test-repl-inspector.mjs @@ -26,7 +26,7 @@ assert(output.accumulator.includes( // Ensures that we do not rely on user-mutable global identifiers: await run([ 'Object.prototype.toJSON = function() { throw new Error() }', - 'delete global, delete globalThis, delete Function, delete eval, delete Object, delete Reflect; Math' + 'delete global, delete globalThis, delete Function, delete eval, delete Object, delete Reflect; Math', ]); assert.match(output.accumulator, /^Object \[Math\] \{\}$/m);