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
scout/forge:scout/947-before-publishing-decide-who-reads-a-pub → main 2 commits fork
9 files
changed +199
−34
README.md +1 −1
| @@ -471,7 +471,7 @@ src/ | ||
| 471 | 471 | cli/ `forge` on your machine: login, sync, status, pins |
| 472 | 472 | apps/ a repository that runs: manifest, checkout, process, data, |
| 473 | 473 | 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) | |
| 475 | 475 | crew/ agents as child processes: the brief, Claude Code headless, |
| 476 | 476 | the runner, dispatch off the outbox |
| 477 | 477 |
scripts/gate.ts +1
| @@ -59,6 +59,7 @@ export const GATE: readonly string[] = [ | ||
| 59 | 59 | "test/steer.test.ts", |
| 60 | 60 | "test/sealed-git.test.ts", |
| 61 | 61 | "test/sealed-issues.test.ts", |
| 62 | "test/run-log-audience.test.ts", | |
| 62 | 63 | "test/sealed-release.test.ts", |
| 63 | 64 | "test/sealed-preview.test.ts", |
| 64 | 65 | "test/session-settle.test.ts", |
src/actions/runs.ts +17 −1
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | * the system's own voice and takes no actor. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { mayViewId } from "../core/sealed.ts"; | |
| 11 | import { canSeeSealed, mayViewId } from "../core/sealed.ts"; | |
| 12 | 12 | import { requireVerified } from "./verify.ts"; |
| 13 | 13 | import type { App } from "../app.ts"; |
| 14 | 14 | import { steerRun } from "../crew/runner.ts"; |
| @@ -82,6 +82,21 @@ export function mayReadRuns( | ||
| 82 | 82 | ); |
| 83 | 83 | } |
| 84 | 84 | |
| 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 | ||
| 85 | 100 | export function listRepoRuns( |
| 86 | 101 | app: App, |
| 87 | 102 | actor: User | undefined, |
| @@ -126,6 +141,7 @@ export function runLog( | ||
| 126 | 141 | repo: Repo, |
| 127 | 142 | runId: string, |
| 128 | 143 | ): Result<RunLogView, "not_found"> { |
| 144 | if (!mayReadRunLogs(app, actor, repo)) return err("not_found"); | |
| 129 | 145 | const found = getRepoRun(app, actor, repo, runId); |
| 130 | 146 | if (!found.ok) return found; |
| 131 | 147 | 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"; | ||
| 15 | 15 | import { |
| 16 | 16 | cancelRepoRun, |
| 17 | 17 | listRepoRuns, |
| 18 | getRepoRun, | |
| 18 | 19 | runLog, |
| 19 | 20 | steerRepoRun, |
| 20 | 21 | } from "../../actions/runs.ts"; |
| @@ -296,10 +297,16 @@ export async function runsList(ctx: Ctx): Promise<Response> { | ||
| 296 | 297 | export async function runPage(ctx: Ctx): Promise<Response> { |
| 297 | 298 | const repo = loadRepo(ctx); |
| 298 | 299 | 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. | |
| 299 | 302 | 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); | |
| 303 | 310 | } |
| 304 | 311 | |
| 305 | 312 | // POST /:owner/:name/runs/:id/steer { text } |
src/web/studio/now.ts +2 −2
| @@ -20,7 +20,7 @@ import { getUsersByIds } from "../../core/users.ts"; | ||
| 20 | 20 | import { repoDir } from "../../git/repos.ts"; |
| 21 | 21 | import type { Ctx } from "../../http/context.ts"; |
| 22 | 22 | import { readRunLogTail } from "../../runs/log.ts"; |
| 23 | import { mayReadRuns } from "../../actions/runs.ts"; | |
| 23 | import { mayReadRunLogs } from "../../actions/runs.ts"; | |
| 24 | 24 | import { loadRepo, repoBase } from "../chrome.ts"; |
| 25 | 25 | import { html, type Html } from "../html.ts"; |
| 26 | 26 | import { fragment } from "../layout.ts"; |
| @@ -169,7 +169,7 @@ export async function nowCard( | ||
| 169 | 169 | (r) => r.kind === "agent" && r.state === "running", |
| 170 | 170 | ); |
| 171 | 171 | const activeTail = |
| 172 | activeRun && mayReadRuns(ctx.app, ctx.auth.user, repo) | |
| 172 | activeRun && mayReadRunLogs(ctx.app, ctx.auth.user, repo) | |
| 173 | 173 | ? logBox({ |
| 174 | 174 | id: "app-run-tail", |
| 175 | 175 | who: voiceFor(runActor(activeRun), ownerFacts), |
src/web/studio/previews.ts +3
| @@ -19,6 +19,7 @@ import { listPulls, type Pull } from "../../core/pulls.ts"; | ||
| 19 | 19 | import { canSeeOwnerFacts, type Repo } from "../../core/repos.ts"; |
| 20 | 20 | import { listRuns } from "../../core/runs.ts"; |
| 21 | 21 | import type { Ctx } from "../../http/context.ts"; |
| 22 | import { mayReadRunLogs } from "../../actions/runs.ts"; | |
| 22 | 23 | import { readRunLogTail } from "../../runs/log.ts"; |
| 23 | 24 | import { loadRepo, repoBase } from "../chrome.ts"; |
| 24 | 25 | import { html, when, type Html } from "../html.ts"; |
| @@ -207,6 +208,8 @@ function productionTail( | ||
| 207 | 208 | const deploying = production.state === "deploying"; |
| 208 | 209 | if (!deploying && production.state !== "running") return none; |
| 209 | 210 | 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; | |
| 210 | 213 | const agentBusy = listRuns(ctx.app.db, repo.id, { |
| 211 | 214 | kind: "agent", |
| 212 | 215 | limit: 8, |
src/web/studio/run.ts +29 −15
| @@ -67,6 +67,7 @@ export async function runView( | ||
| 67 | 67 | run: Run, |
| 68 | 68 | lines: readonly string[], |
| 69 | 69 | truncated: boolean, |
| 70 | logOpen = true, | |
| 70 | 71 | ): Promise<Response> { |
| 71 | 72 | const db = ctx.app.db; |
| 72 | 73 | const base = repoBase(repo); |
| @@ -204,33 +205,46 @@ export async function runView( | ||
| 204 | 205 | )} |
| 205 | 206 | ${when(pull, (p) => previewCard(ctx, repo, p))} |
| 206 | 207 | ${when( |
| 207 | truncated, | |
| 208 | logOpen && truncated, | |
| 208 | 209 | () => |
| 209 | 210 | html`<p class="muted small mb"> |
| 210 | 211 | Showing the last 512 KB of the log; the file on disk has the rest. |
| 211 | 212 | </p>`, |
| 212 | 213 | )} |
| 213 | 214 | ${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, | |
| 215 | 223 | () => |
| 216 | 224 | html`<p class="muted small" id="run-empty"> |
| 217 | 225 | Nothing has been logged yet. |
| 218 | 226 | </p>`, |
| 219 | 227 | )} |
| 220 | 228 | <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 | )} | |
| 234 | 248 | ${when( |
| 235 | 249 | run.kind === "agent", |
| 236 | 250 | () => 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", () => { | ||
| 6013 | 6013 | expect(page).not.toContain("$42.50"); |
| 6014 | 6014 | expect(page).not.toContain("7 turns"); |
| 6015 | 6015 | 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"'); | |
| 6017 | 6018 | } |
| 6018 | 6019 | const ownerPage = await ( |
| 6019 | 6020 | await get(`/alice/beacon/runs/${run.id}`, ownerCookie) |
| @@ -6038,17 +6039,12 @@ describe("a stranger sees the app, not the owner's books", () => { | ||
| 6038 | 6039 | ).json()) as { items: { costUsd: number }[] }; |
| 6039 | 6040 | expect(listAsStranger.items.every((r) => r.costUsd === 0)).toBe(true); |
| 6040 | 6041 | |
| 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); | |
| 6052 | 6048 | const tailAsOwner = await ( |
| 6053 | 6049 | await get( |
| 6054 | 6050 | `/api/v1/repos/alice/beacon/runs/${run.id}/log?follow=1`, |
new above
offline