Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(provider-sharing): reject submodule reads and untrusted shells

MagicLizi1b9f00fa72ed
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.ts
index c1921e6e5b..926410970a 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
@@ -74,6 +74,7 @@ const savedEnv = Object.fromEntries(GIT_ENV_KEYS.map((key) => [key, process.env[
beforeEach(() => {
root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'share-git-credentials-')));
vi.stubEnv('SHELL', '/bin/bash');
+ vi.stubEnv('CLAUDE_CODE_GIT_BASH_PATH', undefined);
vi.stubEnv('RIPGREP_CONFIG_PATH', undefined);
vi.stubEnv('PATH', (process.env.PATH ?? '').split(path.delimiter).map((entry) => path.resolve(entry)).join(path.delimiter));
// 临时目录的上层也可能是 git 仓库(比如整个用户目录):不往上找。开发机自己的全局与系统 git 配置
@@ -529,6 +530,54 @@ describe('GitCredentialHistory', () => {
expect(fs.existsSync(invokedFile)).toBe(false);
});
+ it.each(['absorbed', 'embedded'])('keeps consent when status enters an %s submodule with a helper', async (layout) => {
+ const history = new GitCredentialHistory();
+ expect(await provesSafe(history, 'git status')).toBe(true);
+ const child = path.join(repo, 'child');
+ if (layout === 'absorbed') {
+ git('-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', repo, 'child');
+ } else {
+ git('clone', '--no-hardlinks', '-q', repo, child);
+ git('config', '-f', '.gitmodules', 'submodule.child.path', 'child');
+ git('config', '-f', '.gitmodules', 'submodule.child.url', repo.split(path.sep).join('/'));
+ git('add', '.gitmodules', 'child');
+ }
+ git('commit', '-q', '--no-gpg-sign', '-m', 'submodule fixture');
+ expect(fs.existsSync(path.join(repo, '.git', 'modules'))).toBe(layout === 'absorbed');
+ expect(git('ls-files', '--stage', 'child')).toMatch(/^160000 /);
+ const credentialFile = path.join(root, '.env');
+ const hookFile = path.join(root, 'submodule-fsmonitor.cjs');
+ const invokedFile = path.join(root, 'submodule-helper-invoked');
+ fs.writeFileSync(credentialFile, 'PRIVATE_FIXTURE=submodule-helper\n');
+ fs.writeFileSync(hookFile, 'const fs = require("node:fs");\n'
+ + 'fs.writeFileSync(' + JSON.stringify(invokedFile) + ', "called");\n'
+ + 'process.stderr.write(fs.readFileSync(' + JSON.stringify(credentialFile) + '));\n'
+ + 'process.stdout.write("fixture-token\\0");\n');
+ const hook = [process.execPath, hookFile].map((file) => JSON.stringify(file.split(path.sep).join('/'))).join(' ');
+ git('-C', child, 'config', 'core.fsmonitor', hook);
+ const output = spawnSync('git', ['status'], { cwd: repo, encoding: 'utf8' });
+ expect(output.stdout + output.stderr).toContain('PRIVATE_FIXTURE=submodule-helper');
+ 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 --cached']) {
+ expect(await provesSafe(history, command), command).toBe(false);
+ await requiresCredentialConfirmation(history, command);
+ expect(fs.existsSync(invokedFile), command).toBe(false);
+ }
+ });
+
+ it('fails closed when the submodule directory cannot be inspected', async () => {
+ const history = new GitCredentialHistory();
+ expect(await provesSafe(history, 'git status')).toBe(true);
+ const modules = path.join(repo, '.git', 'modules');
+ const lstat = fsPromises.lstat;
+ const stats = vi.spyOn(fsPromises, 'lstat').mockImplementation((file) => file === modules
+ ? Promise.reject(Object.assign(new Error('unavailable'), { code: 'EACCES' })) : lstat(file));
+ expect(await provesSafe(history, 'git status')).toBe(false);
+ expect(stats).toHaveBeenCalledWith(modules);
+ await requiresCredentialConfirmation(history, 'git status');
+ });
+
it('keeps consent for post-index-change hooks and configured hook directories', async () => {
vi.stubEnv('GIT_OPTIONAL_LOCKS', undefined);
const history = new GitCredentialHistory();
@@ -932,6 +981,43 @@ describe('GitCredentialHistory', () => {
expect(await provesSafe(history, 'git diff')).toBe(true);
});
+ it('keeps consent for custom Bash and other unverified shell executables', async () => {
+ const history = new GitCredentialHistory();
+ const shellFile = path.join(root, process.platform === 'win32' ? 'bash.exe' : 'bash');
+ const credentialFile = path.join(root, '.env');
+ const invokedFile = path.join(root, 'custom-shell-invoked');
+ fs.writeFileSync(credentialFile, 'PRIVATE_FIXTURE=custom-shell\n');
+ fs.writeFileSync(shellFile, '#!/bin/sh\nprintf called > ' + JSON.stringify(invokedFile.split(path.sep).join('/'))
+ + '\ncat ' + JSON.stringify(credentialFile.split(path.sep).join('/')) + '\n');
+ fs.chmodSync(shellFile, 0o700);
+ expect(await provesSafe(history, 'git diff')).toBe(true);
+ vi.stubEnv(process.platform === 'win32' ? 'CLAUDE_CODE_GIT_BASH_PATH' : 'SHELL', shellFile);
+ const selected = executorShell.resolveExecutorShell();
+ expect(selected.file).toBe(shellFile);
+ if (process.platform !== 'win32') {
+ const output = spawnSync(selected.file, ['-c', 'git diff'], { cwd: repo, encoding: 'utf8' });
+ expect(output.stdout + output.stderr).toContain('PRIVATE_FIXTURE=custom-shell');
+ expect(output.status).toBe(0);
+ fs.unlinkSync(invokedFile);
+ }
+ for (const command of ['git diff', 'git status', 'git config --list']) {
+ expect(await provesSafe(history, command), command).toBe(false);
+ await requiresCredentialConfirmation(history, command);
+ expect(fs.existsSync(invokedFile), command).toBe(false);
+ }
+ vi.stubEnv('SHELL', '/bin/bash');
+ vi.stubEnv('CLAUDE_CODE_GIT_BASH_PATH', undefined);
+ expect(await provesSafe(history, 'git diff')).toBe(true);
+ const shell = vi.spyOn(executorShell, 'resolveExecutorShell');
+ for (const name of ['bash', 'bash.exe', 'cmd.exe']) {
+ shell.mockReturnValue({ file: path.join(root, 'custom', name), dialect: name === 'cmd.exe' ? 'cmd' : 'bash',
+ scriptName: 'script.sh', args: ['script.sh'] });
+ expect(await provesSafe(history, 'git diff'), name).toBe(false);
+ }
+ shell.mockRestore();
+ expect(await provesSafe(history, 'git diff')).toBe(true);
+ });
+
it('keeps confirmation for zsh default and compiled startup files without ZDOTDIR', async () => {
const history = new GitCredentialHistory();
const home = path.join(root, 'home');
diff --git a/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts b/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts
index f7df9289a5..e0803a1672 100644
--- a/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts
+++ b/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts
@@ -516,7 +516,24 @@ async function diffHelpersCannotRun(read: GitRead, cwd: string, timeoutMs: numbe
async function shellStartupAllowsGitProbe(waitMs: number): Promise<boolean> {
const shell = resolveExecutorShell();
- if (path.basename(shell.file) !== 'zsh') return true;
+ if (shell.dialect !== 'bash') return false;
+ if (['/bin/bash', '/usr/bin/bash'].includes(shell.file)) return true;
+ if (path.basename(shell.file) !== 'zsh') {
+ if (process.platform !== 'win32' || process.env.CLAUDE_CODE_GIT_BASH_PATH !== undefined) return false;
+ const normalized = (file: string) => path.win32.normalize(file).replace(/[\\/]$/, '').toLowerCase();
+ const roots = [process.env.ProgramFiles, process.env['ProgramFiles(x86)']].filter((root): root is string => {
+ if (!root || !/^[a-z]:\\/i.test(path.win32.normalize(root))) return false;
+ const drive = path.win32.parse(root).root;
+ return ['Program Files', 'Program Files (x86)'].some((directory) => normalized(root) === normalized(path.win32.join(drive, directory)));
+ });
+ const trusted = roots.flatMap((root) => [
+ path.win32.join(root, 'Git', 'bin', 'bash.exe'),
+ path.win32.join(root, 'Git', 'usr', 'bin', 'bash.exe'),
+ ]).map(normalized);
+ if (!trusted.includes(normalized(shell.file))) return false;
+ const actual = await fsp.realpath(shell.file).catch(() => undefined);
+ return actual !== undefined && normalized(actual) === normalized(shell.file);
+ }
if (!['/bin/zsh', '/usr/bin/zsh'].includes(shell.file)) return false;
const home = process.env.HOME;
if (!home || !path.isAbsolute(home)) return false;
@@ -614,16 +631,20 @@ export class GitCredentialHistory {
(error: NodeJS.ErrnoException) => error.code !== 'ENOENT',
)));
if (hookPresent.some(Boolean)) return false;
- if (read.metadataOnly) return true;
+ const modulesPresent = await fsp.lstat(path.join(commonDir, 'modules')).then(
+ () => true,
+ (error: NodeJS.ErrnoException) => error.code !== 'ENOENT',
+ );
+ if (modulesPresent) return false;
// 指向仓库外的文件时 git diff 不经仓库、直接比较文件。
if (read.sub === 'diff' && read.args.some((arg) => !arg.startsWith('-') && isOutside(top, path.resolve(cwd, arg)))) {
return false;
}
- if (await exists(path.join(commonDir, 'modules'))) return false;
const indexTimeout = deadline - Date.now();
if (indexTimeout <= 0 || signal?.aborted) return false;
const index = await indexCredentials(top, indexTimeout);
if (!index) return false;
+ if (read.metadataOnly) return true;
const history = await this.waitForHistory(top, commonDir, deadline, signal);
if (history) {
const credentials = new Set([...history.credentials, ...index]);
diff --git a/docs/product-rules/provider-sharing.md b/docs/product-rules/provider-sharing.md
index 26f1434644..16b73afd58 100644
--- a/docs/product-rules/provider-sharing.md
+++ b/docs/product-rules/provider-sharing.md
@@ -326,6 +326,9 @@ Desktop「设置 → 模型供应商」的左栏(2026-10-08 用户裁决):
环境变量的赋值、移除或清空(含 PATH、加载器与 Git 配置来源)都无法与探针证明一致,照旧弹卡;
宿主继承了 `BASH_ENV` / `ENV` / `ZDOTDIR` 或导出的 Bash 函数时,同样无法证明 shell 启动后仍调用
探针使用的 Git,不授予内容与配置读取豁免;
+ shell 可执行文件本身也要核实:Bash 仅接受固定系统路径的二进制,以及 Windows 标准 Program Files
+ 安装目录里的 Git Bash(核实实际路径没有重定向)。自定义 Bash、Windows 显式 `CLAUDE_CODE_GIT_BASH_PATH`、
+ 未知安装目录或其他 shell 无法与探针证明一致,保留内容、配置和静态只读调用的凭证确认;
实际执行器选择 zsh 时,即使没有 `ZDOTDIR`,也要核实系统级 `zshenv` 与 `HOME/.zshenv`(含各自
`.zwc` 缓存)均不存在;发现文件、核实文件状态失败或超时、HOME 不明确、或自定义 zsh 的启动目录
无法核实,都保留确认。此核实每次执行前重做,不读取或运行启动脚本,不沿用仓库历史的缓存;
@@ -335,6 +338,9 @@ Desktop「设置 → 模型供应商」的左栏(2026-10-08 用户裁决):
配置中出现 `core.fsmonitor` 时同样保留确认(不假设不同 Git 版本对布尔取值的解释一致),
在读取 index 前拦下它,避免 hook 经 stderr 带出凭证。`git status`、`git ls-files`、只列文件名的
diff 等静态只读调用也要核实运行期 helper;无 helper 时仍可豁免,不改变本机任务默认判定;
+ 静态只读调用同样不能略过子模块检查:公共 Git 目录存在 `modules`、index 包含 gitlink,
+ 或核实目录状态失败时保留确认;即使旧式子模块把自己的 `.git` 留在工作目录里也不豁免,
+ 避免 `git status` 进入子模块后执行其独立配置的 helper 或 hook;
配置中出现 `core.hooksPath` 时同样保留确认;未配置时,每次核实仓库公共 Git 目录里的默认
`post-index-change` hook(含 Windows 可执行文件后缀)不存在。发现 hook 或无法核实文件状态
都不授予内容与静态只读豁免,避免 `git status` 刷新 index 时执行 hook、经 stderr 带出凭证;