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

Pages that name a thing describe it in one sentence from its live state, and a private one says only its name and 'private' #1726

Closed opened this
Closed without merging

scout/forge:scout/1412-pages-that-name-a-thing-describe-it-in-o → main 6 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +371 −4
scripts/gate.ts +1
@@ -50,6 +50,7 @@ export const GATE: readonly string[] = [
5050 "test/og-card.test.ts",
5151 "test/og-route.test.ts",
5252 "test/health.test.ts",
53 "test/unfurl.test.ts",
5354 "test/client-address.test.ts",
5455 "test/client-list.test.ts",
5556 "test/contents-api.test.ts",
src/web/pages/org.ts +11 −1
@@ -37,6 +37,7 @@ import { field, readForm, type Form } from "../form.ts";
3737 import { html, when, type Html } from "../html.ts";
3838 import { icon } from "../icons.ts";
3939 import { flashTo, page } from "../layout.ts";
40 import { personLine } from "../unfurl.ts";
4041 import { secretsSection } from "./secrets.ts";
4142 import { agentsOnForge, grantsSection, saveGrants } from "./subscriptions.ts";
4243 import { shelfRows } from "../studio/shelf.ts";
@@ -362,7 +363,16 @@ export async function ownerProfile(ctx: Ctx): Promise<Response> {
362363 )}
363364 ${isOrg ? orgSecrets(ctx, owner.username) : null}`;
364365
365 return page({ ctx, title: owner.username, body });
366 return page({
367 ctx,
368 title: owner.username,
369 description: personLine(
370 owner.username,
371 listReposByOwner(ctx.app.db, owner.username, undefined, { limit: 101 })
372 .length,
373 ),
374 body,
375 });
366376 }
367377
368378 function orgSecrets(ctx: Ctx, name: string): Html | null {
src/web/studio/app.ts +10
@@ -32,6 +32,7 @@ import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts";
3232 import { appTailJs, liveRegionsJs } from "../client.ts";
3333 import { html, when, type Html } from "../html.ts";
3434 import { page } from "../layout.ts";
35 import { envsFor } from "../../apps/runtime.ts";
3536 import { liveClientJs, liveMount } from "../live.ts";
3637 import { followingCard, followingOf } from "../pages/following.ts";
3738 import {
@@ -44,6 +45,7 @@ import {
4445 time,
4546 userTag,
4647 } from "../ui.ts";
48 import { appLine } from "../unfurl.ts";
4749 import { changesHref, tellsHref } from "./nouns.ts";
4850 import { nowCard, nowRegion } from "./now.ts";
4951 import { repoAsks } from "./asks.ts";
@@ -485,6 +487,14 @@ export async function studioApp(
485487 return page({
486488 ctx,
487489 title: `${repo.name}`,
490 description: appLine(
491 repo,
492 repo.visibility === "public"
493 ? envsFor(ctx.app, ctx.auth.user, record).find(
494 (e) => e.env === "production",
495 )
496 : undefined,
497 ),
488498 crumbs: crumbsFor(repo),
489499 palette: repoPalette(ctx, repo),
490500 body,
src/web/studio/change.ts +3 −1
@@ -68,6 +68,7 @@ import {
6868 parseNumber,
6969 type Entry,
7070 } from "../pages/thread.ts";
71 import { threadLine, titleFor } from "../unfurl.ts";
7172 import {
7273 decisionActions,
7374 decisionForm,
@@ -584,7 +585,8 @@ async function changeConversation(
584585
585586 return page({
586587 ctx,
587 title: `${pull.title} · ${repo.name}`,
588 title: titleFor(repo, pull, pull.title),
outdated

title on the line above still carries pull.title for a private repo or sealed thread. It reaches og:title. Derive the title from the repo name in those cases.

589 description: threadLine(repo, pull),
588590 crumbs: crumbsFor(repo),
589591 palette: repoPalette(ctx, repo),
590592 body,
src/web/studio/run.ts +3 −1
@@ -26,6 +26,7 @@ import { page } from "../layout.ts";
2626 import { btn, sha } from "../ui.ts";
2727 import { previewCard } from "../pages/apps.ts";
2828 import { costText, durationText, runTitle, runTokens } from "../pages/runs.ts";
29 import { runLine, titleFor } from "../unfurl.ts";
2930 import { sandboxLine } from "./sandbox.ts";
3031 import { logBox, runVoice, RUN_TRANSCRIPT_CAP } from "./log.ts";
3132 import { changeHref, tellHref } from "./nouns.ts";
@@ -299,7 +300,8 @@ export async function runView(
299300
300301 return page({
301302 ctx,
302 title: `${title} · ${repo.name}`,
303 title: titleFor(repo, issue, title),
304 description: runLine(repo, run),
303305 crumbs: crumbsFor(repo),
304306 palette: repoPalette(ctx, repo),
305307 body,
src/web/studio/tell.ts +3 −1
@@ -59,6 +59,7 @@ import {
5959 originGlyph,
6060 threadContext,
6161 } from "../pages/thread.ts";
62 import { threadLine, titleFor } from "../unfurl.ts";
6263 import { changeHref, tellHref } from "./nouns.ts";
6364 import {
6465 pullWord,
@@ -418,7 +419,8 @@ async function studioTell(
418419
419420 return page({
420421 ctx,
421 title: `${issue.title} · ${repo.name}`,
422 title: titleFor(repo, issue, issue.title),
423 description: threadLine(repo, issue),
422424 crumbs: crumbsFor(repo),
423425 palette: repoPalette(ctx, repo),
424426 body,
src/web/unfurl.ts added +81
@@ -0,0 +1,81 @@
1 /**
2 * The one sentence a link unfurls as, from a thing's live state. A private
3 * repository's pages say their name and "private" and nothing from inside:
4 * the tags are read by whoever fetches the URL, and a cached unfurl outlives
5 * the session that rendered it. A sealed issue is private inside a public
6 * repository and is held to the same rule.
7 */
8 import type { Issue } from "../core/issues.ts";
9 import type { Pull } from "../core/pulls.ts";
10 import type { Repo } from "../core/repos.ts";
11 import type { Run } from "../core/runs.ts";
12 import type { EnvStatus } from "../apps/runtime.ts";
13
14 const PRIVATE = "Private.";
15
16 const firstLine = (text: string): string =>
17 text.split(/\r?\n/).find((l) => l.trim() !== "")?.trim() ?? "";
18
19 const hidden = (repo: Repo): boolean => repo.visibility !== "public";
20
21 /** "Open · first line of the body", or the title when the body is empty. */
22 export function threadLine(repo: Repo, thread: Issue | Pull): string {
23 if (hidden(repo) || thread.sealed) return PRIVATE;
24 const merged = "mergedAt" in thread && thread.mergedAt !== null;
25 const state = merged ? "Merged" : thread.state === "open" ? "Open" : "Closed";
26 const line = firstLine(thread.body) || thread.title;
27 return `${state} · ${line}`;
28 }
29
30 /** "Succeeded · <summary> (commit abc1234)". */
31 export function runLine(repo: Repo, run: Run): string {
32 if (hidden(repo)) return PRIVATE;
33 const verdict = {
34 queued: "Queued",
35 running: "Running",
36 ok: "Passed",
37 failed: "Failed",
38 cancelled: "Cancelled",
39 }[run.state];
40 const summary = firstLine(run.summary);
41 return `${verdict}${summary === "" ? "" : ` · ${summary}`} (commit ${run.commitOid.slice(0, 7)})`;
42 }
43
44 /** "Live" or "Asleep" for an app's production, from the environment's state. */
45 export function appLine(repo: Repo, production: EnvStatus | undefined): string {
46 if (hidden(repo)) return PRIVATE;
47 const state =
48 production === undefined
49 ? "Not deployed yet"
50 : production.state === "running"
51 ? "Live"
52 : production.state === "asleep" || production.dormant !== undefined
53 ? "Asleep"
54 : production.state === "deploying"
55 ? "Deploying"
56 : production.state === "failed"
57 ? "Failing"
58 : "Stopped";
59 return `${repo.ownerName}/${repo.name} app · ${state}`;
60 }
61
62 /** "N public repositories", counted as a stranger sees them; capped at 100+. */
63 export function personLine(username: string, publicRepos: number): string {
64 const n = publicRepos > 100 ? "100+" : String(publicRepos);
65 return `${username} · ${n} public ${publicRepos === 1 ? "repository" : "repositories"}`;
66 }
67
68 /**
69 * The page title for a thread or run: the title joined to the repository
70 * name, or the repository name alone when the repository is not public or
71 * the thread is sealed. The title feeds `<title>` and og:title, which whoever
72 * fetches the URL reads.
73 */
74 export function titleFor(
75 repo: Repo,
76 thread: Issue | Pull | undefined,
77 title: string,
78 ): string {
79 if (hidden(repo) || thread?.sealed) return repo.name;
80 return `${title} · ${repo.name}`;
81 }
test/unfurl.test.ts added +259
@@ -0,0 +1,259 @@
1 /**
2 * A link unfurls as one sentence from the thing's live state, and a private
3 * thing unfurls as "private" only: the tags are read by whoever fetches the
4 * URL, so the body must never reach them, even for the owner's own session.
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 { openIssue } from "../src/actions/issues.ts";
12 import { openPull } from "../src/actions/pulls.ts";
13 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
14 import type { Repo } from "../src/core/repos.ts";
15 import { createApp as createAppRecord } from "../src/core/apps.ts";
16 import { createRun, type Run } from "../src/core/runs.ts";
17 import type { EnvStatus } from "../src/apps/runtime.ts";
18 import { appLine, runLine } from "../src/web/unfurl.ts";
19 import { createUser, type User } from "../src/core/users.ts";
20 import { serve, type Servers } from "../src/http/server.ts";
21 import { quietLogs } from "./helpers/globals.ts";
22
23 quietLogs();
24
25 const PASSWORD = "an-unfurl-passphrase";
26 const SECRET = "the-private-body-line";
27
28 const ok = <T>(
29 r: { ok: true; value: T } | { ok: false; error: unknown },
30 ): T => {
31 if (!r.ok) throw new Error(JSON.stringify(r.error));
32 return r.value;
33 };
34
35 const titles = (page: string): string =>
36 page.match(/<title>([^<]*)<\/title>/)?.[1]?.replace(/ · .*$/, "") ?? "";
37
38 const og = (page: string, property: string): string =>
39 new RegExp(`<meta (?:property|name)="${property}" content="([^"]*)"`).exec(
40 page,
41 )?.[1] ?? "";
42
43 describe("link descriptions", () => {
44 let dataDir = "";
45 let app: App;
46 let servers: Servers;
47 let alice: User;
48 let cookie = "";
49 let open: Repo;
50 let closed: Repo;
51 let openRun: Run;
52 let closedRun: Run;
53
54 const get = async (path: string, signedIn = true): Promise<string> =>
55 await (
56 await fetch(`${servers.url}${path}`, {
57 headers: signedIn ? { cookie } : {},
58 })
59 ).text();
60
61 beforeAll(async () => {
62 dataDir = await mkdtemp(join(tmpdir(), "forge-unfurl-"));
63 app = createApp({ dataDir, port: 0 });
64 servers = await serve(app);
65 alice = ok(
66 await createUser(app.db, {
67 username: "alice",
68 email: "[email protected]",
69 password: PASSWORD,
70 adminIfFirst: true,
71 trusted: true,
72 }),
73 );
74 const res = await fetch(`${servers.url}/signin`, {
75 method: "POST",
76 body: new URLSearchParams({ username: "alice", password: PASSWORD }),
77 redirect: "manual",
78 });
79 cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!;
80 open = ok(await createRepo(app, alice, { name: "open", visibility: "public" }));
81 closed = ok(
82 await createRepo(app, alice, { name: "closed", visibility: "private" }),
83 );
84 for (const repo of [open, closed]) {
85 ok(
86 await commitToBranch(app, alice, repo, {
87 message: "Start",
88 files: [{ path: "README.md", content: "# hi\n" }],
89 }),
90 );
91 }
92 ok(
93 openIssue(app, alice, open, {
94 title: "Public bug",
95 body: "Crashes on start.\nSecond line.",
96 }),
97 );
98 ok(openIssue(app, alice, closed, { title: "Hidden bug", body: SECRET }));
99 ok(
100 openIssue(app, alice, open, {
101 title: "Sealed bug",
102 body: SECRET,
103 sealed: true,
104 }),
105 );
106 const mk = (repo: Repo): Run =>
107 createRun(app.db, {
108 repoId: repo.id,
109 kind: "deploy",
110 env: "production",
111 commitOid: "abcdef1234567890",
112 summary: SECRET,
113 });
114 for (const repo of [open, closed])
115 ok(
116 createAppRecord(app.db, {
117 repoId: repo.id,
118 slug: `${repo.name}-app`,
119 access: "public",
120 }),
121 );
122 openRun = mk(open);
123 closedRun = mk(closed);
124 ok(
125 await commitToBranch(app, alice, closed, {
126 branch: "feat",
127 base: "main",
128 message: "Feat",
129 files: [{ path: "a.txt", content: "a\n" }],
130 }),
131 );
132 ok(
133 await openPull(app, alice, closed, {
134 title: "Hidden pull",
135 body: SECRET,
136 baseBranch: "main",
137 headBranch: "feat",
138 }),
139 );
140 });
141
142 afterAll(async () => {
143 servers.stop();
144 app.close();
145 await rm(dataDir, { recursive: true, force: true });
146 });
147
148 test("a private pull unfurls as private, never its body", async () => {
149 const page = await get("/alice/closed/pulls/2");
150 expect(titles(page)).toBe("closed");
151 expect(page).not.toMatch(/<title>[^<]*Hidden pull/);
152 expect(og(page, "og:title")).not.toContain("Hidden pull");
153 expect(page).not.toContain(`content="${SECRET}`);
154 expect(og(page, "og:description")).toBe("Private.");
155 expect(og(page, "og:description")).not.toContain(SECRET);
156 });
157
158 test("a private repository's issue unfurls as private, never its body", async () => {
159 const page = await get("/alice/closed/issues/1");
160 expect(og(page, "og:description")).toBe("Private.");
161 expect(og(page, "og:title")).toBe("closed");
162 expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`));
163 });
164
165 test("an anonymous reader of a private repository gets neither", async () => {
166 const page = await get("/alice/closed/issues/1", false);
167 expect(page).not.toContain(SECRET);
168 });
169
170 test("a public issue unfurls as its state and first line", async () => {
outdated

This asserts the page still contains the private pull's title. That is the leak. Assert it is absent from <title> and og:title.

171 const page = await get("/alice/open/issues/1", false);
172 expect(og(page, "og:title")).toContain("Public bug");
173 expect(og(page, "og:description")).toBe("Open · Crashes on start.");
174 expect(og(page, "description")).toBe("Open · Crashes on start.");
175 });
176
177 test("a sealed issue in a public repository unfurls as private, for its owner too", async () => {
178 const page = await get("/alice/open/issues/2");
179 expect(titles(page)).toBe("open");
180 expect(page).not.toMatch(/<title>[^<]*Sealed bug/);
181 expect(og(page, "og:title")).not.toContain("Sealed bug");
182 expect(og(page, "og:description")).toBe("Private.");
183 expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`));
184 });
185
186 test("a private repository's run page unfurls as private", async () => {
187 const page = await get(`/alice/closed/runs/${closedRun.id}`);
188 expect(og(page, "og:description")).toBe("Private.");
189 expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`));
190 });
191
192 test("a public run page unfurls as its verdict, summary and commit", async () => {
193 const page = await get(`/alice/open/runs/${openRun.id}`, false);
194 expect(og(page, "og:description")).toBe(
195 `Queued · ${SECRET} (commit abcdef1)`,
196 );
197 });
198
199 test("a private repository's app page unfurls as private", async () => {
200 const page = await get("/alice/closed");
201 expect(og(page, "og:description")).toBe("Private.");
202 });
203
204 test("a public app page unfurls as its name and state", async () => {
205 const page = await get("/alice/open", false);
206 expect(og(page, "og:description")).toBe("alice/open app · Not deployed yet");
207 });
208
209 test("a person's page counts their public repositories only", async () => {
210 const page = await get("/alice", false);
211 expect(og(page, "og:description")).toBe("alice · 1 public repository");
212 });
213 });
214
215 describe("run and app lines", () => {
216 const repo = (visibility: string) =>
217 ({ visibility, ownerName: "alice", name: "r" }) as unknown as Repo;
218 const run = (state: string, summary = "") =>
219 ({ state, summary, commitOid: "0123456789" }) as unknown as Run;
220 const env = (state: string, dormant?: object) =>
221 ({ env: "production", state, dormant }) as unknown as EnvStatus;
222
223 test("a run's verdict follows its state", () => {
224 const verdicts = {
225 queued: "Queued",
226 running: "Running",
227 ok: "Passed",
228 failed: "Failed",
229 cancelled: "Cancelled",
230 };
231 for (const [state, word] of Object.entries(verdicts))
232 expect(runLine(repo("public"), run(state))).toBe(`${word} (commit 0123456)`);
233 expect(runLine(repo("public"), run("ok", "\n All green\nmore"))).toBe(
234 "Passed · All green (commit 0123456)",
235 );
236 });
237
238 test("a run in a private or internal repository says only Private.", () => {
239 for (const v of ["private", "internal"])
240 expect(runLine(repo(v), run("ok", "secret"))).toBe("Private.");
241 });
242
243 test("an app is Live, Asleep, or in the state it is in", () => {
244 const line = (e?: EnvStatus) => appLine(repo("public"), e);
245 expect(line(env("running"))).toBe("alice/r app · Live");
246 expect(line(env("asleep"))).toBe("alice/r app · Asleep");
247 expect(line(env("stopped", { since: "x", idleSince: "y" }))).toBe(
248 "alice/r app · Asleep",
249 );
250 expect(line(env("deploying"))).toBe("alice/r app · Deploying");
251 expect(line(env("failed"))).toBe("alice/r app · Failing");
252 expect(line(env("stopped"))).toBe("alice/r app · Stopped");
253 expect(line(undefined)).toBe("alice/r app · Not deployed yet");
254 });
255
256 test("a private app says only Private.", () => {
257 expect(appLine(repo("private"), env("running"))).toBe("Private.");
258 });
259 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs