Skip to content

Commit 2aae197

Browse files
RafaelGSSbmeck
andcommitted
lib: makeRequireFunction patch when experimental policy
Signed-off-by: RafaelGSS <[email protected]> CVE-ID: CVE-2023-23918 PR-URL: #358 Co-authored-by: Bradley Farias <[email protected]> Reviewed-by: Bradley Farias <[email protected]> Reviewed-by: Michael Dawson <[email protected]>
1 parent 6d17b69 commit 2aae197

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/internal/modules/cjs/loader.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,9 @@ function Module(id = '', parent) {
230230
if (manifest) {
231231
const moduleURL = pathToFileURL(id);
232232
redirects = manifest.getDependencyMapper(moduleURL);
233+
// TODO(rafaelgss): remove the necessity of this branch
234+
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
233235
}
234-
setOwnProperty(this, 'require', makeRequireFunction(this, redirects));
235-
// Loads a module at the given file path. Returns that module's
236-
// `exports` property.
237236
this[require_private_symbol] = internalRequire;
238237
}
239238

@@ -1144,6 +1143,23 @@ Module.prototype.load = function(filename) {
11441143
cascadedLoader.cjsCache.set(this, exports);
11451144
};
11461145

1146+
// Loads a module at the given file path. Returns that module's
1147+
// `exports` property.
1148+
// Note: when using the experimental policy mechanism this function is overridden
1149+
Module.prototype.require = function(id) {
1150+
validateString(id, 'id');
1151+
if (id === '') {
1152+
throw new ERR_INVALID_ARG_VALUE('id', id,
1153+
'must be a non-empty string');
1154+
}
1155+
requireDepth++;
1156+
try {
1157+
return Module._load(id, this, /* isMain */ false);
1158+
} finally {
1159+
requireDepth--;
1160+
}
1161+
};
1162+
11471163
// Resolved path to process.argv[1] will be lazily placed here
11481164
// (needed for setting breakpoint when called with --inspect-brk)
11491165
let resolvedArgv;
@@ -1212,10 +1228,11 @@ function wrapSafe(filename, content, cjsModuleInstance) {
12121228
// Returns exception, if any.
12131229
Module.prototype._compile = function(content, filename) {
12141230
let moduleURL;
1231+
let redirects;
12151232
const manifest = policy()?.manifest;
12161233
if (manifest) {
12171234
moduleURL = pathToFileURL(filename);
1218-
manifest.getDependencyMapper(moduleURL);
1235+
redirects = manifest.getDependencyMapper(moduleURL);
12191236
manifest.assertIntegrity(moduleURL, content);
12201237
}
12211238

@@ -1245,17 +1262,18 @@ Module.prototype._compile = function(content, filename) {
12451262
}
12461263
}
12471264
const dirname = path.dirname(filename);
1265+
const require = makeRequireFunction(this, redirects);
12481266
let result;
12491267
const exports = this.exports;
12501268
const thisValue = exports;
12511269
const module = this;
12521270
if (requireDepth === 0) statCache = new SafeMap();
12531271
if (inspectorWrapper) {
12541272
result = inspectorWrapper(compiledWrapper, thisValue, exports,
1255-
module.require, module, filename, dirname);
1273+
require, module, filename, dirname);
12561274
} else {
12571275
result = ReflectApply(compiledWrapper, thisValue,
1258-
[exports, module.require, module, filename, dirname]);
1276+
[exports, require, module, filename, dirname]);
12591277
}
12601278
hasLoadedAnyUserCJSModule = true;
12611279
if (requireDepth === 0) statCache = null;

lib/internal/modules/helpers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ function makeRequireFunction(mod, redirects) {
115115
};
116116
} else {
117117
require = function require(path) {
118-
return mod[require_private_symbol](mod, path);
118+
// When no policy manifest, the original prototype.require is sustained
119+
return mod.require(path);
119120
};
120121
}
121122

0 commit comments

Comments
 (0)