Skip to content

Commit a33a1a3

Browse files
fasttimeaduh95
authored andcommitted
lib: fix regular expression to detect / and \
PR-URL: nodejs#40325 Fixes: nodejs#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 ec3bd72 commit a33a1a3

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
@@ -278,7 +278,7 @@ function resolveDirectoryEntry(search) {
278278
return resolveExtensions(new URL('index', search));
279279
}
280280

281-
const encodedSepRegEx = /%2F|%2C/i;
281+
const encodedSepRegEx = /%2F|%5C/i;
282282
function finalizeResolution(resolved, base) {
283283
if (RegExpPrototypeTest(encodedSepRegEx, resolved.pathname))
284284
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
@@ -176,10 +176,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
176176
}));
177177
}
178178

179-
// The use of %2F escapes in paths fails loading
179+
// The use of %2F and %5C escapes in paths fails loading
180180
loadFixture('pkgexports/sub/..%2F..%2Fbar.js').catch(mustCall((err) => {
181181
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
182182
}));
183+
loadFixture('pkgexports/sub/..%5C..%5Cbar.js').catch(mustCall((err) => {
184+
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
185+
}));
183186

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

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ const { requireImport, importImport } = importer;
5555
// Backtracking below the package base
5656
['#subpath/sub/../../../belowbase', 'request is not a valid subpath'],
5757
// Percent-encoded slash errors
58-
['#external/subpath/x%2Fy', 'must not include encoded "/"'],
58+
['#external/subpath/x%2Fy', 'must not include encoded "/" or "\\"'],
59+
['#external/subpath/x%5Cy', 'must not include encoded "/" or "\\"'],
5960
// Target must have a name
6061
['#', '#'],
6162
// Initial slash target must have a leading name
6263
['#/initialslash', '#/initialslash'],
6364
// Percent-encoded target paths
64-
['#percent', 'must not include encoded "/"'],
65+
['#encodedslash', 'must not include encoded "/" or "\\"'],
66+
['#encodedbackslash', 'must not include encoded "/" or "\\"'],
6567
]);
6668

6769
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
@@ -26,6 +26,7 @@
2626
"#": "./test.js",
2727
"#/initialslash": "./test.js",
2828
"#notfound": "./notfound.js",
29-
"#percent": "./..%2F/x.js"
29+
"#encodedslash": "./..%2F/x.js",
30+
"#encodedbackslash": "./..%5C/x.js"
3031
}
3132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default ',';

0 commit comments

Comments
 (0)