Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
unify invalid specifier check
  • Loading branch information
guybedford committed Jul 9, 2020
commit 25701919cad359c91a58fe52bec038eb4cebbc4e
21 changes: 9 additions & 12 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const {
SafeWeakMap,
SafeSet,
String,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
StringPrototypeMatch,
Expand Down Expand Up @@ -116,7 +115,10 @@ const {
const asyncESM = require('internal/process/esm_loader');
const ModuleJob = require('internal/modules/esm/module_job');
const { ModuleWrap, kInstantiated } = internalBinding('module_wrap');
const { packageInternalResolve } = require('internal/modules/esm/resolve');
const {
encodedSepRegEx,
packageInternalResolve
} = require('internal/modules/esm/resolve');

const isWindows = process.platform === 'win32';

Expand Down Expand Up @@ -574,16 +576,11 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
if (StringPrototypeStartsWith(resolvedPath, resolvedTargetPath) &&
StringPrototypeIndexOf(resolvedPath, '/node_modules/',
pkgPathPath.length - 1) === -1) {
try {
return fileURLToPath(resolved);
} catch (err) {
if (err.code === 'ERR_INVALID_FILE_URL_PATH' && StringPrototypeIncludes(
err.message, 'must not include encoded "/" characters')) {
throw new ERR_INVALID_MODULE_SPECIFIER(
resolved.pathname, 'must not include encoded "/" characters',
fileURLToPath(baseUrl));
}
}
if (resolvedPath.match(encodedSepRegEx))
throw new ERR_INVALID_MODULE_SPECIFIER(
resolvedPath, 'must not include encoded "/" or "\\" characters',
fileURLToPath(baseUrl));
return fileURLToPath(resolved);
}
const reason = 'request is not a valid subpath for the "exports" ' +
`resolution of ${baseUrl.pathname}package.json`;
Expand Down
20 changes: 8 additions & 12 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ function resolveIndex(search) {
return resolveExtensions(new URL('index', search));
}

const encodedSepRegEx = /%2F|%2C/i;
function finalizeResolution(resolved, base) {
if (getOptionValue('--experimental-specifier-resolution') === 'node') {
let file = resolveExtensionsWithTryExactName(resolved);
Expand All @@ -255,18 +256,12 @@ function finalizeResolution(resolved, base) {
resolved.pathname, fileURLToPath(base), 'module');
}

let path;
try {
path = fileURLToPath(resolved);
} catch (err) {
if (err.code === 'ERR_INVALID_FILE_URL_PATH' &&
err.message.includes('must not include encoded "/" characters')) {
throw new ERR_INVALID_MODULE_SPECIFIER(
resolved.pathname, 'must not include encoded "/" characters',
fileURLToPath(base));
}
throw err;
}
if (encodedSepRegEx.test(resolved.pathname))
throw new ERR_INVALID_MODULE_SPECIFIER(
resolved.pathname, 'must not include encoded "/" or "\\" characters',
fileURLToPath(base));

const path = fileURLToPath(resolved);
const stats = tryStatSync(path);

if (stats.isDirectory()) {
Expand Down Expand Up @@ -850,6 +845,7 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
module.exports = {
DEFAULT_CONDITIONS,
defaultResolve,
encodedSepRegEx,
getPackageType,
packageInternalResolve
};
4 changes: 2 additions & 2 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ function getPathFromURLWin32(url) {
if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F /
(pathname[n + 1] === '5' && third === 99)) { // 5c 5C \
throw new ERR_INVALID_FILE_URL_PATH(
'must not include encoded "\\" or "/" characters'
'must not include encoded \\ or / characters'
);
}
}
Expand Down Expand Up @@ -1331,7 +1331,7 @@ function getPathFromURLPosix(url) {
const third = pathname.codePointAt(n + 2) | 0x20;
if (pathname[n + 1] === '2' && third === 102) {
throw new ERR_INVALID_FILE_URL_PATH(
'must not include encoded "/" characters'
'must not include encoded / characters'
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-fs-whatwg-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if (common.isWindows) {
{
code: 'ERR_INVALID_FILE_URL_PATH',
name: 'TypeError',
message: 'File URL path must not include encoded "\\" or "/" characters'
message: 'File URL path must not include encoded \\ or / characters'
}
);
});
Expand All @@ -76,7 +76,7 @@ if (common.isWindows) {
{
code: 'ERR_INVALID_FILE_URL_PATH',
name: 'TypeError',
message: 'File URL path must not include encoded "/" characters'
message: 'File URL path must not include encoded / characters'
});
});
assert.throws(
Expand Down