Skip to content

Commit 81cd7f3

Browse files
fasttimetargos
authored andcommitted
lib: fix regular expression to detect / and \
Fixes #40305 PR-URL: #40325 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 2b9fcdf commit 81cd7f3

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
@@ -356,7 +356,7 @@ function resolveDirectoryEntry(search) {
356356
return resolveExtensions(new URL('index', search));
357357
}
358358

359-
const encodedSepRegEx = /%2F|%2C/i;
359+
const encodedSepRegEx = /%2F|%5C/i;
360360
/**
361361
* @param {URL} resolved
362362
* @param {string | URL | undefined} base

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
@@ -171,10 +171,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
171171
}));
172172
}
173173

174-
// The use of %2F escapes in paths fails loading
174+
// The use of %2F and %5C escapes in paths fails loading
175175
loadFixture('pkgexports/sub/..%2F..%2Fbar.js').catch(mustCall((err) => {
176176
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
177177
}));
178+
loadFixture('pkgexports/sub/..%5C..%5Cbar.js').catch(mustCall((err) => {
179+
strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER');
180+
}));
178181

179182
// Package export with numeric index properties must throw a validation error
180183
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
@@ -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)