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

Every Set-Cookie and every error response carries the security floor, proven by one table-driven test #13470

Merged opened this

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

Merged

scout/forge:scout/13441-every-set-cookie-and-every-error-respons → main 1 commit fork

  • Merged. scout/13441-every-set-cookie-and-every-error-respons was merged into main as 74b89af.

Review

Sign in to review this change.
1 file changed +158 −0
test/security-floor-kinds.test.ts added +158
@@ -0,0 +1,158 @@
1 /**
2 * One table, every kind of answer (#13441): a page, a 404, a 429, a 500, a
3 * redirect and an API error, each asked over the wire of a forge on plain
4 * http and of one declared https. Every row must leave with
5 * Content-Security-Policy, X-Content-Type-Options and Referrer-Policy, and
6 * every Set-Cookie on it must be HttpOnly and SameSite, and Secure when the
7 * forge is https.
8 *
9 * `security-floor-every-route.test.ts` and `security-floor-errors.test.ts`
10 * walk the kinds and statuses one forge answers; this one adds the 429s and
11 * the redirects a sign-in sends — the rows that also carry cookies — and
12 * asks each row twice, once per scheme.
13 */
14 import { afterAll, describe, expect, test } from "bun:test";
15 import { mkdtemp, rm } from "node:fs/promises";
16 import { tmpdir } from "node:os";
17 import { join } from "node:path";
18 import { createApp, type App } from "../src/app.ts";
19 import { createSession } from "../src/core/sessions.ts";
20 import { serve, type Servers } from "../src/http/server.ts";
21 import { setLimit } from "../src/settings/limits.ts";
22 import { seedAdmin } from "./helpers/admin.ts";
23 import { quietLogs } from "./helpers/globals.ts";
24
25 quietLogs();
26
27 const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-policy"] as const;
28 const PASSWORD = "a-long-family-passphrase";
29 const TRIES = 2;
30
31 type Forge = { app: App; servers: Servers; origin: string; cookie: string; fresh: () => string };
32 type Row = {
33 kind: string;
34 status: number;
35 /** Set when the row must set a cookie, so a cookie check that saw none proves nothing. */
36 setsCookie?: boolean;
37 ask: (f: Forge) => Promise<Response>;
38 };
39
40 const dirs: string[] = [];
41 const stops: (() => void)[] = [];
42
43 afterAll(async () => {
44 for (const stop of stops) stop();
45 for (const dir of dirs) await rm(dir, { recursive: true, force: true });
46 });
47
48 async function forge(publicUrl?: string): Promise<Forge> {
49 const dir = await mkdtemp(join(tmpdir(), "forge-floor-table-"));
50 dirs.push(dir);
51 const app = createApp({ dataDir: dir, port: 0, ...(publicUrl ? { url: publicUrl } : {}) });
52 const servers = await serve(app);
53 stops.push(() => { servers.stop(); app.close(); });
54 const pat = await seedAdmin(app, { username: "pat", email: "[email protected]", password: PASSWORD });
55 if (!pat.ok) throw new Error(pat.error);
56 setLimit(app.db, "signin.per_account", TRIES, pat.value.id);
57 return { app, servers, origin: publicUrl ?? servers.url, cookie: `forge_session=${createSession(app.db, pat.value.id).token}`, fresh: () => `forge_session=${createSession(app.db, pat.value.id).token}` };
58 }
59
60 const get = (f: Forge, path: string, headers: Record<string, string> = {}) =>
61 fetch(`${f.servers.url}${path}`, { redirect: "manual", headers: { "accept-encoding": "identity", ...headers } });
62
63 const post = (f: Forge, path: string, body: string | URLSearchParams, type: string, headers: Record<string, string> = {}) =>
64 fetch(`${f.servers.url}${path}`, {
65 method: "POST",
66 redirect: "manual",
67 headers: { origin: f.origin, "content-type": type, "accept-encoding": "identity", ...headers },
68 body,
69 });
70
71 const form = (f: Forge, path: string, fields: Record<string, string>) =>
72 post(f, path, new URLSearchParams(fields), "application/x-www-form-urlencoded", { accept: "text/html" });
73
74 const api = (f: Forge, path: string, fields: Record<string, string>) =>
75 post(f, path, JSON.stringify(fields), "application/json");
76
77 /** Fill pat's sign-in window with wrong passwords, so the next try is a 429. */
78 async function exhaust(f: Forge): Promise<void> {
79 for (let i = 0; i < TRIES; i++) await (await api(f, "/api/v1/sessions", { username: "pat", password: `wrong-${i}` })).body?.cancel();
80 }
81
82 /** Ask with the database throwing under the handler: the fault a 500 is for. */
83 async function failing(f: Forge, ask: () => Promise<Response>): Promise<Response> {
84 const query = f.app.db.query.bind(f.app.db);
85 f.app.db.query = (() => {
86 throw new Error("SQLITE_IOERR");
87 }) as typeof f.app.db.query;
88 try {
89 return await ask();
90 } finally {
91 f.app.db.query = query;
92 }
93 }
94
95 // In order: the 429 rows fill the window the sign-in rows before them used.
96 const ROWS: Row[] = [
97 { kind: "page", status: 200, ask: (f) => get(f, "/") },
98 { kind: "page, signed in", status: 200, ask: (f) => get(f, "/", { cookie: f.cookie }) },
99 { kind: "404 page", status: 404, ask: (f) => get(f, "/no/such/place", { accept: "text/html" }) },
100 { kind: "404 API", status: 404, ask: (f) => get(f, "/api/v1/repos/pat/nothing") },
101 { kind: "API error, 401", status: 401, ask: (f) => get(f, "/api/v1/user") },
102 { kind: "API error, 403 origin gate", status: 403, ask: (f) => post(f, "/api/v1/sessions", "{}", "application/json", { origin: "https://evil.example" }) },
103 { kind: "API sign-in, 201 with a cookie", status: 201, setsCookie: true, ask: (f) => api(f, "/api/v1/sessions", { username: "pat", password: PASSWORD }) },
104 { kind: "redirect, form sign-in", status: 303, setsCookie: true, ask: (f) => form(f, "/signin", { username: "pat", password: PASSWORD }) },
105 { kind: "redirect, sign-out", status: 303, setsCookie: true, ask: (f) => post(f, "/logout", "", "application/x-www-form-urlencoded", { cookie: f.fresh() }) },
106 { kind: "redirect, mode switch", status: 303, setsCookie: true, ask: (f) => get(f, "/?mode=dev") },
107 { kind: "500 page", status: 500, ask: (f) => failing(f, () => get(f, "/", { cookie: f.cookie, accept: "text/html" })) },
108 { kind: "500 API", status: 500, ask: (f) => failing(f, () => get(f, "/api/v1/user", { cookie: f.cookie })) },
109 { kind: "429 API sign-in", status: 429, ask: async (f) => (await exhaust(f), api(f, "/api/v1/sessions", { username: "pat", password: PASSWORD })) },
110 { kind: "429 form sign-in", status: 429, ask: (f) => form(f, "/signin", { username: "pat", password: PASSWORD }) },
111 ];
112
113 /** What is wrong with one row's answer: its status, a floor header, or a cookie flag. */
114 function wrongWith(row: Row, res: Response, https: boolean): string[] {
115 const name = `${row.kind} (${res.status})`;
116 const wrong: string[] = [];
117 if (res.status !== row.status) wrong.push(`${row.kind} answered ${res.status}, not ${row.status}`);
118 for (const h of FLOOR) if (!res.headers.get(h)) wrong.push(`${name} lacks ${h}`);
119 if (!(res.headers.get("x-content-type-options") ?? "").includes("nosniff")) wrong.push(`${name}: X-Content-Type-Options is not nosniff`);
120 const cookies = res.headers.getSetCookie();
121 if (row.setsCookie === true && cookies.length === 0) wrong.push(`${name} set no cookie`);
122 for (const c of cookies) {
123 const cookieName = c.split("=")[0];
124 const attrs = c.split(";").slice(1).map((a) => a.trim().toLowerCase());
125 if (!attrs.includes("httponly")) wrong.push(`${name}: ${cookieName} is not HttpOnly`);
126 if (!attrs.some((a) => a === "samesite=lax" || a === "samesite=strict")) wrong.push(`${name}: ${cookieName} has no SameSite=Lax|Strict`);
127 if (https && !attrs.includes("secure")) wrong.push(`${name}: ${cookieName} is not Secure over https`);
128 }
129 return wrong;
130 }
131
132 for (const https of [false, true]) {
133 describe(https ? "a forge declared https" : "a forge on plain http", () => {
134 test("every row of the table carries the floor, and every cookie its flags", async () => {
135 const f = await forge(https ? "https://forge.example" : undefined);
136 const wrong: string[] = [];
137 // One at a time: the 500 rows swap the database's query, and the 429 rows count.
138 for (const row of ROWS) {
139 const res = await row.ask(f);
140 await res.body?.cancel();
141 wrong.push(...wrongWith(row, res, https));
142 }
143 expect(wrong).toEqual([]);
144 }, 30_000);
145 });
146 }
147
148 test("the table holds every kind the issue names", () => {
149 const statuses = new Set(ROWS.map((r) => r.status));
150 for (const s of [200, 404, 429, 500, 303, 401]) expect(statuses.has(s)).toBe(true);
151 });
152
153 test("the cookie check fails a cookie that lost a flag", () => {
154 const row: Row = { kind: "fake", status: 200, ask: async () => new Response() };
155 const res = new Response("", { headers: { "set-cookie": "forge_session=x; Path=/; SameSite=Lax" } });
156 expect(wrongWith(row, res, true)).toContain("fake (200): forge_session is not HttpOnly");
157 expect(wrongWith(row, res, true)).toContain("fake (200): forge_session is not Secure over https");
158 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs