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

crewNow shows nothing sealed, private or hidden to someone who may not see it #14920

Merged opened this

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

Merged

scout/forge:scout/14906-crewnow-shows-nothing-sealed-private-or → main 1 commit fork

  • Merged. scout/14906-crewnow-shows-nothing-sealed-private-or was merged into main as 4e47fb1.

Review

Sign in to review this change.
2 files changed +102 −6
src/core/crew_now.ts +12 −6
@@ -6,6 +6,7 @@
66 */
77
88 import type { Database } from "bun:sqlite";
9 import { OWNER_STANDING } from "./visibility.ts";
910
1011 export const CREW_NOW_MAX = 3;
1112
@@ -46,20 +47,25 @@ const clip = (s: string, n: number): string => {
4647 const STATES = ["running", "queued", "ok", "failed", "cancelled"] as const;
4748
4849 const BY_STATE = (n: number): string => `SELECT r.id AS id, u.username AS username, i.title AS title, r.summary AS summary,
49 r.reason AS reason, p.name AS repo, r.state AS state
50 r.reason AS reason, repo.name AS repo, r.state AS state
5051 FROM run r
51 JOIN repo p ON p.id = r.repo_id AND p.visibility = 'public'
52 JOIN repo ON repo.id = r.repo_id AND repo.visibility = 'public'
5253 LEFT JOIN user u ON u.id = r.actor_id
5354 LEFT JOIN issue i ON i.id = r.issue_id
5455 WHERE r.kind = 'agent' AND r.state = ?${n}
56 AND (i.id IS NULL OR i.sealed = 0)
57 AND ${OWNER_STANDING}
58 AND NOT EXISTS (SELECT 1 FROM app WHERE app.repo_id = repo.id AND app.access = 'private')
5559 ORDER BY r.id DESC
5660 LIMIT ?6`;
5761
5862 /**
59 * Only public repositories are shown: this runs for anyone who holds the link, so a private
60 * repository's name, issue title and summary never reach it. Each state is read through the
61 * (kind, state) index, newest first, three rows at most, so the cost does not grow with the
62 * number of runs: one statement, five LIMITed reads, merged here.
63 * Only what a signed-out reader may see is shown: this runs for anyone who holds the link, so
64 * a private repository's name, a sealed report's title (`sealed.ts`), a private app's runs
65 * (`mayReadRuns`) and a suspended or removed owner's repository (`OWNER_STANDING`) never reach
66 * it. No viewer is asked: the page and the terminal screen show everyone the same list. Each state
67 * is read through the (kind, state) index, newest first, three rows at most, so the cost does not
68 * grow with the number of runs: one statement, five LIMITed reads, merged here.
6369 */
6470 const ALL = STATES.map((_, i) => `SELECT * FROM (${BY_STATE(i + 1)})`).join(" UNION ALL ");
6571
test/crew-now-hidden.test.ts added +90
@@ -0,0 +1,90 @@
1 /**
2 * The crew line on a forge link's page (`core/crew_now.ts`) is read for anyone, so it shows
3 * nothing a signed-out reader may not see (#14906): a run on a sealed report, a run of a
4 * private app on a public repository, and a run on a suspended or removed owner's public
5 * repository are left out, and the read stays one statement.
6 */
7 import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App } from "../src/app.ts";
12 import { crewNow } from "../src/core/crew_now.ts";
13 import { createUser } from "../src/core/users.ts";
14 import { once } from "./helpers/statements.ts";
15 import { expectWork } from "./helpers/work.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17
18 quietLogs();
19
20 describe("crewNow leaves out what a stranger may not see", () => {
21 let dir = "";
22 let app: App;
23 let owner = "";
24 const sql = (q: string, ...a: (string | number)[]) => app.db.query(q).run(...a);
25 const run = (id: string, repo: string, summary: string, issue: string | null = null) =>
26 sql(
27 "INSERT INTO run (id, repo_id, kind, state, summary, actor_id, issue_id) VALUES (?1, ?2, 'agent', 'running', ?3, ?4, ?5)",
28 id, repo, summary, owner, issue as string,
29 );
30 const whats = () => crewNow(app.db).map((r) => r.what);
31
32 beforeAll(async () => {
33 dir = await mkdtemp(join(tmpdir(), "forge-crew-now-hidden-"));
34 app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 });
35 const made = await createUser(app.db, {
36 origin: "seed", trusted: true, username: "olga", email: "[email protected]", password: "a-long-test-passphrase",
37 });
38 if (!made.ok) throw new Error("user");
39 owner = made.value.id;
40 sql("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('pub', ?1, 'notes', 'public')", owner);
41 });
42
43 afterAll(async () => {
44 app.close();
45 await rm(dir, { recursive: true, force: true });
46 });
47
48 beforeEach(() => {
49 sql("DELETE FROM run");
50 sql("UPDATE user SET suspended_at = NULL, removed_at = NULL WHERE id = ?1", owner);
51 });
52
53 test("a run on a sealed report shows neither its title nor its summary", () => {
54 sql("INSERT INTO issue (id, repo_id, number, kind, title, author_id, sealed) VALUES ('s1', 'pub', 1, 'issue', 'overflow in the zebra parser', ?1, 1)", owner);
55 sql("INSERT INTO issue (id, repo_id, number, kind, title, author_id, sealed) VALUES ('o1', 'pub', 2, 'issue', 'an open plan', ?1, 0)", owner);
56 run("01", "pub", "sealed work", "s1");
57 run("02", "pub", "open work", "o1");
58 run("03", "pub", "no issue at all");
59 expect(whats().sort()).toEqual(["an open plan", "no issue at all"]);
60 });
61
62 test("a private app's runs never appear, though its repository is public", () => {
63 sql("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('papp', ?1, 'members-only', 'public')", owner);
64 sql("INSERT INTO app (id, repo_id, slug, access) VALUES ('a1', 'papp', 'members-only', 'private')");
65 sql("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('oapp', ?1, 'open-app', 'public')", owner);
66 sql("INSERT INTO app (id, repo_id, slug, access) VALUES ('a2', 'oapp', 'open-app', 'public')");
67 run("11", "papp", "private app work");
68 run("12", "oapp", "public app work");
69 const runs = crewNow(app.db);
70 expect(runs.map((r) => r.what)).toEqual(["public app work"]);
71 expect(runs.some((r) => r.repo === "members-only")).toBe(false);
72 });
73
74 test("a suspended or removed owner's repository is left out", () => {
75 run("21", "pub", "held owner work");
76 expect(whats()).toEqual(["held owner work"]);
77 sql("UPDATE user SET suspended_at = '2026-11-01T00:00:00Z' WHERE id = ?1", owner);
78 expect(whats()).toEqual([]);
79 sql("UPDATE user SET suspended_at = NULL, removed_at = '2026-11-01T00:00:00Z' WHERE id = ?1", owner);
80 expect(whats()).toEqual([]);
81 });
82
83 test("the read is still one statement", async () => {
84 run("31", "pub", "x");
85 const n = await once(app.db, () => {
86 crewNow(app.db);
87 });
88 expectWork(n, { max: 1 });
89 });
90 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs