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

A test walks every public GET route signed out and fails on a 5xx, a slow answer or a missing security header #13881

Merged opened this

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

Merged

scout/forge:scout/13874-a-test-walks-every-public-get-route-sign → main 2 commits fork

  • Merged. scout/13874-a-test-walks-every-public-get-route-sign was merged into main as 5371f27.

Review

Sign in to review this change.
1 file changed +151 −0
test/open-routes-floor.test.ts added +151
@@ -0,0 +1,151 @@
1 /**
2 * Every open GET route keeps the floor, signed out (#13874). The routes come
3 * from both routers, kept where `src/http/route_marks.ts` marks them `open`;
4 * path parameters are filled from a seeded public repository and each is
5 * asked with no cookie. Each must answer below 500, carry
6 * `content-security-policy`, `x-content-type-options: nosniff` and
7 * `referrer-policy`, and answer its headers within `SLOW_MS` — 200 ms on the
8 * test machine with a 5x slack, and one retry, so a cold first request is
9 * noted, not flaky. A stream is not read: its headers are the answer.
10 * A route this cannot fill is listed in `UNFILLED` with its reason, and the
11 * list may not outlive its routes. The asks go through `serve`, not
12 * `forgeHttp(app).handle`: the floor is set at the server's seam, and the
13 * in-process handler answers without it.
14 */
15
16 import { afterAll, beforeAll, expect, test } from "bun:test";
17 import { mkdtempSync, rmSync } from "node:fs";
18 import { tmpdir } from "node:os";
19 import { join } from "node:path";
20 import { createRepo } from "../src/actions/repos.ts";
21 import { createApp, type App } from "../src/app.ts";
22 import { createIssue } from "../src/core/issues.ts";
23 import { createUser } from "../src/core/users.ts";
24 import { markOf } from "../src/http/route_marks.ts";
25 import { buildApiRouter, buildRouter, serve, type Servers } from "../src/http/server.ts";
26 import { quietLogs } from "./helpers/globals.ts";
27 import { proveAll } from "./helpers/verified.ts";
28
29 quietLogs();
30
31 /** 200 ms on the test machine, with the 5x slack the issue names. */
32 const SLOW_MS = 200 * 5;
33
34 /**
35 * What each path parameter is filled with: the seeded owner, its public repository and its first
36 * issue, or a well-formed value nothing seeds.
37 */
38 const FILL: Readonly<Record<string, string>> = {
39 owner: "alice", username: "alice", user: "alice", org: "alice", scope: "alice",
40 name: "notes", repo: "notes", number: "1", n: "1", id: "1", job: "1",
41 ref: "main", branch: "main", oid: "0".repeat(40), sha: "0".repeat(40), commit: "0".repeat(40),
42 // Not seeded: each still has to answer its not-found with the floor.
43 team: "core", env: "prod", file: "README.md", label: "v1", chapter: "1", kind: "desktop", machine: "web",
44 op: "x", group: "a", version: "1", slug: "board", receipt: "r", token: "t", code: "c", spec: "16", stage: "build",
45 };
46
47 /** Open GET routes that cannot be filled in from a seeded public repository, each with why. */
48 const UNFILLED: Readonly<Record<string, string>> = {};
49
50 const openGets = (): string[] => {
51 const all = [...buildRouter().routes(), ...buildApiRouter().routes()];
52 return [...new Set(all.filter((r) => r.method === "GET" && markOf(r.pattern) === "open").map((r) => r.pattern))].sort();
53 };
54
55 /** The path for a pattern, or the parameter nothing fills. */
56 const fill = (pattern: string): { path: string } | { missing: string } => {
57 let missing = "";
58 const path = pattern
59 .replace(/:([A-Za-z_]+)/g, (whole, k: string) => FILL[k] ?? ((missing ||= k), whole))
60 .replace(/\*[A-Za-z_]+$/, "README.md");
61 return missing === "" ? { path } : { missing };
62 };
63
64 let root = "";
65 let app: App;
66 let servers: Servers;
67
68 beforeAll(async () => {
69 root = mkdtempSync(join(tmpdir(), "forge-open-floor-"));
70 app = createApp({ dataDir: root, port: 0 });
71 const u = await createUser(app.db, {
72 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
73 password: "a-good-passphrase", adminIfFirst: true,
74 });
75 if (!u.ok) throw new Error(u.error);
76 proveAll(app);
77 const made = await createRepo(app, u.value, { name: "notes", visibility: "public" });
78 if (!made.ok) throw new Error(made.error);
79 const issue = createIssue(app.db, { repoId: made.value.id, authorId: u.value.id, title: "First", body: "" });
80 if (!issue.ok) throw new Error(issue.error);
81 servers = await serve(app);
82 }, 30_000);
83
84 afterAll(() => {
85 servers.stop();
86 app.close();
87 rmSync(root, { recursive: true, force: true });
88 });
89
90 interface Answer { status: number; ms: number; headers: Headers }
91
92 const ask = async (path: string): Promise<Answer> => {
93 const started = performance.now();
94 const res = await fetch(`${servers.url}${path}`, { headers: { accept: "text/html" }, redirect: "manual" });
95 const ms = performance.now() - started;
96 await res.body?.cancel();
97 return { status: res.status, ms, headers: res.headers };
98 };
99
100 /** What is wrong with one answer, or nothing. */
101 const faults = (a: Answer): string[] => [
102 ...(a.status >= 500 ? [`answered ${a.status}`] : []),
103 ...(a.headers.get("content-security-policy") ? [] : ["no content-security-policy"]),
104 ...(a.headers.get("x-content-type-options") === "nosniff" ? [] : ["no x-content-type-options: nosniff"]),
105 ...(a.headers.get("referrer-policy") ? [] : ["no referrer-policy"]),
106 ...(a.ms > SLOW_MS ? [`took ${Math.round(a.ms)} ms`] : []),
107 ];
108
109 // One test per route, so each answers within the 2 s test budget on its own.
110 const PATTERNS = openGets();
111
112 test("the walk covers the open GET routes, and nothing fills is left unlisted", () => {
113 expect(PATTERNS.length).toBeGreaterThan(100);
114 const unfilled: Record<string, string> = {};
115 for (const pattern of PATTERNS) {
116 if (pattern in UNFILLED) continue;
117 const filled = fill(pattern);
118 if ("missing" in filled) unfilled[pattern] = `nothing fills :${filled.missing}`;
119 }
120 expect(unfilled).toEqual({});
121 });
122
123 for (const pattern of PATTERNS) {
124 const filled = fill(pattern);
125 if (pattern in UNFILLED || "missing" in filled) continue;
126 test(`open GET ${pattern}, signed out, answers below 500 with the security floor and in time`, async () => {
127 let found = faults(await ask(filled.path));
128 if (found.length > 0) found = faults(await ask(filled.path)); // one retry: a cold first answer is noted, not failed
129 expect(found).toEqual([]);
130 });
131 }
132
133 test("each unfilled route is an open GET route still registered, with a reason", () => {
134 const open = new Set(openGets());
135 expect(Object.keys(UNFILLED).filter((p) => !open.has(p))).toEqual([]);
136 for (const reason of Object.values(UNFILLED)) expect(reason.length).toBeGreaterThan(10);
137 });
138
139 test("an answer without a floor header, at 500 or slower than the slack is named", () => {
140 const floor = { "content-security-policy": "default-src 'self'", "x-content-type-options": "nosniff", "referrer-policy": "no-referrer" };
141 expect(faults({ status: 200, ms: 1, headers: new Headers(floor) })).toEqual([]);
142 expect(faults({ status: 500, ms: SLOW_MS + 1, headers: new Headers() })).toEqual([
143 "answered 500", "no content-security-policy", "no x-content-type-options: nosniff", "no referrer-policy", `took ${SLOW_MS + 1} ms`,
144 ]);
145 });
146
147 test("a pattern with a parameter nothing fills names that parameter, so it is listed, not skipped", () => {
148 expect(fill("/:owner/:name/issues/:number")).toEqual({ path: "/alice/notes/issues/1" });
149 expect(fill("/:owner/:name/raw/:ref/*path")).toEqual({ path: "/alice/notes/raw/main/README.md" });
150 expect(fill("/:owner/:brand_new")).toEqual({ missing: "brand_new" });
151 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs