diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/configured_environment.rs b/packages/paperclip-runner/runner/crates/runner-core/src/configured_environment.rs index a2d222436b1..d3641667532 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/configured_environment.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/configured_environment.rs @@ -3,6 +3,8 @@ use crate::local_runner::LocalRunnerError; use std::process::Command; const MARKER: &str = "PAPERCLIP_CONFIGURED_ENV_KEYS"; +/// Names (never values) of configured entries resolved from secrets. +const SECRET_MARKER: &str = "PAPERCLIP_SECRET_ENV_KEYS"; const RESERVED: &[&str] = &[ "PAPERCLIP_OBSERVABILITY_CODEX_CONFIG", "PAPERCLIP_OBSERVABILITY_OPENCODE", @@ -71,6 +73,7 @@ const RESERVED: &[&str] = &[ "PAPERCLIP_APPROVAL_STATUS", "PAPERCLIP_COMPANY_ID", "PAPERCLIP_CONFIGURED_ENV_KEYS", + "PAPERCLIP_SECRET_ENV_KEYS", "PAPERCLIP_EXECUTION_MODE", "PAPERCLIP_EXTERNAL_CHAT_EXECUTION_BOUND_KEY", "PAPERCLIP_GITHUB_AUTH_MODE", @@ -184,11 +187,37 @@ pub(crate) fn apply_configured_environment( } values.push((name, value)); } + // The secret-name marker travels with the projection it describes. Only + // names that this projection carries may cross. + let secret_names = match lookup(SECRET_MARKER) { + None => None, + Some(raw) => { + if raw.len() > 20_000 { + return Err(invalid()); + } + let secret: Vec = serde_json::from_str(&raw).map_err(|_| invalid())?; + if secret.len() > 128 || secret.iter().any(|name| !eligible_key(name)) { + return Err(invalid()); + } + Some( + secret + .into_iter() + .filter(|name| names.contains(name)) + .collect::>(), + ) + } + }; command.envs(values); command.env( MARKER, serde_json::to_string(&names).map_err(|_| invalid())?, ); + if let Some(secret) = secret_names { + command.env( + SECRET_MARKER, + serde_json::to_string(&secret).map_err(|_| invalid())?, + ); + } Ok(()) } @@ -247,6 +276,45 @@ mod tests { ); } + #[test] + fn secret_name_marker_crosses_only_with_its_projection() { + let source = BTreeMap::from([ + (MARKER, "[\"CUSTOM\",\"PLAIN\"]".to_string()), + ("CUSTOM", "custom-secret-value".to_string()), + ("PLAIN", "plain".to_string()), + (SECRET_MARKER, "[\"CUSTOM\",\"NOT_CONFIGURED\"]".to_string()), + ]); + let mut command = Command::new("unused"); + apply_configured_environment(&mut command, |key| source.get(key).cloned()).unwrap(); + let envs: BTreeMap<_, _> = command + .get_envs() + .map(|(key, value)| { + ( + key.to_string_lossy().into_owned(), + value.map(|v| v.to_string_lossy().into_owned()), + ) + }) + .collect(); + assert_eq!( + envs.get(SECRET_MARKER), + Some(&Some("[\"CUSTOM\"]".to_string())) + ); + for raw in ["not-json", "[\"bad=name\"]", "[\"PAPERCLIP_API_KEY\"]"] { + let mut source = source.clone(); + source.insert(SECRET_MARKER, raw.into()); + let mut command = Command::new("unused"); + assert!( + apply_configured_environment(&mut command, |key| source.get(key).cloned()).is_err() + ); + assert_eq!(command.get_envs().count(), 0); + } + let mut legacy = source.clone(); + legacy.remove(SECRET_MARKER); + let mut command = Command::new("unused"); + apply_configured_environment(&mut command, |key| legacy.get(key).cloned()).unwrap(); + assert!(command.get_envs().all(|(key, _)| key != SECRET_MARKER)); + } + #[test] #[cfg(unix)] fn child_receives_only_selected_task_values_across_configuration_changes() { diff --git a/packages/paperclip-runner/scripts/qualify-opencode-runtime.test.mjs b/packages/paperclip-runner/scripts/qualify-opencode-runtime.test.mjs index 9505c8952c9..90434353727 100644 --- a/packages/paperclip-runner/scripts/qualify-opencode-runtime.test.mjs +++ b/packages/paperclip-runner/scripts/qualify-opencode-runtime.test.mjs @@ -3,7 +3,7 @@ import assert from "node:assert/strict"; import { spawn, execFileSync } from "node:child_process"; import { once } from "node:events"; -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { createServer } from "node:http"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; @@ -144,3 +144,113 @@ test("the pinned OpenCode executable serves health, sessions, SSE, and a local-p await readEvents; assert.equal((await api(`/session/${session.id}`, { method: "DELETE" })).status, 200); }); + +// Output custody against the real executable: a command that prints a +// configured secret and a file read of that secret must reach neither the +// model request nor OpenCode's session store. Requires `pnpm build:typescript`. +for (const custody of [false, true]) { + test(`configured secrets ${custody ? "stay out of" : "reach (unprotected baseline)"} OpenCode's store and model requests`, { + skip: !enabled, + timeout: 90_000, + }, async (t) => { + const { prepareOpenCodeOutputCustody } = await import("../dist/drivers/opencode/output-custody.js"); + const secret = `synthetic-qualification-secret-${Date.now().toString(36)}`; + const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-custody-")); + const workspace = join(root, "workspace"); + await mkdir(workspace); + let child; + let step = 0; + const bodies = []; + const provider = createServer(async (request, response) => { + const chunks = []; + for await (const chunk of request) chunks.push(chunk); + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + const body = JSON.parse(raw || "{}"); + response.writeHead(200, { "Content-Type": "text/event-stream" }); + const call = (name, args) => [{ role: "assistant", tool_calls: [{ index: 0, id: `call_${step}`, type: "function", function: { name, arguments: JSON.stringify(args) } }] }, null]; + let frames = [[{ role: "assistant", content: "title" }, null], [{}, "stop"]]; + if (Array.isArray(body.tools) && body.tools.length > 0) { + step += 1; + frames = step === 1 ? [call("bash", { command: "printenv QUALIFY_SECRET; printenv QUALIFY_SECRET > leak.txt; exit 3", description: "print" }), [{}, "tool_calls"]] + : step === 2 ? [call("read", { filePath: join(workspace, "leak.txt") }), [{}, "tool_calls"]] + : [[{ role: "assistant", content: "custody-done" }, null], [{}, "stop"]]; + } + for (const [delta, finish_reason] of frames) response.write(`data: ${JSON.stringify({ id: "q", object: "chat.completion.chunk", created: 1, model: "q", choices: [{ index: 0, delta, finish_reason }] })}\n\n`); + response.end("data: [DONE]\n\n"); + }); + t.after(async () => { + if (child?.pid) { try { process.kill(-child.pid, "SIGKILL"); } catch {} } + provider.closeAllConnections(); + await new Promise((done) => provider.close(done)); + await rm(root, { recursive: true, force: true }); + }); + provider.listen(0, "127.0.0.1"); + await once(provider, "listening"); + const configHome = join(root, "config"); + await mkdir(join(configHome, "opencode"), { recursive: true }); + const env = { + PATH: `${dirname(process.execPath)}:${process.env.PATH ?? "/usr/bin:/bin"}`, + HOME: root, XDG_CONFIG_HOME: configHome, XDG_DATA_HOME: join(root, "data"), XDG_CACHE_HOME: join(root, "cache"), + OPENCODE_DISABLE_PROJECT_CONFIG: "true", OPENCODE_DISABLE_MODELS_FETCH: "true", OPENCODE_DISABLE_DEFAULT_PLUGINS: "true", + OPENCODE_SERVER_USERNAME: "paperclip", OPENCODE_SERVER_PASSWORD: "local-fixture-only", QUALIFY_SECRET: secret, + }; + const config = { + model: "openrouter/q", small_model: "openrouter/q", share: "disabled", autoupdate: false, plugin: [], instructions: [], + enabled_providers: ["openrouter"], + provider: { openrouter: { options: { baseURL: `http://127.0.0.1:${provider.address().port}/v1`, apiKey: "local-fixture-only" }, models: { q: { name: "q", limit: { context: 100000, output: 1000 } } } } }, + permission: { "*": "allow", external_directory: { "*": "allow" } }, + }; + if (custody) { + const prepared = await prepareOpenCodeOutputCustody({ directory: join(root, "custody"), binDirectory: join(root, "bin"), + configDirectory: join(configHome, "opencode"), environment: env, secrets: [{ name: "QUALIFY_SECRET", value: secret }] }); + Object.assign(config, prepared.config); + Object.assign(env, prepared.environment); + } + await writeFile(join(configHome, "opencode", "opencode.json"), JSON.stringify(config)); + const command = resolve(process.env.PAPERCLIP_TEST_OPENCODE_BINARY ?? join(packageRoot, "node_modules/opencode-ai/bin/opencode.exe")); + child = spawn(command, ["serve", "--hostname", "127.0.0.1", "--port", "0"], { cwd: workspace, env, stdio: ["ignore", "pipe", "pipe"], detached: true }); + let output = ""; + for (const stream of [child.stdout, child.stderr]) stream.on("data", (chunk) => { output = `${output}${chunk}`.slice(-16384); }); + let baseUrl; + for (let attempt = 0; attempt < 150 && !baseUrl; attempt++) { baseUrl = output.match(/http:\/\/127\.0\.0\.1:\d+/)?.[0]; if (!baseUrl) await delay(100); } + assert.ok(baseUrl, `OpenCode did not start: ${output}`); + const headers = { Authorization: `Basic ${Buffer.from("paperclip:local-fixture-only").toString("base64")}`, "Content-Type": "application/json" }; + const api = async (path, options = {}) => { + const response = await fetch(`${baseUrl}${path}`, { headers, signal: AbortSignal.timeout(15_000), ...options }); + assert.ok(response.ok, `${path}: ${response.status}`); + return response; + }; + const session = await (await api("/session", { method: "POST", body: "{}" })).json(); + await api(`/session/${session.id}/prompt_async`, { method: "POST", body: JSON.stringify({ model: { providerID: "openrouter", modelID: "q" }, parts: [{ type: "text", text: "go" }] }) }); + let messages = []; + for (let attempt = 0; attempt < 300; attempt++) { + messages = await (await api(`/session/${session.id}/message`)).json(); + if (messages.some((message) => message.parts.some((part) => part.type === "text" && part.text === "custody-done"))) break; + await delay(100); + } + const tools = messages.flatMap((message) => message.parts.filter((part) => part.type === "tool")); + assert.equal(tools.length, 2, JSON.stringify(tools)); + assert.equal(tools[0].state.metadata?.exit, 3); + process.kill(-child.pid, "SIGTERM"); + await once(child, "exit"); + const persisted = []; + const scan = async (directory) => { + for (const entry of await readdir(directory, { withFileTypes: true })) { + const file = join(directory, entry.name); + if (entry.isDirectory()) await scan(file); + else if (entry.isFile() && (await readFile(file)).includes(secret)) persisted.push(file); + } + }; + await scan(join(root, "data")); + const sent = bodies.filter((body) => body.includes(secret)).length; + if (custody) { + assert.deepEqual(persisted, []); + assert.equal(sent, 0); + assert.match(String(tools[0].state.output), /\[REDACTED\]/); + assert.match(String(tools[1].state.output), /\[REDACTED\]/); + } else { + assert.ok(persisted.length > 0 && sent > 0, "the unprotected baseline must reproduce the leak"); + } + }); +} diff --git a/packages/paperclip-runner/src/configured-environment.test.ts b/packages/paperclip-runner/src/configured-environment.test.ts index cb500f5570a..0704ddd4e03 100644 --- a/packages/paperclip-runner/src/configured-environment.test.ts +++ b/packages/paperclip-runner/src/configured-environment.test.ts @@ -1,4 +1,4 @@ -import { CONFIGURED_ENVIRONMENT_KEYS, configuredEnvironment, configuredEnvironmentProjection } from './configured-environment.js'; +import { CONFIGURED_ENVIRONMENT_KEYS, SECRET_ENVIRONMENT_KEYS, configuredEnvironment, configuredEnvironmentProjection, configuredSecretEnvironment } from './configured-environment.js'; import { describe, expect, it, vi } from 'vitest'; import { createSanitizedCodexEnvironment } from './drivers/codex/app-server-transport.js'; import { createIsolatedCodexAppServerArgs } from './drivers/codex/codex-security-config.js'; @@ -132,3 +132,35 @@ describe("configured environment authority", () => { } finally { vi.unstubAllEnvs(); } }); }); + +describe("configured secret names", () => { + const secretSource: NodeJS.ProcessEnv = { ...configuredEnvironmentProjection({ PUBLISHING_API_KEY: "delivered-secret-value", PLAIN_SETTING: "plain-value" }, ["PUBLISHING_API_KEY", "NOT_CONFIGURED"]), PATH: "/bin" }; + + it("marks only configured entries resolved from secrets, by name", () => { + expect(JSON.parse(secretSource[SECRET_ENVIRONMENT_KEYS]!)).toEqual(["PUBLISHING_API_KEY"]); + expect(secretSource[SECRET_ENVIRONMENT_KEYS]).not.toContain("delivered-secret-value"); + expect(configuredSecretEnvironment(secretSource)).toEqual([{ name: "PUBLISHING_API_KEY", value: "delivered-secret-value" }]); + expect(configuredEnvironmentProjection({ PLAIN_SETTING: "plain-value" })[SECRET_ENVIRONMENT_KEYS]).toBeUndefined(); + }); + + it("reads a projection from a controller that predates the marker as having no secrets", () => { + const legacy = { ...secretSource }; + delete legacy[SECRET_ENVIRONMENT_KEYS]; + expect(configuredSecretEnvironment(legacy)).toEqual([]); + }); + + it.each(["not-json", '["bad=name"]', '["PAPERCLIP_API_KEY"]'])("refuses a malformed secret marker %s", raw => { + expect(() => configuredSecretEnvironment({ ...secretSource, [SECRET_ENVIRONMENT_KEYS]: raw })).toThrow("Invalid secret environment projection"); + }); + + it("carries the secret names across the runner spawn and OpenCode runner boundaries", () => { + let launched: NodeJS.ProcessEnv | undefined; + spawnRunner({ connection: { mode: 'connect', connectUrl: 'ws://127.0.0.1:43127' }, stateDirectory: '/tmp/runner-env-proof', identity, ticket: 'fake-ticket', maxOutboxBytes: 262144, p0ReserveBytes: 65536, runnerVersion: '0.3.0', runnerDigest: `sha256:${'a'.repeat(64)}`, environment: secretSource, + processLauncher: spec => { launched = spec.environment; return { child: { pid: 42, exitCode: null, signalCode: null, kill: () => true }, completion: Promise.resolve({ code: 0, signal: null, stdout: '', stderr: '' }) }; }, + }); + expect(configuredSecretEnvironment(launched)).toEqual([{ name: "PUBLISHING_API_KEY", value: "delivered-secret-value" }]); + const opencode = createCapabilityRunnerdProviderEnvironment({ provider: "opencode", options: { environment: secretSource }, identity, codexHome: "/isolated/home", runtimeContextPath: "/isolated/context", hasRuntimeContext: false }); + expect(configuredSecretEnvironment(opencode)).toEqual([{ name: "PUBLISHING_API_KEY", value: "delivered-secret-value" }]); + expect(JSON.parse(opencode[CONFIGURED_ENVIRONMENT_KEYS]!)).not.toContain(SECRET_ENVIRONMENT_KEYS); + }); +}); diff --git a/packages/paperclip-runner/src/configured-environment.ts b/packages/paperclip-runner/src/configured-environment.ts index 5cd30c0e765..643ae914edb 100644 --- a/packages/paperclip-runner/src/configured-environment.ts +++ b/packages/paperclip-runner/src/configured-environment.ts @@ -4,9 +4,12 @@ import { AGENT_OBSERVABILITY_ENV_KEYS } from "./drivers/agent-observability.js"; * Do not mint this projection from ambient process.env. */ export const CONFIGURED_ENVIRONMENT_KEYS = "PAPERCLIP_CONFIGURED_ENV_KEYS"; +/** Names, never values, of configured entries the controller resolved from secrets. */ +export const SECRET_ENVIRONMENT_KEYS = "PAPERCLIP_SECRET_ENV_KEYS"; export const GENERATED_RUNTIME_ENVIRONMENT_KEYS: ReadonlySet = new Set([ CONFIGURED_ENVIRONMENT_KEYS, + SECRET_ENVIRONMENT_KEYS, ...AGENT_OBSERVABILITY_ENV_KEYS, "PAPERCLIP_AGENT_ID", "PAPERCLIP_AGENT_KEY_ID", "PAPERCLIP_AGENT_PUBLIC_KEY", "PAPERCLIP_AGENT_PRIVATE_KEY", "PAPERCLIP_API_KEY", "PAPERCLIP_API_URL", "PAPERCLIP_API_BRIDGE_MODE", "PAPERCLIP_COMPANY_ID", "PAPERCLIP_INSTANCE_ID", @@ -51,10 +54,35 @@ function checkedNames(names: unknown): string[] { return (names as string[]).sort(); } -/** Mint at the server's resolved configuration boundary, before host inheritance. */ -export function configuredEnvironmentProjection(source: NodeJS.ProcessEnv): NodeJS.ProcessEnv { +/** Mint at the server's resolved configuration boundary, before host inheritance. + * `secretKeys` marks which configured entries came from secrets; only their + * names cross the boundary, beside the values the projection already carries. */ +export function configuredEnvironmentProjection(source: NodeJS.ProcessEnv, secretKeys: Iterable = []): NodeJS.ProcessEnv { const names = checkedNames(Object.keys(source).filter(key => source[key] !== undefined && eligibleKey(key))); - return configuredEnvironment({ ...source, [CONFIGURED_ENVIRONMENT_KEYS]: JSON.stringify(names) }); + const projection = configuredEnvironment({ ...source, [CONFIGURED_ENVIRONMENT_KEYS]: JSON.stringify(names) }); + const secret = new Set(secretKeys); + const secretNames = names.filter(name => secret.has(name) && projection[name] !== ""); + if (secretNames.length > 0) projection[SECRET_ENVIRONMENT_KEYS] = JSON.stringify(secretNames); + return projection; +} + +/** + * Configured secret names and values for output custody. A projection from a + * controller that predates this marker carries none. A malformed marker is + * refused rather than read as "no secrets". + */ +export function configuredSecretEnvironment(source: NodeJS.ProcessEnv | undefined): Array<{ name: string; value: string }> { + const raw = source?.[SECRET_ENVIRONMENT_KEYS]; + if (raw === undefined) return []; + const configured = configuredEnvironment(source); + let names: string[]; + try { + if (Buffer.byteLength(raw) > 20_000) throw new Error("oversized"); + names = checkedNames(JSON.parse(raw)); + } catch { throw new Error("Invalid secret environment projection"); } + // Boundaries may narrow the configured projection after minting; a secret + // entry that no longer crosses has no value to protect. + return names.flatMap(name => typeof configured[name] === "string" && configured[name] !== "" ? [{ name, value: configured[name]! }] : []); } /** Accept only an explicitly supplied, controller-minted projection. */ @@ -75,9 +103,12 @@ export function configuredEnvironment(source: NodeJS.ProcessEnv | undefined): No result[name] = value; } result[CONFIGURED_ENVIRONMENT_KEYS] = JSON.stringify(names); + // The secret-name marker travels with the projection it describes. + const secret = source?.[SECRET_ENVIRONMENT_KEYS]; + if (secret !== undefined) result[SECRET_ENVIRONMENT_KEYS] = secret; return result; } export function configuredEnvironmentKeys(source: NodeJS.ProcessEnv | undefined): string[] { - return Object.keys(configuredEnvironment(source)).filter(key => key !== CONFIGURED_ENVIRONMENT_KEYS).sort(); + return Object.keys(configuredEnvironment(source)).filter(key => key !== CONFIGURED_ENVIRONMENT_KEYS && key !== SECRET_ENVIRONMENT_KEYS).sort(); } diff --git a/packages/paperclip-runner/src/drivers/opencode/environment.ts b/packages/paperclip-runner/src/drivers/opencode/environment.ts index c0afa5405ed..4cc64044571 100644 --- a/packages/paperclip-runner/src/drivers/opencode/environment.ts +++ b/packages/paperclip-runner/src/drivers/opencode/environment.ts @@ -1,9 +1,11 @@ -import { CONFIGURED_ENVIRONMENT_KEYS, configuredEnvironment } from "../../configured-environment.js"; +import { CONFIGURED_ENVIRONMENT_KEYS, SECRET_ENVIRONMENT_KEYS, configuredEnvironment } from "../../configured-environment.js"; export function configuredOpenCodeEnvironment(source: NodeJS.ProcessEnv | undefined): NodeJS.ProcessEnv { const environment = configuredEnvironment(source); for (const key of Object.keys(environment)) { if (/^(?:OPENCODE_|XDG_)/i.test(key)) delete environment[key]; + // The secret-name marker is runner input, not task environment. + if (key === SECRET_ENVIRONMENT_KEYS) delete environment[key]; } if (environment[CONFIGURED_ENVIRONMENT_KEYS] !== undefined) { environment[CONFIGURED_ENVIRONMENT_KEYS] = JSON.stringify( diff --git a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts index c03ee20616e..5dab58875b1 100644 --- a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts @@ -403,6 +403,43 @@ describe("OpenCodeServerDriver", () => { } }); + it("protects configured secrets in OpenCode output across controller and runner cutovers in one saved session", async () => { + await chmod(fixture, 0o755); + const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-output-custody-")); + roots.push(root); + const configured = { PUBLISHING_API_KEY: "synthetic-delivered-secret", PLAIN_SETTING: "ordinary-setting" }; + const launch = async (secretKeys: string[]) => { + const driver = new OpenCodeServerDriver({ + model: "openrouter/test-model", command: fixture, runtimeDirectory: root, + environment: { ...configuredEnvironmentProjection(configured, secretKeys), PATH: process.env.PATH }, + }); + const session = await driver.openSession({ runId: `custody-${secretKeys.length}`, normalizedSessionId: "custody", workingDirectory: root }); + try { + const config = JSON.parse(await readFile(join(root, "custody", "config", "opencode", "opencode.json"), "utf8")); + const environment = JSON.parse(await readFile(join(root, "custody", "data", "fake-environment.json"), "utf8")); + return { config, environment, ids: session.ids() }; + } finally { + await session.close({ reason: "test" }); + } + }; + // Saved by a controller that predates secret names: today's launch, unchanged. + const before = await launch([]); + expect(before.config.plugin).toEqual([]); + expect(before.config.shell).toBeUndefined(); + expect(before.environment.keys).not.toContain("PAPERCLIP_OUTPUT_CUSTODY_KEYS"); + // Resumed by a controller that names the secret: custody applies to the same session. + const after = await launch(["PUBLISHING_API_KEY"]); + expect(after.config.shell).toMatch(/output-custody\/[0-9a-f]{24}\/(?:zsh|bash|sh)$/); + expect(after.config.plugin).toEqual([expect.stringMatching(/^file:\/\/.*\/\.paperclip-output-custody\/paperclip-output-custody\.js$/)]); + expect(after.environment.keys).toEqual(expect.arrayContaining(["PAPERCLIP_OUTPUT_CUSTODY_KEYS", "PAPERCLIP_OUTPUT_CUSTODY_NONCE", "PAPERCLIP_OUTPUT_CUSTODY_DIR", "PUBLISHING_API_KEY"])); + expect(after.environment.keys).not.toContain("PAPERCLIP_SECRET_ENV_KEYS"); + expect(JSON.stringify(after.config)).not.toContain(configured.PUBLISHING_API_KEY); + // A rollback to a controller without secret names still launches the saved session. + const rollback = await launch([]); + expect(rollback.config.plugin).toEqual([]); + expect(rollback.ids).toMatchObject({ providerSessionId: expect.any(String) }); + }); + it.each([false, true])("uses the native MCP client ceiling only for durable tool requests (%s)", async durableToolRequests => { await chmod(fixture, 0o755); const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-tool-budget-")); diff --git a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts index 5e1593e01c2..fe9ace59c80 100644 --- a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts +++ b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.ts @@ -33,7 +33,8 @@ import { PROVIDER_PERFORMANCE_ENV } from "../../contracts/provider-performance.j import { annotateOmittedProviderText, parseOpenCodeSseFrames } from "./sse-frames.js"; import { githubCredentialEnvironment } from "../../github-credential-environment.js"; import { configuredOpenCodeEnvironment } from "./environment.js"; -import { CONFIGURED_ENVIRONMENT_KEYS } from "../../configured-environment.js"; +import { CONFIGURED_ENVIRONMENT_KEYS, SECRET_ENVIRONMENT_KEYS, configuredSecretEnvironment } from "../../configured-environment.js"; +import { prepareOpenCodeOutputCustody, redactOpenCodeModelRequest } from "./output-custody.js"; import { CODEX_SKILLLESS_BASE_INSTRUCTIONS, @@ -2455,7 +2456,7 @@ class OpenCodeHarnessSession implements HarnessSession { } /** Retain the reusable gateway key in the runner; the harness gets a session-scoped capability. */ -async function startOpenCodeProviderProxy(baseUrl: string, key: string, model: string, environment: NodeJS.ProcessEnv, onServed?: (served: UpstreamServed | null) => void) { +async function startOpenCodeProviderProxy(baseUrl: string, key: string, model: string, environment: NodeJS.ProcessEnv, onServed?: (served: UpstreamServed | null) => void, secretValues: readonly string[] = []) { const upstreamUrl = new URL(`${baseUrl.replace(/\/+$/, "")}/chat/completions`); const token = randomBytes(32).toString("base64url"); // Agent-local settings keep one runtime's transport configuration out of other sessions. @@ -2511,6 +2512,11 @@ async function startOpenCodeProviderProxy(baseUrl: string, key: string, model: s // Keep OpenCode's unreadable-media notice attributed to the tool result // instead of a synthetic user message the model mistakes for the person. if (attributeUnsupportedToolMedia(payload)) body = Buffer.from(JSON.stringify(payload), "utf8"); + // Defense in depth: no configured secret value leaves for the model. + if (secretValues.length > 0) { + const redacted = redactOpenCodeModelRequest(body, secretValues, payload); + if (redacted) body = redacted; + } try { onServed?.(null); } catch { /* Recording never affects the request. */ } const upstream = await new Promise((resolve, reject) => { const outgoing = (upstreamUrl.protocol === "https:" ? requestHttps : requestHttp)(upstreamUrl, { @@ -2628,7 +2634,7 @@ async function startRuntime(input: { const sensitiveValues = [ ...identityValues, ...Object.entries(taskEnvironment).flatMap(([key, value]) => - key !== CONFIGURED_ENVIRONMENT_KEYS && value ? [value, JSON.stringify(value).slice(1, -1)] : []), + key !== CONFIGURED_ENVIRONMENT_KEYS && key !== SECRET_ENVIRONMENT_KEYS && value ? [value, JSON.stringify(value).slice(1, -1)] : []), password, authHeader, bridge.secret, @@ -2637,6 +2643,21 @@ async function startRuntime(input: { providerEnvironment.PAPERCLIP_AI_PROVIDER_KEY, ].filter((value): value is string => Boolean(value)); input.trace?.addSensitiveValues(sensitiveValues); + // Configured secrets must never reach OpenCode's captured tool output, its + // session store, or the model. Prepared before launch; a launch that cannot + // protect them does not start. + const outputCustody = await prepareOpenCodeOutputCustody({ + directory: join(isolatedHome, ".paperclip-output-custody"), + // Shared by every session of this runtime so the wrapper is written once. + binDirectory: join(resolve(input.options.runtimeDirectory), "output-custody"), + configDirectory: join(configHome, "opencode"), + environment: sanitizedEnvironment(input.options.environment, {}), + secrets: configuredSecretEnvironment(input.options.environment), + }).catch(async error => { + await bridge.close().catch(() => {}); + await removeIsolatedHome(isolatedHome).catch(() => {}); + throw error; + }); const instructionRoot = input.options.runtimeContext?.instructions.bundle.rootPath; // OpenCode canonicalizes tool paths (for example /var -> /private/var on @@ -2656,7 +2677,7 @@ async function startRuntime(input: { ? { baseUrl: input.options.environment.PAPERCLIP_AI_PROVIDER_URL, key: input.options.environment.PAPERCLIP_AI_PROVIDER_KEY ?? "" } : null; const providerProxy = providerRoute - ? await startOpenCodeProviderProxy(providerRoute.baseUrl, providerRoute.key, providerModelId, providerEnvironment, input.onUpstreamServed).catch(async error => { + ? await startOpenCodeProviderProxy(providerRoute.baseUrl, providerRoute.key, providerModelId, providerEnvironment, input.onUpstreamServed, outputCustody?.values ?? []).catch(async error => { await bridge.close().catch(() => {}); await removeIsolatedHome(isolatedHome).catch(() => {}); throw error; @@ -2697,7 +2718,8 @@ async function startRuntime(input: { // The configured entry is already composed exactly once into the session // system prompt; siblings remain available through the read-only root. instructions: [], - plugin: [], + plugin: outputCustody?.config.plugin ?? [], + ...(outputCustody ? { shell: outputCustody.config.shell } : {}), // OpenCode's bundled models.dev snapshot can lag behind OpenRouter's live // catalog. Bind the already-qualified exact model slug into the built-in // provider instead of silently falling back or rejecting a newer model. @@ -2780,6 +2802,7 @@ async function startRuntime(input: { // default plugins before it can submit the first prompt. OPENCODE_DISABLE_MODELS_FETCH: "true", OPENCODE_DISABLE_DEFAULT_PLUGINS: "true", + ...outputCustody?.environment, OPENCODE_SERVER_USERNAME: username, OPENCODE_SERVER_PASSWORD: password, ...performanceDiagnostics?.environment, diff --git a/packages/paperclip-runner/src/drivers/opencode/output-custody.test.ts b/packages/paperclip-runner/src/drivers/opencode/output-custody.test.ts new file mode 100644 index 00000000000..f794b51675a --- /dev/null +++ b/packages/paperclip-runner/src/drivers/opencode/output-custody.test.ts @@ -0,0 +1,168 @@ +import { execFile } from "node:child_process"; +import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { promisify } from "node:util"; +import { afterEach, describe, expect, it } from "vitest"; +import { + OUTPUT_CUSTODY_DIRECTORY_ENV, + OUTPUT_CUSTODY_KEYS_ENV, + OUTPUT_CUSTODY_NONCE_ENV, + outputCustodyValues, + prepareOpenCodeOutputCustody, + redactOpenCodeModelRequest, + type OpenCodeOutputCustody, +} from "./output-custody.js"; + +const run = promisify(execFile); +const roots: string[] = []; +afterEach(async () => { + await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))); +}); + +const SECRET = "synthetic-delivered-secret-7f3a"; +const PLAIN = "ordinary-configured-setting"; + +async function prepared(secrets = [{ name: "PUBLISHING_API_KEY", value: SECRET }]) { + const root = await mkdtemp(join(tmpdir(), "paperclip-output-custody-")); + roots.push(root); + const environment = { PATH: process.env.PATH, PUBLISHING_API_KEY: SECRET, PLAIN_SETTING: PLAIN, + MULTILINE_KEY: "-----BEGIN KEY-----\nline-one-material\nline-two-material\n-----END KEY-----" }; + const custody = await prepareOpenCodeOutputCustody({ + directory: join(root, "launch"), binDirectory: join(root, "bin"), configDirectory: join(root, "config"), + environment, secrets, + }); + return { root, environment, custody }; +} + +/** Run a command through the wrapper the way OpenCode does: ` -c `. */ +async function shell(custody: OpenCodeOutputCustody, environment: NodeJS.ProcessEnv, command: string, options: { ready?: boolean } = {}) { + if (options.ready !== false) { + await writeFile(join(custody.environment[OUTPUT_CUSTODY_DIRECTORY_ENV]!, "ready"), custody.environment[OUTPUT_CUSTODY_NONCE_ENV]!); + } + try { + const result = await run(custody.config.shell, ["-c", command], { env: { ...environment, ...custody.environment }, timeout: 10_000 }); + return { code: 0, output: result.stdout + result.stderr }; + } catch (error) { + const failed = error as { code?: number; stdout?: string; stderr?: string }; + return { code: failed.code ?? -1, output: `${failed.stdout ?? ""}${failed.stderr ?? ""}` }; + } +} + +describe("OpenCode output custody", () => { + it("is not prepared when no configured secret crosses, so older controllers keep today's launch", async () => { + const { custody } = await prepared([]); + expect(custody).toBeNull(); + }); + + it("filters exact secret values from command output before OpenCode can capture it", async () => { + const { custody, environment } = await prepared(); + const result = await shell(custody!, environment, `echo "key=$PUBLISHING_API_KEY"; echo "err $PUBLISHING_API_KEY" >&2; echo "$PLAIN_SETTING"; printf "tail-$PUBLISHING_API_KEY"; exit 7`); + expect(result.code).toBe(7); + expect(result.output).not.toContain(SECRET); + expect(result.output).toContain("key=[REDACTED]"); + expect(result.output).toContain("err [REDACTED]"); + expect(result.output).toContain(PLAIN); + expect(result.output).toContain("tail-[REDACTED]"); + }); + + it("filters each line of a multi-line secret and keeps the exit status of a signalled command", async () => { + const { custody, environment } = await prepared([{ name: "MULTILINE_KEY", value: "-----BEGIN KEY-----\nline-one-material\nline-two-material\n-----END KEY-----" }]); + const result = await shell(custody!, environment, `printf '%s\\n' "$MULTILINE_KEY"; kill -TERM $$`); + expect(result.output).not.toContain("line-one-material"); + expect(result.output).not.toContain("line-two-material"); + expect(result.code).toBe(143); + }); + + it("refuses to run commands until the plugin that protects file reads has started for this launch", async () => { + const { root, custody, environment } = await prepared(); + const marker = join(root, "ran"); + const missing = await shell(custody!, environment, `touch ${marker}`, { ready: false }); + expect(missing.code).toBe(126); + expect(missing.output).toContain("secret output protection is not active"); + await expect(stat(marker)).rejects.toThrow(); + await writeFile(join(custody!.environment[OUTPUT_CUSTODY_DIRECTORY_ENV]!, "ready"), "a-previous-launch"); + const stale = await run(custody!.config.shell, ["-c", `touch ${marker}`], { env: { ...environment, ...custody!.environment } }).catch(error => error); + expect(stale.code).toBe(126); + await expect(stat(marker)).rejects.toThrow(); + }); + + it("does not let a background process hold a finished command open", async () => { + const { custody, environment } = await prepared(); + const started = Date.now(); + const result = await shell(custody!, environment, `(sleep 5; echo late) & echo now`); + expect(result.output.trim()).toBe("now"); + expect(Date.now() - started).toBeLessThan(3_000); + }); + + it("rewrites completed non-shell tool results in the plugin and marks this launch ready", async () => { + const { custody, environment } = await prepared(); + const saved = { ...process.env }; + Object.assign(process.env, environment, custody!.environment); + try { + const module = await import(`${pathToFileURL(custody!.config.plugin[0]!.replace("file://", "")).href}?t=${Date.now()}`); + const hooks = await module.PaperclipOutputCustody({}); + expect(await readFile(join(custody!.environment[OUTPUT_CUSTODY_DIRECTORY_ENV]!, "ready"), "utf8")) + .toBe(custody!.environment[OUTPUT_CUSTODY_NONCE_ENV]); + const output = { title: `read ${SECRET}`, output: `1: ${SECRET}\n2: ${PLAIN}`, metadata: { preview: [SECRET, { nested: SECRET }] }, + content: [{ type: "text", text: `mcp ${SECRET}` }] }; + await hooks["tool.execute.after"]({ tool: "read", sessionID: "s", callID: "c" }, output); + expect(JSON.stringify(output)).not.toContain(SECRET); + expect(output.output).toBe(`1: [REDACTED]\n2: ${PLAIN}`); + } finally { + for (const key of Object.keys(process.env)) if (!(key in saved)) delete process.env[key]; + Object.assign(process.env, saved); + } + }); + + it("names only secret keys and never writes a value to disk", async () => { + const { root, custody } = await prepared(); + expect(custody!.environment[OUTPUT_CUSTODY_KEYS_ENV]).toBe("PUBLISHING_API_KEY"); + const files = await run("/bin/sh", ["-c", `grep -rl -- ${JSON.stringify(SECRET)} ${JSON.stringify(root)} || true`]); + expect(files.stdout.trim()).toBe(""); + }); + + it("keeps the executable wrapper byte-identical across launches", async () => { + const first = await prepared(); + const second = await prepareOpenCodeOutputCustody({ + directory: join(first.root, "launch-2"), binDirectory: join(first.root, "bin"), configDirectory: join(first.root, "config"), + environment: first.environment, secrets: [{ name: "PUBLISHING_API_KEY", value: SECRET }], + }); + expect(second!.config.shell).toBe(first.custody!.config.shell); + expect(second!.environment[OUTPUT_CUSTODY_NONCE_ENV]).not.toBe(first.custody!.environment[OUTPUT_CUSTODY_NONCE_ENV]); + }); +}); + +describe("model request redaction", () => { + const values = outputCustodyValues([SECRET, "quote\"and\\slash-secret"]); + it("removes raw and JSON-escaped secret values and leaves other bodies untouched", () => { + const body = Buffer.from(JSON.stringify({ model: "m", messages: [{ role: "tool", content: `x ${SECRET} y ${PLAIN} quote"and\\slash-secret` }] })); + const redacted = redactOpenCodeModelRequest(body, values)!; + expect(redacted.toString("utf8")).not.toContain(SECRET); + expect(redacted.toString("utf8")).not.toContain("slash-secret"); + expect(JSON.parse(redacted.toString("utf8")).messages[0].content).toBe(`x [REDACTED] y ${PLAIN} [REDACTED]`); + expect(redactOpenCodeModelRequest(Buffer.from(JSON.stringify({ model: "m", messages: [{ content: PLAIN }] })), values)).toBeNull(); + }); + it("changes only string values, never request structure", () => { + const shaped = outputCustodyValues(["true", "model", "messages"]); + const body = Buffer.from(JSON.stringify({ model: "m", stream: true, messages: [{ role: "tool", content: "flag=true" }] })); + const redacted = JSON.parse(redactOpenCodeModelRequest(body, shaped)!.toString("utf8")); + expect(redacted).toEqual({ model: "m", stream: true, messages: [{ role: "tool", content: "flag=[REDACTED]" }] }); + expect(redactOpenCodeModelRequest(Buffer.from(JSON.stringify({ model: "m", stream: true })), shaped)).toBeNull(); + }); + it("does not treat short values as secrets", () => { + expect(outputCustodyValues(["abc", "true"])).toEqual(["true"]); + }); +}); + +describe("secrets custody cannot protect", () => { + it.each([["a multi-line value whose lines are all short", "ab\ncd"], ["a short value", "abc"]])("refuses a launch with %s instead of leaving it unprotected", async (_label, value) => { + const root = await mkdtemp(join(tmpdir(), "paperclip-output-custody-short-")); + roots.push(root); + await expect(prepareOpenCodeOutputCustody({ + directory: join(root, "launch"), binDirectory: join(root, "bin"), configDirectory: join(root, "config"), + environment: { PATH: process.env.PATH, SHORT_SECRET: value }, secrets: [{ name: "SHORT_SECRET", value }], + })).rejects.toMatchObject({ code: "opencode_output_custody_unavailable" }); + }); +}); diff --git a/packages/paperclip-runner/src/drivers/opencode/output-custody.ts b/packages/paperclip-runner/src/drivers/opencode/output-custody.ts new file mode 100644 index 00000000000..42807c1c3e8 --- /dev/null +++ b/packages/paperclip-runner/src/drivers/opencode/output-custody.ts @@ -0,0 +1,324 @@ +import { createHash, randomBytes } from "node:crypto"; +import { accessSync, constants, statSync } from "node:fs"; +import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; +import { basename, delimiter, isAbsolute, join } from "node:path"; + +/** + * Output custody keeps the exact values of configured secrets out of what + * OpenCode captures from tools. OpenCode stores captured tool output in its + * session database (including streamed previews written while a command still + * runs) and sends it to the model, so redacting after capture is too late. + * + * Two layers run inside OpenCode, before any byte is captured: + * - Commands: OpenCode's configured shell is a small POSIX wrapper. It runs the + * shell OpenCode would have used and passes all of its output through an awk + * filter that replaces exact secret values, so OpenCode only reads filtered + * output. + * - Other tools (file reads, searches, MCP results): a local plugin rewrites + * the completed result in `tool.execute.after`, which OpenCode runs before it + * persists or returns that result. + * + * The wrapper fails closed: it runs nothing unless the plugin has started for + * this launch, so no command can write a secret to a file that a later file + * read would return unfiltered. Values never touch disk here: the wrapper, + * filter and plugin read them from the environment OpenCode already holds. + */ + +/** Shorter values are not redacted: they cannot be told apart from ordinary text. */ +export const OUTPUT_CUSTODY_MIN_VALUE_LENGTH = 4; +export const OUTPUT_CUSTODY_REDACTION = "[REDACTED]"; + +export const OUTPUT_CUSTODY_KEYS_ENV = "PAPERCLIP_OUTPUT_CUSTODY_KEYS"; +export const OUTPUT_CUSTODY_NONCE_ENV = "PAPERCLIP_OUTPUT_CUSTODY_NONCE"; +export const OUTPUT_CUSTODY_DIRECTORY_ENV = "PAPERCLIP_OUTPUT_CUSTODY_DIR"; + +export class OutputCustodyUnavailableError extends Error { + readonly code = "opencode_output_custody_unavailable" as const; + constructor(message: string) { + super(message); + this.name = "OutputCustodyUnavailableError"; + } +} + +export interface OpenCodeOutputCustody { + /** OpenCode configuration entries. */ + config: { shell: string; plugin: string[] }; + /** Environment for the OpenCode process. Names and a per-launch nonce only. */ + environment: Record; + /** Exact strings removed from output, longest first. */ + values: string[]; +} + +/** The exact strings to remove for each secret value: the value and each line of a multi-line value. */ +export function outputCustodyValues(values: readonly string[]): string[] { + const result = new Set(); + for (const value of values) { + for (const candidate of [value, ...value.split("\n").map(line => line.replace(/\r$/, ""))]) { + if (candidate.length >= OUTPUT_CUSTODY_MIN_VALUE_LENGTH) result.add(candidate); + } + } + return [...result].sort((left, right) => right.length - left.length || (left < right ? -1 : 1)); +} + +/** Replace every exact secret value in text. */ +export function redactOutputCustodyText(text: string, values: readonly string[]): string { + let result = text; + for (const value of values) if (result.includes(value)) result = result.split(value).join(OUTPUT_CUSTODY_REDACTION); + return result; +} + +/** + * Command output is filtered line by line, so a secret is protected there + * through its lines of at least the minimum length (a single-line value is its + * own line). A value with no such line cannot be told apart from ordinary + * output and is refused rather than left unprotected. + */ +export function assertOutputCustodyProtects(values: readonly string[]): void { + for (const value of values) { + const lines = value.split("\n").map(line => line.replace(/\r$/, "")); + if (value.length > 0 && !lines.some(line => line.length >= OUTPUT_CUSTODY_MIN_VALUE_LENGTH)) { + throw new OutputCustodyUnavailableError( + `A configured secret is too short to keep out of agent output; use a value of at least ${OUTPUT_CUSTODY_MIN_VALUE_LENGTH} characters.`, + ); + } + } +} + +/** + * Remove exact secret values from the string values of a parsed model request + * and serialize it again. Field names and non-string values are never touched, + * so the request keeps its structure. Returns null when no string holds a + * value: a cheap byte scan for each value's raw and JSON-escaped spelling + * keeps the common path to the original bytes. + */ +export function redactOpenCodeModelRequest(body: Buffer, values: readonly string[], payload?: unknown) { + if (!values.some(value => body.includes(value) || body.includes(JSON.stringify(value).slice(1, -1)))) return null; + const parsed = payload ?? JSON.parse(body.toString("utf8")); + let changed = false; + const visit = (node: unknown, depth: number): unknown => { + if (typeof node === "string") { + const redacted = redactOutputCustodyText(node, values); + if (redacted !== node) changed = true; + return redacted; + } + if (!node || typeof node !== "object" || depth > 256) return node; + if (Array.isArray(node)) { + for (let index = 0; index < node.length; index++) node[index] = visit(node[index], depth + 1); + } else { + for (const key of Object.keys(node)) (node as Record)[key] = visit((node as Record)[key], depth + 1); + } + return node; + }; + const result = visit(parsed, 0); + return changed ? Buffer.from(JSON.stringify(result), "utf8") : null; +} + +function executable(path: string): boolean { + try { + if (!statSync(path).isFile()) return false; + accessSync(path, constants.X_OK); + return true; + } catch { + return false; + } +} + +function findExecutable(name: string, environment: NodeJS.ProcessEnv, fallbacks: readonly string[] = []): string | null { + for (const directory of (environment.PATH ?? "").split(delimiter)) { + if (!directory || !isAbsolute(directory)) continue; + const candidate = join(directory, name); + if (executable(candidate)) return candidate; + } + return fallbacks.find(executable) ?? null; +} + +/** + * The shell OpenCode selects when none is configured (its environment has no + * SHELL): zsh on macOS, otherwise the first bash on PATH, otherwise sh. + */ +export function openCodeDefaultShell(environment: NodeJS.ProcessEnv, platform: NodeJS.Platform = process.platform): string { + if (platform === "darwin" && executable("/bin/zsh")) return "/bin/zsh"; + return findExecutable("bash", environment) ?? "/bin/sh"; +} + +const quote = (value: string) => `'${value.replaceAll("'", `'\\''`)}'`; + +/** POSIX awk: exact, non-regex replacement; values come from ENVIRON only. */ +export const OUTPUT_CUSTODY_AWK = `BEGIN { + end = ENVIRON["PAPERCLIP_OUTPUT_CUSTODY_END"] + n = split(ENVIRON["${OUTPUT_CUSTODY_KEYS_ENV}"], keys, " ") + count = 0 + for (i = 1; i <= n; i++) { + value = ENVIRON[keys[i]] + if (length(value) >= ${OUTPUT_CUSTODY_MIN_VALUE_LENGTH}) { count++; vals[count] = value } + m = split(value, parts, "\\n") + for (j = 1; m > 1 && j <= m; j++) { + part = parts[j] + sub(/\\r$/, "", part) + if (length(part) >= ${OUTPUT_CUSTODY_MIN_VALUE_LENGTH}) { count++; vals[count] = part } + } + } + for (i = 2; i <= count; i++) { + v = vals[i]; j = i - 1 + while (j > 0 && length(vals[j]) < length(v)) { vals[j + 1] = vals[j]; j-- } + vals[j + 1] = v + } +} +function redact(line, i, s, out, k) { + for (i = 1; i <= count; i++) { + s = vals[i]; out = "" + while ((k = index(line, s)) > 0) { out = out substr(line, 1, k - 1) "${OUTPUT_CUSTODY_REDACTION}"; line = substr(line, k + length(s)) } + line = out line + } + return line +} +{ + k = length($0) - length(end) + if (length(end) > 0 && k >= 0 && substr($0, k + 1) == end) { + if (k > 0) printf "%s", redact(substr($0, 1, k)) + fflush() + exit 0 + } + print redact($0) + fflush() +} +`; + +/** + * POSIX sh wrapper. Output written after the real shell exits is dropped, as + * OpenCode itself stops reading at exit: the end marker stops the filter, so a + * background process cannot hold the command open. The launch directory comes + * from the environment so the executable stays byte-identical across launches + * (macOS assesses each newly written executable on its first run, which costs + * over 100 ms). + */ +export function outputCustodyWrapper(input: { realShell: string; awk: string; mkfifo: string; remove: string; filter: string }): string { + return `#!/bin/sh +dir=\${${OUTPUT_CUSTODY_DIRECTORY_ENV}:-} +expected= +[ -n "$dir" ] && [ -f "$dir/ready" ] && IFS= read -r expected < "$dir/ready" +if [ -z "$dir" ] || [ -z "\${${OUTPUT_CUSTODY_NONCE_ENV}:-}" ] || [ "$expected" != "$${OUTPUT_CUSTODY_NONCE_ENV}" ]; then + echo "Paperclip did not run this command because secret output protection is not active." >&2 + exit 126 +fi +fifo="$dir/out.$$" +${quote(input.remove)} -f "$fifo" +${quote(input.mkfifo)} -m 600 "$fifo" || exit 126 +end="PAPERCLIP-OUTPUT-END-$${OUTPUT_CUSTODY_NONCE_ENV}-$$" +PAPERCLIP_OUTPUT_CUSTODY_END="$end" ${quote(input.awk)} -f ${quote(input.filter)} < "$fifo" & +filter=$! +exec 3> "$fifo" +${quote(input.remove)} -f "$fifo" +${quote(input.realShell)} "$@" >&3 2>&3 3>&- +code=$? +printf '%s\\n' "$end" >&3 +exec 3>&- +wait "$filter" +exit "$code" +`; +} + +/** OpenCode plugin. Loaded by OpenCode itself; reads values from its own environment. */ +export const OUTPUT_CUSTODY_PLUGIN = `import { writeFileSync } from "node:fs"; +import { join } from "node:path"; + +export const PaperclipOutputCustody = async () => { + const env = process.env; + const values = new Set(); + for (const key of String(env.${OUTPUT_CUSTODY_KEYS_ENV} ?? "").split(" ").filter(Boolean)) { + const value = String(env[key] ?? ""); + for (const candidate of [value, ...value.split("\\n").map((line) => line.replace(/\\r$/, ""))]) { + if (candidate.length >= ${OUTPUT_CUSTODY_MIN_VALUE_LENGTH}) values.add(candidate); + } + } + const ordered = [...values].sort((left, right) => right.length - left.length); + const redact = (text) => { + for (const value of ordered) if (text.includes(value)) text = text.split(value).join(${JSON.stringify(OUTPUT_CUSTODY_REDACTION)}); + return text; + }; + const visit = (value, depth) => { + if (!value || typeof value !== "object" || depth > 64) return; + for (const key of Object.keys(value)) { + const item = value[key]; + if (typeof item === "string") value[key] = redact(item); + else visit(item, depth + 1); + } + }; + writeFileSync(join(String(env.${OUTPUT_CUSTODY_DIRECTORY_ENV}), "ready"), String(env.${OUTPUT_CUSTODY_NONCE_ENV} ?? ""), { mode: 0o600 }); + return { + "tool.execute.after": async (_input, output) => { visit(output, 0); }, + }; +}; +`; + +/** Write a file once; concurrent launches converge on the same bytes through an atomic rename. */ +async function writeStable(path: string, content: string, mode: number): Promise { + try { + if ((await readFile(path, "utf8")) === content) return; + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + } + const temporary = `${path}.${process.pid}.${randomBytes(6).toString("hex")}`; + await writeFile(temporary, content, { mode }); + await rename(temporary, path); +} + +/** + * Prepare output custody for one OpenCode launch, or return null when no + * configured secret needs it. `directory` must be fresh for this launch; + * `binDirectory` is stable across launches and holds the executable wrapper. + * `configDirectory` is OpenCode's global config directory: OpenCode waits for + * a dependency install there before it loads any plugin, so it is marked as + * already installed to keep that install off the first tool call. + */ +export async function prepareOpenCodeOutputCustody(input: { + directory: string; + binDirectory: string; + configDirectory: string; + environment: NodeJS.ProcessEnv; + secrets: ReadonlyArray<{ name: string; value: string }>; + platform?: NodeJS.Platform; +}): Promise { + const secrets = input.secrets.filter(secret => input.environment[secret.name] === secret.value); + assertOutputCustodyProtects(secrets.map(secret => secret.value)); + const values = outputCustodyValues(secrets.map(secret => secret.value)); + if (values.length === 0) return null; + const awk = findExecutable("awk", input.environment, ["/usr/bin/awk", "/bin/awk"]); + const mkfifo = findExecutable("mkfifo", input.environment, ["/usr/bin/mkfifo", "/bin/mkfifo"]); + const remove = findExecutable("rm", input.environment, ["/bin/rm", "/usr/bin/rm"]); + if (!awk || !mkfifo || !remove) { + throw new OutputCustodyUnavailableError("Secret output protection needs awk, mkfifo and rm on this computer."); + } + const realShell = openCodeDefaultShell(input.environment, input.platform); + const wrapperFor = (filter: string) => outputCustodyWrapper({ realShell, awk, mkfifo, remove, filter }); + const identity = createHash("sha256").update(OUTPUT_CUSTODY_AWK).update("\0").update(wrapperFor("")).digest("hex").slice(0, 24); + const bin = join(input.binDirectory, identity); + const filter = join(bin, "redact.awk"); + // Named like the real shell so OpenCode applies the same shell semantics. + const shell = join(bin, basename(realShell)); + const plugin = join(input.directory, "paperclip-output-custody.js"); + await Promise.all([ + mkdir(bin, { recursive: true, mode: 0o700 }), + mkdir(input.directory, { recursive: true, mode: 0o700 }), + mkdir(join(input.configDirectory, "node_modules"), { recursive: true, mode: 0o700 }), + ]); + await Promise.all([ + writeStable(filter, OUTPUT_CUSTODY_AWK, 0o600), + writeStable(shell, wrapperFor(filter), 0o700), + writeFile(plugin, OUTPUT_CUSTODY_PLUGIN, { mode: 0o600 }), + writeFile( + join(input.configDirectory, "package-lock.json"), + `${JSON.stringify({ lockfileVersion: 3, packages: { "": { dependencies: { "@opencode-ai/plugin": "*" } } } })}\n`, + { mode: 0o600, flag: "wx" }, + ).catch((error: NodeJS.ErrnoException) => { if (error.code !== "EEXIST") throw error; }), + ]); + return { + config: { shell, plugin: [`file://${plugin}`] }, + environment: { + [OUTPUT_CUSTODY_KEYS_ENV]: secrets.map(secret => secret.name).join(" "), + [OUTPUT_CUSTODY_NONCE_ENV]: randomBytes(16).toString("hex"), + [OUTPUT_CUSTODY_DIRECTORY_ENV]: input.directory, + }, + values, + }; +} diff --git a/server/src/__tests__/configured-secret-guidance.test.ts b/server/src/__tests__/configured-secret-guidance.test.ts new file mode 100644 index 00000000000..fd3a5c0fed1 --- /dev/null +++ b/server/src/__tests__/configured-secret-guidance.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; +import { configuredSecretGuidance } from "../services/native-runtime/configured-secret-guidance.js"; +import { configuredEnvironmentProjection, configuredSecretEnvironment } from "../vendor/paperclip-runner/index.js"; + +describe("configured secret guidance", () => { + it("tells the agent how to check a secret without printing it, by name only", () => { + const guidance = configuredSecretGuidance(["PUBLISHING_API_KEY", "DEPLOY_TOKEN", "PUBLISHING_API_KEY"])!; + expect(guidance).toContain("`DEPLOY_TOKEN`, `PUBLISHING_API_KEY`"); + expect(guidance).toContain('[ -n "$DEPLOY_TOKEN" ]'); + expect(guidance).toMatch(/Never print, echo, log, or write a secret value/); + }); + + it("adds nothing when the run has no configured secrets", () => { + expect(configuredSecretGuidance([])).toBeNull(); + }); + + it("names only configured entries the controller resolved from secrets", () => { + const projection = configuredEnvironmentProjection( + { PUBLISHING_API_KEY: "delivered-secret-value", REGION: "us-east-1" }, + new Set(["PUBLISHING_API_KEY", "PAPERCLIP_AGENT_PRIVATE_KEY"]), + ); + expect(configuredSecretEnvironment(projection).map(secret => secret.name)).toEqual(["PUBLISHING_API_KEY"]); + expect(JSON.stringify(projection.PAPERCLIP_SECRET_ENV_KEYS)).not.toContain("delivered-secret-value"); + }); +}); diff --git a/server/src/services/heartbeat.ts b/server/src/services/heartbeat.ts index 003e5b8b0c8..2e22d63ef4f 100644 --- a/server/src/services/heartbeat.ts +++ b/server/src/services/heartbeat.ts @@ -407,6 +407,8 @@ import { import { nativeChatWorkspaceCwd } from "./native-runtime/native-chat-workspace.js"; import { + configuredEnvironmentProjection, + configuredSecretEnvironment, type NativeExecutionInput, type NativeSessionBackend, } from "../vendor/paperclip-runner/index.js"; @@ -3576,6 +3578,9 @@ export function heartbeatService( runtimeConfig, runtimeSkillEntries, runScopedMentionedSkillKeys, + configuredSecretNames: configuredSecretEnvironment( + configuredEnvironmentProjection(configuredTaskEnvironment, secretKeys), + ).map(secret => secret.name), instructionCopy: preparedInstructions.instructionCopy, agentIdentity, issueAssigneeOverrides, @@ -3644,6 +3649,7 @@ export function heartbeatService( resolvedConfig, resolvedInstanceSettings, configuredTaskEnvironment, + secretKeys, adapterEnv, agentIdentity, observability, diff --git a/server/src/services/heartbeat/runtime-execution.ts b/server/src/services/heartbeat/runtime-execution.ts index 93a7dfdd239..da895e24d19 100644 --- a/server/src/services/heartbeat/runtime-execution.ts +++ b/server/src/services/heartbeat/runtime-execution.ts @@ -148,6 +148,8 @@ export interface HeartbeatRuntimeExecutionInput { config: Pick & { resolvedInstanceSettings: { experimental: Pick["getExperimental"]>>, "enableWorkspaceBranchReconcileForward" | "enableWorkspaceDirtyQuarantineRepair"> }; configuredTaskEnvironment: Parameters[0]; + /** Names of configured entries resolved from secrets; values never leave the projection. */ + secretKeys?: ReadonlySet; adapterEnv: Parameters[0]; agentIdentity: AdapterExecutionContext["agentIdentity"]; observability?: AdapterExecutionContext["observability"]; @@ -247,7 +249,7 @@ export async function executeHeartbeatRuntime(db: Db, input: HeartbeatRuntimeExe const { nativeRuntimeResolution, nativeExecution, nativeRunnerInstanceId, adapter, runtimeForAdapter, getNativeFreshSessionHandoff, getFreshSessionHandoff, runOptions } = input.runtime; const { persistedExecutionWorkspace, executionWorkspace, executionTarget, workspaceOperationRecorder, nativeWorkspaceSync, workspaceRestoreSource, remoteExecution } = input.workspace; const remoteWorkspaceAuthority = readRemoteWorkspaceAuthority(persistedExecutionWorkspace?.metadata); - const { runtimeConfig, resolvedConfig, resolvedInstanceSettings, configuredTaskEnvironment, adapterEnv, agentIdentity, observability, managedAiRuntime, runtimeEnv, useHostGitHub, githubSelection } = input.config; + const { runtimeConfig, resolvedConfig, resolvedInstanceSettings, configuredTaskEnvironment, secretKeys, adapterEnv, agentIdentity, observability, managedAiRuntime, runtimeEnv, useHostGitHub, githubSelection } = input.config; const { runtimeSessionParamsForAdapter, configuredModel, sessionConfigMetadata, goalCheckpointSession } = input.session; const { nativeInstructionWorkingCopy, collectStoppedInstructions, nativeInstructionReservation, releaseInstructionCopy } = input.instructions; const { providerTraceCapture, nativeRunnerPreparationSpans, attestedQuestionResponseAtMs, attemptStartedAtMs, environmentAcquireStartedAtMs, environmentRealizeEndedAtMs } = input.trace; @@ -715,7 +717,7 @@ export async function executeHeartbeatRuntime(db: Db, input: HeartbeatRuntimeExe managedAiCredentialHome: managedAiRuntime ? String((managedAiRuntime.config.env as Record).CODEX_HOME) : undefined, dotWorkspaceRoot: nativeExecution.provider.kind === "openai_dot" && resolvedConfig.dotWorkspaceAccess === true ? executionWorkspace.cwd : undefined, runnerEnvironment: { - ...configuredEnvironmentProjection(configuredTaskEnvironment), + ...configuredEnvironmentProjection(configuredTaskEnvironment, secretKeys ?? []), ...buildNativeProviderEnvironment( adapterEnv, process.env, diff --git a/server/src/services/heartbeat/runtime-selection.ts b/server/src/services/heartbeat/runtime-selection.ts index 18880ba4f57..7496219d026 100644 --- a/server/src/services/heartbeat/runtime-selection.ts +++ b/server/src/services/heartbeat/runtime-selection.ts @@ -88,6 +88,7 @@ import { buildPlanReviewContext } from "../plan-review-context.js"; import { normalizePaperclipWakePayload, selectPaperclipTaskMarkdown } from "@paperclipai/adapter-utils/server-utils"; import type { ProviderResourceDisposition } from "../environment-runtime.js"; import { stageNativeRuntimeSkillAssets, bindStagedNativeRuntimeSkillAssets } from "../native-runtime/runtime-skill-preservation.js"; +import { configuredSecretGuidance } from "../native-runtime/configured-secret-guidance.js"; type Run = typeof heartbeatRuns.$inferSelect; type Agent = typeof agents.$inferSelect; @@ -139,6 +140,8 @@ export interface HeartbeatRuntimeSelectionInput { runtimeConfig: Record; runtimeSkillEntries: Parameters[0]["runtimeSkillEntries"]; runScopedMentionedSkillKeys: string[]; + /** Configured entries resolved from secrets, by name only. */ + configuredSecretNames?: readonly string[]; instructionCopy: Awaited["prepare"]>>; agentIdentity: { keyId: string } | undefined; issueAssigneeOverrides: { adapterConfig: Record | null } | null; @@ -825,6 +828,7 @@ export async function selectHeartbeatRuntime(db: Db, input: HeartbeatRuntimeSele !["openai_dot_mcp", "grok_bot_mcp", "muse_external", "cursor_cloud_api"].includes(nativeRuntimeResolution.profile.backend) && projectRepositoryPaths.length > 0 ? `## Project repositories\nThe task workspace also contains these editable Git repositories:\n${projectRepositoryPaths.map((repo) => `- ${repo}`).join("\n")}` : null, + configuredSecretGuidance(input.config.configuredSecretNames ?? []), ].filter(Boolean).join("\n\n"), initialCommunicationGuidance: nativeReviewRequest ? null : readNonEmptyString(context.paperclipTaskCommunicationGuidance), wakePayload: context.paperclipWake, diff --git a/server/src/services/native-runtime/configured-secret-guidance.ts b/server/src/services/native-runtime/configured-secret-guidance.ts new file mode 100644 index 00000000000..fc9be6d926d --- /dev/null +++ b/server/src/services/native-runtime/configured-secret-guidance.ts @@ -0,0 +1,16 @@ +/** + * Task guidance for configured secrets. Names only: the agent already receives + * these variables in its command environment. The runtime also keeps their + * values out of captured output, so this guidance is a first line of defense, + * not the guarantee. + */ +export function configuredSecretGuidance(names: readonly string[]): string | null { + const unique = [...new Set(names)].filter(name => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name)).sort(); + if (unique.length === 0) return null; + const example = unique[0]!; + return [ + "## Configured secrets", + `Commands receive these secrets as environment variables: ${unique.map(name => `\`${name}\``).join(", ")}.`, + `To check that one is set, use \`[ -n "$${example}" ]\`. Never print, echo, log, or write a secret value to a file; pass it only to the program that needs it.`, + ].join("\n"); +} diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index b00c43c10be..57408cd3455 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -18023,7 +18023,7 @@ async function createRunnerdBackendWithinSessionClaim( for (const key of hostPathKeys) delete effectiveRunnerEnvironmentBase[key]; if (configured.PAPERCLIP_CONFIGURED_ENV_KEYS !== undefined) { effectiveRunnerEnvironmentBase.PAPERCLIP_CONFIGURED_ENV_KEYS = JSON.stringify( - Object.keys(configured).filter(key => key !== "PAPERCLIP_CONFIGURED_ENV_KEYS" && !hostPathKeys.has(key)), + Object.keys(configured).filter(key => key !== "PAPERCLIP_CONFIGURED_ENV_KEYS" && key !== "PAPERCLIP_SECRET_ENV_KEYS" && !hostPathKeys.has(key)), ); } effectiveRunnerEnvironmentBase.HOME = home; diff --git a/server/src/vendor/paperclip-runner/index.ts b/server/src/vendor/paperclip-runner/index.ts index a7c2708f747..cc9c90edd31 100644 --- a/server/src/vendor/paperclip-runner/index.ts +++ b/server/src/vendor/paperclip-runner/index.ts @@ -231,6 +231,7 @@ export const digestPaperclipSemanticContent = runner.digestPaperclipSemanticCont export const CONFIGURED_ENVIRONMENT_KEYS = runner.CONFIGURED_ENVIRONMENT_KEYS; export const GENERATED_RUNTIME_ENVIRONMENT_KEYS = runner.GENERATED_RUNTIME_ENVIRONMENT_KEYS; export const configuredEnvironmentProjection = runner.configuredEnvironmentProjection; +export const configuredSecretEnvironment = runner.configuredSecretEnvironment; export const configuredEnvironment = runner.configuredEnvironment; export const PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS = runner.PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS;