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

Every forge page carries og:title, og:description, og:image and twitter:card, and a private page reveals only its title and 'private' #2435

Parked opened this
Closed without merging

scout/forge:scout/2349-every-forge-page-carries-og-title-og-des → main 3 commits fork 4 threads · 4 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
14 files changed +305 −27
README.md +11 −3
@@ -1006,9 +1006,17 @@ Two of them carry a figure beside the words:
10061006 50%), with the top three findings worst first. Public repositories' apps only,
10071007 as `/audit` shows them signed out.
10081008
1009 The map page (public repositories) and `/audit` name their `.png` as
1010 `og:image` with `twitter:card=summary_large_image`; every other page keeps the
1011 forge icon.
1009 Every page carries `og:title`, `og:description`, an `og:image` that answers
1010 200 `image/png`, and `twitter:card=summary_large_image`
1011 (`test/og-every-page.test.ts` walks them). A page names its own card
1012 (repository, issue, pull, run, app, map, audit) through `cardLink` in
1013 `src/web/og/link.ts`, which applies the card route's own test: only what a
1014 signed-out reader may open. Anything else (a private repository, a sealed
1015 report, a hidden run log, a private app, the person page and every top-level
1016 page) gets the site card, `/og/site.png`. A signed-in reader of a private
1017 repository gets the title, `Private.` and the site card. A signed-out reader
1018 gets the same 404 and tags as for a repository that does not exist, so a
1019 guess never confirms one.
10121020
10131021 ## Develop
10141022
src/http/server.ts +2
@@ -1161,6 +1161,8 @@ export function buildRouter(): Router<Handler> {
11611161 router.add("GET", "/og/pull/:ref.svg", ogCard("pull"));
11621162 router.add("GET", "/og/run/:ref.svg", ogCard("run"));
11631163 router.add("GET", "/og/map/:ref.svg", ogCard("map"));
1164 router.add("GET", "/og/site.svg", ogCard("site"));
1165 router.add("GET", "/og/site.png", ogCard("site", "png"));
11641166 router.add("GET", "/og/audit.svg", ogCard("audit"));
11651167 router.add("GET", "/og/repo/:ref.png", ogCard("repo", "png"));
11661168 router.add("GET", "/og/app/:ref.png", ogCard("app", "png"));
src/web/describe.ts +6
@@ -57,6 +57,12 @@ export function runLine(o: {
5757 return say(`${o.word}${at}`, firstLine(o.summary));
5858 }
5959
60 /** A repository: its own description, or "Private." for a private one. */
61 export function repoLine(o: { private: boolean; description: string }): string {
62 if (o.private) return PRIVATE_LINE;
63 return firstLine(o.description);
64 }
65
6066 /** A person or an organization, by what is public about them. */
6167 export function ownerLine(o: {
6268 org: boolean;
src/web/layout.ts +13 −17
@@ -37,6 +37,7 @@ import { prefersJson } from "./form.ts";
3737 import { html, raw, when, type Html } from "./html.ts";
3838 import { noRoom } from "../spawn/door.ts";
3939 import { icon, iconSprite, inlineIcons, themeGlyph } from "./icons.ts";
40 import { SITE_CARD, SITE_LINE } from "./og/link.ts";
4041 import { btn, kbd, menu, pill, type MenuItem } from "./ui.ts";
4142
4243 /** One row of the command palette: title, href, category. */
@@ -67,7 +68,7 @@ export interface PageOptions {
6768 * `/og/audit.png` (`pages/og.ts`). Only for a page whose card answers a
6869 * signed-out unfurler; absent, the forge's icon stands in.
6970 */
70 ogImage?: string;
71 ogImage?: string | undefined;
7172 /** This page's feed, advertised in the head so a reader can find it. */
7273 feed?: { href: string; title: string };
7374 /** Breadcrumb segments rendered in the topbar, e.g. owner / repo. */
@@ -529,7 +530,10 @@ function footer(ctx: Ctx): Html {
529530
530531 // ---- the page ----
531532
532 const SITE_LINE = "Your code, your machine.";
533 /** The page's own card when it names one of ours, else the site's. */
534 function cardPath(own: string | undefined): string {
535 return own !== undefined && own.startsWith("/og/") ? own : SITE_CARD;
536 }
533537
534538 function pageDescription(options: PageOptions): string {
535539 const own = options.description?.trim().split(/\r?\n/)[0]?.trim() ?? "";
@@ -605,21 +609,13 @@ export function page(options: PageOptions, status = 200): Response {
605609 />`
606610 : null
607611 }
608 ${
609 options.ogImage !== undefined && options.ogImage.startsWith("/og/")
610 ? html`<meta
611 property="og:image"
612 content="${ctx.url.origin}${options.ogImage}"
613 />
614 <meta property="og:image:width" content="1200" />
615 <meta property="og:image:height" content="630" />
616 <meta name="twitter:card" content="summary_large_image" />`
617 : html`<meta
618 property="og:image"
619 content="${ctx.url.origin}/icons/forge-512.png"
620 />
621 <meta name="twitter:card" content="summary" />`
622 }
612 <meta
613 property="og:image"
614 content="${ctx.url.origin}${cardPath(options.ogImage)}"
615 />
616 <meta property="og:image:width" content="1200" />
617 <meta property="og:image:height" content="630" />
618 <meta name="twitter:card" content="summary_large_image" />
623619 <link rel="manifest" href="/manifest.webmanifest" />
624620 ${links.map(
625621 (l) =>
src/web/pages/og.ts +8 −1
@@ -42,6 +42,7 @@ import { isSafeName } from "../../git/guard.ts";
4242 import type { Ctx } from "../../http/context.ts";
4343 import { notFound } from "../../http/respond.ts";
4444 import { auditInput, type AuditRow } from "../og/audit.ts";
45 import { SITE_LINE } from "../og/link.ts";
4546 import { renderCard, signature, type CardInput } from "../og/card.ts";

The card route now depends on the page shell, and layout.ts depends on og/link.ts. Move SITE_LINE beside SITE_CARD, so the dependency runs one way only.

4647 import { appImages, embedShots, pullImages } from "../og/looks.ts";
4748 import { mapInput } from "../og/map.ts";
@@ -146,7 +147,7 @@ function waiting(ctx: Ctx, repo: Repo, issue: Issue): string {
146147 return `waits on ${shown.join(", ")}${more > 0 ? ` +${more}` : ""}`;
147148 }
148149
149 export type OgKind = "repo" | "app" | "issue" | "pull" | "run" | "map" | "audit";
150 export type OgKind = "site" | "repo" | "app" | "issue" | "pull" | "run" | "map" | "audit";
150151
151152 /** The audit page's own bound. */
152153 const AUDIT_LIMIT = 200;
@@ -313,11 +314,17 @@ async function auditCardInput(ctx: Ctx): Promise<CardInput> {
313314 return auditInput(rows);
314315 }
315316
317 /** The card for any page that has none of its own: the forge, in a line. */
318 function siteInput(): CardInput {
319 return { kind: "page", title: "forge", lines: [SITE_LINE] };
320 }
321
316322 async function resolve(ctx: Ctx, kind: OgKind, ref: string): Promise<CardInput | undefined> {
317323 const db = ctx.app.db;
318324 if (kind === "issue" || kind === "pull") return threadInput(ctx, kind, ref);
319325 if (kind === "run") return runInput(ctx, ref);
320326 if (kind === "map") return mapCardInput(ctx, ref);
327 if (kind === "site") return ref === "" ? siteInput() : undefined;
321328 if (kind === "audit") return ref === "" ? auditCardInput(ctx) : undefined;
322329 if (kind === "repo") {
323330 const repo = twoPart(ctx, ref);
src/web/pages/repo.ts +6 −2
@@ -86,6 +86,8 @@ import {
8686 finderDialog,
8787 loadExplorer,
8888 } from "./explorer.ts";
89 import { cardLink } from "../og/link.ts";
90 import { repoLine } from "../describe.ts";
8991
9092 export { crumbsFor, loadRepo, repoBase };
9193 export { pathCrumbs, refMenu, refSet, refSetFrom } from "./refs.ts";
@@ -988,7 +990,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
988990 return page({
989991 ctx,
990992 title: repo.name,
991 description: repo.description,
993 description: repoLine({ private: repo.visibility === "private", description: repo.description }),
994 ogImage: cardLink(ctx, { kind: "repo", repo }),
992995 feed: {
993996 href: `${repoBase(repo)}/activity.atom`,
994997 title: `${repo.ownerName}/${repo.name} activity`,
@@ -1035,7 +1038,8 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
10351038 return page({
10361039 ctx,
10371040 title: repo.name,
1038 description: repo.description,
1041 description: repoLine({ private: repo.visibility === "private", description: repo.description }),
1042 ogImage: cardLink(ctx, { kind: "repo", repo }),
10391043 feed: {
10401044 href: `${repoBase(repo)}/activity.atom`,
10411045 title: `${repo.ownerName}/${repo.name} activity`,
src/web/studio/app.ts +2
@@ -55,6 +55,7 @@ import { appPanels } from "./previews.ts";
5555 import { noCrewLine, servingHistory, type WordCtx } from "./words.ts";
5656 import { crewKeyNote } from "./crew.ts";
5757 import { noteAppOpened } from "../../core/next-steps.ts";
58 import { cardLink } from "../og/link.ts";
5859
5960 /**
6061 * What asks the viewer in this repository, muted or not: a mute quiets the
@@ -491,6 +492,7 @@ export async function studioApp(
491492 return page({
492493 ctx,
493494 title: `${repo.name}`,
495 ogImage: cardLink(ctx, { kind: "app", repo, record }),
494496 description: appLine({
495497 private: repo.visibility === "private",
496498 name: repo.name,
src/web/studio/change.ts +2
@@ -86,6 +86,7 @@ import { holdCard } from "./hold.ts";
8686 import { pullMayRun } from "../../core/pull_trust.ts";
8787 import { changeHref } from "./nouns.ts";
8888 import { pullWord, servingHistory, wordBadge, type WordCtx } from "./words.ts";
89 import { cardLink } from "../og/link.ts";
8990
9091 /** The pull and its repo, or the response that refuses to say why not. */
9192 export function pullContext(ctx: Ctx): { repo: Repo; pull: Pull } | Response {
@@ -622,6 +623,7 @@ async function changeConversation(
622623 return page({
623624 ctx,
624625 title: `${pull.title} · ${repo.name}`,
626 ogImage: cardLink(ctx, { kind: "thread", repo, issue: pull }),
625627 description: threadLine({
626628 private: repo.visibility === "private",
627629 word,
src/web/studio/run.ts +2
@@ -33,6 +33,7 @@ import { logBox, runVoice, RUN_TRANSCRIPT_CAP } from "./log.ts";
3333 import { changeHref, tellHref } from "./nouns.ts";
3434 import { unnamedRun, voiceFor, type Actor } from "./voice.ts";
3535 import { runWord, servingHistory, wordBadge, type WordCtx } from "./words.ts";
36 import { cardLink } from "../og/link.ts";
3637
3738 /**
3839 * The one control a live run has: a person who can write here stops a
@@ -317,6 +318,7 @@ export async function runView(
317318 return page({
318319 ctx,
319320 title: `${title} · ${repo.name}`,
321 ogImage: cardLink(ctx, { kind: "run", repo, runId: run.id }),
320322 description: runLine({
321323 private: repo.visibility === "private",
322324 word,
src/web/studio/tell.ts +2
@@ -69,6 +69,7 @@ import {
6969 servingHistory,
7070 type WordCtx,
7171 } from "./words.ts";
72 import { cardLink } from "../og/link.ts";
7273
7374 /** The pull whose merge closed this issue, from the event it left behind —
7475 * the same record `stateEvents` (thread.ts) reads for the timeline's own
@@ -420,6 +421,7 @@ async function studioTell(
420421 return page({
421422 ctx,
422423 title: `${issue.title} · ${repo.name}`,
424 ogImage: cardLink(ctx, { kind: "thread", repo, issue }),
423425 description: threadLine({
424426 private: repo.visibility === "private" || issue.sealed,
425427 word: wordInfo?.word ?? (issue.state === "open" ? "open" : "closed"),
test/og-designs.test.ts +3 −3
@@ -340,14 +340,14 @@ describe("over HTTP", () => {
340340 expect(svg).not.toContain("hidden");
341341 });
342342
343 test("the map page names its card only when the repo is public; the audit page always", async () => {
343 test("the map page names its card only when the repo is public; the audit page always; any other page has the site's", async () => {
344344 const page = await (await get("/alice/shown/map")).text();
345345 expect(page).toContain(`content="${base}/og/map/alice~shown.png"`);
346346 expect(page).toContain(`content="summary_large_image"`);
347347 const audit = await (await get("/audit")).text();
348348 expect(audit).toContain(`content="${base}/og/audit.png"`);
349349 const home = await (await get("/")).text();
350 expect(home).toContain("/icons/forge-512.png");
351 expect(home).not.toContain("summary_large_image");
350 expect(home).toContain(`content="${base}/og/site.png"`);
351 expect(home).toContain(`content="summary_large_image"`);
352352 });
353353 });
test/og-every-page.test.ts added +194
@@ -0,0 +1,194 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdtemp, rm } from "node:fs/promises";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { createApp as createForge, type App } from "../src/app.ts";
6 import { createRepo } from "../src/actions/repos.ts";
7 import { createApp } from "../src/core/apps.ts";
8 import { setSealed } from "../src/core/sealed.ts";
9 import { createIssue } from "../src/core/issues.ts";
10 import { getRepo } from "../src/core/repos.ts";
11 import { createRun } from "../src/core/runs.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { serve, type Servers } from "../src/http/server.ts";
14
15 /** Every page a link can be pasted from carries a card, written from its state. */
16 describe("link previews on every page", () => {
17 let dataDir: string;
18 let app: App;
19 let servers: Servers;
20 let base: string;
21 let runId = "";
22 let sealedRunId = "";
23 let sealedId = "";
24
25 beforeAll(async () => {
26 dataDir = await mkdtemp(join(tmpdir(), "forge-og-all-"));
27 app = createForge({ dataDir, port: 0 });
28 servers = await serve(app);
29 base = servers.url;
30 const u = await createUser(app.db, {
31 trusted: true,
32 username: "alice",
33 email: "[email protected]",
34 password: "alice-passphrase",
35 adminIfFirst: true,
36 });
37 if (!u.ok) throw new Error(u.error);
38 for (const [name, visibility, description, access] of [
39 ["shown", "public", "A public thing for the cards", "public"],
40 ["hidden", "private", "Secret plans nobody may read", "public"],
41 ["closed", "public", "A public repository with a private app", "private"],
outdated

Every app here is public, so cardLink's private-app branch is never exercised. Make one of them private (or add a third on the public repo).

42 ] as const) {
43 const made = await createRepo(app, u.value, { name, visibility, description });
44 if (!made.ok) throw new Error(String(made.error));
45 const rec = createApp(app.db, { repoId: made.value.id, slug: `${name}-app`, access });
46 if (!rec.ok) throw new Error(rec.error);
47 }
48 const id = (n: string) => getRepo(app.db, "alice", n)!.id;
49 const mk = (repo: string, title: string, kind: "issue" | "pull") => {
50 const i = createIssue(app.db, {
51 repoId: id(repo),
52 authorId: u.value.id,
53 title,
54 kind,
55 body: `Body of ${title}`,
56 });
57 if (!i.ok) throw new Error(String(i.error));
58 if (kind === "pull")
59 app.db
60 .query(
61 `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id)
62 VALUES (?1, 'main', 'topic', '', '', ?2)`,
63 )
64 .run(i.value.id, id(repo));
65 };
66 mk("shown", "Shown issue", "issue");
67 mk("shown", "Shown pull", "pull");
68 mk("hidden", "Hidden issue", "issue");
69 // A sealed report and a run on it, and a private-access app, all on the public repository.
70 const sealed = createIssue(app.db, {
71 repoId: id("shown"),
72 authorId: u.value.id,
73 title: "Sealed report",
74 kind: "issue",
75 body: "Body of the sealed report",
76 });
77 if (!sealed.ok) throw new Error(String(sealed.error));
78 sealedId = sealed.value.id;
79 setSealed(app.db, sealedId, true);
80 sealedRunId = createRun(app.db, { repoId: id("shown"), kind: "agent", summary: "sealed work", issueId: sealedId }).id;
81 runId = createRun(app.db, { repoId: id("shown"), kind: "agent", summary: "did a thing" }).id;
82 });
83
84 afterAll(async () => {
85 await servers.stop();
86 await rm(dataDir, { recursive: true, force: true });
87 });
88
89 const metas = (htmlText: string) => {
90 const out = new Map<string, string>();
91 for (const m of htmlText.matchAll(/<meta\s+(?:property|name)="([^"]+)"\s+content="([^"]*)"/g))
92 out.set(m[1]!, m[2]!);
93 return out;
94 };
95
96 const TOP = [
97 "/", "/explore", "/signin", "/signup", "/audit", "/map", "/apps", "/releases",
98 "/search", "/flow", "/system", "/contribute", "/resilience", "/board", "/asks",
99 "/asked", "/family", "/new", "/migrate",
100 ];
101 const things = () => [
102 "/alice/shown",
103 "/alice/shown/issues/1",
104 "/alice/shown/pulls/2",
105 `/alice/shown/runs/${runId}`,
106 "/alice",
107 "/alice/shown/app",
108 ];
109
110 const carries = async (path: string) => {
111 const res = await fetch(base + path, { redirect: "follow" });
112 expect(res.status, path).toBe(200);
113 const m = metas(await res.text());
114 expect(m.get("og:title"), path).toBeTruthy();
115 expect(m.get("og:description"), path).toBeTruthy();
116 expect(m.get("twitter:card"), path).toBe("summary_large_image");
117 const image = m.get("og:image");
118 expect(image, path).toBeTruthy();
119 const img = await fetch(image!.replace(/^https?:\/\/[^/]+/, base));
120 expect(img.status, `${path} → ${image}`).toBe(200);
121 expect(img.headers.get("content-type"), path).toBe("image/png");
122 return m;
123 };
124
125 for (const path of TOP)
126 test(`${path} carries a card`, async () => {
127 await carries(path);
128 });
129
130 test("repo, issue, pull, run, person and app pages carry a card written from their state", async () => {
131 const want: Record<string, RegExp> = {
132 "/alice/shown": /A public thing for the cards/,
133 "/alice/shown/issues/1": /Body of Shown issue|Shown issue/,
134 "/alice/shown/pulls/2": /Body of Shown pull|Shown pull/,
135 [`/alice/shown/runs/${runId}`]: /did a thing/,
136 "/alice": /repositor/i,
137 "/alice/shown/app": /not running/,
138 };
139 for (const path of things()) {
140 const m = await carries(path);
141 expect(m.get("og:description")!, path).toMatch(want[path]!);
142 expect(m.get("og:image"), path).toContain("/og/");
143 expect(m.get("og:image"), path).not.toContain("icons");
144 }
145 });
146
147 test("a private repository, seen anonymously, is the same page as a missing one", async () => {
148 const miss = metas(await (await fetch(base + "/alice/nothing")).text());
149 for (const path of ["/alice/hidden", "/alice/hidden/issues/1", "/alice/hidden/runs/x"]) {
150 const res = await fetch(base + path);
151 expect(res.status, path).toBe(404);
152 const m = metas(await res.text());
153 expect([...m.entries()], path).toEqual([...miss.entries()]);
154 expect([...m.values()].join("\n"), path).not.toMatch(/Secret plans|Hidden issue|hidden/);
155 expect(m.get("twitter:card"), path).toBe("summary_large_image");
156 }
157 for (const card of ["/og/repo/alice~hidden", "/og/issue/alice~hidden~1", "/og/app/hidden-app"])
158 for (const ext of ["svg", "png"])
159 expect((await fetch(`${base}${card}.${ext}`)).status, card).toBe(404);
160 });
161
162 test("a signed-in reader of a private repository unfurls as 'Private.' with the site card", async () => {
163 const signin = await fetch(base + "/signin", {
164 method: "POST",
165 redirect: "manual",
166 headers: { "content-type": "application/x-www-form-urlencoded", origin: base },
167 body: "username=alice&password=alice-passphrase",
168 });
169 const cookie = (signin.headers.getSetCookie?.() ?? []).map((c) => c.split(";")[0]).join("; ");
170 expect(cookie).toBeTruthy();
171 for (const path of ["/alice/hidden", "/alice/hidden/issues/1"]) {
172 const res = await fetch(base + path, { headers: { cookie } });
173 expect(res.status, path).toBe(200);
174 const m = metas(await res.text());
175 expect(m.get("og:description"), path).toBe("Private.");
176 expect(m.get("og:image"), path).toEndWith("/og/site.png");
177 }
178 // Readable here, but not to a signed-out fetcher: the card falls back to the site's.
179 const sealedNumber = (app.db.query("SELECT number FROM issue WHERE id = ?1").get(sealedId) as { number: number }).number;
180 const hereOnly = [
181 `/alice/shown/issues/${sealedNumber}`,
182 `/alice/shown/runs/${sealedRunId}`,
183 "/alice/closed/app",
184 ];
185 for (const path of hereOnly) {
186 const res = await fetch(base + path, { headers: { cookie } });
187 expect(res.status, path).toBe(200);
188 expect(metas(await res.text()).get("og:image"), path).toEndWith("/og/site.png");
189 }
190 expect((await fetch(`${base}/og/issue/alice~shown~${sealedNumber}.png`)).status).toBe(404);
191 expect((await fetch(`${base}/og/run/alice~shown~${sealedRunId}.png`)).status).toBe(404);
192 expect((await fetch(`${base}/og/app/closed-app.png`)).status).toBe(404);
193 });
194 });
test/standards.test.ts +1 −1
@@ -172,7 +172,7 @@ describe("what the open web expects", () => {
172172 expect(html).toContain(
173173 'rel="apple-touch-icon" href="/icons/forge-180.png"',
174174 );
175 expect(html).toContain('name="twitter:card" content="summary"');
175 expect(html).toContain(`name="twitter:card" content="summary_large_image"`);
176176 expect(html).toContain("viewport-fit=cover");
177177 });
178178

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs