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

Every response carries the security floor, proved by a test that fetches one page of each kind #13674

Merged opened this

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

Merged

mason/forge:mason/13668-every-response-carries-the-security-floo → main 1 commit fork

  • Merged. mason/13668-every-response-carries-the-security-floo was merged into main as 73656e5.

Review

Sign in to review this change.
1 file changed +25 −5
test/security-floor-kinds.test.ts +25 −5
@@ -1,10 +1,9 @@
11 /**
22 * 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.
3 * redirect, a JSON answer, a static asset and an API error, each asked over the wire of a forge on
4 * plain http and of one declared https. Every row must leave with Content-Security-Policy,
5 * X-Content-Type-Options and Referrer-Policy, and every Set-Cookie on it must be HttpOnly and
6 * SameSite, and Secure when the forge is https.
87 *
98 * `security-floor-every-route.test.ts` and `security-floor-errors.test.ts`
109 * walk the kinds and statuses one forge answers; this one adds the 429s and
@@ -19,6 +18,7 @@ import { createApp, type App } from "../src/app.ts";
1918 import { createSession } from "../src/core/sessions.ts";
2019 import { serve, type Servers } from "../src/http/server.ts";
2120 import { setLimit } from "../src/settings/limits.ts";
21 import { spriteAsset, stylesheetAsset } from "../src/web/asset.ts";
2222 import { seedAdmin } from "./helpers/admin.ts";
2323 import { quietLogs } from "./helpers/globals.ts";
2424
@@ -60,6 +60,10 @@ async function forge(publicUrl?: string): Promise<Forge> {
6060 const get = (f: Forge, path: string, headers: Record<string, string> = {}) =>
6161 fetch(`${f.servers.url}${path}`, { redirect: "manual", headers: { "accept-encoding": "identity", ...headers } });
6262
63 let assetOrigin = "";
64 /** Assets are the same on every forge; ask whichever forge the current table is walking. */
65 const getAsset = (path: string) => fetch(`${assetOrigin}${path}`, { redirect: "manual", headers: { "accept-encoding": "identity" } });
66
6367 const post = (f: Forge, path: string, body: string | URLSearchParams, type: string, headers: Record<string, string> = {}) =>
6468 fetch(`${f.servers.url}${path}`, {
6569 method: "POST",
@@ -96,6 +100,9 @@ async function failing(f: Forge, ask: () => Promise<Response>): Promise<Response
96100 const ROWS: Row[] = [
97101 { kind: "page", status: 200, ask: (f) => get(f, "/") },
98102 { kind: "page, signed in", status: 200, ask: (f) => get(f, "/", { cookie: f.cookie }) },
103 { kind: "JSON API answer", status: 200, ask: (f) => get(f, "/api/v1/user", { cookie: f.cookie }) },
104 { kind: "static asset, stylesheet", status: 200, ask: () => getAsset(`/assets/${stylesheetAsset().name}.css`) },
105 { kind: "static asset, sprite", status: 200, ask: () => getAsset(`/assets/${spriteAsset().name}.svg`) },
99106 { kind: "404 page", status: 404, ask: (f) => get(f, "/no/such/place", { accept: "text/html" }) },
100107 { kind: "404 API", status: 404, ask: (f) => get(f, "/api/v1/repos/pat/nothing") },
101108 { kind: "API error, 401", status: 401, ask: (f) => get(f, "/api/v1/user") },
@@ -133,6 +140,7 @@ for (const https of [false, true]) {
133140 describe(https ? "a forge declared https" : "a forge on plain http", () => {
134141 test("every row of the table carries the floor, and every cookie its flags", async () => {
135142 const f = await forge(https ? "https://forge.example" : undefined);
143 assetOrigin = f.servers.url;
136144 const wrong: string[] = [];
137145 // One at a time: the 500 rows swap the database's query, and the 429 rows count.
138146 for (const row of ROWS) {
@@ -148,6 +156,18 @@ for (const https of [false, true]) {
148156 test("the table holds every kind the issue names", () => {
149157 const statuses = new Set(ROWS.map((r) => r.status));
150158 for (const s of [200, 404, 429, 500, 303, 401]) expect(statuses.has(s)).toBe(true);
159 const kinds = ROWS.map((r) => r.kind);
160 for (const k of ["page", "JSON API answer", "404 page", "500 page", "redirect, form sign-in"]) expect(kinds).toContain(k);
161 expect(kinds.some((k) => k.startsWith("static asset"))).toBe(true);
162 });
163
164 test("a response missing a floor header is named by its kind", () => {
165 const row: Row = { kind: "static asset, fake", status: 200, ask: async () => new Response() };
166 const res = new Response("", { headers: { "referrer-policy": "no-referrer" } });
167 const wrong = wrongWith(row, res, false);
168 expect(wrong).toContain("static asset, fake (200) lacks content-security-policy");
169 expect(wrong).toContain("static asset, fake (200) lacks x-content-type-options");
170 expect(wrong).not.toContain("static asset, fake (200) lacks referrer-policy");
151171 });
152172
153173 test("the cookie check fails a cookie that lost a flag", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs