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

Every response of the pages worker carries the security floor, proven by a test that fetches each page kind #14529

Merged opened this

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

Merged

scout/forge:scout/14522-every-response-of-the-pages-worker-carri → main 2 commits fork 1 thread · 1 unresolved

  • Merged. scout/14522-every-response-of-the-pages-worker-carri was merged into main as 14ebc8d.

Review

Sign in to review this change.
1 file changed +157 −0
test/security-floor-pages.test.ts added +157
@@ -0,0 +1,157 @@
1 /**
2 * The security floor (`securityFloor` in src/http/respond.ts) on every kind of
3 * page a person reads (#14522): home, explore, a repository, a pull, an issue,
4 * waves, the changelog, status, a 404 page and the error page, each asked
5 * signed out and signed in over the wire of one seeded forge. Where a page
6 * answers with an ETag it is asked again with it, and the 304 it then gives
7 * must carry the floor too: a 304 is written by the page itself (waves, the
8 * changelog, a repository's front page) with a bare header bag, which is where
9 * a page that skipped the floor would hide.
10 *
11 * Every miss is named with the page and its path, so a page that lacks a
12 * header fails here saying which. `security-floor-routes.test.ts` walks the
13 * whole route table and `security-floor-every-route.test.ts` the kinds of
14 * answer; this one is the reader's pages, each with its revalidation.
15 *
16 * Time (#14522 sets 3 s): about 2.2 s a file warm, measured three times. Of
17 * that, about 1.7 s is importing the server, which every server test pays; the
18 * seed (one repository, two commits, an issue and a pull) is about 0.1 s, and
19 * the page walk with its 304s about 0.4 s. A first run on a cold transpile
20 * cache took 4.9 s.
21 */
22 import { afterAll, beforeAll, expect, test } from "bun:test";
23 import { mkdtemp, rm } from "node:fs/promises";
24 import { tmpdir } from "node:os";
25 import { join } from "node:path";
26 import { createApp, type App } from "../src/app.ts";
27 import { openIssue } from "../src/actions/issues.ts";
28 import { commitToBranch } from "../src/actions/repos.ts";
29 import { createPull } from "../src/core/pulls.ts";
30 import { createRepo } from "../src/core/repos.ts";
31 import { createSession } from "../src/core/sessions.ts";
32 import { repoDir } from "../src/git/repos.ts";
33 import { serve, type Servers } from "../src/http/server.ts";
34 import { seedAdmin } from "./helpers/admin.ts";
35 import { quietLogs } from "./helpers/globals.ts";
36
37 quietLogs();
38
39 const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-policy"] as const;
40
41 type Page = { kind: string; path: string; status: number };
42
43 let dataDir = "";
44 let app: App, servers: Servers;
45 let cookie = "";
46 let pages: Page[] = [];
47
outdated

The per-file time includes this seeding (app, repo, two commits, a pull). It is what takes the file past the 3 s the issue sets. Make it cheaper, or measure and show that it is under 3 s.

48 beforeAll(async () => {
49 dataDir = await mkdtemp(join(tmpdir(), "forge-floor-pages-"));
50 app = createApp({ dataDir, port: 0 });
51 servers = await serve(app);
52 const admin = await seedAdmin(app, { username: "ada", email: "[email protected]", password: "correct-horse-battery" });
53 if (!admin.ok) throw new Error(admin.error);
54 const made = await createRepo(app.db, app.reposRoot, { ownerId: admin.value.id, name: "notes", visibility: "public" });
55 if (!made.ok) throw new Error(String(made.error));
56 const repo = made.value;
57 const seed = await commitToBranch(app, admin.value, repo, { message: "seed", files: [{ path: "a.txt", content: "a\n" }] });
58 if (!seed.ok) throw new Error(String(seed.error));
59 const feat = await commitToBranch(app, admin.value, repo, { branch: "feat", files: [{ path: "b.txt", content: "b\n" }] });
60 if (!feat.ok) throw new Error(String(feat.error));
61 const issue = openIssue(app, admin.value, repo, { title: "a question" });
62 if (!issue.ok) throw new Error(String(issue.error));
63 const pull = await createPull(app.db, repoDir(app.reposRoot, "ada", "notes"), {
64 repoId: repo.id,
65 authorId: admin.value.id,
66 title: "an answer",
67 baseBranch: repo.defaultBranch,
68 headBranch: "feat",
69 });
70 if (!pull.ok) throw new Error(String(pull.error));
71 cookie = `forge_session=${createSession(app.db, admin.value.id).token}`;
72 pages = [
73 { kind: "home", path: "/", status: 200 },
74 { kind: "explore", path: "/explore", status: 200 },
75 { kind: "repository", path: "/ada/notes", status: 200 },
76 { kind: "pull", path: `/ada/notes/pulls/${pull.value.number}`, status: 200 },
77 { kind: "issue", path: `/ada/notes/issues/${issue.value.number}`, status: 200 },
78 { kind: "waves", path: "/ada/notes/waves", status: 200 },
79 { kind: "changelog", path: "/changelog", status: 200 },
80 { kind: "status", path: "/status", status: 200 },
81 { kind: "404 page", path: "/no/such/page/here", status: 404 },
82 { kind: "404 of a pull", path: "/ada/notes/pulls/999", status: 404 },
83 ];
84 }, 30_000);
85
86 afterAll(async () => {
87 servers?.stop();
88 app?.close();
89 await rm(dataDir, { recursive: true, force: true });
90 });
91
92 async function ask(path: string, signedIn: boolean, extra: Record<string, string> = {}): Promise<Response> {
93 const headers = new Headers({ accept: "text/html", "accept-encoding": "identity", ...extra });
94 if (signedIn) headers.set("cookie", cookie);
95 return fetch(`${servers.url}${path}`, { headers, redirect: "manual", signal: AbortSignal.timeout(5_000) });
96 }
97
98 /** The floor headers an answer lacks, each named with the page, its path and the status. */
99 function lacking(page: Page, res: Response, who: string): string[] {
100 return FLOOR.filter((h) => !res.headers.get(h)).map((h) => `${page.kind} ${page.path} (${who}, ${res.status}) lacks ${h}`);
101 }
102
103 /** What is wrong with one page: a missing header on its answer or its 304, or the wrong status. */
104 async function wrongWith(page: Page, signedIn: boolean): Promise<string[]> {
105 const who = signedIn ? "signed in" : "signed out";
106 const res = await ask(page.path, signedIn);
107 await res.body?.cancel();
108 const wrong = lacking(page, res, who);
109 if (res.status !== page.status) wrong.push(`${page.kind} ${page.path} (${who}) answered ${res.status}, not ${page.status}`);
110 const etag = res.headers.get("etag");
111 if (etag === null || res.status !== 200) return wrong;
112 const again = await ask(page.path, signedIn, { "if-none-match": etag });
113 await again.body?.cancel();
114 wrong.push(...lacking(page, again, `${who}, revalidated`));
115 return wrong;
116 }
117
118 test("every page kind carries the floor, signed out and signed in", async () => {
119 const wrong = (await Promise.all(pages.flatMap((p) => [wrongWith(p, false), wrongWith(p, true)]))).flat();
120 expect(wrong).toEqual([]);
121 }, 15_000);
122
123 test("the pages that revalidate answer 304, and that 304 carries the floor", async () => {
124 // A page that gave no ETag would make the 304 half of the walk above prove
125 // nothing; these are the ones that write their own 304.
126 const wrong: string[] = [];
127 for (const path of ["/ada/notes/waves", "/changelog"]) {
128 const first = await ask(path, false);
129 await first.body?.cancel();
130 const etag = first.headers.get("etag");
131 if (etag === null) {
132 wrong.push(`${path} gave no etag`);
133 continue;
134 }
135 const again = await ask(path, false, { "if-none-match": etag });
136 await again.body?.cancel();
137 if (again.status !== 304) wrong.push(`${path} revalidated as ${again.status}, not 304`);
138 wrong.push(...lacking({ kind: "revalidated", path, status: 304 }, again, "signed out"));
139 }
140 expect(wrong).toEqual([]);
141 });
142
143 test("the error page, a page whose handler throws, carries the floor", async () => {
144 const query = app.db.query.bind(app.db);
145 app.db.query = (() => {
146 throw new Error("the disk went away");
147 }) as typeof app.db.query;
148 let res: Response;
149 try {
150 res = await ask("/explore", true);
151 } finally {
152 app.db.query = query;
153 }
154 await res.body?.cancel();
155 expect(res.status).toBe(500);
156 expect(lacking({ kind: "error page", path: "/explore", status: 500 }, res, "signed in")).toEqual([]);
157 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs