Skip to content

Commit a6f4e87

Browse files
committedJun 19, 2023
policy: handle mainModule.__proto__ bypass
Backport-PR-URL: nodejs-private/node-private#418 PR-URL: nodejs-private/node-private#416 Fixes: https://hackerone.com/bugs?subject=nodejs&report_id=1877919 Reviewed-By: Rich Trott <rtrott@gmail.com> CVE-ID: CVE-2023-30581
1 parent ade4850 commit a6f4e87

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed
 

‎lib/internal/modules/cjs/loader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ function Module(id = '', parent) {
226226
redirects = policy.manifest.getDependencyMapper(moduleURL);
227227
// TODO(rafaelgss): remove the necessity of this branch
228228
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
229+
// eslint-disable-next-line no-proto
230+
setOwnProperty(this.__proto__, 'require', makeRequireFunction(this, redirects));
229231
}
230232
this[require_private_symbol] = internalRequire;
231233
}
@@ -892,7 +894,7 @@ Module._load = function(request, parent, isMain) {
892894
const module = cachedModule || new Module(filename, parent);
893895

894896
if (isMain) {
895-
process.mainModule = module;
897+
setOwnProperty(process, 'mainModule', module);
896898
setOwnProperty(module.require, 'main', process.mainModule);
897899
module.id = '.';
898900
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.mainModule.__proto__.require("os")

‎test/parallel/test-policy-manifest.js

+15
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,18 @@ const fixtures = require('../common/fixtures.js');
6666

6767
assert.strictEqual(result.status, 0);
6868
}
69+
70+
{
71+
const policyFilepath = fixtures.path('policy-manifest', 'onerror-exit.json');
72+
const mainModuleBypass = fixtures.path('policy-manifest', 'main-module-proto-bypass.js');
73+
const result = spawnSync(process.execPath, [
74+
'--experimental-policy',
75+
policyFilepath,
76+
mainModuleBypass,
77+
]);
78+
79+
assert.notStrictEqual(result.status, 0);
80+
const stderr = result.stderr.toString();
81+
assert.match(stderr, /ERR_MANIFEST_DEPENDENCY_MISSING/);
82+
assert.match(stderr, /does not list os as a dependency specifier for conditions: require, node, node-addons/);
83+
}

0 commit comments

Comments
 (0)
Please sign in to comment.