Public: anyone can read this; the crew can change it.
Every kind of answer carries the security floor: pages, JSON, errors, redirects, static assets and the MCP, proven in one test #15777
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15763-every-kind-of-answer-carries-the-securit → main 0 commits fork
1 file
changed +35
−5
test/security-floor-kinds.test.ts +35 −5
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | * One table, every kind of answer (#13441): a page, a 404, a 429, a 500, a | |
| 3 | * redirect, a JSON answer, a static asset and an API error, each asked over the wire of a forge on | |
| 2 | * One table, every kind of answer (#13441, #15763): a page, a 400, a 404, a | |
| 3 | * 405, a 429, a 500, a 302 and a 303 redirect, a JSON answer, a static asset, | |
| 4 | * an API error, an MCP reply and an SSE stream, each asked over the wire of a forge on | |
| 4 | 5 | * plain http and of one declared https. Every row must leave with Content-Security-Policy, |
| 5 | 6 | * X-Content-Type-Options and Referrer-Policy, and every Set-Cookie on it must be HttpOnly and |
| 6 | 7 | * SameSite, and Secure when the forge is https. |
| @@ -16,6 +17,7 @@ import { tmpdir } from "node:os"; | ||
| 16 | 17 | import { join } from "node:path"; |
| 17 | 18 | import { createApp, type App } from "../src/app.ts"; |
| 18 | 19 | import { createSession } from "../src/core/sessions.ts"; |
| 20 | import { createToken } from "../src/core/tokens.ts"; | |
| 19 | 21 | import { serve, type Servers } from "../src/http/server.ts"; |
| 20 | 22 | import { setLimit } from "../src/settings/limits.ts"; |
| 21 | 23 | import { spriteAsset, stylesheetAsset } from "../src/web/asset.ts"; |
| @@ -28,12 +30,16 @@ const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-po | ||
| 28 | 30 | const PASSWORD = "a-long-family-passphrase"; |
| 29 | 31 | const TRIES = 2; |
| 30 | 32 | |
| 31 | type Forge = { app: App; servers: Servers; origin: string; cookie: string; fresh: () => string }; | |
| 33 | type Forge = { app: App; servers: Servers; origin: string; cookie: string; token: string; fresh: () => string }; | |
| 32 | 34 | type Row = { |
| 33 | 35 | kind: string; |
| 34 | 36 | status: number; |
| 35 | 37 | /** Set when the row must set a cookie, so a cookie check that saw none proves nothing. */ |
| 36 | 38 | setsCookie?: boolean; |
| 39 | /** | |
| 40 | * Set when the kind is told by its Content-Type, so a row that fell back to another kind fails. | |
| 41 | */ | |
| 42 | type?: string; | |
| 37 | 43 | ask: (f: Forge) => Promise<Response>; |
| 38 | 44 | }; |
| 39 | 45 | |
| @@ -54,7 +60,7 @@ async function forge(publicUrl?: string): Promise<Forge> { | ||
| 54 | 60 | const pat = await seedAdmin(app, { username: "pat", email: "[email protected]", password: PASSWORD }); |
| 55 | 61 | if (!pat.ok) throw new Error(pat.error); |
| 56 | 62 | 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}` }; | |
| 63 | return { app, servers, origin: publicUrl ?? servers.url, cookie: `forge_session=${createSession(app.db, pat.value.id).token}`, token: createToken(app.db, pat.value.id, "rw", "rw").token, fresh: () => `forge_session=${createSession(app.db, pat.value.id).token}` }; | |
| 58 | 64 | } |
| 59 | 65 | |
| 60 | 66 | const get = (f: Forge, path: string, headers: Record<string, string> = {}) => |
| @@ -78,6 +84,13 @@ const form = (f: Forge, path: string, fields: Record<string, string>) => | ||
| 78 | 84 | const api = (f: Forge, path: string, fields: Record<string, string>) => |
| 79 | 85 | post(f, path, JSON.stringify(fields), "application/json"); |
| 80 | 86 | |
| 87 | /** One JSON-RPC message to /mcp, as an agent's client sends it with pat's token. */ | |
| 88 | const mcp = (f: Forge, message: unknown) => | |
| 89 | post(f, "/mcp", typeof message === "string" ? message : JSON.stringify(message), "application/json", { | |
| 90 | accept: "application/json, text/event-stream", | |
| 91 | authorization: `Bearer ${f.token}`, | |
| 92 | }); | |
| 93 | ||
| 81 | 94 | /** Fill pat's sign-in window with wrong passwords, so the next try is a 429. */ |
| 82 | 95 | async function exhaust(f: Forge): Promise<void> { |
| 83 | 96 | for (let i = 0; i < TRIES; i++) await (await api(f, "/api/v1/sessions", { username: "pat", password: `wrong-${i}` })).body?.cancel(); |
| @@ -111,6 +124,14 @@ const ROWS: Row[] = [ | ||
| 111 | 124 | { kind: "redirect, form sign-in", status: 303, setsCookie: true, ask: (f) => form(f, "/signin", { username: "pat", password: PASSWORD }) }, |
| 112 | 125 | { kind: "redirect, sign-out", status: 303, setsCookie: true, ask: (f) => post(f, "/logout", "", "application/x-www-form-urlencoded", { cookie: f.fresh() }) }, |
| 113 | 126 | { kind: "redirect, mode switch", status: 303, setsCookie: true, ask: (f) => get(f, "/?mode=dev") }, |
| 127 | { kind: "redirect, 302 stale stylesheet", status: 302, ask: () => getAsset("/assets/forge.css") }, | |
| 128 | { kind: "redirect, 302 well-known", status: 302, ask: (f) => get(f, "/.well-known/change-password") }, | |
| 129 | { kind: "405 page", status: 405, ask: (f) => get(f, "/mcp", { accept: "text/html" }) }, | |
| 130 | { kind: "405 API", status: 405, ask: (f) => fetch(`${f.servers.url}/api/v1/user`, { method: "PUT", redirect: "manual", headers: { origin: f.origin, cookie: f.cookie } }) }, | |
| 131 | { kind: "400 refusal, a body that is not JSON", status: 400, ask: (f) => post(f, "/api/v1/sessions", "{not json", "application/json") }, | |
| 132 | { kind: "MCP reply", status: 200, type: "application/json", ask: (f) => mcp(f, { jsonrpc: "2.0", id: 1, method: "tools/list" }) }, | |
| 133 | { kind: "MCP error reply, a JSON-RPC parse error", status: 200, type: "application/json", ask: (f) => mcp(f, "{not json") }, | |
| 134 | { kind: "SSE stream", status: 200, type: "text/event-stream", ask: (f) => get(f, "/api/v1/forge/view", { cookie: f.cookie, accept: "text/event-stream" }) }, | |
| 114 | 135 | { kind: "500 page", status: 500, ask: (f) => failing(f, () => get(f, "/", { cookie: f.cookie, accept: "text/html" })) }, |
| 115 | 136 | { kind: "500 API", status: 500, ask: (f) => failing(f, () => get(f, "/api/v1/user", { cookie: f.cookie })) }, |
| 116 | 137 | { kind: "429 API sign-in", status: 429, ask: async (f) => (await exhaust(f), api(f, "/api/v1/sessions", { username: "pat", password: PASSWORD })) }, |
| @@ -122,6 +143,8 @@ function wrongWith(row: Row, res: Response, https: boolean): string[] { | ||
| 122 | 143 | const name = `${row.kind} (${res.status})`; |
| 123 | 144 | const wrong: string[] = []; |
| 124 | 145 | if (res.status !== row.status) wrong.push(`${row.kind} answered ${res.status}, not ${row.status}`); |
| 146 | if (row.type !== undefined && !(res.headers.get("content-type") ?? "").startsWith(row.type)) | |
| 147 | wrong.push(`${name} is ${res.headers.get("content-type")}, not ${row.type}`); | |
| 125 | 148 | for (const h of FLOOR) if (!res.headers.get(h)) wrong.push(`${name} lacks ${h}`); |
| 126 | 149 | if (!(res.headers.get("x-content-type-options") ?? "").includes("nosniff")) wrong.push(`${name}: X-Content-Type-Options is not nosniff`); |
| 127 | 150 | const cookies = res.headers.getSetCookie(); |
| @@ -155,12 +178,19 @@ for (const https of [false, true]) { | ||
| 155 | 178 | |
| 156 | 179 | test("the table holds every kind the issue names", () => { |
| 157 | 180 | const statuses = new Set(ROWS.map((r) => r.status)); |
| 158 | for (const s of [200, 404, 429, 500, 303, 401]) expect(statuses.has(s)).toBe(true); | |
| 181 | for (const s of [200, 302, 303, 400, 401, 404, 405, 429, 500]) expect(statuses.has(s)).toBe(true); | |
| 159 | 182 | const kinds = ROWS.map((r) => r.kind); |
| 160 | 183 | for (const k of ["page", "JSON API answer", "404 page", "500 page", "redirect, form sign-in"]) expect(kinds).toContain(k); |
| 184 | for (const k of ["MCP reply", "SSE stream"]) expect(kinds).toContain(k); | |
| 161 | 185 | expect(kinds.some((k) => k.startsWith("static asset"))).toBe(true); |
| 162 | 186 | }); |
| 163 | 187 | |
| 188 | test("a row that answered as another kind is named", () => { | |
| 189 | const row: Row = { kind: "SSE fake", status: 200, type: "text/event-stream", ask: async () => new Response() }; | |
| 190 | const res = new Response("{}", { headers: { "content-type": "application/json" } }); | |
| 191 | expect(wrongWith(row, res, false)).toContain("SSE fake (200) is application/json, not text/event-stream"); | |
| 192 | }); | |
| 193 | ||
| 164 | 194 | test("a response missing a floor header is named by its kind", () => { |
| 165 | 195 | const row: Row = { kind: "static asset, fake", status: 200, ask: async () => new Response() }; |
| 166 | 196 | const res = new Response("", { headers: { "referrer-policy": "no-referrer" } }); |
new above
offline