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

A secret's value is masked in every run log line and check log line #13180

Merged opened this

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

Merged

torque/forge:torque/13159-a-secret-s-value-is-masked-in-every-run → main 1 commit fork

  • Merged. torque/13159-a-secret-s-value-is-masked-in-every-run was merged into main as ef66834.

Review

Sign in to review this change.
4 files changed +225 −12
src/crew/runner.ts +6
@@ -1126,6 +1126,11 @@ async function work(
11261126 run.id,
11271127 );
11281128 const dir = workdir.dir;
1129 // The run's own token, held by value (and its base64 and URL-encoded
1130 // forms) until the `finally` below: its shape alone is caught, but a
1131 // tool that echoes it encoded is not (#13159).
1132 const tokenHold = `run:${run.id}:token`;
1133 holdValues(tokenHold, [token.token]);
11291134 try {
11301135 if (workdir.own)
11311136 write(`another run holds the issue's clone; working in ${dir}`);
@@ -1940,6 +1945,7 @@ async function work(
19401945 if (!pausing) early?.settle(ending);
19411946 grant?.release();
19421947 releaseValues(modelHold);
1948 releaseValues(tokenHold);
19431949 await workdir.release();
19441950 deleteToken(app.db, agent.user.id, token.id);
19451951 // Whatever the run left in its temp directory, however much: removed off this thread.
src/kernel/log.ts +41 −9
@@ -28,7 +28,17 @@ export function redactSecrets(text: string): string {
2828 */
2929 const held = new Map<string, string[]>();
3030 let heldForms: string[] = [];
31 let heldWords: RegExp | undefined;
32 /** Shorter than this is not held: a three-letter value is a word, not a secret. */
3133 const MIN_HELD = 4;
34 /**
35 * Shorter than this is masked only as a whole word (#13159): a five-letter
36 * value like `hello` must not take `othello` and `hellos` with it. Its
37 * encoded forms are matched the same way, being no longer.
38 */
39 export const MIN_ANYWHERE = 6;
40 /** What a held value becomes, wherever it was. */
41 export const MASK = "[redacted]";
3242
3343 export const formsOf = (value: string): string[] => [
3444 value,
@@ -36,17 +46,34 @@ export const formsOf = (value: string): string[] => [
3646 encodeURIComponent(value),
3747 ];
3848
49 /** A value as a literal inside a pattern. */
50 const literal = (value: string): string => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
51
52 /**
53 * One pattern for every short value, longest first, bounded by anything that
54 * is not a word character. Each start is tried against a fixed set of
55 * literals of at most `MIN_ANYWHERE` characters, so a line costs time
56 * linear in its length however long it runs.
57 */
58 export function wordsPattern(values: readonly string[]): RegExp | undefined {
59 if (values.length === 0) return undefined;
60 const sorted = [...new Set(values)].sort((a, b) => b.length - a.length);
61 return new RegExp(`(?<![\\p{L}\\p{N}_])(?:${sorted.map(literal).join("|")})(?![\\p{L}\\p{N}_])`, "gu");
62 }
63
3964 const rebuild = (): void => {
40 heldForms = [...new Set([...held.values()].flat())].sort(
41 (a, b) => b.length - a.length,
42 );
65 const all = [...new Set([...held.values()].flat())];
66 heldForms = all.filter((v) => v.length >= MIN_ANYWHERE).sort((a, b) => b.length - a.length);
67 heldWords = wordsPattern(all.filter((v) => v.length < MIN_ANYWHERE));
4368 };
4469
4570 /** Start redacting these values everywhere, until `releaseValues(key)`. */
4671 export function holdValues(key: string, values: readonly string[]): void {
4772 held.set(
4873 key,
49 values.filter((v) => v.length >= MIN_HELD).flatMap(formsOf),
74 values
75 .filter((v) => v.length >= MIN_HELD)
76 .flatMap((v) => (v.length < MIN_ANYWHERE ? [v] : formsOf(v))),
5077 );
5178 rebuild();
5279 }
@@ -55,14 +82,19 @@ export function releaseValues(key: string): void {
5582 if (held.delete(key)) rebuild();
5683 }
5784
58 /** Replace every held value in `text` with a marker. */
85 /**
86 * Replace every held value in `text` with `MASK`: a long one wherever it
87 * appears, a short one as a whole word. Given `values`, only those, each
88 * wherever it appears (the caller chose them).
89 */
5990 export function redactValues(
6091 text: string,
61 values: readonly string[] = heldForms,
92 values?: readonly string[],
6293 ): string {
6394 let out = text;
64 for (const value of values)
65 if (value !== "" && out.includes(value)) out = out.split(value).join("[redacted]");
95 for (const value of values ?? heldForms)
96 if (value !== "" && out.includes(value)) out = out.split(value).join(MASK);
97 if (values === undefined && heldWords !== undefined) out = out.replace(heldWords, MASK);
6698 return out;
6799 }
68100
@@ -72,7 +104,7 @@ export function redactHeld(text: string): string {
72104 }
73105
74106 /** Whether any value is held: the cheap test before walking a payload. */
75 export const anyHeld = (): boolean => heldForms.length > 0;
107 export const anyHeld = (): boolean => heldForms.length > 0 || heldWords !== undefined;
76108
77109 export interface Log {
78110 debug(message: string, fields?: LogFields): void;
src/runs/log.ts +6 −3
@@ -5,9 +5,12 @@
55 * line appear the moment it is written, on both surfaces, without a poll.
66 *
77 * Every line passes through `redactHeld` on the way in: credential shapes,
8 * and the exact values a run was granted. A run's token
9 * is in its environment, a curl in the agent's transcript may echo a
10 * header, and a log page is read by people the token was never meant for.
8 * and the exact values a run was granted (its secrets, its model credential
9 * and its own token). A run's token is in its environment, a curl in the
10 * agent's transcript may echo a header, and a log page is read by people the
11 * token was never meant for. A value of six characters or more is masked
12 * wherever it appears; a shorter one only as a whole word (#13159). A check
13 * writes its lines through here too, so its log is masked the same way.
1114 */
1215
1316 import { during } from "../perf/jobs.ts";
test/run-log-mask.test.ts added +172
@@ -0,0 +1,172 @@
1 /**
2 * A secret's value is masked in every run log line (#13159): the values a
3 * run holds — its granted secrets and its own token — become `[redacted]`
4 * before a line is stored or streamed, so the run page, the API and the MCP
5 * `run_log` tool all read it masked. A check writes through the same
6 * `appendRunLog`. A value under six characters is masked only as a whole word.
7 */
8 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { createApp, type App } from "../src/app.ts";
13 import { createRepo } from "../src/actions/repos.ts";
14 import { createRun } from "../src/core/runs.ts";
15 import { createToken } from "../src/core/tokens.ts";
16 import { createUser } from "../src/core/users.ts";
17 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";
20 import { quietLogs } from "./helpers/globals.ts";
21
22 quietLogs();
23
24 const LONG = "s3cr3t-Value-91x";
25 const SHORT = "pq7z";
26
27 describe("held values in a line", () => {
28 afterAll(() => releaseValues("mask-unit"));
29
30 test("a long value is masked wherever it appears, with its encoded forms", () => {
31 holdValues("mask-unit", [LONG]);
32 const b64 = Buffer.from(LONG).toString("base64").replace(/=+$/, "");
33 expect(redactValues(`x${LONG}y`)).toBe(`x${MASK}y`);
34 expect(redactValues(`auth ${b64}`)).toBe(`auth ${MASK}`);
35 expect(redactValues(`q=${encodeURIComponent(LONG)}`)).not.toContain(LONG);
36 });
37
38 test("a short value is masked only as a whole word", () => {
39 holdValues("mask-unit", ["hello"]);
40 expect(redactValues("say hello, then othello and hellos")).toBe(`say ${MASK}, then othello and hellos`);
41 expect(redactValues("key=hello;")).toBe(`key=${MASK};`);
42 expect(redactValues("hello")).toBe(MASK);
43 expect(redactValues("héllo hello_x xhello")).toBe("héllo hello_x xhello");
44 });
45
46 test("a value with pattern characters is matched literally", () => {
47 holdValues("mask-unit", ["a.b+", "(x|y)"]);
48 expect(redactValues("a.b+ axb+ (x|y) x")).toBe(`${MASK} axb+ ${MASK} x`);
49 });
50
51 test("a value under four characters is not held", () => {
52 holdValues("mask-unit", ["abc"]);
53 expect(redactValues("abc")).toBe("abc");
54 });
55
56 test("a long line is masked in linear time", () => {
57 holdValues("mask-unit", [LONG, SHORT, "aaaa", "aaab", "aaaaa"]);
58 const line = "a".repeat(2_000_000);
59 const started = performance.now();
60 expect(redactValues(line)).toBe(line);
61 const big = `${"word ".repeat(400_000)}${SHORT}`;
62 expect(redactValues(big).endsWith(MASK)).toBe(true);
63 expect(performance.now() - started).toBeLessThan(2_000);
64 });
65 });
66
67 describe("a run log line holding a secret", () => {
68 let dataDir: string;
69 let app: App;
70 let http: ForgeHttp;
71 let runId: string;
72 let ro: string;
73 let runToken: string;
74 const seen: string[] = [];
75 const watching = new AbortController();
76
77 const get = async (path: string) => {
78 const res = await http.handle(
79 new Request(`http://forge.test${path}`, {
80 headers: { cookie: "forge_mode=studio", authorization: `Bearer ${ro}` },
81 }),
82 );
83 return { status: res.status, text: await res.text() };
84 };
85
86 beforeAll(async () => {
87 dataDir = await mkdtemp(join(tmpdir(), "forge-run-log-mask-"));
88 app = createApp({ dataDir, port: 0 });
89 http = forgeHttp(app);
90 const u = await createUser(app.db, {
91 origin: "seed",
92 trusted: true,
93 username: "alice",
94 email: "[email protected]",
95 password: "a-good-passphrase",
96 adminIfFirst: true,
97 });
98 if (!u.ok) throw new Error(u.error);
99 ro = createToken(app.db, u.value.id, "ro", "ro").token;
100 runToken = createToken(app.db, u.value.id, "run", "rw").token;
101 const made = await createRepo(app, u.value, { name: "vault", visibility: "private" });
102 if (!made.ok) throw new Error(String(made.error));
103 runId = createRun(app.db, { repoId: made.value.id, kind: "agent", actorId: u.value.id, reason: "assigned" }).id;
104 // What `crew/grant.ts` and `crew/runner.ts` hold for a run.
105 holdValues(`run:${runId}`, [LONG, SHORT]);
106 holdValues(`run:${runId}:token`, [runToken]);
107 subscribeRunLog(runId, (line) => seen.push(line), watching.signal);
108 appendRunLog(dataDir, runId, `MASKLINE env DB_URL=${LONG} pin ${SHORT} pq7z9`);
109 appendRunLog(dataDir, runId, `MASKTOKEN ${encodeURIComponent(runToken)}`);
110 appendRunLog(dataDir, runId, `MASKRAW ${Buffer.from(runToken).toString("base64")}`);
111 }, 30_000);
112
113 afterAll(async () => {
114 watching.abort();
115 releaseValues(`run:${runId}`);
116 releaseValues(`run:${runId}:token`);
117 app.close();
118 await rm(dataDir, { recursive: true, force: true });
119 });
120
121 const masked = (text: string): void => {
122 expect(text).toContain("MASKLINE");
123 expect(text).toContain("pq7z9");
124 expect(text).toContain(MASK);
125 expect(text).not.toContain(LONG);
126 expect(text).not.toMatch(/\bpq7z\b/);
127 expect(text).not.toContain(runToken);
128 expect(text).not.toContain(Buffer.from(runToken).toString("base64").replace(/=+$/, ""));
129 };
130
131 test("the stored file and the live stream hold the mask", () => {
132 const { lines } = readRunLog(dataDir, runId);
133 masked(lines.join("\n"));
134 masked(seen.join("\n"));
135 expect(lines[0]).toBe(`MASKLINE env DB_URL=${MASK} pin ${MASK} pq7z9`);
136 });
137
138 test("the API reads it masked", async () => {
139 const r = await get(`/api/v1/repos/alice/vault/runs/${runId}/log`);
140 expect(r.status).toBe(200);
141 masked(r.text);
142 });
143
144 test("the run page reads it masked", async () => {
145 const r = await get(`/alice/vault/runs/${runId}`);
146 expect(r.status).toBe(200);
147 masked(r.text);
148 });
149
150 test("the MCP run_log tool reads it masked", async () => {
151 const res = await http.handle(
152 new Request("http://forge.test/mcp", {
153 method: "POST",
154 headers: {
155 "content-type": "application/json",
156 accept: "application/json, text/event-stream",
157 authorization: `Bearer ${ro}`,
158 },
159 body: JSON.stringify({
160 jsonrpc: "2.0",
161 id: 1,
162 method: "tools/call",
163 params: { name: "run_log", arguments: { repo: "alice/vault", run: runId } },
164 }),
165 }),
166 );
167 const body: any = await res.json();
168 expect(body.result.isError).not.toBe(true);
169 const text: string = body.result.content[0].text;
170 masked(JSON.parse(text).lines.join("\n"));
171 });
172 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs