Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(provider-sharing): require consent for index change hooks
MagicLizic660aa178adb
diff --git a/apps/desktop/src/main/remote-agent/executor/__tests__/gitCredentials.git-integration.test.ts b/apps/desktop/src/main/remote-agent/executor/__tests__/gitCredentials.git-integration.test.tsindex 4ef92a18c4..c1921e6e5b 100644--- a/apps/desktop/src/main/remote-agent/executor/__tests__/gitCredentials.git-integration.test.ts+++ b/apps/desktop/src/main/remote-agent/executor/__tests__/gitCredentials.git-integration.test.ts@@ -529,6 +529,75 @@ describe('GitCredentialHistory', () => {expect(fs.existsSync(invokedFile)).toBe(false);});+ it('keeps consent for post-index-change hooks and configured hook directories', async () => {+ vi.stubEnv('GIT_OPTIONAL_LOCKS', undefined);+ const history = new GitCredentialHistory();+ const credentialFile = path.join(root, '.env');+ const hooksDir = path.join(root, 'custom-hooks');+ const hookFile = path.join(hooksDir, 'post-index-change');+ const invokedFile = path.join(root, 'post-index-change-invoked');+ fs.mkdirSync(hooksDir);+ fs.writeFileSync(credentialFile, 'PRIVATE_FIXTURE=post-index-change\n');+ fs.writeFileSync(hookFile, '#!/usr/bin/env node\nconst fs = require("node:fs");\n'+ + 'fs.writeFileSync(' + JSON.stringify(invokedFile) + ', "called");\n'+ + 'process.stderr.write(fs.readFileSync(' + JSON.stringify(credentialFile) + '));\n');+ fs.chmodSync(hookFile, 0o700);+ expect(await provesSafe(history, 'git status')).toBe(true);+ git('config', 'core.hooksPath', hooksDir.split(path.sep).join('/'));+ if (process.platform !== 'win32') {+ const changedTime = new Date(Date.now() + 2_000);+ fs.utimesSync(path.join(repo, 'src/app.ts'), changedTime, changedTime);+ const output = spawnSync('git', ['status', '--porcelain'], { cwd: repo, encoding: 'utf8' });+ expect(output.stdout + output.stderr).toContain('PRIVATE_FIXTURE=post-index-change');+ expect(output.status).toBe(0);+ fs.unlinkSync(invokedFile);+ }+ for (const command of ['git status', 'git status --porcelain', 'git diff --name-only', 'git ls-files',+ 'git diff', 'git diff --cached', 'git show HEAD']) {+ expect(await provesSafe(history, command), command).toBe(false);+ await requiresCredentialConfirmation(history, command);+ expect(fs.existsSync(invokedFile), command).toBe(false);+ }+ for (const value of ['', '/dev/null', path.join(root, 'missing-hooks').split(path.sep).join('/')]) {+ git('config', 'core.hooksPath', value);+ expect(await provesSafe(history, 'git status'), value).toBe(false);+ }+ git('config', '--unset', 'core.hooksPath');+ expect(await provesSafe(history, 'git status')).toBe(true);+ const defaultHook = path.resolve(repo, git('rev-parse', '--git-common-dir').trim(), 'hooks', 'post-index-change');+ fs.renameSync(hookFile, defaultHook);+ for (const command of ['git status', 'git diff --cached', 'git diff --name-only']) {+ expect(await provesSafe(history, command), command).toBe(false);+ await requiresCredentialConfirmation(history, command);+ expect(fs.existsSync(invokedFile), command).toBe(false);+ }+ expect(await provesSafe(history, 'git config --get core.autocrlf')).toBe(true);+ fs.unlinkSync(defaultHook);+ expect(await provesSafe(history, 'git status')).toBe(true);+ expect(fs.existsSync(invokedFile)).toBe(false);+ });++ it('checks shared worktree hooks each time and fails closed on hook inspection errors', async () => {+ const history = new GitCredentialHistory();+ const linked = path.join(root, 'linked-worktree');+ git('worktree', 'add', '--detach', '-q', linked, 'HEAD');+ const reads = shareCredentialGitReads('git status', [linked], { cwd: linked, platform: process.platform });+ expect(reads).not.toBeNull();+ expect(await history.provesSafe('git status', reads ?? [])).toBe(true);+ const defaultHook = path.resolve(repo, git('rev-parse', '--git-common-dir').trim(), 'hooks', 'post-index-change');+ fs.writeFileSync(defaultHook, 'hook-fixture\n');+ expect(await history.provesSafe('git status', reads ?? [])).toBe(false);+ fs.unlinkSync(defaultHook);+ expect(await history.provesSafe('git status', reads ?? [])).toBe(true);+ fs.writeFileSync(defaultHook + '.exe', 'hook-fixture\n');+ expect(await history.provesSafe('git status', reads ?? [])).toBe(false);+ fs.unlinkSync(defaultHook + '.exe');+ expect(await history.provesSafe('git status', reads ?? [])).toBe(true);+ const stats = vi.spyOn(fsPromises, 'lstat').mockRejectedValue(Object.assign(new Error('unavailable'), { code: 'EACCES' }));+ expect(await history.provesSafe('git status', reads ?? [])).toBe(false);+ expect(stats).toHaveBeenCalledWith(defaultHook);+ });+it('uses one budget for all Git segments including configuration reads', async () => {vi.spyOn(fsPromises, 'realpath').mockImplementation(async () => {await new Promise((resolve) => setTimeout(resolve, 75));diff --git a/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts b/apps/desktop/src/main/remote-agent/executor/gitCredentials.tsindex bdc6e0958b..f7df9289a5 100644--- a/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts+++ b/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts@@ -503,7 +503,7 @@ async function diffHelpersCannotRun(read: GitRead, cwd: string, timeoutMs: numbeconst newline = entry.indexOf('\n');const key = newline < 0 ? entry : entry.slice(0, newline);const lowered = key.toLowerCase();- if (lowered === 'core.fsmonitor' || /^gpg(?:\..+)?\.program$/i.test(key)) return true;+ if (lowered === 'core.fsmonitor' || lowered === 'core.hookspath' || /^gpg(?:\..+)?\.program$/i.test(key)) return true;const value = newline < 0 ? '' : entry.slice(newline + 1).trim();if (lowered === 'log.showsignature') return !/^(?:false|no|off|0)$/i.test(value);if (newline < 0 || !value) return false;@@ -608,6 +608,12 @@ export class GitCredentialHistory {if (replacements.trim()) return false;const helperTimeout = deadline - Date.now();if (helperTimeout <= 0 || !(await diffHelpersCannotRun(read, cwd, helperTimeout))) return false;+ const hook = path.join(commonDir, 'hooks', 'post-index-change');+ const hookPresent = await Promise.all([hook, hook + '.exe'].map((file) => fsp.lstat(file).then(+ () => true,+ (error: NodeJS.ErrnoException) => error.code !== 'ENOENT',+ )));+ if (hookPresent.some(Boolean)) return false;if (read.metadataOnly) return true;// 指向仓库外的文件时 git diff 不经仓库、直接比较文件。if (read.sub === 'diff' && read.args.some((arg) => !arg.startsWith('-') && isOutside(top, path.resolve(cwd, arg)))) {diff --git a/docs/product-rules/provider-sharing.md b/docs/product-rules/provider-sharing.mdindex cbaaeda06a..26f1434644 100644--- a/docs/product-rules/provider-sharing.md+++ b/docs/product-rules/provider-sharing.md@@ -335,6 +335,9 @@ Desktop「设置 → 模型供应商」的左栏(2026-10-08 用户裁决):配置中出现 `core.fsmonitor` 时同样保留确认(不假设不同 Git 版本对布尔取值的解释一致),在读取 index 前拦下它,避免 hook 经 stderr 带出凭证。`git status`、`git ls-files`、只列文件名的diff 等静态只读调用也要核实运行期 helper;无 helper 时仍可豁免,不改变本机任务默认判定;+ 配置中出现 `core.hooksPath` 时同样保留确认;未配置时,每次核实仓库公共 Git 目录里的默认+ `post-index-change` hook(含 Windows 可执行文件后缀)不存在。发现 hook 或无法核实文件状态+ 都不授予内容与静态只读豁免,避免 `git status` 刷新 index 时执行 hook、经 stderr 带出凭证;签名验证也可能启动外部程序:配置了 `gpg.program` / `gpg.<format>.program`、启用了`log.showSignature`、命令请求 `--show-signature` 或 `%G` 验证格式、配置中的 pretty 格式或别名含 `%G` 时均保留确认,不假设验证器不会读取或输出凭证;历史与文件列表探针始终禁用签名展示;