Skip to content

Commit c5ab884

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 c5ab884

9 files changed

Lines changed: 105 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Test that package map subpaths cannot escape the target package directory,
2+
// while normalized in-package subpaths are still allowed.
3+
import '../common/index.mjs';
4+
import { spawnSyncAndAssert, spawnSyncAndExit } from '../common/child_process.js';
5+
import * as fixtures from '../common/fixtures.mjs';
6+
7+
const map = fixtures.path('package-map/traversal/map.json');
8+
const cwd = fixtures.path('package-map/traversal/app');
9+
10+
function run(specifier) {
11+
return [
12+
'--no-warnings',
13+
'--experimental-package-map', map,
14+
'--input-type=module',
15+
'--eval', `import x from '${specifier}'; console.log(x);`,
16+
];
17+
}
18+
19+
for (const specifier of [
20+
'dep/../secret.mjs',
21+
'dep/%2e%2e/secret.mjs',
22+
'dep/sub/../../secret.mjs',
23+
]) {
24+
spawnSyncAndExit(process.execPath, run(specifier), { cwd }, {
25+
status: 1,
26+
signal: null,
27+
stderr: /ERR_INVALID_MODULE_SPECIFIER/,
28+
});
29+
}
30+
31+
for (const specifier of [
32+
'dep/sub/file.mjs',
33+
'dep/./sub/file.mjs',
34+
'dep/sub/../sub/file.mjs',
35+
]) {
36+
spawnSyncAndAssert(process.execPath, run(specifier), { cwd }, {
37+
stdout: /dep-sub/,
38+
trim: true,
39+
});
40+
}
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';

‎test/parallel/test-require-package-map.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,43 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
376376
assert.strictEqual(status, 0, stderr);
377377
});
378378
});
379+
380+
describe('subpath containment', () => {
381+
const traversalMap = fixtures.path('package-map/traversal/map.json');
382+
const appCwd = fixtures.path('package-map/traversal/app');
383+
384+
function requireSpec(specifier) {
385+
return spawnSync(process.execPath, [
386+
'--no-warnings',
387+
'--experimental-package-map', traversalMap,
388+
'-e',
389+
`console.log(require('${specifier}'));`,
390+
], { cwd: appCwd, encoding: 'utf8' });
391+
}
392+
393+
for (const specifier of [
394+
'dep/../secret.cjs',
395+
'dep/%2e%2e/secret.cjs',
396+
'dep/sub/../../secret.cjs',
397+
]) {
398+
it(`rejects escaping subpath ${specifier}`, () => {
399+
const { status, stderr } = requireSpec(specifier);
400+
assert.match(stderr, /ERR_INVALID_MODULE_SPECIFIER/);
401+
assert.strictEqual(status, 1, stderr);
402+
});
403+
}
404+
405+
for (const specifier of [
406+
'dep/sub/file.cjs',
407+
'dep/./sub/file.cjs',
408+
'dep/sub/../sub/file.cjs',
409+
]) {
410+
it(`allows in-package subpath ${specifier}`, () => {
411+
const { status, stdout, stderr } = requireSpec(specifier);
412+
assert.strictEqual(stderr, '');
413+
assert.match(stdout, /dep-sub-cjs/);
414+
assert.strictEqual(status, 0, stderr);
415+
});
416+
}
417+
});
379418
});

0 commit comments

Comments
 (0)