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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13668-every-response-carries-the-security-floo → main 1 commit fork
1 file
changed +25
−5
test/security-floor-kinds.test.ts +25 −5
| @@ -1,10 +1,9 @@ | ||
| 1 | 1 | /** |
| 2 | 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. | |
| 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. | |
| 8 | 7 | * |
| 9 | 8 | * `security-floor-every-route.test.ts` and `security-floor-errors.test.ts` |
| 10 | 9 | * 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"; | ||
| 19 | 18 | import { createSession } from "../src/core/sessions.ts"; |
| 20 | 19 | import { serve, type Servers } from "../src/http/server.ts"; |
| 21 | 20 | import { setLimit } from "../src/settings/limits.ts"; |
| 21 | import { spriteAsset, stylesheetAsset } from "../src/web/asset.ts"; | |
| 22 | 22 | import { seedAdmin } from "./helpers/admin.ts"; |
| 23 | 23 | import { quietLogs } from "./helpers/globals.ts"; |
| 24 | 24 | |
| @@ -60,6 +60,10 @@ async function forge(publicUrl?: string): Promise<Forge> { | ||
| 60 | 60 | const get = (f: Forge, path: string, headers: Record<string, string> = {}) => |
| 61 | 61 | fetch(`${f.servers.url}${path}`, { redirect: "manual", headers: { "accept-encoding": "identity", ...headers } }); |
| 62 | 62 | |
| 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 | ||
| 63 | 67 | const post = (f: Forge, path: string, body: string | URLSearchParams, type: string, headers: Record<string, string> = {}) => |
| 64 | 68 | fetch(`${f.servers.url}${path}`, { |
| 65 | 69 | method: "POST", |
| @@ -96,6 +100,9 @@ async function failing(f: Forge, ask: () => Promise<Response>): Promise<Response | ||
| 96 | 100 | const ROWS: Row[] = [ |
| 97 | 101 | { kind: "page", status: 200, ask: (f) => get(f, "/") }, |
| 98 | 102 | { 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`) }, | |
| 99 | 106 | { kind: "404 page", status: 404, ask: (f) => get(f, "/no/such/place", { accept: "text/html" }) }, |
| 100 | 107 | { kind: "404 API", status: 404, ask: (f) => get(f, "/api/v1/repos/pat/nothing") }, |
| 101 | 108 | { kind: "API error, 401", status: 401, ask: (f) => get(f, "/api/v1/user") }, |
| @@ -133,6 +140,7 @@ for (const https of [false, true]) { | ||
| 133 | 140 | describe(https ? "a forge declared https" : "a forge on plain http", () => { |
| 134 | 141 | test("every row of the table carries the floor, and every cookie its flags", async () => { |
| 135 | 142 | const f = await forge(https ? "https://forge.example" : undefined); |
| 143 | assetOrigin = f.servers.url; | |
| 136 | 144 | const wrong: string[] = []; |
| 137 | 145 | // One at a time: the 500 rows swap the database's query, and the 429 rows count. |
| 138 | 146 | for (const row of ROWS) { |
| @@ -148,6 +156,18 @@ for (const https of [false, true]) { | ||
| 148 | 156 | test("the table holds every kind the issue names", () => { |
| 149 | 157 | const statuses = new Set(ROWS.map((r) => r.status)); |
| 150 | 158 | 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"); | |
| 151 | 171 | }); |
| 152 | 172 | |
| 153 | 173 | test("the cookie check fails a cookie that lost a flag", () => { |
new above
offline