Skip to content

Commit ddd6558

Browse files
committed
module: reject package map subpaths that escape the target package
Fixes: nodejs#65501 Signed-off-by: islandryu <[email protected]>
1 parent 65f518c commit ddd6558

8 files changed

Lines changed: 95 additions & 0 deletions

File tree

‎lib/internal/modules/package_map.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const {
44
JSONParse,
55
ObjectEntries,
66
SafeMap,
7+
StringPrototypeEndsWith,
78
StringPrototypeIndexOf,
89
StringPrototypeSlice,
10+
StringPrototypeStartsWith,
911
} = primordials;
1012

1113
const assert = require('internal/assert');
@@ -19,6 +21,7 @@ const getPackageMapPath = getLazy(() =>
1921
const fs = require('fs');
2022

2123
const {
24+
ERR_INVALID_MODULE_SPECIFIER,
2225
ERR_PACKAGE_MAP_EXTERNAL_FILE,
2326
ERR_PACKAGE_MAP_INVALID,
2427
ERR_PACKAGE_MAP_KEY_NOT_FOUND,
@@ -193,6 +196,18 @@ class PackageMap {
193196
throw new ERR_PACKAGE_MAP_KEY_NOT_FOUND(targetKey, this.#configPath);
194197
}
195198

199+
if (subpath !== '.') {
200+
let packageBaseURL = pathToFileURL(targetEntry.path);
201+
if (!StringPrototypeEndsWith(packageBaseURL.pathname, '/')) {
202+
packageBaseURL = new URL(`${packageBaseURL.href}/`);
203+
}
204+
const resolved = new URL(subpath, packageBaseURL);
205+
206+
if (!StringPrototypeStartsWith(resolved.pathname, packageBaseURL.pathname)) {
207+
throw new ERR_INVALID_MODULE_SPECIFIER(specifier, 'resolves outside its package', parentPath);
208+
}
209+
}
210+
196211
return { packagePath: targetEntry.path, subpath };
197212
}
198213
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'app';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'dep-sub-cjs';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'dep-sub';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"packages": {
3+
"app": { "url": "./app", "dependencies": { "dep": "dep" } },
4+
"dep": { "url": "./dep", "dependencies": {} }
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'SECRET-OUTSIDE-CJS';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'SECRET-OUTSIDE';
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import '../common/index.mjs';
2+
import { spawnSyncAndAssert, spawnSyncAndExit } from '../common/child_process.js';
3+
import * as fixtures from '../common/fixtures.mjs';
4+
5+
const map = fixtures.path('package-map/traversal/map.json');
6+
const cwd = fixtures.path('package-map/traversal/app');
7+
8+
function runESM(specifier) {
9+
return [
10+
'--no-warnings',
11+
'--experimental-package-map', map,
12+
'--input-type=module',
13+
'--eval', `import x from '${specifier}'; console.log(x);`,
14+
];
15+
}
16+
17+
function runCJS(specifier) {
18+
return [
19+
'--no-warnings',
20+
'--experimental-package-map', map,
21+
'--eval', `console.log(require('${specifier}'));`,
22+
];
23+
}
24+
25+
for (const specifier of [
26+
'dep/../secret.mjs',
27+
'dep/%2e%2e/secret.mjs',
28+
'dep/sub/../../secret.mjs',
29+
]) {
30+
spawnSyncAndExit(process.execPath, runESM(specifier), { cwd }, {
31+
status: 1,
32+
signal: null,
33+
stderr: /ERR_INVALID_MODULE_SPECIFIER/,
34+
});
35+
}
36+
37+
for (const specifier of [
38+
'dep/sub/file.mjs',
39+
'dep/./sub/file.mjs',
40+
'dep/sub/../sub/file.mjs',
41+
]) {
42+
spawnSyncAndAssert(process.execPath, runESM(specifier), { cwd }, {
43+
stdout: /dep-sub/,
44+
trim: true,
45+
});
46+
}
47+
48+
for (const specifier of [
49+
'dep/../secret.cjs',
50+
'dep/%2e%2e/secret.cjs',
51+
'dep/sub/../../secret.cjs',
52+
]) {
53+
spawnSyncAndExit(process.execPath, runCJS(specifier), { cwd }, {
54+
status: 1,
55+
signal: null,
56+
stderr: /ERR_INVALID_MODULE_SPECIFIER/,
57+
});
58+
}
59+
60+
for (const specifier of [
61+
'dep/sub/file.cjs',
62+
'dep/./sub/file.cjs',
63+
'dep/sub/../sub/file.cjs',
64+
]) {
65+
spawnSyncAndAssert(process.execPath, runCJS(specifier), { cwd }, {
66+
stdout: /dep-sub-cjs/,
67+
trim: true,
68+
});
69+
}

0 commit comments

Comments
 (0)