Skip to content

Commit 8894bdd

Browse files
fasttimerichardlau
authored andcommitted
lib: fix regular expression to detect / and \
PR-URL: #40325 Backport-PR-URL: #40564 Fixes: #40305 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zeyu Yang <[email protected]>
1 parent 39583f7 commit 8894bdd

File tree

7 files changed

+19
-5
lines changed

7 files changed

+19
-5
lines changed

lib/internal/modules/esm/resolve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ function resolveDirectoryEntry(search) {
277277
return resolveExtensions(new URL('index', search));
278278
}
279279

280-
const encodedSepRegEx = /%2F|%2C/i;
280+
const encodedSepRegEx = /%2F|%5C/i;
281281
function finalizeResolution(resolved, base) {
282282
if (RegExpPrototypeTest(encodedSepRegEx, resolved.pathname))
283283
throw new ERR_INVALID_MODULE_SPECIFIER(

test/es-module/test-esm-encoded-path.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ import '../common/index.mjs';
22
import assert from 'assert';
33
// ./test-esm-ok.mjs
44
import ok from '../fixtures/es-modules/test-%65%73%6d-ok.mjs';
5+
// ./test-esm-comma,.mjs
6+
import comma from '../fixtures/es-modules/test-esm-comma%2c.mjs';
57

68
assert(ok);
9+
assert(comma);

test/es-module/test-esm-exports.mjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
167167
}));
168168
}
169169

170-
// The use of %2F escapes in paths fails loading
170+
// The use of %2F and %5C escapes in paths fails loading
171171
loadFixture('pkgexports/sub/..%2F..%2Fbar.js').catch(mustCall((err) => {
172172
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
173173
}));
174+
loadFixture('pkgexports/sub/..%5C..%5Cbar.js').catch(mustCall((err) => {
175+
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
176+
}));
174177

175178
// Package export with numeric index properties must throw a validation error
176179
loadFixture('pkgexports-numeric').catch(mustCall((err) => {

test/es-module/test-esm-imports.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ const { requireImport, importImport } = importer;
5353
// Backtracking below the package base
5454
['#subpath/sub/../../../belowbase', 'request is not a valid subpath'],
5555
// Percent-encoded slash errors
56-
['#external/subpath/x%2Fy', 'must not include encoded "/"'],
56+
['#external/subpath/x%2Fy', 'must not include encoded "/" or "\\"'],
57+
['#external/subpath/x%5Cy', 'must not include encoded "/" or "\\"'],
5758
// Target must have a name
5859
['#', '#'],
5960
// Initial slash target must have a leading name
6061
['#/initialslash', '#/initialslash'],
6162
// Percent-encoded target paths
62-
['#percent', 'must not include encoded "/"'],
63+
['#encodedslash', 'must not include encoded "/" or "\\"'],
64+
['#encodedbackslash', 'must not include encoded "/" or "\\"'],
6365
]);
6466

6567
for (const [specifier, expected] of invalidImportSpecifiers) {

test/es-module/test-esm-pkgname.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ importFixture('as%2Ff').catch(mustCall((err) => {
77
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
88
}));
99

10+
importFixture('as%5Cf').catch(mustCall((err) => {
11+
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
12+
}));
13+
1014
importFixture('as\\df').catch(mustCall((err) => {
1115
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
1216
}));

test/fixtures/es-modules/pkgimports/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"#": "./test.js",
2626
"#/initialslash": "./test.js",
2727
"#notfound": "./notfound.js",
28-
"#percent": "./..%2F/x.js"
28+
"#encodedslash": "./..%2F/x.js",
29+
"#encodedbackslash": "./..%5C/x.js"
2930
}
3031
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default ',';

0 commit comments

Comments
 (0)