diff --git a/docs/PI.md b/docs/PI.md index 6443a0b7..4a9261c0 100644 --- a/docs/PI.md +++ b/docs/PI.md @@ -59,10 +59,18 @@ pointed there (`PI_CODING_AGENT_DIR=`): accounts, MCP servers and packages added either way are there for both. Conversations are files under its `sessions/`, which is what a session resumes from after a restart and what deleting a conversation removes. -A profile's token never touches a file: the driver hands the session's -process the profile in one environment variable and the token in another, -which the registration names (`$CODEDECK_PI_PROVIDER_TOKEN`), so Pi reads -the token from its own environment. +A profile's token never touches a file, nor Pi's environment: the profile +travels in an environment variable, the token on a pipe of its own (file +descriptor 3), which the CodeDeck extension reads once as Pi starts and +registers as the profile's key. Everything Pi starts — the model's shells, +background shells, sub-agents, MCP and language servers — inherits Pi's +environment, and on Linux any process of the same user can read a process's +starting environment back from `/proc`, so a token there would be the +model's to print. A sub-agent's Pi gets the token on its own pipe the same +way. An operator's own Pi executable that wraps Pi must hand file +descriptor 3 on to it; one that does not leaves the profile's requests +without a key (Pi's error says so, and the bridge log has the extension's +line naming the missing token). Pi runs with `--offline`, version checks and telemetry off: it makes no network request of its own beyond the model's. Its model catalog is therefore diff --git a/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts b/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts index de9c3aaa..31a139bd 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts @@ -34,13 +34,15 @@ import { EventEmitter } from 'node:events'; interface FakeStdin extends EventEmitter { destroyed: boolean; write(chunk: string): boolean; - end(): void; + end(chunk?: string): void; } export interface FakeChild extends EventEmitter { stdout: EventEmitter; stderr: EventEmitter; stdin: FakeStdin; + /** stdin, stdout, stderr and the provider token's pipe (fd 3). */ + stdio: [FakeStdin, EventEmitter, EventEmitter, FakeStdin]; pid: number; kill(signal?: string): boolean; } @@ -78,6 +80,10 @@ export class FakePi { readonly uiResponses: Array> = []; /** Dialogs answered by allow/deny, by their call id. */ readonly answers = new Map(); + /** What the driver wrote on the token pipe (fd 3), and whether it closed + * it — as the extension reads it, to the end. */ + token = ''; + tokenClosed = false; private readonly options: FakePiOptions; private nextCall = 0; private pendingDialog: { uiId: string; call: ScriptedCall } | undefined; @@ -106,6 +112,18 @@ export class FakePi { }, }) as FakeStdin; child.stdin = stdin; + const tokenPipe = Object.assign(new EventEmitter(), { + destroyed: false, + write: (chunk: string): boolean => { + this.token += chunk; + return true; + }, + end: (chunk?: string): void => { + if (chunk !== undefined) this.token += chunk; + this.tokenClosed = true; + }, + }) as FakeStdin; + child.stdio = [stdin, child.stdout, child.stderr, tokenPipe]; child.pid = 4242; child.kill = (): boolean => { if (!ended) { diff --git a/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts index b4e879e1..4c4a77da 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts @@ -19,7 +19,7 @@ interface Harness { driver: PiDriver; fake: FakePi; home: string; - spawns: Array<{ command: string; args: string[]; env: Record; cwd: string }>; + spawns: Array<{ command: string; args: string[]; env: Record; cwd: string; stdio: string[] }>; logs: string[]; session?: DriverSession; } @@ -42,8 +42,8 @@ function withDriver(options: { fake?: FakePiOptions; killTree?: (pid: number) => runtime, home, baseEnv: {}, - spawnFn: ((command: string, args: string[], opts: { env: Record; cwd: string }) => { - spawns.push({ command, args, env: opts.env, cwd: opts.cwd }); + spawnFn: ((command: string, args: string[], opts: { env: Record; cwd: string; stdio: string[] }) => { + spawns.push({ command, args, env: opts.env, cwd: opts.cwd, stdio: opts.stdio }); return fake.child; }) as never, ...(options.killTree ? { killTree: options.killTree } : {}), @@ -181,7 +181,7 @@ describe('PiSession startup', () => { expect(ended).not.toHaveProperty('resumeLost'); }); - it('binds a provider profile through the environment, token kept out of the profile JSON', async () => { + it("binds a provider profile through the environment, and hands its token over on Pi's token pipe only", async () => { const h = withDriver(); await ready(h, { model: 'fake-model-1', @@ -197,8 +197,19 @@ describe('PiSession startup', () => { expect(spawn.args[spawn.args.indexOf('--model') + 1]).toBe('codedeck-fake/fake-model-1'); const profile = JSON.parse(spawn.env.CODEDECK_PI_PROVIDER!) as Record; expect(profile).toMatchObject({ id: 'codedeck-fake', label: 'Fake', baseUrl: 'https://api.example.com/v1' }); - expect(JSON.stringify(profile)).not.toContain('sk-secret'); - expect(spawn.env.CODEDECK_PI_PROVIDER_TOKEN).toBe('sk-secret'); + // Nowhere in the environment, under any name: everything Pi starts + // inherits it. + expect(JSON.stringify(spawn.env)).not.toContain('sk-secret'); + expect(spawn.stdio).toHaveLength(4); + expect(h.fake.token).toBe('sk-secret'); + expect(h.fake.tokenClosed).toBe(true); + }); + + it('opens no token pipe for a session without a profile', async () => { + const h = withDriver(); + await ready(h); + expect(h.spawns[0]!.stdio).toHaveLength(3); + expect(h.fake.token).toBe(''); }); it('refuses a provider profile with an insecure endpoint before it starts', () => { diff --git a/packages/agent-host/src/drivers/pi/__tests__/piExtension.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piExtension.test.ts index 83c63b07..8dbde88e 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piExtension.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piExtension.test.ts @@ -1,7 +1,7 @@ /** * The CodeDeck Pi extension: the module the driver writes into Pi's home. It * must gate every tool call and register a bound provider profile with the - * token read from the environment; and the text the host carries must import + * token read from its pipe; and the text the host carries must import * nothing Pi does not provide. */ import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; @@ -11,7 +11,7 @@ import { describe, expect, it } from 'vitest'; import { bundledPiExtension, installPiExtension, PI_PERMISSION_PREFIX, piExtensionPath } from '../extension'; import type { CatalogModel } from '../extension/catalog'; import { installGate } from '../extension/gate'; -import { registerProfile } from '../extension/provider'; +import { configLiteral, readProviderToken, registerProfile } from '../extension/provider'; import { installShellCheck } from '../extension/shells'; import { subagentTools, taskResult } from '../extension/task'; @@ -55,6 +55,9 @@ describe('the provider profile', () => { function registered( env: Record, catalog: () => CatalogModel[] = () => [], + /** `null`: no token arrived. */ + token: string | null = 'sk-value', + warnings: string[] = [], ): { id: string; config: Record } | undefined { let provider: { id: string; config: Record } | undefined; registerProfile( @@ -64,31 +67,62 @@ describe('the provider profile', () => { }) as never, }, env, + token ?? undefined, catalog, + (line) => warnings.push(line), ); return provider; } - it('registers a bound profile at the endpoint the driver resolved, with the token reference', () => { - const provider = registered({ - CODEDECK_PI_PROVIDER: JSON.stringify({ - id: 'codedeck-fake', - label: 'Fake', - baseUrl: 'https://api.example.com/v1', - models: [{ id: 'fake-model-1', label: 'Fake model', contextWindow: 64_000 }], - }), - CODEDECK_PI_PROVIDER_TOKEN: 'sk-value', - }); + const FAKE_PROFILE = JSON.stringify({ + id: 'codedeck-fake', + label: 'Fake', + baseUrl: 'https://api.example.com/v1', + models: [{ id: 'fake-model-1', label: 'Fake model', contextWindow: 64_000 }], + }); + + it('registers a bound profile at the endpoint the driver resolved, with the token as its key', () => { + const provider = registered({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }); expect(provider?.id).toBe('codedeck-fake'); expect(provider?.config).toMatchObject({ name: 'Fake', baseUrl: 'https://api.example.com/v1', api: 'openai-completions', - apiKey: '$CODEDECK_PI_PROVIDER_TOKEN', + apiKey: 'sk-value', models: [expect.objectContaining({ id: 'fake-model-1', name: 'Fake model', contextWindow: 64_000 })], }); - // The secret itself never reaches the registration. - expect(JSON.stringify(provider)).not.toContain('sk-value'); + }); + + it("escapes a token Pi would read as a variable or a command (a real Pi resolves it: piFakeModel)", () => { + expect(configLiteral('sk-plain')).toBe('sk-plain'); + expect(configLiteral('sk$HOME')).toBe('sk$$HOME'); + expect(configLiteral('$$')).toBe('$$$$'); + expect(configLiteral('!cat x')).toBe('$!cat x'); + expect(configLiteral('!a$b')).toBe('$!a$$b'); + }); + + it('registers a profile whose token did not arrive keyless, and says so', () => { + const warnings: string[] = []; + const provider = registered({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, () => [], null, warnings); + expect(provider?.config).not.toHaveProperty('apiKey'); + expect(warnings).toEqual([expect.stringContaining("no token arrived for the provider profile 'codedeck-fake'")]); + }); + + it("reads the token from its pipe only for a Pi started with a profile, and a pipe that is not there gives none", () => { + const fds: number[] = []; + const read = (fd: number): string => { + fds.push(fd); + return 'sk-piped'; + }; + expect(readProviderToken({}, read)).toBeUndefined(); + expect(fds).toEqual([]); + expect(readProviderToken({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, read)).toBe('sk-piped'); + expect(fds).toEqual([3]); + const missing = (): string => { + throw Object.assign(new Error('bad file descriptor'), { code: 'EBADF' }); + }; + expect(readProviderToken({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, missing)).toBeUndefined(); + expect(readProviderToken({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, () => '')).toBeUndefined(); }); it('takes what Pi knows of a model, through a gateway route or a vendor prefix', () => { diff --git a/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts index 34c593eb..47bffe11 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts @@ -43,8 +43,16 @@ interface FakeModelServer { stop(): Promise; } +/** + * The provider profile's token. The model server refuses any other, so a + * turn that runs proves Pi sent this one. It holds what Pi's configuration + * syntax would read as a command (a leading `!`) and as a variable (`$`), so + * it arrives intact only if the extension's escaping matches Pi's resolver. + */ +const PROFILE_TOKEN = '!codedeck$fake-token'; + function startFakeModel(timeoutMs = 15_000): Promise { - const child = spawn(process.execPath, [MODEL_SERVER, '--port', '0'], { stdio: ['ignore', 'pipe', 'pipe'] }); + const child = spawn(process.execPath, [MODEL_SERVER, '--port', '0', '--key', PROFILE_TOKEN], { stdio: ['ignore', 'pipe', 'pipe'] }); return new Promise((resolve, reject) => { let output = ''; const timer = setTimeout(() => { @@ -137,7 +145,7 @@ live('Pi against the fake model', () => { id: 'codedeck-fake', label: 'Fake model', baseUrl: modelUrl, - authToken: 'codedeck-fake-model', + authToken: PROFILE_TOKEN, models: [{ id: FAKE_MODEL_ID, label: 'Fake model' }], defaultModel: FAKE_MODEL_ID, }, @@ -353,6 +361,61 @@ live('Pi against the fake model', () => { TURN_MS, ); + /** + * A shell command that writes its own environment to `env-.txt` and, + * where there is a /proc, the initial environment of the Pi that started it + * to `pi-environ-.txt` — readable by any process of the same user, + * and unchanged by a variable Pi deletes later. Shell builtins only: the + * session's environment has no PATH (`baseEnv: {}`). + */ + const dumpEnvironment = (name: string): string => + `export -p > env-${name}.txt; ` + + `if [ -r /proc/$PPID/environ ]; then while IFS= read -r -d '' v; do echo "$v"; done < /proc/$PPID/environ > pi-environ-${name}.txt; fi; true`; + + /** What a dump holds; the token must be nowhere in it. */ + function readDump(file: string): string { + const text = readFileSync(path.join(cwd, file), 'utf8'); + // The dump is the real thing: Pi's own environment, not an empty file. + expect(text, file).toContain('PI_CODING_AGENT_DIR'); + return text; + } + + it( + "keeps the profile's token out of every process the model starts: its shell, a background shell, and Pi's own environment", + async () => { + const ctx = start({ permission: () => ({ outcome: 'selected', optionId: 'allow' }) }); + await turn(ctx, `/tool ${dumpEnvironment('shell')}`); + await turn(ctx, `/background ${dumpEnvironment('background')}`); + await ctx.waitFor( + (event) => event.type === 'entries' && event.entries.some((entry) => entry.entryType === 'background_task' && entry.status === 'completed'), + TURN_MS, + ); + + for (const name of ['shell', 'background']) { + expect(readDump(`env-${name}.txt`)).not.toContain(PROFILE_TOKEN); + if (process.platform === 'linux') expect(readDump(`pi-environ-${name}.txt`)).not.toContain(PROFILE_TOKEN); + } + // No output shown on the phone carries it either. + expect(JSON.stringify(ctx.events)).not.toContain(PROFILE_TOKEN); + }, + TURN_MS, + ); + + it( + "gives a sub-agent's Pi the token on its pipe, and keeps it out of the sub-agent's shell", + async () => { + const ctx = start({ permission: () => ({ outcome: 'selected', optionId: 'allow' }) }); + await turn(ctx, `/task /tool ${dumpEnvironment('subagent')}`); + + // The sub-agent's own requests carried the token: the model server + // refuses any other, and its command ran. + expect(readDump('env-subagent.txt')).not.toContain(PROFILE_TOKEN); + if (process.platform === 'linux') expect(readDump('pi-environ-subagent.txt')).not.toContain(PROFILE_TOKEN); + expect(JSON.stringify(ctx.events)).not.toContain(PROFILE_TOKEN); + }, + TURN_MS, + ); + it( "sizes a gateway's model from Pi's own catalog, which the extension reads inside Pi", async () => { @@ -368,7 +431,7 @@ live('Pi against the fake model', () => { id: 'codedeck-fake', label: 'Fake gateway', baseUrl: modelUrl, - authToken: 'codedeck-fake-model', + authToken: PROFILE_TOKEN, models: [{ id: routed }], defaultModel: routed, }, diff --git a/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts index d01dd032..b4f2ec48 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts @@ -13,7 +13,7 @@ import { describe, expect, it } from 'vitest'; import { mergeDiagnostics, type DiagnosticsResult, type SpawnServer } from '../extension/lsp/client'; import { changedFile, formatDiagnostics, LSP_ENV, MAX_REPORTED, ProjectServers, registerDiagnostics } from '../extension/lsp/diagnostics'; import { serverFor, type Machine } from '../extension/lsp/servers'; -import { PROVIDER_TOKEN_ENV } from '../extension/provider'; +import { PROVIDER_ENV } from '../extension/provider'; const FAKE_SERVER = fileURLToPath(new URL('./fakeLanguageServer.mjs', import.meta.url)); @@ -295,13 +295,13 @@ describe('diagnostics after a change, per kind of server', () => { expect(killed).toHaveLength(1); }); - it("starts the server without the provider profile's token", async () => { - const p = project('pull', { env: { [PROVIDER_TOKEN_ENV]: 'sk-secret', HOME_MARK: 'kept' } }); + it('starts the server without the provider profile', async () => { + const p = project('pull', { env: { [PROVIDER_ENV]: '{"id":"p"}', HOME_MARK: 'kept' } }); try { - expect(await p.change(`ENV ${PROVIDER_TOKEN_ENV}\nENV HOME_MARK\n`)).toBe( - `rust-analyzer reports 2 errors in main.rs:\nmain.rs:1:1 error E1: ${PROVIDER_TOKEN_ENV} is unset\nmain.rs:2:1 error E1: HOME_MARK is set`, + expect(await p.change(`ENV ${PROVIDER_ENV}\nENV HOME_MARK\n`)).toBe( + `rust-analyzer reports 2 errors in main.rs:\nmain.rs:1:1 error E1: ${PROVIDER_ENV} is unset\nmain.rs:2:1 error E1: HOME_MARK is set`, ); - expect(p.env()?.[PROVIDER_TOKEN_ENV]).toBeUndefined(); + expect(p.env()?.[PROVIDER_ENV]).toBeUndefined(); } finally { p.servers.stopAll(); } diff --git a/packages/agent-host/src/drivers/pi/__tests__/piMemory.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piMemory.test.ts index 39a505a7..5fe00a49 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piMemory.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piMemory.test.ts @@ -40,7 +40,7 @@ function tempDir(prefix = 'codedeck-pi-memory-'): string { return dir; } -function harness(env: Record = {}): { +function harness(env: Record = {}, secrets?: string[]): { home: string; run: (cwd: string, params: Record) => Promise; section: (cwd: string) => string | undefined; @@ -57,7 +57,7 @@ function harness(env: Record = {}): { if (event === 'before_agent_start') hook = handler; }) as never, }, - { home, env }, + { home, env, ...(secrets ? { secrets } : {}) }, ); return { home, @@ -264,6 +264,21 @@ describe('the memory tool', () => { expect(saved.content[0]!.text).toContain("Saved memory 'ordinary'"); }); + it("refuses the provider profile's token, which is in no variable", async () => { + const token = 'sk-profile-token-value'; + const memory = harness({}, [token]); + const message = await refusal(memory.run, tempDir(), { + action: 'save', + name: 'leak', + description: 'A note about the endpoint.', + content: `Use ${token} for the gateway.`, + }); + expect(message).toBe( + "memory refused: the note looks like it contains a secret (the provider profile's token). Save where the secret lives, never the secret itself.", + ); + expect(message).not.toContain(token); + }); + it('keeps two working directories apart, even with the same basename', async () => { const memory = harness(); const root = tempDir(); diff --git a/packages/agent-host/src/drivers/pi/driver.ts b/packages/agent-host/src/drivers/pi/driver.ts index 68904340..8945d215 100644 --- a/packages/agent-host/src/drivers/pi/driver.ts +++ b/packages/agent-host/src/drivers/pi/driver.ts @@ -55,7 +55,7 @@ import type { import { PiAccounts } from './accounts'; import { PiMcp } from './mcp'; import { PiPlugins, piCliRunner, type PiCliRunner } from './plugins'; -import { buildPiEnv } from './env'; +import { buildPiEnv, piProviderToken } from './env'; import { PI_PERMISSION_PREFIX, bundledPiExtension, installPiExtension } from './extension'; import { GATE } from './extension/gate'; import { @@ -242,6 +242,8 @@ export class PiSession implements DriverSession { /** The child's environment. Built synchronously, so a provider profile that * cannot be used refuses the session before it starts. */ private readonly env: Record; + /** The bound profile's token, for Pi's token pipe; never in `env`. */ + private readonly token: string | undefined; /** The running process once it accepts commands; rejected if the session * ends first. An option changed while Pi starts waits on it, rather than * being dropped as if it applied. */ @@ -260,6 +262,7 @@ export class PiSession implements DriverSession { // Nobody may be waiting when the session ends early. this.started.catch(() => {}); this.env = buildPiEnv(params, deps.home, deps.baseEnv); + this.token = piProviderToken(params); this.reportUnknownTool = unknownToolReporter((message) => ctx.log(message)); this.coalescer = new PartialCoalescer((update) => { if (!this.ended) this.ctx.emit({ type: 'partial', ...update }); @@ -290,6 +293,7 @@ export class PiSession implements DriverSession { args: command.args, cwd: this.params.cwd, env: this.env, + ...(this.token !== undefined ? { token: this.token } : {}), onEvent: (record) => this.event(record), onUiRequest: (record) => this.uiRequest(record), onExit: (code, signal) => this.exited(code, signal), diff --git a/packages/agent-host/src/drivers/pi/env.ts b/packages/agent-host/src/drivers/pi/env.ts index 28a5be4d..154c2f39 100644 --- a/packages/agent-host/src/drivers/pi/env.ts +++ b/packages/agent-host/src/drivers/pi/env.ts @@ -7,9 +7,9 @@ * Pi reads its endpoint and key from its own `models.json` or `auth.json` * under `$PI_CODING_AGENT_DIR`, or from an extension registration. A profile * is the second kind: its endpoint, label and models travel as JSON in - * `CODEDECK_PI_PROVIDER`, while the token travels in its own variable - * (`CODEDECK_PI_PROVIDER_TOKEN`) and is referenced from the extension by name, - * so the secret never reaches the file written to disk. + * `CODEDECK_PI_PROVIDER`. Its token is not in this environment at all: the + * driver hands it to the extension on a pipe of its own (rpc.ts), since + * every process Pi starts inherits this environment (extension/provider.ts). * * The profile's `baseUrl` is the endpoint as the user saved it; Pi's * OpenAI-compatible provider needs its versioned API root, so `/v1` is @@ -19,14 +19,12 @@ import { isValidProviderBaseUrl, PROVIDER_BASE_URL_ERROR } from '../../sdk/provider'; import { providerApiRoot } from '../../sdk/providerModels'; import type { ProviderBinding, StartSession } from '../../sdk/types'; -import { PROVIDER_ENV, PROVIDER_TOKEN_ENV } from './extension/provider'; +import { PROVIDER_ENV } from './extension/provider'; import { PI_AGENT_DIR_ENV } from './runtime'; /** The profile (no secret) the extension registers, when a session is bound * to one. */ export const PI_PROVIDER_ENV = PROVIDER_ENV; -/** The profile's auth token, read by the extension through `$`-interpolation. */ -export const PI_PROVIDER_TOKEN_ENV = PROVIDER_TOKEN_ENV; /** The provider registration as the extension's JSON variable carries it. */ export interface PiProviderProfile { @@ -50,13 +48,33 @@ export function toPiProviderProfile(provider: ProviderBinding): PiProviderProfil }; } +/** + * The token a session's Pi gets on its token pipe: the bound profile's, or + * nothing without a profile. Throws for an insecure base URL (checked first: + * it decides whether the token may travel on that connection at all; the URL + * is not a secret) or a profile with no token. + */ +export function piProviderToken(params: Pick): string | undefined { + const provider = params.provider ?? undefined; + if (!provider) return undefined; + if (!isValidProviderBaseUrl(provider.baseUrl)) { + throw new Error( + `provider profile '${provider.id}' has an insecure base URL (${provider.baseUrl}) — ` + + `${PROVIDER_BASE_URL_ERROR}. Its API token would travel in cleartext, so the session is refused.`, + ); + } + if (!provider.authToken) throw new Error(`provider profile '${provider.id}' has no stored auth token`); + return provider.authToken; +} + /** * The environment for one spawn. Without a provider: everything inherited, * with the stored or inherited Pi credentials untouched (Pi's own provider * setup in its home applies) and the bridge-level `env` on top (e.g. * `GITHUB_TOKEN`, stored wins). With a provider: the profile's own endpoint - * and token replace any such variable a session might otherwise inherit, so - * the session cannot quietly reach another account. + * replaces any such variable a session might otherwise inherit, so the + * session cannot quietly reach another account; its token goes on the pipe + * (`piProviderToken`). * * `PI_CODING_AGENT_DIR` is always this driver's, set last: a session that * moved it would lose the conversation it is resuming. Throws for an @@ -73,21 +91,11 @@ export function buildPiEnv( if (value !== undefined) env[key] = value; } delete env[PI_PROVIDER_ENV]; - delete env[PI_PROVIDER_TOKEN_ENV]; const provider = params.provider ?? undefined; if (provider) { - // Checked before the token: this decides whether the token may travel on - // this connection at all. The base URL is not a secret. - if (!isValidProviderBaseUrl(provider.baseUrl)) { - throw new Error( - `provider profile '${provider.id}' has an insecure base URL (${provider.baseUrl}) — ` + - `${PROVIDER_BASE_URL_ERROR}. Its API token would travel in cleartext, so the session is refused.`, - ); - } - if (!provider.authToken) throw new Error(`provider profile '${provider.id}' has no stored auth token`); + piProviderToken(params); env[PI_PROVIDER_ENV] = JSON.stringify(toPiProviderProfile(provider)); - env[PI_PROVIDER_TOKEN_ENV] = provider.authToken; } Object.assign(env, params.env ?? {}); diff --git a/packages/agent-host/src/drivers/pi/extension/index.ts b/packages/agent-host/src/drivers/pi/extension/index.ts index 2cb3d870..a4d4a06e 100644 --- a/packages/agent-host/src/drivers/pi/extension/index.ts +++ b/packages/agent-host/src/drivers/pi/extension/index.ts @@ -25,7 +25,7 @@ import { installGate } from './gate'; import { registerMemory } from './memory'; import { registerPlanMode } from './plan'; import { PI_ENTRY_TYPE } from './protocol'; -import { registerProfile } from './provider'; +import { readProviderToken, registerProfile } from './provider'; import { installShellCheck } from './shells'; import { registerTask, SUBAGENT_ENV } from './task'; import { registerTodo } from './todo'; @@ -42,7 +42,10 @@ function builtinCatalog(): CatalogModel[] { } export default function codedeckPi(pi: ExtensionAPI): void { - registerProfile(pi, process.env, builtinCatalog); + // First, before anything can start a process: the token's pipe is read and + // closed here, and the token is passed on only to what needs it. + const token = readProviderToken(process.env); + registerProfile(pi, process.env, token, builtinCatalog); installGate(pi); installShellCheck(pi, () => { try { @@ -67,7 +70,7 @@ export default function codedeckPi(pi: ExtensionAPI): void { registerAsk(pi); registerPlanMode(pi); registerWebFetch(pi); - registerTask(pi, process.env); + registerTask(pi, process.env, token); registerBackground(pi, { shells: { bash: () => getShellConfig(), powershell: () => getPowerShellConfig() }, active: () => pi.getActiveTools(), @@ -76,5 +79,5 @@ export default function codedeckPi(pi: ExtensionAPI): void { // The notes live in Pi's home, which the driver always names; without it // there is nowhere that is the machine's own to keep them. const home = process.env.PI_CODING_AGENT_DIR; - if (home) registerMemory(pi, { home, env: process.env }); + if (home) registerMemory(pi, { home, env: process.env, ...(token !== undefined ? { secrets: [token] } : {}) }); } diff --git a/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts b/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts index db663746..9fc6b4ca 100644 --- a/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts +++ b/packages/agent-host/src/drivers/pi/extension/lsp/diagnostics.ts @@ -17,7 +17,7 @@ import { readFile } from 'node:fs/promises'; import { homedir } from 'node:os'; import * as path from 'node:path'; import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'; -import { PROVIDER_ENV, PROVIDER_TOKEN_ENV } from '../provider'; +import { PROVIDER_ENV } from '../provider'; import { LspClient, spawnServer, type DiagnosticsResult, type SpawnServer } from './client'; import { machine, serverFor, type Machine, type ServerChoice } from './servers'; @@ -119,10 +119,11 @@ export class ProjectServers { constructor(deps: DiagnosticsDeps = {}) { // A server runs the project's build (`cargo check` runs build scripts): - // it gets Pi's environment without the provider profile's token. + // it gets Pi's environment without the provider profile, which is the + // extension's alone. The profile's token is never in that environment + // (provider.ts). const env = { ...(deps.env ?? process.env) }; delete env[PROVIDER_ENV]; - delete env[PROVIDER_TOKEN_ENV]; this.env = env; this.machine = deps.machine ?? machine(env); this.spawn = deps.spawn ?? spawnServer; diff --git a/packages/agent-host/src/drivers/pi/extension/memory.ts b/packages/agent-host/src/drivers/pi/extension/memory.ts index d6e13e83..c1fa6e0b 100644 --- a/packages/agent-host/src/drivers/pi/extension/memory.ts +++ b/packages/agent-host/src/drivers/pi/extension/memory.ts @@ -197,10 +197,11 @@ function regenerateIndex(dir: string): void { } /** The first credential shape in `text`, named without its value. */ -function findSecret(text: string, env: Record): string | undefined { +function findSecret(text: string, env: Record, secrets: readonly string[]): string | undefined { for (const { kind, pattern } of SECRET_PATTERNS) { if (pattern.test(text)) return kind; } + if (secrets.some((secret) => secret !== '' && text.includes(secret))) return "the provider profile's token"; for (const [name, value] of Object.entries(env)) { if (!SECRET_ENV_NAME.test(name)) continue; if (typeof value !== 'string' || value.length < MIN_SECRET_ENV_VALUE) continue; @@ -261,9 +262,16 @@ function requireSaveText(params: MemoryParams): { description: string; content: */ export function registerMemory( pi: Pick, - opts: { home: string; env: Record }, + opts: { + home: string; + env: Record; + /** Secrets held outside the environment (the provider profile's token), + * refused like a secret variable's value. */ + secrets?: readonly string[]; + }, ): void { const { home, env } = opts; + const secrets = opts.secrets ?? []; const tool = { name: PI_TOOL.memory, @@ -299,7 +307,7 @@ export function registerMemory( if (action === 'save') { const { description, content } = requireSaveText(params); - const secret = findSecret([name, description, content].join('\n'), env); + const secret = findSecret([name, description, content].join('\n'), env, secrets); if (secret !== undefined) { throw new Error( `memory refused: the note looks like it contains a secret (${secret}). Save where the secret lives, never the secret itself.`, diff --git a/packages/agent-host/src/drivers/pi/extension/provider.ts b/packages/agent-host/src/drivers/pi/extension/provider.ts index 606ba738..9e908d55 100644 --- a/packages/agent-host/src/drivers/pi/extension/provider.ts +++ b/packages/agent-host/src/drivers/pi/extension/provider.ts @@ -3,19 +3,65 @@ * its own providers. * * The driver passes the profile (no secret) as JSON in `CODEDECK_PI_PROVIDER` - * and the token in `CODEDECK_PI_PROVIDER_TOKEN` (env.ts). The registration - * names the token variable, `$CODEDECK_PI_PROVIDER_TOKEN`, and Pi reads it - * from the environment when it calls the endpoint, so the secret never - * reaches the extension file or Pi's configuration. The profile's models - * become selectable as `/`. + * (env.ts), and the token on a pipe of its own: file descriptor 3, which it + * writes once and closes as Pi starts. The token is never in Pi's + * environment, because everything Pi starts inherits that environment — the + * model's shells, background shells, MCP servers — and on Linux any process + * of the same user can read Pi's initial environment back from + * `/proc//environ`, even after Pi deleted the variable. A pipe is + * inherited by nothing: Node passes a child only the stdio it is given. + * + * The extension reads the pipe once, as Pi loads it, and registers the token + * as the provider's key, so it lives in Pi's memory only. The profile's + * models become selectable as `/`. */ +import { closeSync, readFileSync } from 'node:fs'; import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'; import { describeModel, type CatalogModel } from './catalog'; /** The variable the profile travels in (env.ts sets it). */ export const PROVIDER_ENV = 'CODEDECK_PI_PROVIDER'; -/** The variable the profile's token travels in, named in the registration. */ -export const PROVIDER_TOKEN_ENV = 'CODEDECK_PI_PROVIDER_TOKEN'; +/** The file descriptor the profile's token arrives on, for a Pi started with + * a profile: the driver's spawn (rpc.ts) and a sub-agent's (task.ts) put a + * pipe there. */ +export const PROVIDER_TOKEN_FD = 3; + +/** + * The profile's token, read from its pipe to the end. Only a Pi started with + * a profile has the pipe, so nothing is read without one. A pipe that is not + * there or cannot be read gives no token: the session then fails on its + * first request, saying it has no key, rather than at load. + */ +export function readProviderToken( + env: Record, + read: (fd: number) => string = (fd) => { + try { + return readFileSync(fd, 'utf8'); + } finally { + closeSync(fd); + } + }, +): string | undefined { + if (!env[PROVIDER_ENV]) return undefined; + try { + const token = read(PROVIDER_TOKEN_FD); + return token === '' ? undefined : token; + } catch { + return undefined; + } +} + +/** + * A value Pi takes literally where it reads a configuration value. Pi + * resolves an API key as a template: `$NAME` is an environment variable and a + * leading `!` a shell command (its resolve-config-value), so a token holding + * either would be read as one. `$$` is Pi's literal `$` and `$!` its literal + * `!`. + */ +export function configLiteral(value: string): string { + const escaped = value.replace(/\$/g, '$$$$'); + return escaped.startsWith('!') ? `$${escaped}` : escaped; +} /** Context and output sizes for a model nothing describes. */ export const FALLBACK_CONTEXT_WINDOW = 128_000; @@ -95,24 +141,31 @@ export function profileModel(model: ProfileModel, catalog: readonly CatalogModel } /** - * Register the bound profile, when there is one. A malformed profile is not - * a reason to keep Pi from starting: the session then runs Pi's own models - * and reports what it cannot do. `catalog` is read only when there is a - * profile to describe. + * Register the bound profile, when there is one, with `token` as its key. A + * malformed profile is not a reason to keep Pi from starting: the session + * then runs Pi's own models and reports what it cannot do. Without a token + * the profile is registered keyless, said once on stderr (the driver logs + * it), and Pi refuses its requests for want of a key. `catalog` is read only + * when there is a profile to describe. */ export function registerProfile( pi: Pick, env: Record, + token: string | undefined, catalog: () => readonly CatalogModel[] = () => [], + warn: (line: string) => void = (line) => process.stderr.write(`${line}\n`), ): void { const profile = readProfile(env); if (!profile) return; + if (token === undefined) { + warn(`codedeck: no token arrived for the provider profile '${profile.id}' (file descriptor ${PROVIDER_TOKEN_FD})`); + } const known = catalog(); pi.registerProvider(profile.id, { name: profile.label, baseUrl: profile.baseUrl, api: PROFILE_API, - apiKey: '$' + PROVIDER_TOKEN_ENV, + ...(token !== undefined ? { apiKey: configLiteral(token) } : {}), models: profile.models.map((model) => profileModel(model, known)) as never, }); } diff --git a/packages/agent-host/src/drivers/pi/extension/task.ts b/packages/agent-host/src/drivers/pi/extension/task.ts index 187acb47..f1c6d0e3 100644 --- a/packages/agent-host/src/drivers/pi/extension/task.ts +++ b/packages/agent-host/src/drivers/pi/extension/task.ts @@ -23,8 +23,10 @@ * built-in tools. */ import { spawn } from 'node:child_process'; +import type { Writable } from 'node:stream'; import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'; import { request, type RequestUi } from './channel'; +import { PROVIDER_TOKEN_FD } from './provider'; import { PI_PERMISSION_PREFIX, PI_TOOL, @@ -91,6 +93,9 @@ export interface ChildOptions { thinking?: string; cwd: string; env: Record; + /** The provider profile's token, handed to the child on its own pipe + * (provider.ts), never in its environment. */ + token?: string; prompt: string; subagent: PiSubagent; ui: RequestUi; @@ -118,8 +123,14 @@ export function runSubagent(options: ChildOptions): Promise<{ text: string; erro const child = (options.spawnFn ?? spawn)(options.command, args, { cwd: options.cwd, env: { ...options.env, [SUBAGENT_ENV]: options.kind }, - stdio: ['pipe', 'pipe', 'pipe'], + stdio: options.token !== undefined ? ['pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'], }); + if (options.token !== undefined) { + const pipe = child.stdio[PROVIDER_TOKEN_FD] as Writable | null | undefined; + // A child that died before reading it closes the pipe; its exit says why. + pipe?.on('error', () => {}); + pipe?.end(options.token); + } return new Promise((resolve) => { const facts = new Map }>(); @@ -297,7 +308,11 @@ export function subagentTools(kind: SubagentKind, active: readonly string[]): st return active.includes(PI_TOOL.webFetch) ? [...builtins, PI_TOOL.webFetch] : builtins; } -export function registerTask(pi: Pick, env: Record): void { +export function registerTask( + pi: Pick, + env: Record, + token?: string, +): void { pi.registerTool({ name: PI_TOOL.task, label: 'Sub-agent', @@ -337,6 +352,7 @@ export function registerTask(pi: Pick; stdio: ['pipe', 'pipe', 'pipe'] }, + options: { cwd: string; env: Record; stdio: ['pipe', 'pipe', 'pipe'] | ['pipe', 'pipe', 'pipe', 'pipe'] }, ) => ChildProcess; /** How long a command may go unanswered before its promise rejects. A local @@ -111,6 +114,10 @@ export interface PiRpcOptions { args: string[]; cwd: string; env: Record; + /** The provider profile's token, written once to the child's token pipe + * (file descriptor 3) and closed — never in `env`, which every process + * Pi starts inherits. A secret: never logged. */ + token?: string; /** A session event (everything that is neither a command response nor an * extension UI request). */ onEvent(record: PiRecord): void; @@ -141,8 +148,14 @@ export class PiRpc { this.child = spawnFn(options.command, options.args, { cwd: options.cwd, env: options.env, - stdio: ['pipe', 'pipe', 'pipe'], + stdio: options.token !== undefined ? ['pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'], }); + if (options.token !== undefined) { + const pipe = this.child.stdio[PROVIDER_TOKEN_FD] as Writable | null | undefined; + // A child that died before reading it closes the pipe; its exit says why. + pipe?.on('error', () => {}); + pipe?.end(options.token); + } const stdout = new PiLineDecoder((record) => this.route(record), options.onBadLine); this.child.stdout?.on('data', (chunk: Buffer) => stdout.push(chunk)); diff --git a/packages/agent-host/tools/__tests__/fakeModel.test.mjs b/packages/agent-host/tools/__tests__/fakeModel.test.mjs index 15eafa5b..2f652807 100644 --- a/packages/agent-host/tools/__tests__/fakeModel.test.mjs +++ b/packages/agent-host/tools/__tests__/fakeModel.test.mjs @@ -7,7 +7,7 @@ * required `questions` the call does not carry. */ import { describe, expect, it } from 'vitest'; -import { planReply } from '../fake-model.mjs'; +import { planReply, startFakeModel } from '../fake-model.mjs'; const HARNESS_TOOLS = (shell) => ['ask_user_question', 'create_goal', 'edit', 'glob', 'grep', shell, 'read', 'write'].map((name) => ({ @@ -35,6 +35,28 @@ describe('/write', () => { }); }); +describe('a required key', () => { + it('answers a model request only when it carries the key, as a bearer or an x-api-key', async () => { + const model = await startFakeModel({ key: 'sk-required' }); + try { + const ask = (headers) => + fetch(`${model.url}/v1/chat/completions`, { + method: 'POST', + headers: { 'content-type': 'application/json', ...headers }, + body: JSON.stringify({ messages: [{ role: 'user', content: 'hi' }] }), + }); + const missing = await ask({}); + expect(missing.status).toBe(401); + expect(await missing.text()).not.toContain('sk-'); + expect((await ask({ authorization: 'Bearer sk-other' })).status).toBe(401); + expect((await ask({ authorization: 'Bearer sk-required' })).status).toBe(200); + expect((await ask({ 'x-api-key': 'sk-required' })).status).toBe(200); + } finally { + await model.close(); + } + }); +}); + describe('/tool', () => { for (const shell of ['bash', 'pwsh']) { it(`calls the Harness's ${shell} with the command and description it requires`, () => { diff --git a/packages/agent-host/tools/fake-model.mjs b/packages/agent-host/tools/fake-model.mjs index 387abcbc..2034e407 100644 --- a/packages/agent-host/tools/fake-model.mjs +++ b/packages/agent-host/tools/fake-model.mjs @@ -62,7 +62,11 @@ * * Run standalone for poking at it by hand: * - * node tools/fake-model.mjs [--port N] [--host IP] [--log PATH] [--dump DIR] + * node tools/fake-model.mjs [--port N] [--host IP] [--log PATH] [--dump DIR] [--key KEY] + * + * With a key, a model request that does not carry it (`Authorization: Bearer` + * or `x-api-key`) is answered 401 — for a test that must prove an agent sent + * the key it was given, not just some key. Without one, any key is taken. * * or in-process, which is what `./codedeck live --fake-model` does * (live-probe.mjs starts it, binds the session to it and closes it again): @@ -705,6 +709,7 @@ export function estimateTokens(body) { export function startFakeModel(options = {}) { const log = options.log ?? (() => {}); const dump = options.dump ?? null; + const key = options.key ?? null; if (dump) mkdirSync(dump, { recursive: true }); let requests = 0; @@ -760,6 +765,13 @@ export function startFakeModel(options = {}) { res.end(JSON.stringify(errorBody('anthropic', `the fake model serves /v1/messages and /v1/chat/completions, not ${req.method} ${route}`))); return; } + if (key !== null && req.headers.authorization !== `Bearer ${key}` && req.headers['x-api-key'] !== key) { + // Never echoes what was sent: it may be a real key sent by mistake. + res.writeHead(401, { 'content-type': 'application/json' }); + res.end(JSON.stringify(errorBody(shape, 'the fake model was not sent the key it requires'))); + log('-> 401 wrong or missing key'); + return; + } // Guarded exactly as `replyFor` is, and for the same reason: a request // an agent makes for its own purposes is not the user's turn, however @@ -846,6 +858,7 @@ function main() { port: Number(value('port', 0)), host: value('host', '127.0.0.1'), dump: value('dump', null), + key: value('key', null), log, }).then((model) => { log('Ctrl-C to stop');