fix: protect resolved project paths and Windows aliases

This commit is contained in:
Agent Workbench Dev
2026-10-30 23:01:18 +08:00
parent ca9042e069
commit c1e9575259
2 changed files with 41 additions and 2 deletions
+10 -2
View File
@@ -9,9 +9,14 @@ import { assertMcpResolved } from '../core/tools/mcp-contract.mjs';
export const hash = bytes => createHash('sha256').update(bytes).digest('hex');
const ignored = new Set(['node_modules', '.git', '.next', '.data', 'bin', 'obj', 'dist', 'dist-client', 'dist-server', '__pycache__', '.venv', 'venv', '.tox', '.pytest_cache', '.mypy_cache', '.ruff_cache', 'htmlcov']);
const secretName = /(^|\/)(\.env($|\.)|id_rsa($|\.)|id_ed25519($|\.)|credentials($|\.)|\.ssh($|\/))/i;
const secretName = /(^|\/)(\.env($|[./])|id_rsa($|[./])|id_ed25519($|[./])|credentials($|[./])|\.ssh($|\/)|\.secrets($|\/))/i;
const normalize = value => process.platform === 'win32' ? value.toLowerCase() : value;
const inside = (root, target) => normalize(target) === normalize(root) || normalize(target).startsWith(normalize(root + path.sep));
const restricted = relative => {
const portable = relative.replaceAll('\\', '/');
return secretName.test(portable) || portable.split('/').some(part => part.toLowerCase() === '.git'
|| process.platform === 'win32' && part !== '.' && /[. ]$/.test(part));
};
export function excludedFromInventory(relative) {
const portable = relative.replaceAll('\\', '/');
@@ -22,7 +27,7 @@ export function safePath(project, relative = '', allowMissing = false) {
assertWorkspacePath(project);
if (typeof relative !== 'string' || relative.includes('\0') || relative.includes(':') || path.isAbsolute(relative) || path.win32.isAbsolute(relative)) throw new Error('路径必须相对于项目文件夹。');
const portable = relative.replaceAll('\\', '/');
if (portable.split('/').includes('..') || secretName.test(portable) || portable.split('/').some(part => part.toLowerCase() === '.git')) throw new Error('该路径不在可读取的项目范围内。');
if (portable.split('/').includes('..') || restricted(portable)) throw new Error('该路径不在可读取的项目范围内。');
const root = fs.realpathSync.native(project.root);
if (normalize(root) !== normalize(project.root)) throw new Error('项目位置已变化,请重新打开项目。');
const target = path.resolve(root, portable || '.');
@@ -38,6 +43,9 @@ export function safePath(project, relative = '', allowMissing = false) {
}
const real = fs.realpathSync.native(existing);
if (!inside(root, real)) throw new Error('链接指向项目以外的位置。');
// A project-local link or Windows alias must not turn a public path into
// access to protected Git metadata or credentials, including new files.
if (restricted(path.relative(root, real))) throw new Error('实际路径指向受保护的项目目录或凭据。');
return target;
}
+31
View File
@@ -70,3 +70,34 @@ test('read and search expose bounded text scopes and reject non UTF-8 / oversize
for (const file of ['binary.bin', 'invalid.txt', 'large.txt']) assert.throws(() => readText(project, file));
assert.equal(hash(Buffer.from('abc')), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
});
test('project-local junctions cannot expose or create protected Git and credential files', t => {
const { store, session, project, projectRoot } = fixture(t);
for (const name of ['.git', 'credentials', '.secrets']) {
fs.mkdirSync(path.join(projectRoot, name));
fs.writeFileSync(path.join(projectRoot, name, 'config.txt'), 'protected fixture bytes');
const alias = name.replaceAll('.', '') + '-alias';
fs.symlinkSync(path.join(projectRoot, name), path.join(projectRoot, alias), process.platform === 'win32' ? 'junction' : 'dir');
for (const relative of [name + '/config.txt', alias + '/config.txt']) {
assert.throws(() => readText(project, relative), relative);
assert.throws(() => listFiles(project, relative.split('/')[0]), relative);
}
assert.throws(() => proposeFile(store, session, { path: alias + '/new.txt', content: 'forbidden' }));
assert.equal(fs.existsSync(path.join(projectRoot, name, 'new.txt')), false);
assert.equal(fs.readFileSync(path.join(projectRoot, name, 'config.txt'), 'utf8'), 'protected fixture bytes');
}
assert.equal(session.changes.length, 0);
assert.ok(!listFiles(project).files.some(item => /alias|credentials|secrets|\.git/.test(item.path)));
fs.mkdirSync(path.join(projectRoot, 'public-docs'));
fs.writeFileSync(path.join(projectRoot, 'public-docs', 'info.txt'), 'public fixture');
fs.symlinkSync(path.join(projectRoot, 'public-docs'), path.join(projectRoot, 'docs-alias'), process.platform === 'win32' ? 'junction' : 'dir');
assert.equal(readText(project, 'docs-alias/info.txt').content, 'public fixture');
});
test('Windows trailing-dot and trailing-space path aliases cannot bypass protected paths', { skip: process.platform !== 'win32' }, t => {
const { project } = fixture(t);
assert.equal(safePath(project, '.'), project.root);
for (const relative of ['.git./config', '.git /config', '.env ', 'credentials /config', '.secrets./new.txt']) {
assert.throws(() => safePath(project, relative, true), relative);
}
});