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

Before publishing: decide who reads a public repository's crew run logs, and seal the security work from before sealing existed #1011

Closed opened this in answer to
Closed without merging

scout/forge:scout/947-before-publishing-decide-who-reads-a-pub → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
9 files changed +199 −34
README.md +1 −1
@@ -471,7 +471,7 @@ src/
471471 cli/ `forge` on your machine: login, sync, status, pins
472472 apps/ a repository that runs: manifest, checkout, process, data,
473473 the runtime (environments), the proxy by Host
474 runs/ run logs, appended live
474 runs/ run logs, appended live (on a public repository, read only by its writers and crew; a run's summary stays public)
475475 crew/ agents as child processes: the brief, Claude Code headless,
476476 the runner, dispatch off the outbox
477477 ```
scripts/gate.ts +1
@@ -59,6 +59,7 @@ export const GATE: readonly string[] = [
5959 "test/steer.test.ts",
6060 "test/sealed-git.test.ts",
6161 "test/sealed-issues.test.ts",
62 "test/run-log-audience.test.ts",
6263 "test/sealed-release.test.ts",
6364 "test/sealed-preview.test.ts",
6465 "test/session-settle.test.ts",
src/actions/runs.ts +17 −1
@@ -8,7 +8,7 @@
88 * the system's own voice and takes no actor.
99 */
1010
11 import { mayViewId } from "../core/sealed.ts";
11 import { canSeeSealed, mayViewId } from "../core/sealed.ts";
1212 import { requireVerified } from "./verify.ts";
1313 import type { App } from "../app.ts";
1414 import { steerRun } from "../crew/runner.ts";
@@ -82,6 +82,21 @@ export function mayReadRuns(
8282 );
8383 }
8484
85 /**
86 * Whether `actor` may read the logs of this repository's runs. A log is the
87 * agent's transcript — what it ran, read and concluded — so on a public
88 * repository only its writers and crew read it; a run's summary stays as
89 * public as the run. A private repository's readers read both (#947).
90 */
91 export function mayReadRunLogs(
92 app: App,
93 actor: User | undefined,
94 repo: Repo,
95 ): boolean {
96 if (!mayReadRuns(app, actor, repo)) return false;
97 return repo.visibility !== "public" || canSeeSealed(app.db, repo, actor);
98 }
99
85100 export function listRepoRuns(
86101 app: App,
87102 actor: User | undefined,
@@ -126,6 +141,7 @@ export function runLog(
126141 repo: Repo,
127142 runId: string,
128143 ): Result<RunLogView, "not_found"> {
144 if (!mayReadRunLogs(app, actor, repo)) return err("not_found");
129145 const found = getRepoRun(app, actor, repo, runId);
130146 if (!found.ok) return found;
131147 const { lines, truncated } = readRunLog(app.config.dataDir, runId);
src/web/pages/runs.ts +10 −3
@@ -15,6 +15,7 @@ import type { Database } from "bun:sqlite";
1515 import {
1616 cancelRepoRun,
1717 listRepoRuns,
18 getRepoRun,
1819 runLog,
1920 steerRepoRun,
2021 } from "../../actions/runs.ts";
@@ -296,10 +297,16 @@ export async function runsList(ctx: Ctx): Promise<Response> {
296297 export async function runPage(ctx: Ctx): Promise<Response> {
297298 const repo = loadRepo(ctx);
298299 if (!repo) return notFoundPage(ctx);
300 // The run and its summary are as public as the repository; its log is not
301 // (`mayReadRunLogs`), so a stranger gets the page without the transcript.
299302 const found = runLog(ctx.app, ctx.auth.user, repo, ctx.params.id ?? "");
300 if (!found.ok) return notFoundPage(ctx);
301 const { run, lines, truncated } = found.value;
302 return await runView(ctx, repo, run, lines, truncated);
303 if (found.ok) {
304 const { run, lines, truncated } = found.value;
305 return await runView(ctx, repo, run, lines, truncated);
306 }
307 const run = getRepoRun(ctx.app, ctx.auth.user, repo, ctx.params.id ?? "");
308 if (!run.ok) return notFoundPage(ctx);
309 return await runView(ctx, repo, run.value, [], false, false);
303310 }
304311
305312 // POST /:owner/:name/runs/:id/steer { text }
src/web/studio/now.ts +2 −2
@@ -20,7 +20,7 @@ import { getUsersByIds } from "../../core/users.ts";
2020 import { repoDir } from "../../git/repos.ts";
2121 import type { Ctx } from "../../http/context.ts";
2222 import { readRunLogTail } from "../../runs/log.ts";
23 import { mayReadRuns } from "../../actions/runs.ts";
23 import { mayReadRunLogs } from "../../actions/runs.ts";
2424 import { loadRepo, repoBase } from "../chrome.ts";
2525 import { html, type Html } from "../html.ts";
2626 import { fragment } from "../layout.ts";
@@ -169,7 +169,7 @@ export async function nowCard(
169169 (r) => r.kind === "agent" && r.state === "running",
170170 );
171171 const activeTail =
172 activeRun && mayReadRuns(ctx.app, ctx.auth.user, repo)
172 activeRun && mayReadRunLogs(ctx.app, ctx.auth.user, repo)
173173 ? logBox({
174174 id: "app-run-tail",
175175 who: voiceFor(runActor(activeRun), ownerFacts),
src/web/studio/previews.ts +3
@@ -19,6 +19,7 @@ import { listPulls, type Pull } from "../../core/pulls.ts";
1919 import { canSeeOwnerFacts, type Repo } from "../../core/repos.ts";
2020 import { listRuns } from "../../core/runs.ts";
2121 import type { Ctx } from "../../http/context.ts";
22 import { mayReadRunLogs } from "../../actions/runs.ts";
2223 import { readRunLogTail } from "../../runs/log.ts";
2324 import { loadRepo, repoBase } from "../chrome.ts";
2425 import { html, when, type Html } from "../html.ts";
@@ -207,6 +208,8 @@ function productionTail(
207208 const deploying = production.state === "deploying";
208209 if (!deploying && production.state !== "running") return none;
209210 if (!deploying && !writer) return none;
211 // A deploy's lines are a run's log: public repositories keep them to writers (#947).
212 if (!mayReadRunLogs(ctx.app, ctx.auth.user, repo)) return none;
210213 const agentBusy = listRuns(ctx.app.db, repo.id, {
211214 kind: "agent",
212215 limit: 8,
src/web/studio/run.ts +29 −15
@@ -67,6 +67,7 @@ export async function runView(
6767 run: Run,
6868 lines: readonly string[],
6969 truncated: boolean,
70 logOpen = true,
7071 ): Promise<Response> {
7172 const db = ctx.app.db;
7273 const base = repoBase(repo);
@@ -204,33 +205,46 @@ export async function runView(
204205 )}
205206 ${when(pull, (p) => previewCard(ctx, repo, p))}
206207 ${when(
207 truncated,
208 logOpen && truncated,
208209 () =>
209210 html`<p class="muted small mb">
210211 Showing the last 512 KB of the log; the file on disk has the rest.
211212 </p>`,
212213 )}
213214 ${when(
214 lines.length === 0,
215 !logOpen,
216 () =>
217 html`<p class="muted small" id="run-log-closed">
218 The log is kept to the people who work on this repository.
219 </p>`,
220 )}
221 ${when(
222 logOpen && lines.length === 0,
215223 () =>
216224 html`<p class="muted small" id="run-empty">
217225 Nothing has been logged yet.
218226 </p>`,
219227 )}
220228 <div class="run-live" id="run-live" data-state="${run.state}">
221 ${logBox({
222 id: "run-log",
223 who,
224 cap: all ? Math.max(lines.length, RUN_TRANSCRIPT_CAP) : RUN_TRANSCRIPT_CAP,
225 lines,
226 allHref: `${base}/runs/${run.id}?all=1`,
227 truncated,
228 ...(unfinished ? { stream } : {}),
229 pulse: run.state === "running",
230 extra: html`<button type="button" class="run-jump" id="run-jump" hidden>
231 newer below
232 </button>`,
233 })}
229 ${when(
230 logOpen,
231 () =>
232 logBox({
233 id: "run-log",
234 who,
235 cap: all
236 ? Math.max(lines.length, RUN_TRANSCRIPT_CAP)
237 : RUN_TRANSCRIPT_CAP,
238 lines,
239 allHref: `${base}/runs/${run.id}?all=1`,
240 truncated,
241 ...(unfinished ? { stream } : {}),
242 pulse: run.state === "running",
243 extra: html`<button type="button" class="run-jump" id="run-jump" hidden>
244 newer below
245 </button>`,
246 }),
247 )}
234248 ${when(
235249 run.kind === "agent",
236250 () => html`<p class="muted small crew-note">${directionNote(repo)}</p>`,
test/run-log-audience.test.ts added +128
@@ -0,0 +1,128 @@
1 /**
2 * On a public repository a run's summary is public and its log is not
3 * (#947): the log is the agent's transcript, so only writers and the crew
4 * read it, on every surface — page, API, stream and the live tail.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { addToCrew, createAgent } from "../src/actions/crew.ts";
12 import { createRepo } from "../src/actions/repos.ts";
13 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
14 import { createToken } from "../src/core/tokens.ts";
15 import { createUser } from "../src/core/users.ts";
16 import { serve, type Servers } from "../src/http/server.ts";
17 import { appendRunLog } from "../src/runs/log.ts";
18 import { quietLogs } from "./helpers/globals.ts";
19
20 quietLogs();
21
22 const LINE = "Z7LOGLINE cat /etc/passwd in the sandbox";
23 const SUMMARY = "Z7SUMMARY fixed the thing";
24
25 let dataDir: string;
26 let app: App;
27 let servers: Servers;
28 const token: Record<string, string> = {};
29 const runs: Record<string, string> = {};
30
31 async function get(path: string, who?: string) {
32 const res = await fetch(`${servers.url}${path}`, {
33 headers: who === undefined ? {} : { authorization: `Bearer ${token[who]}` },
34 redirect: "manual",
35 });
36 return { status: res.status, text: await res.text() };
37 }
38
39 beforeAll(async () => {
40 dataDir = await mkdtemp(join(tmpdir(), "forge-runlog-"));
41 app = createApp({ dataDir, port: 0 });
42 servers = await serve(app);
43 const mk = async (username: string, first = false) => {
44 const r = await createUser(app.db, {
45 trusted: true,
46 username,
47 email: `${username}@forge.local`,
48 password: "a-good-passphrase",
49 adminIfFirst: first,
50 });
51 if (!r.ok) throw new Error("user");
52 return r.value;
53 };
54 const alice = await mk("alice", true);
55 const mallory = await mk("mallory");
56 const bot = await createAgent(app, alice, { username: "bot" });
57 if (!bot.ok) throw new Error("agent");
58 for (const [name, visibility] of [
59 ["square", "public"],
60 ["vault", "private"],
61 ] as const) {
62 const r = await createRepo(app, alice, { name, visibility });
63 if (!r.ok) throw new Error("repo");
64 if (name === "square") addToCrew(app, alice, r.value, "bot");
65 const run = createRun(app.db, {
66 repoId: r.value.id,
67 kind: "agent",
68 actorId: bot.value.user.id,
69 reason: "assigned",
70 });
71 claimRun(app.db, run.id);
72 appendRunLog(dataDir, run.id, LINE);
73 finishRun(app.db, run.id, { state: "ok", summary: SUMMARY });
74 runs[name] = run.id;
75 }
76 token.alice = createToken(app.db, alice.id, "t", "rw").token;
77 token.mallory = createToken(app.db, mallory.id, "t", "rw").token;
78 token.bot = createToken(app.db, bot.value.user.id, "t", "rw").token;
79 });
80
81 afterAll(async () => {
82 servers.stop();
83 await rm(dataDir, { recursive: true, force: true });
84 });
85
86 describe("a public repository's run log", () => {
87 test("a stranger reads a public repository's run summary, not its log", async () => {
88 const id = runs.square;
89 for (const who of [undefined, "mallory"]) {
90 const api = await get(`/api/v1/repos/alice/square/runs/${id}`, who);
91 expect(api.status).toBe(200);
92 expect(api.text).toContain("Z7SUMMARY");
93 const list = await get(`/api/v1/repos/alice/square/runs`, who);
94 expect(list.text).toContain("Z7SUMMARY");
95 const page = await get(`/alice/square/runs/${id}`, who);
96 expect(page.status).toBe(200);
97 expect(page.text).toContain("Z7SUMMARY");
98 expect(page.text).not.toContain("Z7LOGLINE");
99 for (const path of [
100 `/api/v1/repos/alice/square/runs/${id}/log`,
101 `/api/v1/repos/alice/square/runs/${id}/log?follow=1`,
102 ]) {
103 const log = await get(path, who);
104 expect(log.status).toBe(404);
105 expect(log.text).not.toContain("Z7LOGLINE");
106 }
107 }
108 });
109
110 test("a writer and the crew read it", async () => {
111 const id = runs.square;
112 for (const who of ["alice", "bot"]) {
113 const log = await get(`/api/v1/repos/alice/square/runs/${id}/log`, who);
114 expect(log.status).toBe(200);
115 expect(log.text).toContain("Z7LOGLINE");
116 const page = await get(`/alice/square/runs/${id}`, who);
117 expect(page.text).toContain("Z7LOGLINE");
118 }
119 });
120
121 test("a private repository's reader keeps reading its logs", async () => {
122 const log = await get(
123 `/api/v1/repos/alice/vault/runs/${runs.vault}/log`,
124 "alice",
125 );
126 expect(log.text).toContain("Z7LOGLINE");
127 });
128 });
test/studio.test.ts +8 −12
@@ -6013,7 +6013,8 @@ describe("a stranger sees the app, not the owner's books", () => {
60136013 expect(page).not.toContain("$42.50");
60146014 expect(page).not.toContain("7 turns");
60156015 expect(page).not.toContain("keeper");
6016 expect(page).toContain('data-who="the crew"');
6016 // The transcript is the writers' (#947); the page says so instead.
6017 expect(page).toContain('id="run-log-closed"');
60176018 }
60186019 const ownerPage = await (
60196020 await get(`/alice/beacon/runs/${run.id}`, ownerCookie)
@@ -6038,17 +6039,12 @@ describe("a stranger sees the app, not the owner's books", () => {
60386039 ).json()) as { items: { costUsd: number }[] };
60396040 expect(listAsStranger.items.every((r) => r.costUsd === 0)).toBe(true);
60406041
6041 const tailAsStranger = await (
6042 await get(
6043 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,
6044 strangerCookie,
6045 )
6046 ).text();
6047 expect(tailAsStranger).toContain("event: done");
6048 // The cost by its key, not its digits: a timestamp's seconds can read
6049 // "42.5" too, and once did.
6050 expect(tailAsStranger).not.toContain('"costUsd":42.5');
6051 expect(tailAsStranger).toContain('"costUsd":0');
6042 // A stranger gets no stream at all on a public repository, so no cost in it.
6043 const tailAsStranger = await get(
6044 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,
6045 strangerCookie,
6046 );
6047 expect(tailAsStranger.status).toBe(404);
60526048 const tailAsOwner = await (
60536049 await get(
60546050 `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`,

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs