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

Merged opened this

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

Merged

scout/forge:scout/15763-every-kind-of-answer-carries-the-securit → main 0 commits fork

  • Merged. scout/15763-every-kind-of-answer-carries-the-securit was merged into main as 344d605.

Review

Sign in to review this change.
1 file changed +35 −5
test/security-floor-kinds.test.ts +35 −5
@@ -1,6 +1,7 @@
11 /**
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
45 * plain http and of one declared https. Every row must leave with Content-Security-Policy,
56 * X-Content-Type-Options and Referrer-Policy, and every Set-Cookie on it must be HttpOnly and
67 * SameSite, and Secure when the forge is https.
@@ -16,6 +17,7 @@ import { tmpdir } from "node:os";
1617 import { join } from "node:path";
1718 import { createApp, type App } from "../src/app.ts";
1819 import { createSession } from "../src/core/sessions.ts";
20 import { createToken } from "../src/core/tokens.ts";
1921 import { serve, type Servers } from "../src/http/server.ts";
2022 import { setLimit } from "../src/settings/limits.ts";
2123 import { spriteAsset, stylesheetAsset } from "../src/web/asset.ts";
@@ -28,12 +30,16 @@ const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-po
2830 const PASSWORD = "a-long-family-passphrase";
2931 const TRIES = 2;
3032
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 };
3234 type Row = {
3335 kind: string;
3436 status: number;
3537 /** Set when the row must set a cookie, so a cookie check that saw none proves nothing. */
3638 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;
3743 ask: (f: Forge) => Promise<Response>;
3844 };
3945
@@ -54,7 +60,7 @@ async function forge(publicUrl?: string): Promise<Forge> {
5460 const pat = await seedAdmin(app, { username: "pat", email: "[email protected]", password: PASSWORD });
5561 if (!pat.ok) throw new Error(pat.error);
5662 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}` };
5864 }
5965
6066 const get = (f: Forge, path: string, headers: Record<string, string> = {}) =>
@@ -78,6 +84,13 @@ const form = (f: Forge, path: string, fields: Record<string, string>) =>
7884 const api = (f: Forge, path: string, fields: Record<string, string>) =>
7985 post(f, path, JSON.stringify(fields), "application/json");
8086
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
8194 /** Fill pat's sign-in window with wrong passwords, so the next try is a 429. */
8295 async function exhaust(f: Forge): Promise<void> {
8396 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[] = [
111124 { kind: "redirect, form sign-in", status: 303, setsCookie: true, ask: (f) => form(f, "/signin", { username: "pat", password: PASSWORD }) },
112125 { kind: "redirect, sign-out", status: 303, setsCookie: true, ask: (f) => post(f, "/logout", "", "application/x-www-form-urlencoded", { cookie: f.fresh() }) },
113126 { 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" }) },
114135 { kind: "500 page", status: 500, ask: (f) => failing(f, () => get(f, "/", { cookie: f.cookie, accept: "text/html" })) },
115136 { kind: "500 API", status: 500, ask: (f) => failing(f, () => get(f, "/api/v1/user", { cookie: f.cookie })) },
116137 { 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[] {
122143 const name = `${row.kind} (${res.status})`;
123144 const wrong: string[] = [];
124145 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}`);
125148 for (const h of FLOOR) if (!res.headers.get(h)) wrong.push(`${name} lacks ${h}`);
126149 if (!(res.headers.get("x-content-type-options") ?? "").includes("nosniff")) wrong.push(`${name}: X-Content-Type-Options is not nosniff`);
127150 const cookies = res.headers.getSetCookie();
@@ -155,12 +178,19 @@ for (const https of [false, true]) {
155178
156179 test("the table holds every kind the issue names", () => {
157180 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);
159182 const kinds = ROWS.map((r) => r.kind);
160183 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);
161185 expect(kinds.some((k) => k.startsWith("static asset"))).toBe(true);
162186 });
163187
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
164194 test("a response missing a floor header is named by its kind", () => {
165195 const row: Row = { kind: "static asset, fake", status: 200, ask: async () => new Response() };
166196 const res = new Response("", { headers: { "referrer-policy": "no-referrer" } });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs