Public: anyone can read this; the crew can change it.
Every kind of answer carries the security floor: one test fetches a page, JSON, media, a 404, a redirect and a stream #16554
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16301-every-kind-of-answer-carries-the-securit → main 0 commits fork
1 file
changed +27
−3
test/security-floor-kinds.test.ts +27 −3
| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * One table, every kind of answer (#13441, #15763, #15967): a page, a 400, a |
| 3 | 3 | * 404, a 405, a 429, a 500, a 302 and a 303 redirect, a JSON answer, a hashed |
| 4 | * static asset, an API error, an MCP reply, an SSE stream and git smart-http, each asked over the | |
| 4 | * static asset, an API error, an MCP reply, an SSE stream, git smart-http and | |
| 5 | * media (#16301: a picture, a byte range buffered and streamed, a 416, the raw | |
| 6 | * text), each asked over the | |
| 5 | 7 | * wire of a forge on plain http and of one declared https. Every row must leave with |
| 6 | 8 | * Content-Security-Policy, X-Content-Type-Options and Referrer-Policy, and every Set-Cookie on it |
| 7 | 9 | * must be HttpOnly and SameSite, and Secure when the forge is https. |
| @@ -15,7 +17,7 @@ import { afterAll, describe, expect, test } from "bun:test"; | ||
| 15 | 17 | import { mkdtemp, rm } from "node:fs/promises"; |
| 16 | 18 | import { tmpdir } from "node:os"; |
| 17 | 19 | import { join } from "node:path"; |
| 18 | import { createRepo } from "../src/actions/repos.ts"; | |
| 20 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 19 | 21 | import { createApp, type App } from "../src/app.ts"; |
| 20 | 22 | import { createSession } from "../src/core/sessions.ts"; |
| 21 | 23 | import { createToken } from "../src/core/tokens.ts"; |
| @@ -30,6 +32,13 @@ quietLogs(); | ||
| 30 | 32 | const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-policy"] as const; |
| 31 | 33 | const PASSWORD = "a-long-family-passphrase"; |
| 32 | 34 | const TRIES = 2; |
| 35 | const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 1, 2, 3]); | |
| 36 | /** Over the media route's pooled read, so a range of it is streamed off git. */ | |
| 37 | const CLIP = (() => { | |
| 38 | const bytes = new Uint8Array(1_100_000); | |
| 39 | bytes.set(new TextEncoder().encode("\0\0\0\x20ftypisom"), 0); | |
| 40 | return bytes; | |
| 41 | })(); | |
| 33 | 42 | |
| 34 | 43 | type Forge = { app: App; servers: Servers; origin: string; cookie: string; token: string; fresh: () => string }; |
| 35 | 44 | type Row = { |
| @@ -63,6 +72,15 @@ async function forge(publicUrl?: string): Promise<Forge> { | ||
| 63 | 72 | setLimit(app.db, "signin.per_account", TRIES, pat.value.id); |
| 64 | 73 | const repo = await createRepo(app, pat.value, { name: "floor", visibility: "public" }); |
| 65 | 74 | if (!repo.ok) throw new Error(String(repo.error)); |
| 75 | const media = await commitToBranch(app, pat.value, repo.value, { | |
| 76 | message: "media", | |
| 77 | files: [ | |
| 78 | { path: "logo.png", content: PNG }, | |
| 79 | { path: "clip.mp4", content: CLIP }, | |
| 80 | { path: "notes.txt", content: "plain words\n" }, | |
| 81 | ], | |
| 82 | }); | |
| 83 | if (!media.ok) throw new Error(String(media.error)); | |
| 66 | 84 | 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}` }; |
| 67 | 85 | } |
| 68 | 86 | |
| @@ -139,6 +157,11 @@ const ROWS: Row[] = [ | ||
| 139 | 157 | { kind: "git smart-http, streamed upload-pack", status: 200, type: "application/x-git-upload-pack-result", ask: (f) => post(f, "/pat/floor.git/git-upload-pack", "0014command=ls-refs\n0000", "application/x-git-upload-pack-request", { "git-protocol": "version=2" }) }, |
| 140 | 158 | { kind: "git smart-http, 400 dumb http", status: 400, ask: (f) => get(f, "/pat/floor.git/info/refs") }, |
| 141 | 159 | { kind: "git smart-http, 401 push without credentials", status: 401, ask: (f) => get(f, "/pat/floor.git/info/refs?service=git-receive-pack") }, |
| 160 | { kind: "media, a picture", status: 200, type: "image/png", ask: (f) => get(f, "/pat/floor/media/main/logo.png") }, | |
| 161 | { kind: "media, a byte range of a buffered file", status: 206, type: "image/png", ask: (f) => get(f, "/pat/floor/media/main/logo.png", { range: "bytes=0-3" }) }, | |
| 162 | { kind: "media, a streamed byte range", status: 206, type: "video/mp4", ask: (f) => get(f, "/pat/floor/media/main/clip.mp4", { range: "bytes=1000000-" }) }, | |
| 163 | { kind: "media, 416 range past the end", status: 416, ask: (f) => get(f, "/pat/floor/media/main/logo.png", { range: "bytes=999-" }) }, | |
| 164 | { kind: "raw file text", status: 200, type: "text/plain", ask: (f) => get(f, "/pat/floor/raw/main/notes.txt") }, | |
| 142 | 165 | { kind: "500 page", status: 500, ask: (f) => failing(f, () => get(f, "/", { cookie: f.cookie, accept: "text/html" })) }, |
| 143 | 166 | { kind: "500 API", status: 500, ask: (f) => failing(f, () => get(f, "/api/v1/user", { cookie: f.cookie })) }, |
| 144 | 167 | { kind: "429 API sign-in", status: 429, ask: async (f) => (await exhaust(f), api(f, "/api/v1/sessions", { username: "pat", password: PASSWORD })) }, |
| @@ -185,11 +208,12 @@ for (const https of [false, true]) { | ||
| 185 | 208 | |
| 186 | 209 | test("the table holds every kind the issue names", () => { |
| 187 | 210 | const statuses = new Set(ROWS.map((r) => r.status)); |
| 188 | for (const s of [200, 302, 303, 400, 401, 404, 405, 429, 500]) expect(statuses.has(s)).toBe(true); | |
| 211 | for (const s of [200, 206, 302, 303, 400, 401, 404, 405, 416, 429, 500]) expect(statuses.has(s)).toBe(true); | |
| 189 | 212 | const kinds = ROWS.map((r) => r.kind); |
| 190 | 213 | for (const k of ["page", "JSON API answer", "404 page", "500 page", "redirect, form sign-in"]) expect(kinds).toContain(k); |
| 191 | 214 | for (const k of ["MCP reply", "SSE stream", "git smart-http, refs advertisement"]) expect(kinds).toContain(k); |
| 192 | 215 | expect(kinds.some((k) => k.startsWith("static asset"))).toBe(true); |
| 216 | expect(kinds.filter((k) => k.startsWith("media")).length).toBeGreaterThanOrEqual(3); | |
| 193 | 217 | }); |
| 194 | 218 | |
| 195 | 219 | test("a row that answered as another kind is named", () => { |
new above
offline