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

small: A not-found page for /owner/name offers a search for that name and says a private app looks the same #13894

Merged opened this

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

Merged

scout/forge:scout/13872-small-a-not-found-page-for-owner-name-of → main 7 commits fork 2 threads · 2 unresolved

  • Merged. scout/13872-small-a-not-found-page-for-owner-name-of was merged into main as 24cac9a.

Review

Sign in to review this change.
7 files changed +107 −9
forge.json +1 −1
@@ -5,7 +5,7 @@
55 "lab--forge-trevato-apps.open-platform.sh"
66 ],
77 "app": {
8 "looks": ["/", "/docs", "/explore", "/board", "/notes", "/review", "/releases", "/apps", "/security", "/system", "/maintenance", "/talk", "/flow", "/resilience", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/security/controls", "/settings/grants", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/preview-admin/sample/waves", "/preview-admin/sample/space", "/looks/missing", "/looks/failed"],
8 "looks": ["/", "/docs", "/explore", "/board", "/notes", "/review", "/releases", "/apps", "/security", "/system", "/maintenance", "/talk", "/flow", "/resilience", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/security/controls", "/settings/grants", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/preview-admin/sample/waves", "/preview-admin/sample/space", "/looks/missing", "/looks/failed", "/nope/nothing"],
99 "looksAsOwner": ["/", "/board", "/flow", "/boards?help", "/settings/crew", "/settings/crew?help", "/settings/security/controls", "/settings/grants", "/talk", "/system", "/maintenance", "/resilience", "/preview-admin/sample", "/preview-admin/sample/drop", "/preview-admin/sample/data-move", "/preview-admin/sample/cut-over", "/preview-admin/sample/pulls/3", "/preview-admin/sample/next?pick", "/preview-admin/sample/bets", "/preview-admin/sample/waves", "/preview-admin/sample/space"],
1010 "run": "bun run src/main.ts",
1111 "install": "",
src/web/chrome.ts +18 −1
@@ -88,6 +88,11 @@ export function notFoundPage(ctx: Ctx, opts: { plain?: boolean } = {}): Response
8888 tries.length === 0
8989 ? html``
9090 : html`<p class="muted">Try ${tries.map((t, i) => html`${i > 0 ? ", " : ""}${t}`)}</p>`;
91 const term = get ? searchTermFor(ctx.url.pathname) : undefined;
92 const hint =
93 term === undefined
94 ? html``
95 : html`<p class="muted"><a href="/search?q=${encodeURIComponent(term)}">Search for ${term}</a></p>`;
outdated

Principle 1: the line above already says 'does not exist, or is not visible to you'; this sentence says the same thing again. State it once.

9196 const id = requestId();
9297 const offerSignIn = get && ctx.auth.user === undefined;
9398 const res = page(
@@ -102,7 +107,7 @@ export function notFoundPage(ctx: Ctx, opts: { plain?: boolean } = {}): Response
102107 action: offerSignIn
103108 ? btn("Sign in", { href: notFoundSignIn(ctx), variant: "primary" })
104109 : btn("Go home", { href: "/", variant: "primary" }),
105 })}${didYouMean}${disclosure("Request id", html`<p class="muted small">Quote <code>${id}</code> if you ask about this page.</p>`, { small: true })}`,
110 })}${didYouMean}${hint}${disclosure("Request id", html`<p class="muted small">Quote <code>${id}</code> if you ask about this page.</p>`, { small: true })}`,
106111 },
107112 404,
108113 );
@@ -110,6 +115,18 @@ export function notFoundPage(ctx: Ctx, opts: { plain?: boolean } = {}): Response
110115 return res;
111116 }
112117
118 /** What to search for from a path shaped /owner or /owner/name: the last segment. */
119 const NAME_SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
120
121 function searchTermFor(pathname: string): string | undefined {
122 const parts = pathname.split("/").slice(1);
123 if (parts.length > 2 || (parts.length === 2 && parts[1] === "")) return undefined;
outdated

The same regex is written twice. Name it once (e.g. NAME_SEGMENT) and test both segments against it.

124 const last = parts[parts.length - 1] ?? "";
125 return NAME_SEGMENT.test(last) && NAME_SEGMENT.test(parts[0] ?? "")
126 ? last
127 : undefined;
128 }
129
113130 /** The sign-in door from a 404: back here through `returnTo`; behind an edge, the edge's. */
114131 function notFoundSignIn(ctx: Ctx): string {
115132 return activeEdgeToken(ctx.app) === undefined ? withReturn("/signin", returnTo(ctx)) : signInHref(ctx);
test/forge-looks.test.ts +2 −2
@@ -191,9 +191,9 @@ test("`/` signed out is its own look at phone and desktop, taken with no session
191191 test("a look below the top level is the preview seed's, taken as the owner", () => {
192192 const { looks, looksAsOwner } = manifest();
193193 for (const p of looks.filter((p) => !isTopLevel(p))) {
194 expect(p.startsWith("/preview-admin/sample") || p === "/settings/crew" || p === "/settings/security/controls" || p === "/settings/grants" || p === "/looks/missing" || p === "/looks/failed" || isHelpLook(p)).toBe(true);
194 expect(p.startsWith("/preview-admin/sample") || p === "/settings/crew" || p === "/settings/security/controls" || p === "/settings/grants" || p === "/looks/missing" || p === "/looks/failed" || p === "/nope/nothing" || isHelpLook(p)).toBe(true);
195195 // the not-found and failed pages are the same page to everyone: no owner needed
196 if (!p.startsWith("/looks/")) expect(looksAsOwner).toContain(p);
196 if (!p.startsWith("/looks/") && p !== "/nope/nothing") expect(looksAsOwner).toContain(p);
197197 }
198198 });
199199
test/not-found-search.test.ts added +79
@@ -0,0 +1,79 @@
1 /** A 404 shaped /owner/name offers a search for the name; other 404s stay as they were. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createUser } from "../src/core/users.ts";
8 import { serve, type Servers } from "../src/http/server.ts";
9 import { quietLogs } from "./helpers/globals.ts";
10
11 quietLogs();
12 let dir: string;
13 let app: App;
14 let servers: Servers;
15 let cookie = "";
16
17 beforeAll(async () => {
18 dir = await mkdtemp(join(tmpdir(), "forge-nf-search-"));
19 app = createApp({ dataDir: dir, port: 0 });
20 servers = await serve(app);
21 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
22 if (!made.ok) throw new Error(made.error);
23 const res = await fetch(`${servers.url}/signin`, { method: "POST", body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), redirect: "manual" });
24 cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!;
25 }, 30_000);
26 afterAll(async () => {
27 servers.stop();
28 app.close();
29 await rm(dir, { recursive: true, force: true });
30 });
31
32 const get = async (path: string, signedIn = false) => {
33 const res = await fetch(`${servers.url}${path}`, { headers: signedIn ? { cookie } : {} });
34 return { status: res.status, body: await res.text() };
35 };
36
37 test("/owner/name offers a search for name; the 404 says once that a private app looks the same", async () => {
38 const { status, body } = await get("/nope/nothing");
39 expect(status).toBe(404);
40 expect(body).toContain('href="/search?q=nothing"');
41 expect(body).toContain("Search for nothing");
42 expect(body).not.toContain("A private app looks the same");
43 expect(body.match(/not visible to you/g)?.length).toBe(1);
44 expect(body).toContain("Sign in");
45 });
46
47 test("/owner alone searches for that word", async () => {
48 expect((await get("/nopeperson")).body).toContain('href="/search?q=nopeperson"');
49 });
50
51 test("a signed-in reader sees the search but no Sign in button", async () => {
52 const { body } = await get("/nope/nothing", true);
53 expect(body).toContain("Search for nothing");
54 expect(body).not.toContain(">Sign in<");
55 expect(body).toContain("Go home");
56 });
57
58 test("other 404s are unchanged: deeper paths and odd segments get no search", async () => {
59 for (const path of ["/a/b/c", "/a/b/", "/a/%3Cb%3E"]) {
60 const { status, body } = await get(path);
61 expect(status).toBe(404);
62 expect(body).not.toContain("/search?q=");
63 }
64 });
65
66 test("a 404 that is not /owner/name is the page it was: two of them differ only by path and id", async () => {
67 const norm = (b: string, p: string) =>
68 b.replace(/<code>[0-9a-f]{8}<\/code>/, "<code>ID</code>").replaceAll(encodeURIComponent(p), "PATHQ").replaceAll(p, "PATH");
69 const a = await get("/a/b/c");
70 const b = await get("/x/y/z");
71 expect(norm(a.body, "/a/b/c")).toBe(norm(b.body, "/x/y/z"));
72 expect(a.body).not.toContain("Search for");
73 });
74
75 test("the search offer adds under 400 bytes to a 404", async () => {
76 const shaped = (await get("/nope/nothing")).body.length;
77 const plain = (await get("/a/b/c")).body.length;
78 expect(Math.abs(shaped - plain)).toBeLessThan(400);
79 });
test/not-found-way-on.test.ts +4 −2
@@ -45,7 +45,9 @@ test("a private repo and a missing one read the same signed out", async () => {
4545 main(b)
4646 .replace(/<code>[0-9a-f]{8}<\/code>/, "<code>ID</code>")
4747 .replaceAll(encodeURIComponent(p), "PATHQ")
48 .replaceAll(p, "PATH");
48 .replaceAll(p, "PATH")
49 // the search offer echoes the asked name, so it differs by that name alone
50 .replace(/q=\w+">Search for \w+</, 'q=NAME">Search for NAME<');
4951 expect(norm(priv.body, "/alice/secret")).toBe(norm(miss.body, "/alice/nothere"));
5052 });
5153
@@ -54,7 +56,7 @@ test("the 404 says one sentence, offers one button (sign in, back here, when sig
5456 const m = main(await res.text());
5557 expect(m).toContain("does not exist, or is not visible to you");
5658 expect(m.match(/class="btn/g)?.length).toBe(1);
57 expect(m).not.toContain("/search");
59 expect(m).toContain("/search?q=secret"); // a link, not a second button
5860 expect(m).toContain("/signin?next=");
5961 expect(m).toContain("<details");
6062 const id = /<code>([0-9a-f]{8})<\/code>/.exec(m)?.[1];
test/own-session.test.ts +1 −1
@@ -69,7 +69,7 @@ describe("a forge with no platform edge", () => {
6969 for (const path of ["/olive/vault", "/settings/accounts"]) {
7070 const anon = await get(path);
7171 expect(anon.status).not.toBe(200);
72 expect(await anon.text()).not.toContain("vault</");
72 expect(await anon.text()).not.toContain("<h1>vault</");
7373 const authed = await get(path, { cookie });
7474 expect(authed.status).toBe(200);
7575 }
test/web-ui.test.ts +2 −2
@@ -453,11 +453,11 @@ describe("web ui", () => {
453453 const absent = await (await fetch(`${base}/kim/nothing`)).text();
454454
455455 const main = (t: string) =>
456 t.slice(t.indexOf("<main"), t.indexOf("</main>")).replaceAll(/<code>[0-9a-f]{8}<\/code>/g, "<code>ID</code>").replaceAll(/next=%2Fkim%2F\w+/g, "next=PATH");
456 t.slice(t.indexOf("<main"), t.indexOf("</main>")).replaceAll(/<code>[0-9a-f]{8}<\/code>/g, "<code>ID</code>").replaceAll(/next=%2Fkim%2F\w+/g, "next=PATH").replace(/q=\w+">Search for \w+</, "q=NAME\">Search for NAME<");
457457 expect(main(hidden)).toBe(main(absent));
458458 expect(main(absent)).toContain("Sign in");
459459 expect(main(absent)).not.toContain("Create an account");
460 expect(main(absent)).not.toContain("Search for");
460 expect(main(absent)).toContain("Search for NAME");
461461
462462 const odd = await (await fetch(`${base}/%3Cx%3E`)).text();
463463 expect(odd).not.toContain("<x>");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs