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 unverified Git probe runtimes

MagicLizi861417548cb5
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 cc0f3df707..66ec7220c4 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
@@ -10,7 +10,10 @@ import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { shareCredentialGitReads } from '../../credentials';
+import { RemoteExecutor } from '../executor';
+import { ExecutorGate, type ExecutorAction } from '../gate';
import { GitCredentialHistory } from '../gitCredentials';
+import { ExecutorWorkspace } from '../workspace';
// Windows 上每次 spawn git 要数百毫秒。
vi.setConfig({ testTimeout: process.platform === 'win32' ? 120_000 : 60_000 });
@@ -40,11 +43,35 @@ async function provesSafe(history: GitCredentialHistory, command: string, cwd =
return history.provesSafe(command, reads ?? []);
}
+async function requiresCredentialConfirmation(history: GitCredentialHistory, command: string): Promise<void> {
+ const workspace = new ExecutorWorkspace({ workingDir: repo });
+ const gate = new ExecutorGate(workspace, 'full', Date.now, { confirmCredentials: true });
+ const confirm = vi.fn(async () => false);
+ const executor = new RemoteExecutor({
+ workspace,
+ gate,
+ rgPath: 'rg',
+ tempDir: path.join(root, 'executor'),
+ confirm,
+ gitCredentials: history,
+ });
+ const action: ExecutorAction = { kind: 'exec', command, cwd: repo };
+ try {
+ expect(gate.authorize(action).ok, command).toBe(false);
+ expect(await executor.confirm(action), command).toBe(false);
+ expect(confirm, command).toHaveBeenCalledExactlyOnceWith(action);
+ expect(gate.authorize(action).ok, command).toBe(false);
+ } finally {
+ await executor.close();
+ }
+}
+
const GIT_ENV_KEYS = ['GIT_CEILING_DIRECTORIES', 'GIT_CONFIG_GLOBAL', 'GIT_CONFIG_NOSYSTEM'] as const;
const savedEnv = Object.fromEntries(GIT_ENV_KEYS.map((key) => [key, process.env[key]]));
beforeEach(() => {
root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'share-git-credentials-')));
+ vi.stubEnv('PATH', (process.env.PATH ?? '').split(path.delimiter).map((entry) => path.resolve(entry)).join(path.delimiter));
// 临时目录的上层也可能是 git 仓库(比如整个用户目录):不往上找。开发机自己的全局与系统 git 配置
// 也不读,免得里面的令牌影响结果。
process.env.GIT_CEILING_DIRECTORIES = root;
@@ -61,6 +88,7 @@ beforeEach(() => {
});
afterEach(() => {
+ vi.unstubAllEnvs();
for (const key of GIT_ENV_KEYS) {
if (savedEnv[key] === undefined) delete process.env[key];
else process.env[key] = savedEnv[key];
@@ -273,6 +301,79 @@ describe('GitCredentialHistory', () => {
expect(Date.now() - started).toBeLessThan(2_000);
});
+ it('keeps confirmation when env removes variables or clears the environment', async () => {
+ const history = new GitCredentialHistory();
+ const home = path.join(root, 'home');
+ fs.mkdirSync(home);
+ fs.writeFileSync(path.join(home, '.gitconfig'), '[http]\n\textraHeader = global-runtime-fixture\n');
+ const systemConfig = path.join(root, 'system-gitconfig');
+ fs.writeFileSync(systemConfig, '[http]\n\textraHeader = system-runtime-fixture\n');
+ vi.stubEnv('HOME', home);
+ vi.stubEnv('USERPROFILE', home);
+ vi.stubEnv('XDG_CONFIG_HOME', path.join(root, 'xdg'));
+ vi.stubEnv('GIT_CONFIG_SYSTEM', systemConfig);
+ expect(await provesSafe(history, 'git config --list')).toBe(true);
+ const removedEnv = { ...process.env };
+ delete removedEnv.GIT_CONFIG_GLOBAL;
+ delete removedEnv.GIT_CONFIG_NOSYSTEM;
+ const output = execFileSync('git', ['config', '--list'], { cwd: repo, env: removedEnv, encoding: 'utf8' });
+ expect(output).toContain('http.extraheader=global-runtime-fixture');
+ expect(output).toContain('http.extraheader=system-runtime-fixture');
+ for (const command of [
+ 'env -u GIT_CONFIG_GLOBAL -u GIT_CONFIG_NOSYSTEM git config --list',
+ 'env -uGIT_CONFIG_GLOBAL git config --list',
+ 'env --unset GIT_CONFIG_GLOBAL git config --list',
+ 'env --unset=GIT_CONFIG_GLOBAL git config --list',
+ 'env -u HOME git diff',
+ 'env -i git config --list',
+ 'env --ignore-environment git diff --cached',
+ 'env - git config --list',
+ 'exec -c git config --list',
+ ]) {
+ expect(await provesSafe(history, command), command).toBe(false);
+ await requiresCredentialConfirmation(history, command);
+ }
+ });
+
+ it('keeps confirmation for explicit executables, wrappers and runtime overrides', async () => {
+ const history = new GitCredentialHistory();
+ const executable = path.join(repo, 'git');
+ write('git', '#!/bin/sh\nprintf "runtime-mismatch\\n"\n');
+ if (process.platform !== 'win32') {
+ fs.chmodSync(executable, 0o755);
+ expect(execFileSync(executable, ['diff'], { cwd: repo, encoding: 'utf8' })).toBe('runtime-mismatch\n');
+ }
+ expect(await provesSafe(history, 'git diff')).toBe(true);
+ expect(await provesSafe(history, 'git diff --cached')).toBe(true);
+ expect(await provesSafe(history, 'git config --list')).toBe(true);
+ for (const command of [
+ './git diff',
+ './git config --list',
+ '"' + executable + '" diff',
+ '/usr/bin/git config --list',
+ 'git.exe diff',
+ 'GIT diff',
+ 'PATH=/other/bin git diff',
+ 'env PATH=/other/bin git config --list',
+ 'LD_PRELOAD=/other/lib.so git diff',
+ 'DYLD_INSERT_LIBRARIES=/other/lib.dylib git config --list',
+ 'command -p git diff',
+ 'env git config --list',
+ ]) {
+ expect(await provesSafe(history, command), command).toBe(false);
+ await requiresCredentialConfirmation(history, command);
+ }
+ });
+
+ it('keeps confirmation when the inherited PATH has a relative search directory', async () => {
+ const history = new GitCredentialHistory();
+ const command = 'git diff';
+ expect(await provesSafe(history, command)).toBe(true);
+ vi.stubEnv('PATH', '.' + path.delimiter + process.env.PATH);
+ expect(await provesSafe(history, command)).toBe(false);
+ await requiresCredentialConfirmation(history, command);
+ });
+
it('keeps asking where the scan does not reach', async () => {
const history = new GitCredentialHistory();
const outside = path.join(root, 'outside');
diff --git a/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts b/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts
index 69ffc8ce7b..0cd57349c6 100644
--- a/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts
+++ b/apps/desktop/src/main/remote-agent/executor/gitCredentials.ts
@@ -447,6 +447,9 @@ export class GitCredentialHistory {
/** 这些 git 调用(credentials.ts shareCredentialGitReads 的结果)在本机核实后都读不到、打不出凭证。 */
async provesSafe(command: string, reads: readonly GitRead[], signal?: AbortSignal): Promise<boolean> {
if (reads.length === 0 || GIT_ENV_ASSIGNMENT.test(command) || GIT_CONFIG_ENV_ASSIGNMENT.test(command)) return false;
+ const searchPath = process.env.PATH;
+ if (!searchPath || searchPath.split(path.delimiter).some((entry) => !path.isAbsolute(entry))) return false;
+ if (reads.some((read) => read.usesInheritedGitRuntime !== true)) return false;
// The history/index probe runs before the complete shell command. If an earlier
// segment may change Git state, its result can become stale before the later read;
// keep the credential confirmation in that case instead of granting an exemption.
diff --git a/docs/product-rules/provider-sharing.md b/docs/product-rules/provider-sharing.md
index 1cdb604c33..e4a561d0bd 100644
--- a/docs/product-rules/provider-sharing.md
+++ b/docs/product-rules/provider-sharing.md
@@ -298,6 +298,10 @@ Desktop「设置 → 模型供应商」的左栏(2026-10-08 用户裁决):
- 打出 git 配置的命令(`git remote -v` / `get-url` / `show`、读 `git config`、`git var -l`)按受邀者
电脑上生效的 git 配置核实:要打出的那些项里没有密钥(地址里嵌的令牌或密码、`http.extraHeader`、
密码与令牌类配置项)就不弹卡,有才确认;
+ - 上述 Git 核实仅用于命令段直接调用 PATH 中的裸 `git`,且沿用宿主环境、PATH 各项为绝对目录时。
+ 显式执行文件路径(如 `./git`)、`git.exe`、任何命令包装器(包括 `env`、`command`、`exec`),以及
+ 环境变量的赋值、移除或清空(含 PATH、加载器与 Git 配置来源)都无法与探针证明一致,照旧弹卡;
+ 前面的命令段可能修改 Git 状态、运行环境,或写法认不出时也不能豁免。
- 再收紧(2026-10-12 用户裁决),同样只用于分享任务:命令里直接写着密钥(`https://用户:密码@`、
地址里的令牌、`Authorization` 头的取值、常见服务的令牌格式与私钥)、用 `<提交>:<路径>` 写出凭证类文件
(`git show HEAD:.aws/credentials`)、`git credential fill` 这类打出保存的登录凭证的命令都要确认。
diff --git a/packages/maker-core/src/agents/shared/auto-review.test.ts b/packages/maker-core/src/agents/shared/auto-review.test.ts
index 04ad4eed9b..00725b4f0b 100644
--- a/packages/maker-core/src/agents/shared/auto-review.test.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.test.ts
@@ -4757,6 +4757,36 @@ describe('shellCommandReadsCredentials — 分享任务的附加选项', () => {
}
});
+ it('Git 探针只把不改运行环境的裸 git 调用标成可沿用宿主运行环境', () => {
+ for (const executable of [
+ 'git',
+ './git',
+ '/usr/bin/git',
+ 'git.exe',
+ 'GIT',
+ 'env -u GIT_CONFIG_GLOBAL git',
+ 'env --unset=GIT_CONFIG_GLOBAL git',
+ 'env -i git',
+ 'env --ignore-environment git',
+ 'exec -c git',
+ 'PATH=/other/bin git',
+ 'LD_PRELOAD=/other/lib.so git',
+ 'command -p git',
+ ]) {
+ for (const subcommand of ['diff', 'config --list']) {
+ const command = executable + ' ' + subcommand;
+ expect(shellCommandReadsCredentials(command, roots), command).toBe(subcommand === 'diff');
+ const reads: GitRead[] = [];
+ const collect = (read: GitRead) => {
+ reads.push(read);
+ return true;
+ };
+ expect(shellCommandReadsCredentials(command, roots, { gitContentRead: collect, gitConfigRead: collect }), command).toBe(false);
+ expect(reads, command).toEqual([expect.objectContaining({ usesInheritedGitRuntime: executable === 'git' })]);
+ }
+ }
+ });
+
it('gitContentRead:git 查看内容先问调用方,并带上 git 实际运行的目录', () => {
const reads: GitRead[] = [];
const record = (read: GitRead) => {
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 70db3b4ce3..8b44232247 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -3239,6 +3239,7 @@ export type GitRead = {
args: readonly string[];
/** git 实际运行的目录(已跟随前面各段的 cd 与 -C);认不出时为 undefined。 */
cwd?: string;
+ usesInheritedGitRuntime?: boolean;
/**
* 当前 git 读取前是否有无法证明只读的 shell 段。前置段可能先修改工作区或索引,
* 因而使执行器在命令运行前取得的凭证扫描结果过时;调用方应拒绝凭证豁免。
@@ -6336,7 +6337,11 @@ function shellCommandReadsDotenv(
if (opts.gitConfigRead && (bin === 'git' || bin.startsWith('git-credential'))) {
const read = gitConfigReadOf(tokens, workspaceRoots, opts, segmentCwds[segmentIndex]);
- if (read && !opts.gitConfigRead({ ...read, priorSegmentsMayMutate: priorMutation })) return true;
+ if (read && !opts.gitConfigRead({
+ ...read,
+ priorSegmentsMayMutate: priorMutation,
+ usesInheritedGitRuntime: rawTokens[0] === 'git',
+ })) return true;
}
if (bin === 'git') {
@@ -6359,6 +6364,7 @@ function shellCommandReadsDotenv(
args: invocation.args,
cwd: configOverride ? undefined : gitInvocationCwd(tokens, segmentCwds[segmentIndex]),
priorSegmentsMayMutate: priorMutation,
+ usesInheritedGitRuntime: rawTokens[0] === 'git',
})) return true;
continue;
}