Skip to content

Commit

Permalink
policy: handle mainModule.__proto__ bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Jun 20, 2023
1 parent a4afe82 commit daaa43e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = '.';
}
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/errors/force_colors.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ throw new Error('Should include grayed stack trace')

Error: Should include grayed stack trace
at Object.<anonymous> (/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

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/policy-manifest/main-module-proto-bypass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.mainModule.__proto__.require("os")
15 changes: 15 additions & 0 deletions test/parallel/test-policy-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
}

0 comments on commit daaa43e

Please sign in to comment.