Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): fail closed on inherited cd semantics
Chris97c414de9b7d
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.tsindex 20f3180448..f3f51cd590 100644--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts@@ -240,7 +240,7 @@ describe('cindy-bridge extension source', () => {realpathSync,statSync,path,- process: { cwd: () => tempRoot, env: { ...process.env, HOME: tempRoot } },+ process: { cwd: () => tempRoot, env: { HOME: tempRoot, PATH: process.env.PATH } },};runInNewContext(compiled, context);@@ -263,9 +263,13 @@ describe('cindy-bridge extension source', () => {const nestedCdRedirectOrdinaryLink = path.join(nestedDir, cdRedirectName);const nestedOrdinaryReadName = 'ordinary-after-cd.txt';const nestedOrdinaryReadLink = path.join(nestedDir, nestedOrdinaryReadName);+ const cdPathRoot = path.join(tempRoot, 'cdpath-root');+ const cdPathSubDir = path.join(cdPathRoot, 'sub');+ const cdPathSecretLink = path.join(cdPathSubDir, 'link');mkdirSync(path.dirname(secretPath), { recursive: true });mkdirSync(nestedDir, { recursive: true });mkdirSync(dashDir, { recursive: true });+ mkdirSync(cdPathSubDir, { recursive: true });writeFileSync(secretPath, 'FAKE PRIVATE KEY');writeFileSync(ordinaryPath, 'ordinary');symlinkSync(secretPath, secretLink);@@ -278,6 +282,7 @@ describe('cindy-bridge extension source', () => {symlinkSync(ordinaryPath, nestedScopedOrdinaryLink);symlinkSync(ordinaryPath, nestedCdRedirectOrdinaryLink);symlinkSync(ordinaryPath, nestedOrdinaryReadLink);+ symlinkSync(secretPath, cdPathSecretLink);symlinkSync(ordinaryPath, ordinaryLink);expect(context.collectResolvedCredentialPaths?.({ path: secretLink })).toEqual([@@ -348,6 +353,56 @@ describe('cindy-bridge extension source', () => {expect(context.collectResolvedCredentialPaths?.(context.bashInputReadTargets?.({ command: nestedCommand }),)).toEqual([realpathSync(secretPath)]);++ context.process.env.CDPATH = cdPathRoot;+ for (const cdRedirectOperator of ['<', '<>']) {+ const command = `cd sub ${cdRedirectOperator}ordinary-link.txt && cat <link`;+ expect(context.bashInputReadEvidence?.({ command }), command).toEqual({+ targets: [ordinaryLink, path.join(tempRoot, 'link')],+ unresolved: true,+ });+ }+ expect(context.bashInputReadEvidence?.({+ command: `cd ./nested <ordinary-link.txt && cat <${nestedOrdinaryReadName}`,+ })).toEqual({+ targets: [ordinaryLink, nestedOrdinaryReadLink],+ unresolved: false,+ });+ expect(context.bashInputReadEvidence?.({+ command: `cd ${nestedDir} && cat <${nestedOrdinaryReadName}`,+ })).toEqual({ targets: [nestedOrdinaryReadLink], unresolved: false });+ context.process.env.CDPATH = '.:';+ expect(context.bashInputReadEvidence?.({ command: 'cd nested && cat <nested-innocent.txt' }))+ .toEqual({ targets: [nestedSecretLink], unresolved: false });+ delete context.process.env.CDPATH;++ context.process.env.BASHOPTS = 'checkwinsize:cdable_vars';+ context.process.env.sub = cdPathSubDir;+ expect(context.bashInputReadEvidence?.({ command: 'cd sub && cat <link' }))+ .toEqual({ targets: [path.join(tempRoot, 'link')], unresolved: true });+ delete context.process.env.BASHOPTS;+ delete context.process.env.sub;++ context.process.env.BASH_ENV = path.join(tempRoot, 'shell-startup');+ expect(context.bashInputReadEvidence?.({+ command: `cd ${nestedDir} && cat <${nestedOrdinaryReadName}`,+ })).toEqual({+ targets: [path.join(tempRoot, nestedOrdinaryReadName)],+ unresolved: true,+ });+ delete context.process.env.BASH_ENV;+ context.process.env.ENV = 'development';+ expect(context.bashInputReadEvidence?.({ command: nestedCommand }))+ .toEqual({ targets: [nestedSecretLink], unresolved: false });+ delete context.process.env.ENV;+ for (const builtin of ['cd', 'pushd']) {+ context.process.env[`BASH_FUNC_${builtin}%%`] = '() { builtin cd "$HOME"; }';+ expect(context.bashInputReadEvidence?.({+ command: `${builtin} ${nestedDir} && cat <${nestedOrdinaryReadName}`,+ }), builtin).toEqual({ targets: [path.join(tempRoot, nestedOrdinaryReadName)], unresolved: true });+ delete context.process.env[`BASH_FUNC_${builtin}%%`];+ }+const redirectedDirectoryCommands = [`cd ${nestedDir} >/dev/null && cat <nested-innocent.txt`,`cd ${nestedDir} 2>/dev/null 3>&1 && cat <nested-innocent.txt`,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.tsindex 097e0a13fc..fa82a64105 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@@ -2154,6 +2154,79 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa},);+ it.skipIf(process.platform === 'win32' || !canSymlink)(+ 'auto mode fail closes inherited CDPATH while explicit relative cd keeps ordinary reads fast',+ { timeout: 60_000 },+ async () => {+ const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-perm-auto-bash-cdpath-'));+ const cdPathRoot = mkdtempSync(path.join(tmpdir(), 'pi-perm-auto-bash-cdpath-root-'));+ const previousCdPath = process.env.CDPATH;+ try {+ const secretPath = path.join(workingDir, 'secrets', '.env');+ const ordinaryPath = path.join(workingDir, 'ordinary.txt');+ const localSubDir = path.join(workingDir, 'sub');+ const cdPathSubDir = path.join(cdPathRoot, 'sub');+ mkdirSync(path.dirname(secretPath), { recursive: true });+ mkdirSync(localSubDir);+ mkdirSync(cdPathSubDir);+ writeFileSync(secretPath, 'FAKE_CDPATH_DOTENV_SECRET=must-not-leak');+ writeFileSync(ordinaryPath, 'ordinary-cdpath-content');+ symlinkSync(ordinaryPath, path.join(workingDir, 'root-input'));+ symlinkSync(ordinaryPath, path.join(localSubDir, 'link'));+ symlinkSync(secretPath, path.join(cdPathSubDir, 'link'));+ process.env.CDPATH = cdPathRoot;++ for (const [sessionId, command] of [+ ['perm-auto-bash-cdpath-post-cd', 'cd sub && cat<link'],+ ['perm-auto-bash-cdpath-cd-read', 'cd sub <root-input && cat<link'],+ ['perm-auto-bash-cdpath-cd-read-write', 'cd sub <>root-input && cat<link'],+ ] as const) {+ scriptedResponses.length = 0;+ scriptedResponses.push(+ anthropicToolUseBody('bash', { command }),+ anthropicStreamBody('bash CDPATH turn finished'),+ );+ const reqBefore = seenRequests.length;+ const { resolverTools } = await runPermissionTurn({+ sessionId,+ workingDir,+ permissionMode: 'auto',+ resolverBehavior: 'deny',+ });+ const followUp = seenRequests.slice(reqBefore).map((request) => request.body);+ expect(resolverTools, command).toEqual(['bash']);+ expect(followUp.some((body) => body.includes('FAKE_CDPATH_DOTENV_SECRET')), command)+ .toBe(false);+ expect(followUp.some((body) => body.includes('User denied this tool call via Cindy.')), command)+ .toBe(true);+ }++ scriptedResponses.length = 0;+ scriptedResponses.push(+ anthropicToolUseBody('bash', { command: 'cd ./sub <root-input && cat<link' }),+ anthropicStreamBody('bash explicit relative cd turn finished'),+ );+ const ordinaryReqBefore = seenRequests.length;+ const ordinaryTurn = await runPermissionTurn({+ sessionId: 'perm-auto-bash-cdpath-explicit-relative',+ workingDir,+ permissionMode: 'auto',+ resolverBehavior: 'deny',+ });+ expect(ordinaryTurn.resolverTools).toEqual([]);+ const ordinaryFollowUp = seenRequests.slice(ordinaryReqBefore).map((request) => request.body);+ expect(ordinaryFollowUp.some((body) => body.includes('ordinary-cdpath-content'))).toBe(true);+ expect(ordinaryFollowUp.some((body) => body.includes('FAKE_CDPATH_DOTENV_SECRET'))).toBe(false);+ } finally {+ if (previousCdPath === undefined) delete process.env.CDPATH;+ else process.env.CDPATH = previousCdPath;+ rmSync(workingDir, { recursive: true, force: true });+ rmSync(cdPathRoot, { recursive: true, force: true });+ scriptedResponses.length = 0;+ }+ },+ );+it.skipIf(!canSymlink)('auto mode keeps ordinary bash input redirect symlinks on the readonly fast path',{ 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.tsindex 4827ceecd8..c87e1f7c5e 100644--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts@@ -295,6 +295,27 @@ function bashOperatorAfterRedirections(command: string, start: number): BashPostreturn { operator: '', end: cursor, unresolved: false, hadRedirection };}+function bashInheritedDirectoryChangeIsUncertain(target: string): boolean {+ if (path.sep !== '/') return false;++ // Non-interactive Bash startup files and exported builtin overrides can change+ // directory semantics before this command runs; their contents are not static evidence.+ const bashEnv = process.env.BASH_ENV;+ if (typeof bashEnv === 'string' && bashEnv.trim().length > 0) return true;+ if (Object.keys(process.env).some((name) => /^BASH_FUNC_(?:cd|pushd)%%$/.test(name))) return true;++ const bashOptions = (process.env.BASHOPTS ?? '').split(':');+ if (bashOptions.includes('cdable_vars') && /^[A-Za-z_][A-Za-z0-9_]*$/.test(target)) return true;++ // Bash does not consult CDPATH for absolute paths or operands beginning with ./ or ../.+ if (path.isAbsolute(target) || /^(?:\.{1,2})(?:\/|$)/.test(target) || target.startsWith('~')) {+ return false;+ }+ const cdPath = process.env.CDPATH;+ if (typeof cdPath !== 'string' || cdPath.length === 0) return false;+ return cdPath.split(':').some((entry) => entry.length > 0 && path.normalize(entry) !== '.');+}+function bashWorkingDirectoryCandidates(command: string): BashPathCandidates {let candidates = [process.cwd()];let unresolved = false;@@ -327,6 +348,10 @@ function bashWorkingDirectoryCandidates(command: string): BashPathCandidates {}const operator = operatorEvidence.operator;if (!operator || operator === '|' || operator === '&') continue;+ if (bashInheritedDirectoryChangeIsUncertain(parsed.target)) {+ unresolved = true;+ continue;+ }const changed: string[] = [];for (const cwd of candidates) {