Public: anyone can read this; the crew can change it.
A secret an app holds never appears in a log, an event, an API answer or a page, proven by planting one and searching every surface #13510
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13481-a-secret-an-app-holds-never-appears-in-a → main 5 commits fork
5 files
changed +342
−4
docs/APPS.md +4 −1
| @@ -1014,7 +1014,10 @@ and `@host` widens the shell sandbox's egress for that run only. From the | ||
| 1014 | 1014 | moment they are opened the values are redacted (plain, base64, URL-encoded) |
| 1015 | 1015 | from the run log, the agent's last message and every event payload; before |
| 1016 | 1016 | the forge commits for the agent it scrubs them from the tree, and a branch |
| 1017 | whose commits hold one is not pushed (`crew/leak.ts`). The names held are | |
| 1017 | whose commits hold one is not pushed (`crew/leak.ts`); an agent's own push | |
| 1018 | that brings such a commit is refused in pre-receive, before any ref moves. | |
| 1019 | `test/secrets-never-surface.test.ts` plants one value and searches every | |
| 1020 | surface the forge writes for it. The names held are | |
| 1018 | 1021 | recorded in `run_secret` and shown on the run page; the grant is dropped when |
| 1019 | 1022 | the run ends, and a sweep closes a crash's. A model that pushes by itself |
| 1020 | 1023 | cannot be stopped from pushing a value it transformed beyond those encodings. |
src/crew/leak.ts +32 −3
| @@ -3,7 +3,8 @@ | ||
| 3 | 3 | * forge commits or pushes for an agent, the clone is read for the values |
| 4 | 4 | * the run held: files in the tree are scrubbed in place, and commits the |
| 5 | 5 | * agent already made are searched, because history cannot be scrubbed |
| 6 | * quietly — a branch that holds a value is not pushed. | |
| 6 | * quietly — a branch that holds a value is not pushed. An agent that | |
| 7 | * pushes for itself meets the same search in pre-receive (`pushHolds`). | |
| 7 | 8 | */ |
| 8 | 9 | |
| 9 | 10 | import { runGit, runGitCapture } from "../git/env.ts"; |
| @@ -51,9 +52,37 @@ export async function historyHolds( | ||
| 51 | 52 | values: readonly string[], |
| 52 | 53 | env: Record<string, string>, |
| 53 | 54 | ): Promise<boolean> { |
| 54 | const forms = [...new Set(values.flatMap(formsOf))]; | |
| 55 | return logHolds(dir, [range], [...new Set(values.flatMap(formsOf))], env); | |
| 56 | } | |
| 57 | ||
| 58 | /** What an agent's push that carries a held value is told. */ | |
| 59 | export const PUSH_HOLDS_SECRET = | |
| 60 | "a secret this run holds is in these commits: the push is refused; take it out of the history and push again"; | |
| 61 | ||
| 62 | /** | |
| 63 | * Whether a push brings a commit that carries one of `forms` (already | |
| 64 | * encoded) in its patch or message: the commits reachable from `tips` and | |
| 65 | * from no ref the repository has. Run in pre-receive with the push's | |
| 66 | * quarantine env, so the objects it sent are readable. | |
| 67 | */ | |
| 68 | export async function pushHolds( | |
| 69 | dir: string, | |
| 70 | tips: readonly string[], | |
| 71 | forms: readonly string[], | |
| 72 | env: Record<string, string>, | |
| 73 | ): Promise<boolean> { | |
| 74 | if (tips.length === 0) return false; | |
| 75 | return logHolds(dir, [...tips, "--not", "--all"], forms, env); | |
| 76 | } | |
| 77 | ||
| 78 | async function logHolds( | |
| 79 | dir: string, | |
| 80 | revs: readonly string[], | |
| 81 | forms: readonly string[], | |
| 82 | env: Record<string, string>, | |
| 83 | ): Promise<boolean> { | |
| 55 | 84 | if (forms.length === 0) return false; |
| 56 | const out = await runGitCapture(["log", "-p", "--no-color", range], { | |
| 85 | const out = await runGitCapture(["log", "-p", "--no-color", ...revs], { | |
| 57 | 86 | cwd: dir, |
| 58 | 87 | env, |
| 59 | 88 | }); |
src/http/server.ts +11
| @@ -118,6 +118,8 @@ import { hookEnv, ZERO_OID, type HookRequest } from "../git/hooks.ts"; | ||
| 118 | 118 | import { carryApproval } from "../core/carry_approval.ts"; |
| 119 | 119 | import { resolveOid } from "../git/merge.ts"; |
| 120 | 120 | import { repoDir } from "../git/repos.ts"; |
| 121 | import { PUSH_HOLDS_SECRET, pushHolds } from "../crew/leak.ts"; | |
| 122 | import { heldLongForms } from "../kernel/log.ts"; | |
| 121 | 123 | import { asReader, heldPushConfig, pushCarriesWhatItNames, SEND_WHAT_YOU_NAME } from "../git/own_refs.ts"; |
| 122 | 124 | import { holdFor, lendersBeyond } from "../core/fork_reach.ts"; |
| 123 | 125 | import { |
| @@ -2347,6 +2349,15 @@ async function handleHook(app: App, request: Request): Promise<Response> { | ||
| 2347 | 2349 | } |
| 2348 | 2350 | } |
| 2349 | 2351 | } |
| 2352 | // A crew run's push that carries a value the forge holds (a granted secret, the run's token, | |
| 2353 | // the model credential) is refused before any ref moves: history is not scrubbed quietly, | |
| 2354 | // and the forge's own push for the run refuses the same (`crew/leak.ts`). | |
| 2355 | if (actor?.kind === "agent" && heldLongForms().length > 0) { | |
| 2356 | const tips = input.updates.map((u) => u.newOid).filter((oid) => !ZERO_OID.test(oid)); | |
| 2357 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 2358 | if (await pushHolds(dir, tips, heldLongForms(), input.gitEnv)) | |
| 2359 | return new Response(PUSH_HOLDS_SECRET, { status: 403 }); | |
| 2360 | } | |
| 2350 | 2361 | // A pusher who may not read the repository this fork borrows its objects |
| 2351 | 2362 | // from sends every new object the push names: a tip named and not sent |
| 2352 | 2363 | // would be the lender's commit under the fork's own refs (`git/own_refs.ts`). |
src/kernel/log.ts +7
| @@ -103,6 +103,13 @@ export function redactHeld(text: string): string { | ||
| 103 | 103 | return redactValues(redactSecrets(text)); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | /** | |
| 107 | * The held values long enough to match wherever they appear, with their | |
| 108 | * encoded forms: what a search of other text (a push's commits) looks for. | |
| 109 | * A short value is a word, and a word in a diff is not a leak. | |
| 110 | */ | |
| 111 | export const heldLongForms = (): readonly string[] => heldForms; | |
| 112 | ||
| 106 | 113 | /** Whether any value is held: the cheap test before walking a payload. */ |
| 107 | 114 | export const anyHeld = (): boolean => heldForms.length > 0 || heldWords !== undefined; |
| 108 | 115 |
test/secrets-never-surface.test.ts added +288
| @@ -0,0 +1,288 @@ | ||
| 1 | /** | |
| 2 | * A secret an app holds never appears where the forge writes text (#13481). | |
| 3 | * One sentinel is planted as the app's own variable and as the repository's | |
| 4 | * secret; the app starts and prints everything it was handed, and a crew run | |
| 5 | * that declares the secret echoes its environment into its stream, its last | |
| 6 | * word, a commit message and a committed file. Then every surface the forge | |
| 7 | * writes is searched for the sentinel, plain and encoded: the run logs, the | |
| 8 | * events, the repository API, the feed and the rendered pages. Each surface | |
| 9 | * is named, so a leak says where it is. | |
| 10 | */ | |
| 11 | ||
| 12 | import type { Database } from "bun:sqlite"; | |
| 13 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 14 | import { chmodSync, writeFileSync } from "node:fs"; | |
| 15 | import { join } from "node:path"; | |
| 16 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 17 | import { assignIssue, openIssue } from "../src/actions/issues.ts"; | |
| 18 | import { newApp, setAppEnvVar } from "../src/actions/apps.ts"; | |
| 19 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 20 | import { getIssueById } from "../src/core/issues.ts"; | |
| 21 | import { listPulls } from "../src/core/pulls.ts"; | |
| 22 | import type { Repo } from "../src/core/repos.ts"; | |
| 23 | import { listRuns, type Run } from "../src/core/runs.ts"; | |
| 24 | import { setSecret } from "../src/core/secret_store.ts"; | |
| 25 | import type { User } from "../src/core/users.ts"; | |
| 26 | import { startCrew, type CrewHandle } from "../src/crew/dispatch.ts"; | |
| 27 | import { PUSH_HOLDS_SECRET } from "../src/crew/leak.ts"; | |
| 28 | import { runGitCapture } from "../src/git/env.ts"; | |
| 29 | import { repoDir } from "../src/git/repos.ts"; | |
| 30 | import { formsOf } from "../src/kernel/log.ts"; | |
| 31 | import { readRunLog } from "../src/runs/log.ts"; | |
| 32 | import { | |
| 33 | deploySettled, | |
| 34 | signedInCookie, | |
| 35 | startAppsForge, | |
| 36 | stopAppsForge, | |
| 37 | type AppsForge, | |
| 38 | } from "./helpers/apps-forge.ts"; | |
| 39 | import { agentRunSettled } from "./helpers/crew-standin.ts"; | |
| 40 | import { quietLogs } from "./helpers/globals.ts"; | |
| 41 | ||
| 42 | quietLogs(); | |
| 43 | ||
| 44 | /** Unique, and shaped like nothing `redactSecrets` knows: only holding it can catch it. */ | |
| 45 | const SENTINEL = "sntl7Qx3vK9pLm2Wz8Rb"; | |
| 46 | const FORMS = formsOf(SENTINEL); | |
| 47 | ||
| 48 | /** The app prints all it was handed as it starts, plain and encoded. */ | |
| 49 | const FILES = [ | |
| 50 | { | |
| 51 | path: "forge.json", | |
| 52 | content: `${JSON.stringify({ app: { run: "bun run src/server.ts", health: "/healthz" } })}\n`, | |
| 53 | }, | |
| 54 | { | |
| 55 | path: "src/server.ts", | |
| 56 | content: `const e = process.env; | |
| 57 | console.log("holding " + e.PLANTED); | |
| 58 | console.error("encoded " + Buffer.from(e.PLANTED ?? "").toString("base64") + " " + encodeURIComponent(e.PLANTED ?? "")); | |
| 59 | Bun.serve({ port: Number(e.PORT), hostname: "127.0.0.1", fetch: () => new Response("ok") }); | |
| 60 | `, | |
| 61 | }, | |
| 62 | ]; | |
| 63 | ||
| 64 | /** | |
| 65 | * A stand-in for Claude Code that tells everything it holds: its whole | |
| 66 | * environment into the stream, the secret in a tool call, a file and its last | |
| 67 | * word. Asked to PUSH-IT, it also commits the secret in a message and pushes | |
| 68 | * that itself. | |
| 69 | */ | |
| 70 | const tellingClaude = (dir: string): string => { | |
| 71 | const binary = join(dir, "fake-claude-that-tells"); | |
| 72 | writeFileSync( | |
| 73 | binary, | |
| 74 | `#!/bin/sh | |
| 75 | if [ "$1" = "--version" ]; then echo "2.1.0 (fake)"; exit 0; fi | |
| 76 | IFS= read -r first | |
| 77 | echo '{"type":"system","subtype":"init","session_id":"tells-1","model":"fake"}' | |
| 78 | env | |
| 79 | if printf '%s' "$first" | grep -q "You are the reviewer on this crew"; then | |
| 80 | printf '{"type":"result","subtype":"success","is_error":false,"result":"Reviewed with %s","total_cost_usd":0.004,"num_turns":1,"session_id":"tells-2","terminal_reason":"completed"}\\n' "$PLANTED" | |
| 81 | exit 0 | |
| 82 | fi | |
| 83 | printf '%s\\n' "PLANTED=$PLANTED" > HELD.env | |
| 84 | echo "work $(date +%s%N)" >> CREW.md | |
| 85 | if printf '%s' "$first" | grep -q PUSH-IT; then | |
| 86 | git add -A && git commit -qm "crew work holding $PLANTED" && git push -q -u origin HEAD | |
| 87 | fi | |
| 88 | printf '{"type":"assistant","message":{"id":"m1","content":[{"type":"text","text":"my environment holds %s"},{"type":"tool_use","name":"Bash","input":{"command":"echo %s"}}],"usage":{"input_tokens":1,"output_tokens":1}}}\\n' "$PLANTED" "$PLANTED" | |
| 89 | printf '{"type":"result","subtype":"success","is_error":false,"result":"Done. PLANTED is %s","total_cost_usd":0.01,"num_turns":2,"session_id":"tells-1","terminal_reason":"completed"}\\n' "$PLANTED" | |
| 90 | `, | |
| 91 | ); | |
| 92 | chmodSync(binary, 0o755); | |
| 93 | return binary; | |
| 94 | }; | |
| 95 | ||
| 96 | /** The surfaces searched once each; every run adds its log, API answers and page beside them. */ | |
| 97 | const SURFACES = [ | |
| 98 | "history alice/vault", | |
| 99 | "history bot/vault", | |
| 100 | "events", | |
| 101 | "API repository", | |
| 102 | "API issues", | |
| 103 | "API issue", | |
| 104 | "API issue comments", | |
| 105 | "API runs", | |
| 106 | "API events", | |
| 107 | "API pulls", | |
| 108 | "API commits", | |
| 109 | "API app", | |
| 110 | "API app variables", | |
| 111 | "feed", | |
| 112 | "home", | |
| 113 | "page repository", | |
| 114 | "page issue", | |
| 115 | "page pulls", | |
| 116 | "page pull", | |
| 117 | "page pull files", | |
| 118 | "API pull", | |
| 119 | "page commits", | |
| 120 | "page app settings", | |
| 121 | ]; | |
| 122 | ||
| 123 | const pullNumber = (db: Database, repoId: string): number | undefined => listPulls(db, repoId)[0]?.number; | |
| 124 | ||
| 125 | describe("a planted secret surfaces nowhere", () => { | |
| 126 | let forge: AppsForge; | |
| 127 | let crew: CrewHandle; | |
| 128 | let repo: Repo; | |
| 129 | let bot: User; | |
| 130 | let cookie = ""; | |
| 131 | /** The run that pushed the secret itself, and the one that left it for the forge to commit. */ | |
| 132 | let pushed: Run; | |
| 133 | let agentRun: Run; | |
| 134 | let production: Run; | |
| 135 | ||
| 136 | /** Each surface searched, by name, with the text it showed. */ | |
| 137 | const surfaces = new Map<string, string>(); | |
| 138 | const leaked = (text: string): boolean => FORMS.some((f) => text.includes(f)); | |
| 139 | ||
| 140 | const base = (): string => `/${repo.ownerName}/${repo.name}`; | |
| 141 | const api = (): string => `/api/v1/repos/${repo.ownerName}/${repo.name}`; | |
| 142 | async function read(name: string, path: string): Promise<void> { | |
| 143 | const res = await fetch(`${forge.servers.url}${path}`, { | |
| 144 | headers: { cookie, accept: path.startsWith("/api/") ? "application/json" : "text/html" }, | |
| 145 | redirect: "manual", | |
| 146 | }); | |
| 147 | expect({ path, status: res.status }).toEqual({ path, status: 200 }); | |
| 148 | surfaces.set(name, await res.text()); | |
| 149 | } | |
| 150 | ||
| 151 | beforeAll(async () => { | |
| 152 | forge = await startAppsForge(); | |
| 153 | const { app, alice } = forge; | |
| 154 | const made = await newApp(app, alice, { name: "vault", description: "Holds a key", visibility: "public" }); | |
| 155 | if (!made.ok) throw new Error("app"); | |
| 156 | repo = made.value.repo; | |
| 157 | const set = await setAppEnvVar(app, alice, repo, "PLANTED", SENTINEL, "all"); | |
| 158 | if (!set.ok) throw new Error(set.error); | |
| 159 | expect(setSecret(app.db, forge.dataDir, "repo", repo.id, "PLANTED", SENTINEL).ok).toBe(true); | |
| 160 | const committed = await commitToBranch(app, alice, repo, { message: "Print what it holds", files: FILES }); | |
| 161 | if (!committed.ok) throw new Error(JSON.stringify(committed.error)); | |
| 162 | const deploy = await deploySettled( | |
| 163 | app, | |
| 164 | repo.id, | |
| 165 | (r) => r.env === "production" && r.commitOid === committed.value.commitOid, | |
| 166 | ); | |
| 167 | if (deploy.state !== "ok") throw new Error(deploy.summary); | |
| 168 | production = deploy; | |
| 169 | // The app's first lines are in its deploy's log before anything is searched. | |
| 170 | const deadline = Date.now() + 10_000; | |
| 171 | const started = (): boolean => readRunLog(forge.dataDir, deploy.id).lines.some((l) => l.includes("encoded ")); | |
| 172 | while (!started() && Date.now() < deadline) { | |
| 173 | await Bun.sleep(20); // real-timer: the deployed app is a real process writing its own log | |
| 174 | } | |
| 175 | if (!started()) throw new Error("the deployed app never wrote its first lines"); | |
| 176 | ||
| 177 | const agent = await createAgent(app, alice, { username: "bot", brief: "Be brief." }); | |
| 178 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 179 | bot = agent.value.user; | |
| 180 | expect(addToCrew(app, alice, repo, "bot").ok).toBe(true); | |
| 181 | crew = await startCrew(app, { | |
| 182 | forgeUrl: forge.servers.url, | |
| 183 | localUrl: forge.servers.url, | |
| 184 | binary: tellingClaude(forge.dataDir), | |
| 185 | concurrency: 1, | |
| 186 | budgetUsd: 1, | |
| 187 | runTimeoutMs: 60_000, | |
| 188 | networkRetryScale: 0, | |
| 189 | reviewBudgetMs: 4_000, | |
| 190 | tickMs: 50, | |
| 191 | }); | |
| 192 | const runFor = async (title: string): Promise<Run> => { | |
| 193 | const opened = openIssue(app, alice, repo, { | |
| 194 | title, | |
| 195 | body: "---\nsecrets: PLANTED\n---\nEcho your environment.", | |
| 196 | }); | |
| 197 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 198 | expect(assignIssue(app, alice, repo, opened.value, bot).ok).toBe(true); | |
| 199 | return agentRunSettled(app, repo, (r) => r.issueId === opened.value.id, 60_000); | |
| 200 | }; | |
| 201 | pushed = await runFor("PUSH-IT with what you hold"); | |
| 202 | agentRun = await runFor("Say what you hold"); | |
| 203 | const issue = { value: { number: getIssueById(app.db, agentRun.issueId!)!.number } }; | |
| 204 | // The run's pull, with its description, is opened as the run lands. | |
| 205 | const opened = Date.now() + 20_000; | |
| 206 | let pull: number | undefined; | |
| 207 | while ((pull = pullNumber(app.db, repo.id)) === undefined && Date.now() < opened) { | |
| 208 | await Bun.sleep(20); // real-timer: the crew's real run opens its pull on its own clock | |
| 209 | } | |
| 210 | if (pull === undefined) throw new Error("the crew run opened no pull"); | |
| 211 | await crew.stop(); | |
| 212 | ||
| 213 | cookie = await signedInCookie(forge.servers); | |
| 214 | const runs = listRuns(app.db, repo.id, { limit: 200 }); | |
| 215 | for (const run of runs) surfaces.set(`run log ${run.kind} ${run.id}`, readRunLog(forge.dataDir, run.id).lines.join("\n")); | |
| 216 | for (const [owner, name] of [["alice", "vault"], ["bot", "vault"]] as const) { | |
| 217 | const all = await runGitCapture(["log", "-p", "--no-color", "--all"], { | |
| 218 | cwd: repoDir(app.reposRoot, owner, name), | |
| 219 | }); | |
| 220 | expect(all.code).toBe(0); | |
| 221 | surfaces.set(`history ${owner}/${name}`, all.stdout); | |
| 222 | } | |
| 223 | surfaces.set( | |
| 224 | "events", | |
| 225 | app.db | |
| 226 | .query<{ kind: string; payload: string }, []>("SELECT kind, payload FROM event") | |
| 227 | .all() | |
| 228 | .map((e) => `${e.kind} ${e.payload}`) | |
| 229 | .join("\n"), | |
| 230 | ); | |
| 231 | const number = issue.value.number; | |
| 232 | await read("API repository", api()); | |
| 233 | await read("API issues", `${api()}/issues`); | |
| 234 | await read("API issue", `${api()}/issues/${number}`); | |
| 235 | await read("API issue comments", `${api()}/issues/${number}/comments`); | |
| 236 | await read("API runs", `${api()}/runs`); | |
| 237 | for (const run of runs) { | |
| 238 | await read(`API run ${run.id}`, `${api()}/runs/${run.id}`); | |
| 239 | await read(`API run log ${run.id}`, `${api()}/runs/${run.id}/log`); | |
| 240 | } | |
| 241 | await read("API events", `${api()}/events`); | |
| 242 | await read("API pulls", `${api()}/pulls`); | |
| 243 | await read("API commits", `${api()}/commits`); | |
| 244 | await read("API app", `${api()}/app`); | |
| 245 | await read("API app variables", `${api()}/app/env`); | |
| 246 | await read("feed", "/feed"); | |
| 247 | await read("home", "/"); | |
| 248 | await read("page repository", base()); | |
| 249 | await read("page issue", `${base()}/issues/${number}`); | |
| 250 | await read("page pulls", `${base()}/pulls`); | |
| 251 | await read("page pull", `${base()}/pulls/${pull}`); | |
| 252 | await read("page pull files", `${base()}/pulls/${pull}/files`); | |
| 253 | await read("API pull", `${api()}/pulls/${pull}`); | |
| 254 | await read("page commits", `${base()}/commits`); | |
| 255 | for (const run of runs) await read(`page run ${run.id}`, `${base()}/runs/${run.id}`); | |
| 256 | await read("page app settings", `${base()}/settings/app`); | |
| 257 | }, 240_000); | |
| 258 | ||
| 259 | afterAll(async () => { | |
| 260 | await crew?.stop(); | |
| 261 | await forge.runtime.stop(); | |
| 262 | await stopAppsForge(forge); | |
| 263 | }); | |
| 264 | ||
| 265 | test("the app was handed it and the crew run held it: the planting is real", () => { | |
| 266 | expect(agentRun.state).toBe("ok"); | |
| 267 | const deployLog = surfaces.get(`run log deploy ${production.id}`) ?? ""; | |
| 268 | expect(deployLog).toContain("holding [redacted]"); | |
| 269 | const runLog = surfaces.get(`run log agent ${agentRun.id}`) ?? ""; | |
| 270 | expect(runLog).toContain("secrets: holding PLANTED for this run"); | |
| 271 | expect(runLog).toContain("scrubbed a granted secret value from HELD.env"); | |
| 272 | }); | |
| 273 | ||
| 274 | test("a run that pushes the secret itself is refused at the push, and nothing of it lands", () => { | |
| 275 | const log = surfaces.get(`run log agent ${pushed.id}`) ?? ""; | |
| 276 | expect(log).toContain(PUSH_HOLDS_SECRET); | |
| 277 | expect(log).toContain("refused: a granted secret value is in the commits; not pushed"); | |
| 278 | }); | |
| 279 | ||
| 280 | test("every surface searched is named, and none holds the sentinel in any form", () => { | |
| 281 | const names = [...surfaces.keys()]; | |
| 282 | expect(names.filter((n) => !/ [0-9a-z]{26}$/.test(n))).toEqual(SURFACES); | |
| 283 | // Every run, each as its log, its API answer, its API log and its page. | |
| 284 | for (const kind of ["run log agent", "run log deploy", "API run", "API run log", "page run"]) | |
| 285 | expect(names.some((n) => n.startsWith(`${kind} `))).toBe(true); | |
| 286 | expect(names.filter((n) => leaked(surfaces.get(n)!))).toEqual([]); | |
| 287 | }); | |
| 288 | }); |
new above
offline