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

+1
View File
@@ -8,6 +8,7 @@
- AI操作记录只追加SQLite,记录发起人、上下文、模型结果、操作、Git写文件/提交/测试/恢复;按账号授权且脱敏,不保存内部思考。审计不可用时阻止启动模型/操作,重启运行中结果标为待核验。
- 管理员显式选择已跟踪源码文件生成修复建议,页面展示前后内容;执行前复核权限、仓库及baseline commit。只在独立Git工作区/codex/ai分支写入,成功待审查;固定测试失败恢复基线,保留证据提交。超时/未确认退出/元数据篡改保留锁与needs_review,不冒称自动回退。Git工作区不是OS沙箱,测试以服务账号运行,UI明确提示;不自动合并/部署、不恢复数据库、不办理BPM、不重发unknown。
- 验证:235项当前业务回归通过;最新应用启动/关闭、操作审计翻页、UI修复审查和Git恢复专项37项通过。DSH实际官方运行包已通过本机假模型验证,无真实外部模型调用。补充旧web-closure-ui的5项失败在39afafe基线完全复现(旧字段编辑器等断言/夹具),未计作通过;本次DSH路由单独验收。
- 真实共享仓库补验:Git隔离环境不继承全局safe.directory,因此按管理员选定的原生规范路径仅为当次Git命令增加精确信任项,兼容Z盘映射/UNC及Windows大小写;不修改全局Git配置。对应Git修复/权限/审计26项专项通过,真实仓库读取验证通过。
- 用户批准的两部分设计与计划见同日mail-dsh-controlled-optimization文档。原重复巡检保持删除,五天受限测试及订单数据不因助手接入改变。
## 2026-10-27 PDF导出记录接入自动归档
+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){
+1
View File
@@ -38,3 +38,4 @@ test('source requiring redaction is rejected instead of returned as altered comp
test('journal cursor pages older actor-scoped history without overlaps',t=>{const f=fixture(t);for(let i=0;i<125;i++){f.journal.append({operationId:'alice-operation',actor:'alice',step:'event',status:'done'});f.journal.append({operationId:'bob-operation',actor:'bob',step:'event',status:'done'});}const first=f.journal.list({actor:'alice'});assert.equal(first.length,100);const beforeId=first.at(-1).id,second=f.journal.list({actor:'alice',operationId:'alice-operation',beforeId});assert.equal(second.length,25);assert.equal(new Set([...first,...second].map(row=>row.id)).size,125);assert.ok(second.every(row=>row.id<beforeId&&row.actor==='alice'));assert.deepEqual(f.journal.list({actor:'alice',operationId:'bob-operation',beforeId}),[]);for(const cursor of [0,-1,1.5,'2',null,NaN,Number.MAX_SAFE_INTEGER+1])assert.throws(()=>f.journal.list({actor:'alice',beforeId:cursor}),/cursor/i);});
test('repair journal names file changes, Git commit, validation and review steps',async t=>{const f=fixture(t),r=await f.runner.run(request),steps=f.journal.list({actor:request.actor}).filter(e=>e.operationId===r.operationId).map(e=>e.step);for(const step of ['prepare','files_written','commit','validate','review'])assert(steps.includes(step),step);});
test('long validation logs retain the final test summary in the audit record',async t=>{const f=fixture(t,"console.log('x'.repeat(24000));console.log('FINAL_TEST_SUMMARY passed')"),r=await f.runner.run(request),last=f.journal.list({actor:request.actor}).find(e=>e.operationId===r.operationId&&e.step==='review');assert.match(last.detail,/FINAL_TEST_SUMMARY passed/);});
test('explicit administrator-selected repository works without global safe-directory configuration',t=>{const f=fixture(t);f.runner.env.GIT_TEST_ASSUME_DIFFERENT_OWNER='1';const read=f.runner.readSources(['src/app.cjs']);assert.equal(read.files[0].content,'original');});