Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): block bash /proc environ reads and symlinked credential paths under full access
Chrisb3184ac5af68
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 c8c9c56be8..7171bd0982 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@@ -10,7 +10,7 @@*/import { createServer, type Server } from 'node:http';-import { mkdtempSync, existsSync, rmSync, writeFileSync } from 'node:fs';+import { mkdtempSync, existsSync, rmSync, writeFileSync, mkdirSync, symlinkSync } from 'node:fs';import { tmpdir } from 'node:os';import path from 'node:path';import { fileURLToPath } from 'node:url';@@ -991,6 +991,75 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa},);+ it(+ 'full access blocks bash reads of process environ (parent /proc holds the secrets)',+ { timeout: 60_000 },+ async () => {+ // codex 回归:spawn 边界只剥子进程 env,父 pi 进程仍持有 token;bash+ // `cat /proc/self/environ` 同 UID 直取 → 即使 Full access 也硬拦。+ const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-perm-bash-environ-'));+ try {+ scriptedResponses.length = 0;+ scriptedResponses.push(+ anthropicToolUseBody('bash', { command: 'cat /proc/self/environ' }),+ anthropicStreamBody('bash environ turn finished'),+ );+ const reqBefore = seenRequests.length;+ const { resolverTools } = await runPermissionTurn({+ sessionId: 'perm-bash-environ',+ workingDir,+ permissionMode: 'bypassPermissions',+ resolverBehavior: 'allow',+ });+ expect(resolverTools).toEqual([]);+ const followUp = seenRequests.slice(reqBefore).map((r) => r.body);+ expect(followUp.some((b) => b.includes('Cindy blocks reading process environment'))).toBe(true);+ expect(followUp.some((b) => b.includes('CINDY_PI_SESSION_TOKEN='))).toBe(false);+ } finally {+ rmSync(workingDir, { recursive: true, force: true });+ scriptedResponses.length = 0;+ }+ },+ );++ it(+ 'full access blocks credential reads reached through a workspace symlink',+ { timeout: 60_000 },+ async () => {+ // greptile 回归:未解析路径命不中特征,但工作区内符号链接可指向敏感目标;+ // realpath 跟随后再判 → 即使 Full access 也硬拦。+ const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-perm-symlink-cred-'));+ try {+ // 真实敏感文件(路径含 id_rsa,命中凭证特征)+ 工作区内指向它的无害名字符号链接。+ mkdirSync(path.join(workingDir, 'secrets'), { recursive: true });+ const secretPath = path.join(workingDir, 'secrets', 'id_rsa');+ writeFileSync(secretPath, 'FAKE PRIVATE KEY');+ const linkPath = path.join(workingDir, 'innocent.txt');+ symlinkSync(secretPath, linkPath);++ scriptedResponses.length = 0;+ scriptedResponses.push(+ anthropicToolUseBody('read', { path: linkPath }),+ anthropicStreamBody('symlink cred turn finished'),+ );+ const reqBefore = seenRequests.length;+ const { resolverTools } = await runPermissionTurn({+ sessionId: 'perm-symlink-cred',+ workingDir,+ permissionMode: 'bypassPermissions',+ resolverBehavior: 'allow',+ });+ expect(resolverTools).toEqual([]);+ const followUp = seenRequests.slice(reqBefore).map((r) => r.body);+ expect(followUp.some((b) => b.includes('Cindy blocks reading credential or key paths'))).toBe(true);+ expect(followUp.some((b) => b.includes('FAKE PRIVATE KEY'))).toBe(false);+ } finally {+ rmSync(workingDir, { recursive: true, force: true });+ scriptedResponses.length = 0;+ }+ },+ );+it('plain reads still pass the bridge untouched (no popup, tool executes)',{ 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 10ee096d21..b689b6f1ea 100644--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts@@ -30,7 +30,7 @@ export const CINDY_BRIDGE_EXTENSION_FILENAME = 'cindy-bridge.ts';export const CINDY_BRIDGE_EXTENSION_SOURCE = String.raw`/*** cindy-bridge (generated by Cindy — do not edit; overwritten on session start)*/-import { readFileSync } from 'node:fs';+import { readFileSync, realpathSync } from 'node:fs';import path from 'node:path';import { createBashTool } from '@earendil-works/pi-coding-agent';@@ -81,6 +81,37 @@ function touchesCredentialPath(input: unknown, depth = 0): boolean {return false;}+// 未解析的路径字符串命不中凭证特征,但工作区内的符号链接可指向 /proc/self/environ 等+// 敏感目标;path.resolve 不跟随链接,故 touchesCredentialPath 会漏判(greptile 报)。这里+// 对每个字符串叶子额外 realpathSync 跟随链接,用**解析后**的真实目标再匹配一次特征。+// realpathSync 对不存在/非路径字符串会抛,catch 即跳过(仍有原始字符串检查兜底)。+function resolvesToCredentialPath(input: unknown, depth = 0): boolean {+ if (depth > 6 || input == null) return false;+ if (typeof input === 'string') {+ if (!input) return false;+ try {+ const resolved = realpathSync(input);+ return CREDENTIAL_PATH_PATTERNS.some((re) => re.test(resolved));+ } catch {+ return false;+ }+ }+ if (Array.isArray(input)) return input.some((v) => resolvesToCredentialPath(v, depth + 1));+ if (typeof input === 'object') {+ return Object.values(input as Record<string, unknown>).some((v) => resolvesToCredentialPath(v, depth + 1));+ }+ return false;+}++// 从 bash 子进程读取任意进程的初始环境(/proc/<pid|self>/environ)是绕过密钥剥离的旁路:+// spawn 边界虽从子进程 env 删了 Cindy 私密变量,但父 pi 进程仍持有它们,同 UID 下+// cat /proc/PPID/environ 可直接取回代理 token / 网关 key / BYOM key(codex 报)。+// 无法从 JS 侧让 /proc 不可读,故在工具边界对这类命令一律硬拦(含 Full access)。+const PROC_ENVIRON_READ_RE = /\/proc\/[^/\s'"]*\/environ\b/i;+function commandReadsProcessEnviron(command: unknown): boolean {+ return typeof command === 'string' && PROC_ENVIRON_READ_RE.test(command);+}+function currentPermissionState(): {mode: 'ask' | 'bypassPermissions';readOnlyRoots: string[];@@ -321,12 +352,19 @@ export default async function cindyBridge(pi: any) {) {return { block: true, reason: 'Cindy extra reference directories are read-only.' };}+ // bash 读取任意进程的初始环境(/proc/<pid|self>/environ)是绕过密钥剥离的旁路:+ // spawn 边界虽删了子进程 env 的私密变量,父 pi 进程仍持有,cat /proc/PPID/environ+ // 同 UID 直取代理 token / 网关 / BYOM key(codex 报)→ 一律硬拦,含 Full access。+ if (event.toolName === 'bash' && commandReadsProcessEnviron(event.input?.command)) {+ return { block: true, reason: 'Cindy blocks reading process environment (/proc/*/environ), even with Full access.' };+ }// 凭证/密钥路径的内置只读工具(read/grep/find/ls)在 Pi 父进程内执行,而父进程 env// 必须保留代理会话 token 与 BYOM keys($ENV 请求期解析、bridge client 使用)。Full// access 若放行 read /proc/self/environ 这类路径,模型可直接取 token 调回环代理,- // 绕过审批盗刷当前会话额度(greptile 报)→ 即使 bypassPermissions 也硬拦。bash 无此- // 通道(spawn 边界已按名单剥离真值),维持既有档位约束即可。- const credentialRead = READONLY_BUILTINS.has(event.toolName) && touchesCredentialPath(event.input);+ // 绕过审批盗刷当前会话额度(greptile 报)→ 即使 bypassPermissions 也硬拦。原始路径+ // 命不中时还要跟随符号链接:工作区内的 link 可指向敏感目标,realpath 后再判一次。+ const credentialRead = READONLY_BUILTINS.has(event.toolName)+ && (touchesCredentialPath(event.input) || resolvesToCredentialPath(event.input));if (credentialRead && permission.mode === 'bypassPermissions') {return { block: true, reason: 'Cindy blocks reading credential or key paths, even with Full access.' };}