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

Merged opened this

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

Merged

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

  • Merged. scout/16301-every-kind-of-answer-carries-the-securit was merged into main as bb0e002.

Review

Sign in to review this change.
1 file changed +27 −3
test/security-floor-kinds.test.ts +27 −3
@@ -1,7 +1,9 @@
11 /**
22 * One table, every kind of answer (#13441, #15763, #15967): a page, a 400, a
33 * 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
57 * wire of a forge on plain http and of one declared https. Every row must leave with
68 * Content-Security-Policy, X-Content-Type-Options and Referrer-Policy, and every Set-Cookie on it
79 * must be HttpOnly and SameSite, and Secure when the forge is https.
@@ -15,7 +17,7 @@ import { afterAll, describe, expect, test } from "bun:test";
1517 import { mkdtemp, rm } from "node:fs/promises";
1618 import { tmpdir } from "node:os";
1719 import { join } from "node:path";
18 import { createRepo } from "../src/actions/repos.ts";
20 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1921 import { createApp, type App } from "../src/app.ts";
2022 import { createSession } from "../src/core/sessions.ts";
2123 import { createToken } from "../src/core/tokens.ts";
@@ -30,6 +32,13 @@ quietLogs();
3032 const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-policy"] as const;
3133 const PASSWORD = "a-long-family-passphrase";
3234 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 })();
3342
3443 type Forge = { app: App; servers: Servers; origin: string; cookie: string; token: string; fresh: () => string };
3544 type Row = {
@@ -63,6 +72,15 @@ async function forge(publicUrl?: string): Promise<Forge> {
6372 setLimit(app.db, "signin.per_account", TRIES, pat.value.id);
6473 const repo = await createRepo(app, pat.value, { name: "floor", visibility: "public" });
6574 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));
6684 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}` };
6785 }
6886
@@ -139,6 +157,11 @@ const ROWS: Row[] = [
139157 { 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" }) },
140158 { kind: "git smart-http, 400 dumb http", status: 400, ask: (f) => get(f, "/pat/floor.git/info/refs") },
141159 { 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") },
142165 { kind: "500 page", status: 500, ask: (f) => failing(f, () => get(f, "/", { cookie: f.cookie, accept: "text/html" })) },
143166 { kind: "500 API", status: 500, ask: (f) => failing(f, () => get(f, "/api/v1/user", { cookie: f.cookie })) },
144167 { 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]) {
185208
186209 test("the table holds every kind the issue names", () => {
187210 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);
189212 const kinds = ROWS.map((r) => r.kind);
190213 for (const k of ["page", "JSON API answer", "404 page", "500 page", "redirect, form sign-in"]) expect(kinds).toContain(k);
191214 for (const k of ["MCP reply", "SSE stream", "git smart-http, refs advertisement"]) expect(kinds).toContain(k);
192215 expect(kinds.some((k) => k.startsWith("static asset"))).toBe(true);
216 expect(kinds.filter((k) => k.startsWith("media")).length).toBeGreaterThanOrEqual(3);
193217 });
194218
195219 test("a row that answered as another kind is named", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs