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

Merged opened this

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

Merged

torque/forge:torque/13598-every-error-response-400-404-405-413-429 → main 1 commit fork

  • Merged. torque/13598-every-error-response-400-404-405-413-429 was merged into main as 1e1f4e8.

Review

Sign in to review this change.
1 file changed +51 −12
test/security-floor-errors.test.ts +51 −12
@@ -1,10 +1,14 @@
11 /**
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,
55 * X-Content-Type-Options and Referrer-Policy. A 500 must also say nothing of
66 * the fault: no stack, no path, no secret.
77 *
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 *
812 * `security-floor-every-route.test.ts` walks the kinds of answer; this one
913 * walks the error statuses, page and API side by side, so a status that only
1014 * one side covers cannot hide a responder that skips the floor on the other.
@@ -16,9 +20,11 @@ import { join } from "node:path";
1620 import { createApp, type App } from "../src/app.ts";
1721 import { createRepo } from "../src/core/repos.ts";
1822 import { createSession } from "../src/core/sessions.ts";
23 import { CRAWL_BUDGET } from "../src/http/crawl.ts";
1924 import { BODY_MAX } from "../src/http/limits.ts";
2025 import { apiError, forbidden, json, methodNotAllowed, notFound, respondAccess, tryAgain, unauthorized } from "../src/http/respond.ts";
2126 import { serve, type Servers } from "../src/http/server.ts";
27 import { setLimit } from "../src/settings/limits.ts";
2228 import { seedAdmin } from "./helpers/admin.ts";
2329 import { quietLogs } from "./helpers/globals.ts";
2430
@@ -28,7 +34,10 @@ const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-po
2834 const SECRET = "hunter2-the-database-password";
2935
3036 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 };
3241
3342 let dataDir = "";
3443 let app: App, servers: Servers;
@@ -43,6 +52,9 @@ beforeAll(async () => {
4352 const made = await createRepo(app.db, app.reposRoot, { ownerId: admin.value.id, name: "notes", visibility: "public" });
4453 if (!made.ok) throw new Error(String(made.error));
4554 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");
4658 }, 30_000);
4759
4860 afterAll(async () => {
@@ -109,6 +121,30 @@ const PROVOKED = (): Provoke[] => [
109121 signedIn: true,
110122 init: { method: "POST", headers: { origin: servers.url, "content-type": "application/json", "idempotency-key": "k1" }, body: tooBig() },
111123 },
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 },
112148 // 500: the database throws under a handler
113149 { status: 500, side: "page", path: "/", signedIn: true, failing: true, init: { headers: HTML } },
114150 { 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 }> {
126162 try {
127163 // A fresh connection each: an oversize body closes its connection, and a
128164 // 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();
136175 return { res, body: await res.text() };
137176 } finally {
138177 app.db.query = query;
@@ -153,7 +192,7 @@ test("each error status, on a page and on the API, carries the floor", async ()
153192
154193 test("every status in the issue is walked on both sides", () => {
155194 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])
157196 for (const side of ["page", "api"]) expect(walked.has(`${status}:${side}`)).toBe(true);
158197 });
159198

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs