module: improve resolve paths validation
This commit adds input validation to require.resolve()'s paths option. Prior to this change, passing in a non-array value lead to a misleading 'module not found' error. Refs: https://github.com/nodejs/node/issues/27583 PR-URL: https://github.com/nodejs/node/pull/27613 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
ae749e7a29
commit
a608caa5ff
@ -53,6 +53,7 @@ const { compileFunction } = internalBinding('contextify');
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
ERR_INVALID_ARG_VALUE,
|
ERR_INVALID_ARG_VALUE,
|
||||||
|
ERR_INVALID_OPT_VALUE,
|
||||||
ERR_REQUIRE_ESM
|
ERR_REQUIRE_ESM
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
const { validateString } = require('internal/validators');
|
const { validateString } = require('internal/validators');
|
||||||
@ -573,28 +574,33 @@ Module._resolveFilename = function(request, parent, isMain, options) {
|
|||||||
|
|
||||||
var paths;
|
var paths;
|
||||||
|
|
||||||
if (typeof options === 'object' && options !== null &&
|
if (typeof options === 'object' && options !== null) {
|
||||||
Array.isArray(options.paths)) {
|
if (Array.isArray(options.paths)) {
|
||||||
const isRelative = request.startsWith('./') || request.startsWith('../') ||
|
const isRelative = request.startsWith('./') ||
|
||||||
(isWindows && request.startsWith('.\\') || request.startsWith('..\\'));
|
request.startsWith('../') ||
|
||||||
|
(isWindows && request.startsWith('.\\') ||
|
||||||
|
request.startsWith('..\\'));
|
||||||
|
|
||||||
if (isRelative) {
|
if (isRelative) {
|
||||||
paths = options.paths;
|
paths = options.paths;
|
||||||
} else {
|
} else {
|
||||||
const fakeParent = new Module('', null);
|
const fakeParent = new Module('', null);
|
||||||
|
|
||||||
paths = [];
|
paths = [];
|
||||||
|
|
||||||
for (var i = 0; i < options.paths.length; i++) {
|
for (var i = 0; i < options.paths.length; i++) {
|
||||||
const path = options.paths[i];
|
const path = options.paths[i];
|
||||||
fakeParent.paths = Module._nodeModulePaths(path);
|
fakeParent.paths = Module._nodeModulePaths(path);
|
||||||
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
|
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
|
||||||
|
|
||||||
for (var j = 0; j < lookupPaths.length; j++) {
|
for (var j = 0; j < lookupPaths.length; j++) {
|
||||||
if (!paths.includes(lookupPaths[j]))
|
if (!paths.includes(lookupPaths[j]))
|
||||||
paths.push(lookupPaths[j]);
|
paths.push(lookupPaths[j]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (options.paths !== undefined) {
|
||||||
|
throw new ERR_INVALID_OPT_VALUE('options.paths', options.paths);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
paths = Module._resolveLookupPaths(request, parent);
|
paths = Module._resolveLookupPaths(request, parent);
|
||||||
|
8
test/fixtures/require-resolve.js
vendored
8
test/fixtures/require-resolve.js
vendored
@ -84,3 +84,11 @@ assert.throws(() => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test paths option validation
|
||||||
|
common.expectsError(() => {
|
||||||
|
require.resolve('.\\three.js', { paths: 'foo' })
|
||||||
|
}, {
|
||||||
|
code: 'ERR_INVALID_OPT_VALUE',
|
||||||
|
type: TypeError,
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user