Public: anyone can read this; the crew can change it.
Every error response (400, 404, 405, 413, 429, 500) keeps the security floor headers, proven by provoking each #13611
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13598-every-error-response-400-404-405-413-429 → main 1 commit fork
1 file
changed +51
−12
test/security-floor-errors.test.ts +51 −12
| @@ -1,10 +1,14 @@ | ||
| 1 | 1 | /** |
| 2 | * The security floor on the error paths nobody looks at (#10949): each of | |
| 3 | * 400, 401, 403, 404, 405, 413 and 500, provoked once on a page route and | |
| 4 | * once on an API route, must leave with Content-Security-Policy, | |
| 2 | * The security floor on the error paths nobody looks at (#10949, #13598): | |
| 3 | * each of 400, 401, 403, 404, 405, 413, 429 and 500, provoked once on a page | |
| 4 | * route and once on an API route, must leave with Content-Security-Policy, | |
| 5 | 5 | * X-Content-Type-Options and Referrer-Policy. A 500 must also say nothing of |
| 6 | 6 | * the fault: no stack, no path, no secret. |
| 7 | 7 | * |
| 8 | * These are the app's own answers, through the real handler. An error the | |
| 9 | * platform's edge writes before a request reaches the forge (its own 502, a | |
| 10 | * gateway timeout) is outside the app and not covered here. | |
| 11 | * | |
| 8 | 12 | * `security-floor-every-route.test.ts` walks the kinds of answer; this one |
| 9 | 13 | * walks the error statuses, page and API side by side, so a status that only |
| 10 | 14 | * one side covers cannot hide a responder that skips the floor on the other. |
| @@ -16,9 +20,11 @@ import { join } from "node:path"; | ||
| 16 | 20 | import { createApp, type App } from "../src/app.ts"; |
| 17 | 21 | import { createRepo } from "../src/core/repos.ts"; |
| 18 | 22 | import { createSession } from "../src/core/sessions.ts"; |
| 23 | import { CRAWL_BUDGET } from "../src/http/crawl.ts"; | |
| 19 | 24 | import { BODY_MAX } from "../src/http/limits.ts"; |
| 20 | 25 | import { apiError, forbidden, json, methodNotAllowed, notFound, respondAccess, tryAgain, unauthorized } from "../src/http/respond.ts"; |
| 21 | 26 | import { serve, type Servers } from "../src/http/server.ts"; |
| 27 | import { setLimit } from "../src/settings/limits.ts"; | |
| 22 | 28 | import { seedAdmin } from "./helpers/admin.ts"; |
| 23 | 29 | import { quietLogs } from "./helpers/globals.ts"; |
| 24 | 30 | |
| @@ -28,7 +34,10 @@ const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-po | ||
| 28 | 34 | const SECRET = "hunter2-the-database-password"; |
| 29 | 35 | |
| 30 | 36 | type Side = "page" | "api"; |
| 31 | type Provoke = { status: number; side: Side; path: string; init?: RequestInit; signedIn?: boolean; failing?: boolean }; | |
| 37 | /** | |
| 38 | * `times`: how often the request is sent, the last answer the one judged (a limit filled first). | |
| 39 | */ | |
| 40 | type Provoke = { status: number; side: Side; path: string; init?: RequestInit; signedIn?: boolean; failing?: boolean; times?: number }; | |
| 32 | 41 | |
| 33 | 42 | let dataDir = ""; |
| 34 | 43 | let app: App, servers: Servers; |
| @@ -43,6 +52,9 @@ beforeAll(async () => { | ||
| 43 | 52 | const made = await createRepo(app.db, app.reposRoot, { ownerId: admin.value.id, name: "notes", visibility: "public" }); |
| 44 | 53 | if (!made.ok) throw new Error(String(made.error)); |
| 45 | 54 | cookie = `forge_session=${createSession(app.db, admin.value.id).token}`; |
| 55 | // One password attempt a minute for the whole forge, so the second is a | |
| 56 | // 429; sessions by cookie never count against it. | |
| 57 | if (!setLimit(app.db, "signin.per_forge", 1, "seed").ok) throw new Error("limit"); | |
| 46 | 58 | }, 30_000); |
| 47 | 59 | |
| 48 | 60 | afterAll(async () => { |
| @@ -109,6 +121,30 @@ const PROVOKED = (): Provoke[] => [ | ||
| 109 | 121 | signedIn: true, |
| 110 | 122 | init: { method: "POST", headers: { origin: servers.url, "content-type": "application/json", "idempotency-key": "k1" }, body: tooBig() }, |
| 111 | 123 | }, |
| 124 | // 429: the forge-wide sign-in window, filled by the first attempt | |
| 125 | { | |
| 126 | status: 429, | |
| 127 | side: "page", | |
| 128 | path: "/signin", | |
| 129 | times: 2, | |
| 130 | init: { method: "POST", headers: { ...own(), "content-type": "application/x-www-form-urlencoded" }, body: "username=ada&password=wrong" }, | |
| 131 | }, | |
| 132 | { | |
| 133 | status: 429, | |
| 134 | side: "api", | |
| 135 | path: "/api/v1/sessions", | |
| 136 | times: 2, | |
| 137 | init: { method: "POST", headers: { origin: servers.url, "content-type": "application/json" }, body: '{"username":"ada","password":"wrong"}' }, | |
| 138 | }, | |
| 139 | // A crawler past its deep budget: `crawlRefused` writes a bare Response, | |
| 140 | // so only the door lays its floor. | |
| 141 | { | |
| 142 | status: 429, | |
| 143 | side: "page", | |
| 144 | path: "/ada/notes/commits", | |
| 145 | times: CRAWL_BUDGET.deep + 1, | |
| 146 | init: { headers: { ...HTML, "user-agent": "Mozilla/5.0 (compatible; Floorbot/1.0)", "x-forwarded-for": "10.9.0.1" } }, | |
| 147 | }, | |
| 112 | 148 | // 500: the database throws under a handler |
| 113 | 149 | { status: 500, side: "page", path: "/", signedIn: true, failing: true, init: { headers: HTML } }, |
| 114 | 150 | { status: 500, side: "api", path: "/api/v1/user", signedIn: true, failing: true }, |
| @@ -126,13 +162,16 @@ async function provoke(p: Provoke): Promise<{ res: Response; body: string }> { | ||
| 126 | 162 | try { |
| 127 | 163 | // A fresh connection each: an oversize body closes its connection, and a |
| 128 | 164 | // client that reused it would read the runtime's own bare 400 instead. |
| 129 | const res = await fetch(`${servers.url}${p.path}`, { | |
| 130 | ...p.init, | |
| 131 | headers, | |
| 132 | keepalive: false, | |
| 133 | redirect: "manual", | |
| 134 | signal: AbortSignal.timeout(10_000), | |
| 135 | }); | |
| 165 | const send = () => | |
| 166 | fetch(`${servers.url}${p.path}`, { | |
| 167 | ...p.init, | |
| 168 | headers, | |
| 169 | keepalive: false, | |
| 170 | redirect: "manual", | |
| 171 | signal: AbortSignal.timeout(10_000), | |
| 172 | }); | |
| 173 | for (let i = 1; i < (p.times ?? 1); i++) await (await send()).text(); | |
| 174 | const res = await send(); | |
| 136 | 175 | return { res, body: await res.text() }; |
| 137 | 176 | } finally { |
| 138 | 177 | app.db.query = query; |
| @@ -153,7 +192,7 @@ test("each error status, on a page and on the API, carries the floor", async () | ||
| 153 | 192 | |
| 154 | 193 | test("every status in the issue is walked on both sides", () => { |
| 155 | 194 | const walked = new Set(PROVOKED().map((p) => `${p.status}:${p.side}`)); |
| 156 | for (const status of [400, 401, 403, 404, 405, 413, 500]) | |
| 195 | for (const status of [400, 401, 403, 404, 405, 413, 429, 500]) | |
| 157 | 196 | for (const side of ["page", "api"]) expect(walked.has(`${status}:${side}`)).toBe(true); |
| 158 | 197 | }); |
| 159 | 198 |
new above
offline