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' #2379

Parked opened this
Closed without merging

scout/forge:scout/2349-every-forge-page-carries-og-title-og-des → main 2 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
14 files changed +273 −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 +15 −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 } 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,12 @@ function footer(ctx: Ctx): Html {
529530
530531 // ---- the page ----
531532
532 const SITE_LINE = "Your code, your machine.";
533 export const SITE_LINE = "Your code, your machine.";
534
535 /** The page's own card when it names one of ours, else the site's. */
536 function cardPath(own: string | undefined): string {
537 return own !== undefined && own.startsWith("/og/") ? own : SITE_CARD;
538 }
533539
534540 function pageDescription(options: PageOptions): string {
535541 const own = options.description?.trim().split(/\r?\n/)[0]?.trim() ?? "";
@@ -605,21 +611,13 @@ export function page(options: PageOptions, status = 200): Response {
605611 />`
606612 : null
607613 }
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 }
614 <meta
615 property="og:image"
616 content="${ctx.url.origin}${cardPath(options.ogImage)}"
617 />
618 <meta property="og:image:width" content="1200" />
619 <meta property="og:image:height" content="630" />
620 <meta name="twitter:card" content="summary_large_image" />
623621 <link rel="manifest" href="/manifest.webmanifest" />
624622 ${links.map(
625623 (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 "../layout.ts";
4546 import { renderCard, signature, type CardInput } from "../og/card.ts";
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 +163
@@ -0,0 +1,163 @@
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 { createIssue } from "../src/core/issues.ts";
9 import { getRepo } from "../src/core/repos.ts";
10 import { createRun } from "../src/core/runs.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { serve, type Servers } from "../src/http/server.ts";
13
14 /** Every page a link can be pasted from carries a card, written from its state. */
15 describe("link previews on every page", () => {
16 let dataDir: string;
17 let app: App;
18 let servers: Servers;
19 let base: string;
20 let runId = "";
21
22 beforeAll(async () => {
23 dataDir = await mkdtemp(join(tmpdir(), "forge-og-all-"));
24 app = createForge({ dataDir, port: 0 });
25 servers = await serve(app);
26 base = servers.url;
27 const u = await createUser(app.db, {
28 trusted: true,
29 username: "alice",
30 email: "[email protected]",
31 password: "alice-passphrase",
32 adminIfFirst: true,
33 });
34 if (!u.ok) throw new Error(u.error);
35 for (const [name, visibility, description] of [
36 ["shown", "public", "A public thing for the cards"],
37 ["hidden", "private", "Secret plans nobody may read"],
38 ] as const) {
39 const made = await createRepo(app, u.value, { name, visibility, description });
40 if (!made.ok) throw new Error(String(made.error));
41 const rec = createApp(app.db, { repoId: made.value.id, slug: `${name}-app`, access: "public" });
42 if (!rec.ok) throw new Error(rec.error);
43 }
44 const id = (n: string) => getRepo(app.db, "alice", n)!.id;
45 const mk = (repo: string, title: string, kind: "issue" | "pull") => {
46 const i = createIssue(app.db, {
47 repoId: id(repo),
48 authorId: u.value.id,
49 title,
50 kind,
51 body: `Body of ${title}`,
52 });
53 if (!i.ok) throw new Error(String(i.error));
54 if (kind === "pull")
55 app.db
56 .query(
57 `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id)
58 VALUES (?1, 'main', 'topic', '', '', ?2)`,
59 )
60 .run(i.value.id, id(repo));
61 };
62 mk("shown", "Shown issue", "issue");
63 mk("shown", "Shown pull", "pull");
64 mk("hidden", "Hidden issue", "issue");
65 runId = createRun(app.db, { repoId: id("shown"), kind: "agent", summary: "did a thing" }).id;
66 });
67
68 afterAll(async () => {
69 await servers.stop();
70 await rm(dataDir, { recursive: true, force: true });
71 });
72
73 const metas = (htmlText: string) => {
74 const out = new Map<string, string>();
75 for (const m of htmlText.matchAll(/<meta\s+(?:property|name)="([^"]+)"\s+content="([^"]*)"/g))
76 out.set(m[1]!, m[2]!);
77 return out;
78 };
79
80 const TOP = [
81 "/", "/explore", "/signin", "/signup", "/audit", "/map", "/apps", "/releases",
82 "/search", "/flow", "/system", "/contribute", "/resilience", "/board", "/asks",
83 "/asked", "/family", "/new", "/migrate",
84 ];
85 const things = () => [
86 "/alice/shown",
87 "/alice/shown/issues/1",
88 "/alice/shown/pulls/2",
89 `/alice/shown/runs/${runId}`,
90 "/alice",
91 "/alice/shown/app",
92 ];
93
94 const carries = async (path: string) => {
95 const res = await fetch(base + path, { redirect: "follow" });
96 expect(res.status, path).toBe(200);
97 const m = metas(await res.text());
98 expect(m.get("og:title"), path).toBeTruthy();
99 expect(m.get("og:description"), path).toBeTruthy();
100 expect(m.get("twitter:card"), path).toBe("summary_large_image");
101 const image = m.get("og:image");
102 expect(image, path).toBeTruthy();
103 const img = await fetch(image!.replace(/^https?:\/\/[^/]+/, base));
104 expect(img.status, `${path} → ${image}`).toBe(200);
105 expect(img.headers.get("content-type"), path).toBe("image/png");
106 return m;
107 };
108
109 for (const path of TOP)
110 test(`${path} carries a card`, async () => {
111 await carries(path);
112 });
113
114 test("repo, issue, pull, run, person and app pages carry a card written from their state", async () => {
115 const want: Record<string, RegExp> = {
116 "/alice/shown": /A public thing for the cards/,
117 "/alice/shown/issues/1": /Body of Shown issue|Shown issue/,
118 "/alice/shown/pulls/2": /Body of Shown pull|Shown pull/,
119 [`/alice/shown/runs/${runId}`]: /did a thing/,
120 "/alice": /alice/,
121 "/alice/shown/app": /shown-app|shown/,
122 };
123 for (const path of things()) {
124 const m = await carries(path);
125 expect(m.get("og:description")! + m.get("og:title")!, path).toMatch(want[path]!);
126 expect(m.get("og:image"), path).toContain("/og/");
127 expect(m.get("og:image"), path).not.toContain("icons");
128 }
129 });
130
131 test("a private repository, seen anonymously, is the same page as a missing one", async () => {
132 const miss = metas(await (await fetch(base + "/alice/nothing")).text());
133 for (const path of ["/alice/hidden", "/alice/hidden/issues/1", "/alice/hidden/runs/x"]) {
134 const res = await fetch(base + path);
135 expect(res.status, path).toBe(404);
136 const m = metas(await res.text());
137 expect([...m.entries()], path).toEqual([...miss.entries()]);
138 expect([...m.values()].join("\n"), path).not.toMatch(/Secret plans|Hidden issue|hidden/);
139 expect(m.get("twitter:card"), path).toBe("summary_large_image");
140 }
141 for (const card of ["/og/repo/alice~hidden", "/og/issue/alice~hidden~1", "/og/app/hidden-app"])
142 for (const ext of ["svg", "png"])
143 expect((await fetch(`${base}${card}.${ext}`)).status, card).toBe(404);
144 });
145
146 test("a signed-in reader of a private repository unfurls as 'Private.' with the site card", async () => {
147 const signin = await fetch(base + "/signin", {
148 method: "POST",
149 redirect: "manual",
150 headers: { "content-type": "application/x-www-form-urlencoded", origin: base },
151 body: "username=alice&password=alice-passphrase",
152 });
153 const cookie = (signin.headers.getSetCookie?.() ?? []).map((c) => c.split(";")[0]).join("; ");
154 expect(cookie).toBeTruthy();
155 for (const path of ["/alice/hidden", "/alice/hidden/issues/1"]) {
156 const res = await fetch(base + path, { headers: { cookie } });
157 expect(res.status, path).toBe(200);
158 const m = metas(await res.text());
159 expect(m.get("og:description"), path).toBe("Private.");
160 expect(m.get("og:image"), path).toEndWith("/og/site.png");
161 }
162 });
163 });
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