Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/internal/modules/package_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const {
JSONParse,
ObjectEntries,
SafeMap,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeStartsWith,
} = primordials;

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

const {
ERR_INVALID_MODULE_SPECIFIER,
ERR_PACKAGE_MAP_EXTERNAL_FILE,
ERR_PACKAGE_MAP_INVALID,
ERR_PACKAGE_MAP_KEY_NOT_FOUND,
Expand Down Expand Up @@ -193,6 +196,18 @@ class PackageMap {
throw new ERR_PACKAGE_MAP_KEY_NOT_FOUND(targetKey, this.#configPath);
}

if (subpath !== '.') {
let packageBaseURL = pathToFileURL(targetEntry.path);
if (!StringPrototypeEndsWith(packageBaseURL.pathname, '/')) {
packageBaseURL = new URL(`${packageBaseURL.href}/`);
}
const resolved = new URL(subpath, packageBaseURL);

if (!StringPrototypeStartsWith(resolved.pathname, packageBaseURL.pathname)) {
throw new ERR_INVALID_MODULE_SPECIFIER(specifier, 'resolves outside its package', parentPath);
}
}

return { packagePath: targetEntry.path, subpath };
}
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/package-map/traversal/app/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'app';
1 change: 1 addition & 0 deletions test/fixtures/package-map/traversal/dep/sub/file.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'dep-sub-cjs';
1 change: 1 addition & 0 deletions test/fixtures/package-map/traversal/dep/sub/file.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'dep-sub';
6 changes: 6 additions & 0 deletions test/fixtures/package-map/traversal/map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"packages": {
"app": { "url": "./app", "dependencies": { "dep": "dep" } },
"dep": { "url": "./dep", "dependencies": {} }
}
}
1 change: 1 addition & 0 deletions test/fixtures/package-map/traversal/secret.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'SECRET-OUTSIDE-CJS';
1 change: 1 addition & 0 deletions test/fixtures/package-map/traversal/secret.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'SECRET-OUTSIDE';
69 changes: 69 additions & 0 deletions test/parallel/test-package-map-traversal.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import '../common/index.mjs';
import { spawnSyncAndAssert, spawnSyncAndExit } from '../common/child_process.js';
import * as fixtures from '../common/fixtures.mjs';

const map = fixtures.path('package-map/traversal/map.json');
const cwd = fixtures.path('package-map/traversal/app');

function runESM(specifier) {
return [
'--no-warnings',
'--experimental-package-map', map,
'--input-type=module',
'--eval', `import x from '${specifier}'; console.log(x);`,
];
}

function runCJS(specifier) {
return [
'--no-warnings',
'--experimental-package-map', map,
'--eval', `console.log(require('${specifier}'));`,
];
}

for (const specifier of [
'dep/../secret.mjs',
'dep/%2e%2e/secret.mjs',
'dep/sub/../../secret.mjs',
]) {
spawnSyncAndExit(process.execPath, runESM(specifier), { cwd }, {
status: 1,
signal: null,
stderr: /ERR_INVALID_MODULE_SPECIFIER/,
});
}

for (const specifier of [
'dep/sub/file.mjs',
'dep/./sub/file.mjs',
'dep/sub/../sub/file.mjs',
]) {
spawnSyncAndAssert(process.execPath, runESM(specifier), { cwd }, {
stdout: /dep-sub/,
trim: true,
});
}

for (const specifier of [
'dep/../secret.cjs',
'dep/%2e%2e/secret.cjs',
'dep/sub/../../secret.cjs',
]) {
spawnSyncAndExit(process.execPath, runCJS(specifier), { cwd }, {
status: 1,
signal: null,
stderr: /ERR_INVALID_MODULE_SPECIFIER/,
});
}

for (const specifier of [
'dep/sub/file.cjs',
'dep/./sub/file.cjs',
'dep/sub/../sub/file.cjs',
]) {
spawnSyncAndAssert(process.execPath, runCJS(specifier), { cwd }, {
stdout: /dep-sub-cjs/,
trim: true,
});
}
Loading