fix(security): add path validation to git tool handler

handleGit was missing checkPathAllowed validation on the working
directory (cwd), allowing git operations to execute in any directory
bypassing the security policy. Added validation for:
- The cwd used by all git operations
- The clone destination directory (write permission check)
This commit is contained in:
thzxx
2026-04-24 13:58:19 +08:00
parent 15b4895351
commit dbeba20286
+6
View File
@@ -12,6 +12,10 @@ export async function handleGit(params: { action: string; path?: string; files?:
try {
const cwd = params.path ? path.resolve(params.path) : getWorkspaceDir();
// 安全检查:验证工作目录路径
const dirCheck = checkPathAllowed(cwd, 'read');
if (!dirCheck.ok) return { success: false, error: dirCheck.reason };
async function runGit(args: string[]): Promise<{ stdout: string; stderr: string; code: number }> {
return new Promise((resolve) => {
const git = spawn('git', args, { cwd, stdio: ['pipe', 'pipe', 'pipe'], env: { ...process.env, LANG: 'en_US.UTF-8' } });
@@ -180,6 +184,8 @@ export async function handleGit(params: { action: string; path?: string; files?:
case 'clone': {
if (!params.url) return { success: false, error: '请提供仓库 URL' };
const destDir = params.path ? path.resolve(params.path) : getWorkspaceDir();
const cloneDirCheck = checkPathAllowed(destDir, 'write');
if (!cloneDirCheck.ok) return { success: false, error: cloneDirCheck.reason };
const r = await runGit(['clone', params.url, destDir]);
if (r.code !== 0) return { success: false, error: r.stderr };
return { success: true, action: 'clone', url: params.url, destination: destDir };