diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 60fa7e79d19693..508123c6e306ff 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -231,6 +231,8 @@ function Module(id = '', parent) { redirects = manifest.getDependencyMapper(moduleURL); // TODO(rafaelgss): remove the necessity of this branch setOwnProperty(this, 'require', makeRequireFunction(this, redirects)); + // eslint-disable-next-line no-proto + setOwnProperty(this.__proto__, 'require', makeRequireFunction(this, redirects)); } this[require_private_symbol] = internalRequire; } @@ -943,7 +945,7 @@ Module._load = function(request, parent, isMain) { const module = cachedModule || new Module(filename, parent); if (isMain) { - process.mainModule = module; + setOwnProperty(process, 'mainModule', module); setOwnProperty(module.require, 'main', process.mainModule); module.id = '.'; } diff --git a/test/fixtures/errors/force_colors.snapshot b/test/fixtures/errors/force_colors.snapshot index a38745e396a8d9..b52735370ea0f3 100644 --- a/test/fixtures/errors/force_colors.snapshot +++ b/test/fixtures/errors/force_colors.snapshot @@ -4,10 +4,10 @@ throw new Error('Should include grayed stack trace') Error: Should include grayed stack trace at Object. (/test*force_colors.js:1:7) - at Module._compile (node:internal*modules*cjs*loader:1255:14) - at Module._extensions..js (node:internal*modules*cjs*loader:1309:10) - at Module.load (node:internal*modules*cjs*loader:1113:32) - at Module._load (node:internal*modules*cjs*loader:960:12) + at Module._compile (node:internal*modules*cjs*loader:1257:14) + at Module._extensions..js (node:internal*modules*cjs*loader:1311:10) + at Module.load (node:internal*modules*cjs*loader:1115:32) + at Module._load (node:internal*modules*cjs*loader:962:12)  at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:83:12)  at node:internal*main*run_main_module:23:47 diff --git a/test/fixtures/policy-manifest/main-module-proto-bypass.js b/test/fixtures/policy-manifest/main-module-proto-bypass.js new file mode 100644 index 00000000000000..6111aae140ed0a --- /dev/null +++ b/test/fixtures/policy-manifest/main-module-proto-bypass.js @@ -0,0 +1 @@ +process.mainModule.__proto__.require("os") diff --git a/test/parallel/test-policy-manifest.js b/test/parallel/test-policy-manifest.js index f8bebdf4cf69f1..5dfadb3631de66 100644 --- a/test/parallel/test-policy-manifest.js +++ b/test/parallel/test-policy-manifest.js @@ -66,3 +66,18 @@ const fixtures = require('../common/fixtures.js'); assert.strictEqual(result.status, 0); } + +{ + const policyFilepath = fixtures.path('policy-manifest', 'onerror-exit.json'); + const mainModuleBypass = fixtures.path('policy-manifest', 'main-module-proto-bypass.js'); + const result = spawnSync(process.execPath, [ + '--experimental-policy', + policyFilepath, + mainModuleBypass, + ]); + + assert.notStrictEqual(result.status, 0); + const stderr = result.stderr.toString(); + assert.match(stderr, /ERR_MANIFEST_DEPENDENCY_MISSING/); + assert.match(stderr, /does not list os as a dependency specifier for conditions: require, node, node-addons/); +}