fix: scope AI Git trust to the selected shared repository

This commit is contained in:
abaishu0730 committed 2026-10-27 11:01:59 +08:00
1 parent e2b3e66a5b
commit aebba31363
3 files changed
+4 -2

No files matched your search

+2 -2
View File
@@ -6,8 +6,8 @@ function noLinks(target){let cursor=path.resolve(target);while(true){try{if(fs.l
function safeEnvironment(){const env={};for(const name of ['SystemRoot','WINDIR','COMSPEC','PATH','PATHEXT','TEMP','TMP','LANG','LC_ALL'])if(process.env[name])env[name]=process.env[name];env.GIT_CONFIG_NOSYSTEM='1';env.GIT_CONFIG_GLOBAL=process.platform==='win32'?'NUL':'/dev/null';env.GIT_TERMINAL_PROMPT='0';return env;}
function validateFiles(files,roots=['src','web','tests','docs']){if(!Array.isArray(files)||files.length<1||files.length>20)throw fail('Provide 1 to 20 files');let bytes=0;const seen=new Set();return files.map(file=>{if(!file||Object.keys(file).some(key=>!['path','content'].includes(key))||typeof file.path!=='string'||typeof file.content!=='string')throw fail('Invalid proposed file');const name=file.path,parts=name.split('/');if(name.length>240||!roots.includes(parts[0])||parts.length<2||parts.some(p=>!p||p==='.'||p==='..'||p.startsWith('.')||/[\\:\x00-\x1f<>"|?*]/.test(p)||/[. ]$/.test(p)||/^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(p)||/^(node_modules|output|outputs|dist|build|coverage|credentials?|secrets?|package(?:-lock)?\.json|npm-shrinkwrap\.json)$/i.test(p))||! /\.(?:cjs|mjs|js|css|html|json|md|txt)$/i.test(name)||/(?:^|[/_.-])(?:credentials?|secrets?|tokens?|passwords?|private[-_]?keys?)(?:[/_.-]|$)/i.test(name))throw fail('Path is outside repair allowlist');if(file.content.includes('\0'))throw fail('Binary content is forbidden');bytes+=Buffer.byteLength(file.content);if(bytes>512*1024||seen.has(name.toLowerCase()))throw fail('Proposal too large or duplicate path');seen.add(name.toLowerCase());return {path:name,content:file.content};});}
class CodeRepairRunner{
constructor({repoRoot,repairRoot,journal,validator,allowedRoots=['src','web','tests','docs'],gitCommand='git'}){if(!path.isAbsolute(repoRoot)||!path.isAbsolute(repairRoot)||!journal||!validator||!path.isAbsolute(validator.command)||!Array.isArray(validator.args)||validator.args.some(arg=>typeof arg!=='string'))throw fail('Trusted absolute repair configuration required');noLinks(repoRoot);noLinks(repairRoot);this.repoRoot=fs.realpathSync(repoRoot);fs.mkdirSync(repairRoot,{recursive:true});this.repairRoot=fs.realpathSync(repairRoot);if(inside(this.repoRoot,this.repairRoot)||inside(this.repairRoot,this.repoRoot))throw fail('Repair root must be separate from active repository');if(allowedRoots.some(root=>!['src','web','tests','docs'].includes(root)))throw fail('Invalid allowlist');this.roots=[...allowedRoots];this.journal=journal;this.validator={command:validator.command,args:[...validator.args],timeoutMs:Math.min(300000,Math.max(100,validator.timeoutMs||60000))};this.gitCommand=gitCommand;this.env=safeEnvironment();if(validator.env){if(Object.keys(validator.env).some(key=>key!=='NODE_PATH')||typeof validator.env.NODE_PATH!=='string'||!path.isAbsolute(validator.env.NODE_PATH))throw fail('Only trusted absolute NODE_PATH may be supplied');this.validator.env={NODE_PATH:validator.env.NODE_PATH};}this.lockPath=path.join(this.repairRoot,'.runner-lock');this.hooks=path.join(this.repairRoot,'.disabled-hooks');fs.mkdirSync(this.hooks,{recursive:true});noLinks(this.hooks);}
git(cwd,args){return execFileSync(this.gitCommand,['-c','core.hooksPath='+this.hooks,'-c','core.fsmonitor=false','-c','commit.gpgSign=false','-c','user.name=AI Repair','-c','user.email=ai-repair@localhost.invalid',...args],{cwd,env:this.env,encoding:'utf8',timeout:30000,maxBuffer:1024*1024,windowsHide:true,stdio:['ignore','pipe','pipe']}).trim();}
constructor({repoRoot,repairRoot,journal,validator,allowedRoots=['src','web','tests','docs'],gitCommand='git'}){if(!path.isAbsolute(repoRoot)||!path.isAbsolute(repairRoot)||!journal||!validator||!path.isAbsolute(validator.command)||!Array.isArray(validator.args)||validator.args.some(arg=>typeof arg!=='string'))throw fail('Trusted absolute repair configuration required');noLinks(repoRoot);noLinks(repairRoot);this.repoRoot=fs.realpathSync.native(repoRoot);fs.mkdirSync(repairRoot,{recursive:true});this.repairRoot=fs.realpathSync.native(repairRoot);if(inside(this.repoRoot,this.repairRoot)||inside(this.repairRoot,this.repoRoot))throw fail('Repair root must be separate from active repository');if(allowedRoots.some(root=>!['src','web','tests','docs'].includes(root)))throw fail('Invalid allowlist');this.roots=[...allowedRoots];this.journal=journal;this.validator={command:validator.command,args:[...validator.args],timeoutMs:Math.min(300000,Math.max(100,validator.timeoutMs||60000))};this.gitCommand=gitCommand;this.env=safeEnvironment();if(validator.env){if(Object.keys(validator.env).some(key=>key!=='NODE_PATH')||typeof validator.env.NODE_PATH!=='string'||!path.isAbsolute(validator.env.NODE_PATH))throw fail('Only trusted absolute NODE_PATH may be supplied');this.validator.env={NODE_PATH:validator.env.NODE_PATH};}this.lockPath=path.join(this.repairRoot,'.runner-lock');this.hooks=path.join(this.repairRoot,'.disabled-hooks');fs.mkdirSync(this.hooks,{recursive:true});noLinks(this.hooks);}
git(cwd,args){const selected=fs.realpathSync.native(cwd);if(path.relative(this.repoRoot,selected)!==''&&!inside(this.repairRoot,selected))throw fail('Git directory is outside configured repair scope');const gitPath=value=>{const normalized=value.replaceAll('\\','/');return normalized.startsWith('//')?'%(prefix)/'+normalized:normalized;};return execFileSync(this.gitCommand,['-c','safe.directory='+gitPath(this.repoRoot),'-c','safe.directory='+gitPath(selected),'-c','core.hooksPath='+this.hooks,'-c','core.fsmonitor=false','-c','commit.gpgSign=false','-c','user.name=AI Repair','-c','user.email=ai-repair@localhost.invalid',...args],{cwd,env:this.env,encoding:'utf8',timeout:30000,maxBuffer:1024*1024,windowsHide:true,stdio:['ignore','pipe','pipe']}).trim();}
async stopValidator(child){
const method=process.platform==='win32'?'taskkill-tree':'process-group';let commandSucceeded=false;
if(Number.isInteger(child.pid)&&child.pid>0){