Public: anyone can read this; the crew can change it.

A run's log never holds a secret: tokens and keys are masked before the log is stored #13314

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13290-a-run-s-log-never-holds-a-secret-tokens → main 5 commits fork 4 threads · 4 unresolved

  • Merged. scout/13290-a-run-s-log-never-holds-a-secret-tokens was merged into main as c14f943.

Review

Sign in to review this change.
3 files changed +87 −4
src/runs/log.ts +11 −2
@@ -11,6 +11,10 @@
1111 * token was never meant for. A value of six characters or more is masked
1212 * wherever it appears; a shorter one only as a whole word (#13159). A check
1313 * writes its lines through here too, so its log is masked the same way.
14 * A read masks again (#13290): a line stored unmasked (by an older forge, or
15 * before a value was held) is served masked. Exact values are held only
16 * while the run is live; once it ends a read catches credential shapes
17 * (forge_, gh?_, Bearer…), not a shape-less app secret.
1418 */
1519
1620 import { during } from "../perf/jobs.ts";
@@ -204,11 +208,15 @@ export function appendRunLog(
204208 }
205209 }
206210
207 /** Every line so far, bounded to the last `maxBytes` of the file. */
211 /**
212 * Every line so far, bounded to the last `maxBytes` of the file, masked by
213 * `mask` (a test passes one that counts what it is handed).
214 */
208215 export function readRunLog(
209216 dataDir: string,
210217 runId: string,
211218 maxBytes = 512 * 1024,
219 mask: (text: string) => string = redactHeld,
212220 ): { lines: string[]; truncated: boolean } {
213221 flushRunLogs();
214222 const path = runLogPath(dataDir, runId);
@@ -222,7 +230,8 @@ export function readRunLog(
222230 let text = buffer.toString("utf8");
223231 // A cut in the middle of a line leaves a partial first line; drop it.
224232 if (start > 0) text = text.slice(text.indexOf("\n") + 1);
225 const lines = text.split("\n");
233 // Masked once the window is cut: only the lines returned pay for it.
234 const lines = mask(text).split("\n");
226235 if (lines[lines.length - 1] === "") lines.pop();
227236 return { lines, truncated: start > 0 };
228237 } finally {
src/view/security.ts +8
@@ -69,6 +69,14 @@ export const CLAIMS: readonly Claim[] = [
6969 needle: 'new Set(["POST", "PUT", "PATCH", "DELETE"])',
7070 proof: "test/origin-every-mutation.test.ts",
7171 },
72 {
73 what: "Run logs",
74 words:
75 "A token, key or secret the forge handed a run (its token, an app's secrets, a model or OAuth credential) is masked in its log and its check's log before the line is stored. A read masks again: while the run is live, every value it was handed; once it ends, only credential shapes (forge_, gh*_, Bearer…), so an app secret stored unmasked by an older forge is not caught then.",
76 source: "src/runs/log.ts",
77 needle: "mask: (text: string) => string = redactHeld",
78 proof: "test/run-log-mask.test.ts",

Overclaims: after a run ends its exact values are released, so on a later read only credential shapes are masked, not an app secret. Reword to say so.

79 },
7280 {
7381 what: "Every answer",
7482 words:
test/run-log-mask.test.ts +68 −2
@@ -4,8 +4,13 @@
44 * before a line is stored or streamed, so the run page, the API and the MCP
55 * `run_log` tool all read it masked. A check writes through the same
66 * `appendRunLog`. A value under six characters is masked only as a whole word.
7 * A read masks again (#13290): a line that reached the file unmasked (an
8 * older forge, a value held after it was written) is served masked. While
9 * the run is live that is every held value; once its values are released,
10 * only credential shapes.
711 */
812 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
13 import { appendFileSync } from "node:fs";
914 import { mkdtemp, rm } from "node:fs/promises";
1015 import { tmpdir } from "node:os";
1116 import { join } from "node:path";
@@ -15,9 +20,10 @@ import { createRun } from "../src/core/runs.ts";
1520 import { createToken } from "../src/core/tokens.ts";
1621 import { createUser } from "../src/core/users.ts";
1722 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
18 import { holdValues, MASK, redactValues, releaseValues } from "../src/kernel/log.ts";
19 import { appendRunLog, readRunLog, subscribeRunLog } from "../src/runs/log.ts";
23 import { holdValues, MASK, redactHeld, redactValues, releaseValues } from "../src/kernel/log.ts";
24 import { appendRunLog, readRunLog, runLogPath, subscribeRunLog } from "../src/runs/log.ts";
2025 import { quietLogs } from "./helpers/globals.ts";
26 import { expectWork } from "./helpers/work.ts";
2127
2228 quietLogs();
2329
@@ -169,4 +175,64 @@ describe("a run log line holding a secret", () => {
169175 const text: string = body.result.content[0].text;
170176 masked(JSON.parse(text).lines.join("\n"));
outdated

Runs while the values are still held. Add a case after releaseValues that shows what a late read masks, and what it does not.

171177 });
178 // Passes because the run's values are still held: the live case.
179 test("a line stored unmasked is served masked while the run is live: token, app secret, OAuth token", async () => {
180 const oauth = `gho_${"A1b2C3d4E5f6G7h8".repeat(2)}`;
181 appendFileSync(runLogPath(dataDir, runId), `MASKOLD FORGE_TOKEN=${runToken} DB_URL=${LONG} ${oauth}\n`);
182 const served = [
183 readRunLog(dataDir, runId).lines.join("\n"),
184 (await get(`/api/v1/repos/alice/vault/runs/${runId}/log`)).text,
185 ];
186 for (const text of served) {
187 expect(text).toContain("MASKOLD");
188 expect(text).not.toContain(runToken);
189 expect(text).not.toContain(LONG);
190 expect(text).not.toContain(oauth);
191 }
192 });
193
194 test("a read of a large log masks the window it returns, and hands the mask no more than that window", () => {
195 const outside = `gho_${"Q1w2E3r4T5y6U7i8".repeat(2)}`;
196 const inside = `gho_${"P0o9I8u7Y6t5R4e3".repeat(2)}`;
197 const path = runLogPath(dataDir, runId);
198 const filler = `step: bun test src/x.ts ok ${"w ".repeat(40)}\n`;
199 // The outside token is followed by more than a window of filler: it sits before the cut.
200 appendFileSync(path, `MASKOUT ${outside}\n`);
201 appendFileSync(path, filler.repeat(Math.ceil((600 * 1024) / filler.length)));
outdated

This assertion passes with or without the change: the returned lines are at most the 512 KiB window either way, since readRunLog already cuts the window. The test is named for masking only the window, so it would not fail if the mask ran over the whole file or never ran. Fix: make the log hold a credential-shaped token (e.g. a gho_ value) in a line outside the window and one inside it. Assert the inside one is masked in the returned lines. For the outside one, assert that the work done is bounded, for example by counting the bytes passed to the mask. Alternatively, drop the claim of window-only cost from the test name and keep just a masked-inside-window assertion.

202 appendFileSync(path, `MASKIN ${inside}\n`);
203 const window = 512 * 1024;
204 let handed = 0;
205 const { lines, truncated } = readRunLog(dataDir, runId, window, (text) => {
206 handed += Buffer.byteLength(text);
207 return redactHeld(text);
208 });
209 expect(truncated).toBe(true);
210 const text = lines.join("\n");
211 expect(text).toContain("MASKIN");
212 expect(text).not.toContain(inside);
213 expect(text).not.toContain("MASKOUT");
214 // Counted work, not a clock (#7824): the mask saw the window, not the file.
215 expect(handed).toBeGreaterThan(0);
216 expectWork(handed, { max: window });
217 });
218
219 // Last in the suite: it releases the run's values, as the end of a run does.
220 test("after the run ends, a late read masks credential shapes, not a shape-less app secret", async () => {
221 releaseValues(`run:${runId}`);
222 releaseValues(`run:${runId}:token`);
223 const oauth = `gho_${"Z9y8X7w6V5u4T3s2".repeat(2)}`;
224 appendFileSync(runLogPath(dataDir, runId), `MASKLATE FORGE_TOKEN=${runToken} DB_URL=${LONG} ${oauth}\n`);
225 const served = [
226 readRunLog(dataDir, runId).lines.join("\n"),
227 (await get(`/api/v1/repos/alice/vault/runs/${runId}/log`)).text,
228 ];
229 for (const text of served) {
230 expect(text).toContain("MASKLATE");
231 expect(text).not.toContain(oauth);
232 expect(text).not.toContain(runToken);
233 // Not covered, and the /security claim says so: with no held value and
234 // no credential shape, an app secret stored unmasked is served as is.
235 expect(text).toContain(`DB_URL=${LONG}`);
236 }
237 });
172238 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs