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 escaped credential targets

Chrise4cb7671e029
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 a81ec2047d..20f3180448 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
@@ -249,6 +249,7 @@ describe('cindy-bridge extension source', () => {
const ordinaryPath = path.join(tempRoot, 'ordinary.txt');
const secretLink = path.join(tempRoot, 'innocent.txt');
const ordinaryLink = path.join(tempRoot, 'ordinary-link.txt');
+ const escapedSecretLink = path.join(tempRoot, 'innocent\\q');
const nestedDir = path.join(tempRoot, 'nested');
const dashDir = path.join(tempRoot, '-credential-dir');
const nestedSecretLink = path.join(nestedDir, 'nested-innocent.txt');
@@ -257,6 +258,11 @@ describe('cindy-bridge extension source', () => {
const scopedLinkName = 'scoped-innocent.txt';
const rootScopedSecretLink = path.join(tempRoot, scopedLinkName);
const nestedScopedOrdinaryLink = path.join(nestedDir, scopedLinkName);
+ const cdRedirectName = 'cd-innocent';
+ const rootCdRedirectSecretLink = path.join(tempRoot, cdRedirectName);
+ const nestedCdRedirectOrdinaryLink = path.join(nestedDir, cdRedirectName);
+ const nestedOrdinaryReadName = 'ordinary-after-cd.txt';
+ const nestedOrdinaryReadLink = path.join(nestedDir, nestedOrdinaryReadName);
mkdirSync(path.dirname(secretPath), { recursive: true });
mkdirSync(nestedDir, { recursive: true });
mkdirSync(dashDir, { recursive: true });
@@ -267,7 +273,11 @@ describe('cindy-bridge extension source', () => {
symlinkSync(secretPath, dashSecretLink);
symlinkSync(secretPath, lateSecretLink);
symlinkSync(secretPath, rootScopedSecretLink);
+ symlinkSync(secretPath, escapedSecretLink);
+ symlinkSync(secretPath, rootCdRedirectSecretLink);
symlinkSync(ordinaryPath, nestedScopedOrdinaryLink);
+ symlinkSync(ordinaryPath, nestedCdRedirectOrdinaryLink);
+ symlinkSync(ordinaryPath, nestedOrdinaryReadLink);
symlinkSync(ordinaryPath, ordinaryLink);
expect(context.collectResolvedCredentialPaths?.({ path: secretLink })).toEqual([
@@ -288,6 +298,30 @@ describe('cindy-bridge extension source', () => {
expect(context.collectResolvedCredentialPaths?.(
context.bashInputReadTargets?.({ command: ordinaryCommand }),
)).toEqual([]);
+ const escapedBackslashCommand = 'cat <"innocent\\\\q"';
+ expect(context.bashInputReadEvidence?.({ command: escapedBackslashCommand })).toEqual({
+ targets: [escapedSecretLink],
+ unresolved: true,
+ });
+ expect(context.collectResolvedCredentialPaths?.(
+ context.bashInputReadTargets?.({ command: escapedBackslashCommand }),
+ )).toEqual([realpathSync(secretPath)]);
+ for (const cdRedirectOperator of ['<>', '<']) {
+ const command = `cd ${nestedDir} ${cdRedirectOperator}${cdRedirectName} && cat <${nestedOrdinaryReadName}`;
+ const evidence = context.bashInputReadEvidence?.({ command });
+ expect(evidence?.unresolved, command).toBe(false);
+ expect(evidence?.targets, command).toEqual([
+ rootCdRedirectSecretLink,
+ nestedOrdinaryReadLink,
+ ]);
+ expect(context.collectResolvedCredentialPaths?.(evidence?.targets), command)
+ .toEqual([realpathSync(secretPath)]);
+ }
+ expect(context.collectResolvedCredentialPaths?.(
+ context.bashInputReadTargets?.({
+ command: `cd ${nestedDir} <ordinary-link.txt && cat <${nestedOrdinaryReadName}`,
+ }),
+ )).toEqual([]);
const readWriteSecretCommand = `cat 3<>${secretLink}`;
expect(context.parseShellInputRedirections?.(readWriteSecretCommand)).toEqual({
command: readWriteSecretCommand,
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 f62d083b6e..097e0a13fc 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
@@ -2110,29 +2110,43 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-perm-auto-bash-symlink-dotenv-'));
try {
const secretPath = path.join(workingDir, 'secrets', '.env');
+ const ordinaryPath = path.join(workingDir, 'ordinary.txt');
const subDir = path.join(workingDir, 'sub');
- const linkPath = path.join(subDir, 'link');
+ const postCdLink = path.join(subDir, 'link');
+ const escapedLinkName = 'innocent\\q';
+ const cdRedirectLinkName = 'cd-innocent';
mkdirSync(path.dirname(secretPath), { recursive: true });
mkdirSync(subDir);
writeFileSync(secretPath, 'FAKE_REDIRECT_DOTENV_SECRET=must-not-leak');
- symlinkSync('../secrets/.env', linkPath);
-
- scriptedResponses.length = 0;
- scriptedResponses.push(
- anthropicToolUseBody('bash', { command: 'cd sub >/dev/null && cat<link' }),
- anthropicStreamBody('bash symlink dotenv turn finished'),
- );
- const reqBefore = seenRequests.length;
- const { resolverTools } = await runPermissionTurn({
- sessionId: 'perm-auto-bash-symlink-dotenv',
- workingDir,
- permissionMode: 'auto',
- resolverBehavior: 'deny',
- });
- expect(resolverTools).toEqual(['bash']);
- const followUp = seenRequests.slice(reqBefore).map((r) => r.body);
- expect(followUp.some((b) => b.includes('FAKE_REDIRECT_DOTENV_SECRET'))).toBe(false);
- expect(followUp.some((b) => b.includes('User denied this tool call via Cindy.'))).toBe(true);
+ writeFileSync(ordinaryPath, 'ordinary-content');
+ symlinkSync('../secrets/.env', postCdLink);
+ symlinkSync(secretPath, path.join(workingDir, escapedLinkName));
+ symlinkSync(secretPath, path.join(workingDir, cdRedirectLinkName));
+ symlinkSync(ordinaryPath, path.join(subDir, cdRedirectLinkName));
+ symlinkSync(ordinaryPath, path.join(subDir, 'ordinary'));
+
+ for (const [sessionId, command] of [
+ ['perm-auto-bash-symlink-dotenv-post-cd', 'cd sub >/dev/null && cat<link'],
+ ['perm-auto-bash-symlink-dotenv-backslash', `cat <"${escapedLinkName}"`],
+ ['perm-auto-bash-symlink-dotenv-cd-redirect', `cd sub <>${cdRedirectLinkName} && cat <ordinary`],
+ ] as const) {
+ scriptedResponses.length = 0;
+ scriptedResponses.push(
+ anthropicToolUseBody('bash', { command }),
+ anthropicStreamBody('bash symlink dotenv turn finished'),
+ );
+ const reqBefore = seenRequests.length;
+ const { resolverTools } = await runPermissionTurn({
+ sessionId,
+ workingDir,
+ permissionMode: 'auto',
+ resolverBehavior: 'deny',
+ });
+ const followUp = seenRequests.slice(reqBefore).map((r) => r.body);
+ expect(resolverTools, `${command}\n${followUp.join('\n')}`).toEqual(['bash']);
+ expect(followUp.some((b) => b.includes('FAKE_REDIRECT_DOTENV_SECRET')), command).toBe(false);
+ expect(followUp.some((b) => b.includes('User denied this tool call via Cindy.')), command).toBe(true);
+ }
} finally {
rmSync(workingDir, { recursive: true, force: true });
scriptedResponses.length = 0;
@@ -2148,14 +2162,19 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
try {
const targetPath = path.join(workingDir, 'ordinary-target.txt');
const subDir = path.join(workingDir, 'sub');
+ const redirectLinkName = 'ordinary-redirect-link';
const linkPath = path.join(subDir, 'link');
mkdirSync(subDir);
writeFileSync(targetPath, 'ordinary-bash-symlink-content');
+ symlinkSync(targetPath, path.join(workingDir, redirectLinkName));
+ symlinkSync('../ordinary-target.txt', path.join(subDir, redirectLinkName));
symlinkSync('../ordinary-target.txt', linkPath);
scriptedResponses.length = 0;
scriptedResponses.push(
- anthropicToolUseBody('bash', { command: 'cd sub >/dev/null && cat<link' }),
+ anthropicToolUseBody('bash', {
+ command: `cd sub <${redirectLinkName} && cat<link`,
+ }),
anthropicStreamBody('bash ordinary symlink turn finished'),
);
const reqBefore = seenRequests.length;
@@ -2168,6 +2187,26 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
expect(resolverTools).toEqual([]);
const followUp = seenRequests.slice(reqBefore).map((r) => r.body);
expect(followUp.some((b) => b.includes('ordinary-bash-symlink-content'))).toBe(true);
+
+ scriptedResponses.length = 0;
+ scriptedResponses.push(
+ anthropicToolUseBody('bash', {
+ command: `cd sub <>${redirectLinkName} && cat<link`,
+ }),
+ anthropicStreamBody('bash ordinary read-write symlink turn finished'),
+ );
+ const readWriteReqBefore = seenRequests.length;
+ const readWriteTurn = await runPermissionTurn({
+ sessionId: 'perm-auto-bash-symlink-plain-read-write',
+ workingDir,
+ permissionMode: 'auto',
+ resolverBehavior: 'allow',
+ });
+ expect(readWriteTurn.resolverTools).toEqual(['bash']);
+ const readWriteFollowUp = seenRequests.slice(readWriteReqBefore).map((r) => r.body);
+ expect(readWriteFollowUp.some((b) => b.includes('ordinary-bash-symlink-content'))).toBe(true);
+ expect(readWriteFollowUp.some((b) => b.includes('Cindy blocks reading credential or key paths')))
+ .toBe(false);
} finally {
rmSync(workingDir, { recursive: true, force: true });
scriptedResponses.length = 0;
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 db4dacea9e..4827ceecd8 100644
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@ -195,6 +195,10 @@ function bashInputReadEvidence(input: unknown): BashInputReadEvidence {
const targets = parsed.targets.flatMap((target, index) => {
const cwdEvidence = bashWorkingDirectoryCandidates(parsed.targetPrefixes[index] ?? '');
unresolved ||= cwdEvidence.unresolved;
+ // POSIX allows a literal backslash in a filename, while the embedded runtime's
+ // tool-call and execution stages have not normalized that shell input consistently.
+ // Preserve the target and require consent unless canonical evidence settles it.
+ unresolved ||= path.sep === '/' && target.includes('\\');
return cwdEvidence.paths.flatMap((cwd) => {
const expanded = bashExpandedPathCandidates(target, cwd, parsed.targetMayExpand[index] === true);
unresolved ||= expanded.unresolved;
diff --git a/packages/maker-core/src/agents/shared/shell-input-redirections.test.ts b/packages/maker-core/src/agents/shared/shell-input-redirections.test.ts
index faab3e5aa4..1fcd28fc3b 100644
--- a/packages/maker-core/src/agents/shared/shell-input-redirections.test.ts
+++ b/packages/maker-core/src/agents/shared/shell-input-redirections.test.ts
@@ -11,6 +11,7 @@ describe('parseShellInputRedirections', () => {
['cat<.e\\nv', '.env', 'cat'],
["cat <'$TARGET'", '$TARGET', 'cat'],
['cat <*.txt', '*.txt', 'cat'],
+ ['cat <"innocent\\q"', 'innocent\\q', 'cat'],
])('extracts the static input target from %s', (command, target, stripped) => {
const parsed = parseShellInputRedirections(command);
expect(parsed.command.trim()).toBe(stripped);
diff --git a/packages/maker-core/src/agents/shared/shell-input-redirections.ts b/packages/maker-core/src/agents/shared/shell-input-redirections.ts
index 7615b3a24a..f024725db0 100644
--- a/packages/maker-core/src/agents/shared/shell-input-redirections.ts
+++ b/packages/maker-core/src/agents/shared/shell-input-redirections.ts
@@ -24,7 +24,14 @@ export function readShellRedirectionTarget(
continue;
}
if (char === '\\' && quote === '"' && cursor + 1 < command.length) {
- if (command[cursor + 1] !== '\n') target += command[cursor + 1];
+ const escaped = command[cursor + 1];
+ if (escaped !== '\n') {
+ if (escaped === '$' || escaped.charCodeAt(0) === 96 || escaped === '"' || escaped === '\\') {
+ target += escaped;
+ } else {
+ target += '\\' + escaped;
+ }
+ }
cursor += 2;
continue;
}