lib: fix regular expression to detect / and \

Fixes #40305

PR-URL: https://github.com/nodejs/node/pull/40325
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
This commit is contained in:
Francesco Trotta 2021-10-05 07:42:08 +02:00 committed by Node.js GitHub Bot
parent f233cb2c29
commit 8ac9aef8b1
7 changed files with 19 additions and 5 deletions

View File

@ -356,7 +356,7 @@ function resolveDirectoryEntry(search) {
return resolveExtensions(new URL('index', search));
}
const encodedSepRegEx = /%2F|%2C/i;
const encodedSepRegEx = /%2F|%5C/i;
/**
* @param {URL} resolved
* @param {string | URL | undefined} base

View File

@ -2,5 +2,8 @@ import '../common/index.mjs';
import assert from 'assert';
// ./test-esm-ok.mjs
import ok from '../fixtures/es-modules/test-%65%73%6d-ok.mjs';
// ./test-esm-comma,.mjs
import comma from '../fixtures/es-modules/test-esm-comma%2c.mjs';
assert(ok);
assert(comma);

View File

@ -171,10 +171,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
}));
}
// The use of %2F escapes in paths fails loading
// The use of %2F and %5C escapes in paths fails loading
loadFixture('pkgexports/sub/..%2F..%2Fbar.js').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
loadFixture('pkgexports/sub/..%5C..%5Cbar.js').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
// Package export with numeric index properties must throw a validation error
loadFixture('pkgexports-numeric').catch(mustCall((err) => {

View File

@ -53,13 +53,15 @@ const { requireImport, importImport } = importer;
// Backtracking below the package base
['#subpath/sub/../../../belowbase', 'request is not a valid subpath'],
// Percent-encoded slash errors
['#external/subpath/x%2Fy', 'must not include encoded "/"'],
['#external/subpath/x%2Fy', 'must not include encoded "/" or "\\"'],
['#external/subpath/x%5Cy', 'must not include encoded "/" or "\\"'],
// Target must have a name
['#', '#'],
// Initial slash target must have a leading name
['#/initialslash', '#/initialslash'],
// Percent-encoded target paths
['#percent', 'must not include encoded "/"'],
['#encodedslash', 'must not include encoded "/" or "\\"'],
['#encodedbackslash', 'must not include encoded "/" or "\\"'],
]);
for (const [specifier, expected] of invalidImportSpecifiers) {

View File

@ -7,6 +7,10 @@ importFixture('as%2Ff').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
importFixture('as%5Cf').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));
importFixture('as\\df').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
}));

View File

@ -26,6 +26,7 @@
"#": "./test.js",
"#/initialslash": "./test.js",
"#notfound": "./notfound.js",
"#percent": "./..%2F/x.js"
"#encodedslash": "./..%2F/x.js",
"#encodedbackslash": "./..%5C/x.js"
}
}

View File

@ -0,0 +1 @@
export default ',';