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(pi): preserve selector credential evidence

Chrise824d79db142
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
index e43d95f785..a0bf73604c 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
@@ -153,6 +153,8 @@ describe('cindy-bridge extension source', () => {
expect(evidence('find', { pattern: '.e{o,p}v', path: 'src' }).touchesCredential).toBe(false);
expect(evidence('find', { pattern: '.e[o-p]v', path: 'src' }).touchesCredential).toBe(false);
expect(evidence('grep', { pattern: 'KEY', path: 'src', glob: '.environment*' }).touchesCredential).toBe(false);
+ expect(evidence('grep', { pattern: 'KEY', path: 'src', glob: '!.env*' }).touchesCredential).toBe(false);
+ expect(evidence('grep', { pattern: 'KEY', path: 'src', globs: ['*', '!.env*'] }).touchesCredential).toBe(false);
expect(evidence('grep', { pattern: 'KEY', path: 'src', glob: '[!.]*.ts' }).touchesCredential).toBe(false);
expect(evidence('find', { pattern: '.env', path: 'src' }).touchesCredential).toBe(true);
expect(evidence('read', { path: '.env.local', offset: 1 }).touchesCredential).toBe(true);
@@ -175,6 +177,7 @@ describe('cindy-bridge extension source', () => {
'(globalThis as any).bashInputReadTargets = bashInputReadTargets;',
'(globalThis as any).bashInputReadEvidence = bashInputReadEvidence;',
'(globalThis as any).parseShellInputRedirections = parseShellInputRedirections;',
+ '(globalThis as any).resolvedCredentialEvidenceForHost = resolvedCredentialEvidenceForHost;',
].join('\n');
const compiled = ts.transpileModule(executableSource, {
compilerOptions: {
@@ -199,6 +202,10 @@ describe('cindy-bridge extension source', () => {
targetMayExpand: boolean[];
hasUnresolvedTarget: boolean;
};
+ resolvedCredentialEvidenceForHost?: (
+ paths: readonly string[],
+ credentialRead: boolean,
+ ) => string[] | null;
} = {
globSync,
realpathSync,
@@ -238,6 +245,9 @@ describe('cindy-bridge extension source', () => {
realpathSync(secretPath),
]);
expect(context.collectResolvedCredentialPaths?.({ path: ordinaryLink })).toEqual([]);
+ expect(context.resolvedCredentialEvidenceForHost?.([], true)).toBeNull();
+ expect(context.resolvedCredentialEvidenceForHost?.([], false)).toEqual([]);
+ expect(context.resolvedCredentialEvidenceForHost?.([secretPath], true)).toEqual([secretPath]);
const secretCommand = `cat<${secretLink}`;
const ordinaryCommand = `cat<${ordinaryLink}`;
@@ -338,7 +348,7 @@ describe('cindy-bridge extension source', () => {
});
expect(source).toContain("event.toolName === 'bash'\n ? bashInputReadEvidence(event.input)");
expect(source).toContain('bashReadEvidence.unresolved || touchesCredentialPath(bashReadTargets)');
- expect(source).toContain('resolvedCredentialPaths: resolvedCredentialReadPaths');
+ expect(source).toContain('resolvedCredentialPaths: credentialEvidenceForHost');
} finally {
rmSync(tempRoot, { recursive: true, force: true });
}
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts
index 7a377212aa..21f04aa83b 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts
@@ -1622,6 +1622,54 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
},
);
+ it(
+ 'auto mode escalates selector-only dotenv grep evidence',
+ { timeout: 60_000 },
+ async () => {
+ const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-grep-dotenv-selector-'));
+ mkdirSync(path.join(workingDir, 'src'));
+ writeFileSync(path.join(workingDir, 'src', '.env.local'), 'SELECTOR_SECRET=must-not-leak\n');
+ writeFileSync(path.join(workingDir, 'src', 'source.ts'), 'SAFE_SELECTOR=visible\n');
+ try {
+ scriptedResponses.length = 0;
+ scriptedResponses.push(
+ anthropicToolUseBody('grep', { pattern: 'SELECTOR_SECRET', path: 'src', glob: '.env*' }),
+ anthropicStreamBody('grep selector credential turn finished'),
+ );
+ const reqBefore = seenRequests.length;
+ const { resolverTools } = await runPermissionTurn({
+ sessionId: 'pi-grep-dotenv-selector',
+ workingDir,
+ permissionMode: 'auto',
+ resolverBehavior: 'deny',
+ });
+ expect(resolverTools).toEqual(['grep']);
+ const followUp = seenRequests.slice(reqBefore).map((request) => request.body).join('\n');
+ expect(followUp).not.toContain('SELECTOR_SECRET=must-not-leak');
+ expect(followUp).toContain('User denied this tool call via Cindy.');
+
+ scriptedResponses.push(
+ anthropicToolUseBody('grep', { pattern: 'SAFE_SELECTOR', path: 'src', glob: '!.env*' }),
+ anthropicStreamBody('grep negative selector turn finished'),
+ );
+ const safeReqBefore = seenRequests.length;
+ const safeTurn = await runPermissionTurn({
+ sessionId: 'pi-grep-negative-dotenv-selector',
+ workingDir,
+ permissionMode: 'auto',
+ resolverBehavior: 'deny',
+ });
+ expect(safeTurn.resolverTools).toEqual([]);
+ const safeFollowUp = seenRequests.slice(safeReqBefore).map((request) => request.body).join('\n');
+ expect(safeFollowUp).toContain('source.ts:1: SAFE_SELECTOR=visible');
+ expect(safeFollowUp).not.toContain('SELECTOR_SECRET=must-not-leak');
+ } finally {
+ rmSync(workingDir, { recursive: true, force: true });
+ scriptedResponses.length = 0;
+ }
+ },
+ );
+
it(
'Review directory grep returns safe matches without credential-file contents',
{ timeout: 60_000 },
diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
index 789a330fcb..554cc886cd 100644
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@ -332,6 +332,13 @@ function collectResolvedCredentialPaths(input: unknown, depth = 0): string[] {
return [];
}
+function resolvedCredentialEvidenceForHost(
+ paths: readonly string[],
+ credentialRead: boolean,
+): string[] | null {
+ return credentialRead && paths.length === 0 ? null : [...paths];
+}
+
// 从 bash 子进程读取任意进程的初始环境(/proc/<pid|self>/environ)是绕过密钥剥离的旁路:
// spawn 边界虽从子进程 env 删了 Cindy 私密变量,但父 pi 进程仍持有它们,同 UID 下
// cat /proc/PPID/environ 可直接取回代理 token / 网关 key / BYOM key(codex 报)。
@@ -702,6 +709,7 @@ function expandReadonlySelector(selector: string): string[] | null {
}
function readonlySelectorTouchesCredential(selector: string): boolean {
+ if (selector.trimStart().startsWith('!')) return false;
const expanded = expandReadonlySelector(selector);
if (!expanded) return true;
const dotenvProbes = ['.env', '.env.local', '.env.production', '.env.secret'];
@@ -1452,6 +1460,10 @@ export default async function cindyBridge(pi: any) {
const credentialRead = readonlyCredentialEvidence?.touchesCredential === true
|| (event.toolName === 'bash' && (bashReadEvidence.unresolved || touchesCredentialPath(bashReadTargets)))
|| resolvedCredentialReadPaths.length > 0;
+ const credentialEvidenceForHost = resolvedCredentialEvidenceForHost(
+ resolvedCredentialReadPaths,
+ credentialRead,
+ );
if (credentialRead && permission.mode === 'bypassPermissions') {
return { block: true, reason: 'Cindy blocks reading credential or key paths, even with Full access.' };
}
@@ -1464,7 +1476,7 @@ export default async function cindyBridge(pi: any) {
JSON.stringify({
toolName: event.toolName,
input: event.input ?? {},
- resolvedCredentialPaths: resolvedCredentialReadPaths,
+ resolvedCredentialPaths: credentialEvidenceForHost,
}),
);
} catch {