mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-23 00:05:27 +00:00
When npm hoists ajv beside ecc-universal, path.relative(repoRoot, ...) recorded sources like ../ajv/..., which doctor flags as unsafe-repair-source and repair refuses. Record dependency sources as node_modules/<name>[/node_modules/<name>...]/<file>, mirroring the installed layout, and resolve each package from the repository with Node module resolution at plan and repair time. Paths still reject traversal, absolute paths, and malformed package names, and resolved files must stay inside their package root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.5 KiB
JavaScript
65 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('path');
|
|
const { assertWithinTrustedRoot } = require('../path-safety');
|
|
|
|
// npm may hoist a dependency beside the ECC package instead of nesting it, so
|
|
// a path relative to the repository would need `..`. Dependency sources are
|
|
// recorded as `node_modules/<name>[/node_modules/<name>...]/<file>` instead,
|
|
// mirroring the installed layout, and Node resolves each package from the
|
|
// repository when the source is read.
|
|
const PACKAGE_DIR = 'node_modules';
|
|
const PACKAGE_NAME = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/;
|
|
|
|
function isPackageSourcePath(sourceRelativePath) {
|
|
return typeof sourceRelativePath === 'string'
|
|
&& sourceRelativePath.split(/[/\\]+/)[0] === PACKAGE_DIR;
|
|
}
|
|
|
|
function formatPackageSourcePath(packageChain, relativeFile = '') {
|
|
const segments = packageChain.flatMap(name => [PACKAGE_DIR, ...name.split('/')]);
|
|
return path.posix.join(...segments, ...relativeFile.split(/[/\\]+/).filter(Boolean));
|
|
}
|
|
|
|
function parsePackageSourcePath(sourceRelativePath) {
|
|
const parts = String(sourceRelativePath).split(/[/\\]+/);
|
|
const packageChain = [];
|
|
let index = 0;
|
|
while (parts[index] === PACKAGE_DIR) {
|
|
const scoped = String(parts[index + 1] || '').startsWith('@');
|
|
const name = scoped ? `${parts[index + 1]}/${parts[index + 2] || ''}` : parts[index + 1];
|
|
if (!PACKAGE_NAME.test(String(name || ''))) {
|
|
throw new Error(`Invalid package source path: ${sourceRelativePath}`);
|
|
}
|
|
packageChain.push(name);
|
|
index += scoped ? 3 : 2;
|
|
}
|
|
const relativeFile = parts.slice(index);
|
|
if (packageChain.length === 0 || relativeFile.some(part => part === '' || part === '.' || part === '..')) {
|
|
throw new Error(`Invalid package source path: ${sourceRelativePath}`);
|
|
}
|
|
return { packageChain, relativeFile };
|
|
}
|
|
|
|
// Returns null when a package in the chain cannot be resolved. Throws when the
|
|
// path itself is malformed.
|
|
function resolvePackageSourcePath(repoRoot, sourceRelativePath) {
|
|
const { packageChain, relativeFile } = parsePackageSourcePath(sourceRelativePath);
|
|
let packageRoot = repoRoot;
|
|
for (const name of packageChain) {
|
|
try {
|
|
packageRoot = path.dirname(require.resolve(`${name}/package.json`, { paths: [packageRoot] }));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
return assertWithinTrustedRoot(path.join(packageRoot, ...relativeFile), packageRoot, 'read package source');
|
|
}
|
|
|
|
module.exports = {
|
|
formatPackageSourcePath,
|
|
isPackageSourcePath,
|
|
parsePackageSourcePath,
|
|
resolvePackageSourcePath,
|
|
};
|