Skip to content

Commit 119a590

Browse files
cjihrigtargos
authored andcommitted
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: #27583 PR-URL: #27613 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6624f80 commit 119a590

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

lib/internal/modules/cjs/loader.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const { compileFunction } = internalBinding('contextify');
5353

5454
const {
5555
ERR_INVALID_ARG_VALUE,
56+
ERR_INVALID_OPT_VALUE,
5657
ERR_REQUIRE_ESM
5758
} = require('internal/errors').codes;
5859
const { validateString } = require('internal/validators');
@@ -573,28 +574,33 @@ Module._resolveFilename = function(request, parent, isMain, options) {
573574

574575
var paths;
575576

576-
if (typeof options === 'object' && options !== null &&
577-
Array.isArray(options.paths)) {
578-
const isRelative = request.startsWith('./') || request.startsWith('../') ||
579-
(isWindows && request.startsWith('.\\') || request.startsWith('..\\'));
577+
if (typeof options === 'object' && options !== null) {
578+
if (Array.isArray(options.paths)) {
579+
const isRelative = request.startsWith('./') ||
580+
request.startsWith('../') ||
581+
(isWindows && request.startsWith('.\\') ||
582+
request.startsWith('..\\'));
580583

581-
if (isRelative) {
582-
paths = options.paths;
583-
} else {
584-
const fakeParent = new Module('', null);
584+
if (isRelative) {
585+
paths = options.paths;
586+
} else {
587+
const fakeParent = new Module('', null);
585588

586-
paths = [];
589+
paths = [];
587590

588-
for (var i = 0; i < options.paths.length; i++) {
589-
const path = options.paths[i];
590-
fakeParent.paths = Module._nodeModulePaths(path);
591-
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
591+
for (var i = 0; i < options.paths.length; i++) {
592+
const path = options.paths[i];
593+
fakeParent.paths = Module._nodeModulePaths(path);
594+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
592595

593-
for (var j = 0; j < lookupPaths.length; j++) {
594-
if (!paths.includes(lookupPaths[j]))
595-
paths.push(lookupPaths[j]);
596+
for (var j = 0; j < lookupPaths.length; j++) {
597+
if (!paths.includes(lookupPaths[j]))
598+
paths.push(lookupPaths[j]);
599+
}
596600
}
597601
}
602+
} else if (options.paths !== undefined) {
603+
throw new ERR_INVALID_OPT_VALUE('options.paths', options.paths);
598604
}
599605
} else {
600606
paths = Module._resolveLookupPaths(request, parent);

test/fixtures/require-resolve.js

+8
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ assert.throws(() => {
8484
);
8585
}
8686
}
87+
88+
// Test paths option validation
89+
common.expectsError(() => {
90+
require.resolve('.\\three.js', { paths: 'foo' })
91+
}, {
92+
code: 'ERR_INVALID_OPT_VALUE',
93+
type: TypeError,
94+
});

0 commit comments

Comments
 (0)