Public: anyone can read this; the crew can change it.

A response of `/` that arrives without Content-Security-Policy is found and a test pins every response path to the floor #12775

Merged opened this

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

Merged

scout/forge:scout/12753-a-response-of-that-arrives-without-conte → main 4 commits fork 3 threads · 3 unresolved

  • Merged. scout/12753-a-response-of-that-arrives-without-conte was merged into main as 7f032a3.

Review

Sign in to review this change.
2 files changed +244 −16
src/apps/proxy.ts +27 −16
@@ -1267,11 +1267,10 @@ export async function proxyApp(
12671267 const admitted = admit(runtime, request, target);
12681268 const sessionMs = performance.now() - admitting;
12691269 // A refused answer names no phases: wake runs before `admit`, so its time
1270 // would tell a caller who may not read the app whether it was asleep.
1271 if (!admitted.ok)
1272 return url.pathname.startsWith("/__forge/")
1273 ? securityFloor(admitted.error)
1274 : admitted.error;
1270 // would tell a caller who may not read the app whether it was asleep. It
1271 // is the forge's answer, never the app's, so it carries the floor: `/` of
1272 // a private app (a 303 to sign in, a 404) left without it (#12753).
1273 if (!admitted.ok) return securityFloor(admitted.error);
12751274 const { status, port, grant: vouched } = admitted.value;
12761275 runtime.touch(target.app, target.env);
12771276 if (readsForgeTiming(runtime, target, vouched?.who)) forgeReaders.add(request);
@@ -1926,17 +1925,30 @@ ${runLink}
19261925 <p>${exits.join(" · ")}</p>
19271926 ${report ? '<p><a href="/__forge/report">Report a problem</a> to the people who keep it; no account needed.</p>' : ""}
19281927 `;
1929 return new Response(html, {
1930 status: deploying ? 503 : gone || absent ? 404 : 502,
1931 headers: {
1932 "content-type": "text/html; charset=utf-8",
1933 "cache-control": "no-store",
1934 ...(deploying ? { "retry-after": "3" } : {}),
1935 ...(report ? { link: links.join(", ") } : {}),
1936 },
1937 });
1928 // The forge's own page, not the app's: it leaves past the server's floor
1929 // with the app's answers (`proxyApp`, `relayAnswer`), so it carries the
1930 // floor itself, with the policy its markup needs: inline style, links, a
1931 // meta refresh, no script. A forge run as an app shows this for its own
1932 // `/` while it restarts: the `/` that left with no policy (#12753).
1933 return securityFloor(
1934 new Response(html, {
1935 status: deploying ? 503 : gone || absent ? 404 : 502,
1936 headers: {
1937 "content-type": "text/html; charset=utf-8",
1938 "cache-control": "no-store",
1939 "content-security-policy": HOLDING_POLICY,
1940 ...(deploying ? { "retry-after": "3" } : {}),
1941 ...(report ? { link: links.join(", ") } : {}),
1942 },
1943 }),
1944 );
19381945 }
19391946
1947 /** The policy of the forge's own holding pages: their inline style, nothing that runs. */
1948 const HOLDING_POLICY =
1949 "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'; " +
1950 "base-uri 'none'; form-action 'none'";
1951
19401952 /** The short page an archived repository's app answers with: 503, not a 502. */
19411953 function archivedPage(target: AppTarget): Response {
19421954 const name = escape(target.app.slug);
@@ -1960,8 +1972,7 @@ function archivedPage(target: AppTarget): Response {
19601972 "cache-control": "no-store",
19611973 "x-content-type-options": "nosniff",
19621974 "referrer-policy": "same-origin",
1963 "content-security-policy":
1964 "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'",
1975 "content-security-policy": HOLDING_POLICY,
19651976 },
19661977 });
19671978 }
test/security-floor-paths.test.ts added +217
@@ -0,0 +1,217 @@
1 /**
2 * `/` through every path an answer to it can take, each leaving with the
3 * security floor (#12753): the first request of a cold process, a compressed
4 * page, HEAD and a 304 from the forge, and `/` of an app through the proxy,
5 * whose answers leave past the server's floor (`proxyApp` is returned as it
6 * is): the holding page of an app that is not running, starting, or stopped
7 * answering mid-request (`relayAnswer`), the archived page, a private app's
8 * 303 to sign in and its 404, and the proxy's own 304 of an app's page.
9 *
10 * The cause: `/` of an app that is not answering is `unavailable()` in
11 * apps/proxy.ts, and it left with no policy, no nosniff and no
12 * referrer-policy. Production is an app of its parent forge, so while it
13 * restarts its parent answers its `/` with that page: the one run of
14 * `scripts/v1-proven.ts` that found no policy on `/` met it. The test below
15 * that asks `/` of a stopped app failed on the parent before the fix.
16 */
17 import { afterAll, beforeAll, expect, test } from "bun:test";
18 import { newApp } from "../src/actions/apps.ts";
19 import type { EnvStatus } from "../src/apps/runtime.ts";
20 import type { AppRecord } from "../src/core/apps.ts";
21 import { line, prove } from "../src/core/v1_proof.ts";
22 import {
23 fetchAsHost,
24 startAppsForge,
25 stopAppsForge,
26 type AppsForge,
27 } from "./helpers/apps-forge.ts";
28 import { quietLogs } from "./helpers/globals.ts";
29
30 quietLogs();
31
32 // The commit the forge names in /llms.txt: a deploy stamps it, a test does here.
33 const stamped = process.env["FORGE_COMMIT"];
34 let forge: AppsForge;
35 let upstream: ReturnType<typeof Bun.serve> | undefined;
36
37 /** Each app's status as the test says it is: the runtime itself starts nothing. */
38 const statuses = new Map<string, EnvStatus | undefined>();
39
40 async function appNamed(
41 name: string,
42 visibility: "public" | "private",
43 ): Promise<AppRecord> {
44 const made = await newApp(forge.app, forge.alice, { name, visibility });
45 if (!made.ok) throw new Error(String(made.error));
46 return made.value.app;
47 }
48
49 beforeAll(async () => {
50 process.env["FORGE_COMMIT"] = "a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6a7b8c9d0";
51 forge = await startAppsForge({ start: false });
52 const real = forge.runtime.envStatus.bind(forge.runtime);
53 forge.runtime.envStatus = (record, env) =>
54 statuses.has(record.slug) ? statuses.get(record.slug) : real(record, env);
55 }, 30_000);
56
57 afterAll(async () => {
58 upstream?.stop(true);
59 if (forge !== undefined) {
60 await forge.runtime.stop();
61 await stopAppsForge(forge);
62 }
63 if (stamped === undefined) delete process.env["FORGE_COMMIT"];
64 else process.env["FORGE_COMMIT"] = stamped;
65 });
66
67 function floorOf(res: Response): Record<string, string | null> {
68 const csp = res.headers.get("content-security-policy") ?? "";
69 return {
70 csp: /default-src/.test(csp) ? "set" : null,
71 nosniff: res.headers.get("x-content-type-options"),
72 referrer: res.headers.get("referrer-policy") === null ? null : "set",
73 };
74 }
75 const FLOOR = { csp: "set", nosniff: "nosniff", referrer: "set" };
76
77 const home = (init: RequestInit = {}) =>
78 fetch(`${forge.servers.url}/`, {
79 redirect: "manual",
80 signal: AbortSignal.timeout(10_000),
81 ...init,
82 });
83
84 /** `/` of the app `slug`, as a person's browser asks for it. */
outdated

This calls unavailable() directly. Send / through the proxy relay path as well, so the path that leaked is exercised end to end, and cover the archived-app page too.

85 const appHome = (slug: string, headers: Record<string, string> = {}) =>
86 fetchAsHost(forge.servers, `${slug}.apps.test`, "/", {
87 headers: { accept: "text/html", ...headers },
88 signal: AbortSignal.timeout(10_000),
89 });
90
91 // First: nothing else has been asked of this process yet.
92 test("the first `/` of a cold process carries the floor", async () => {
93 const res = await home({ headers: { "accept-encoding": "identity" } });
94 await res.text();
95 expect(res.status).toBe(200);
96 expect(floorOf(res)).toEqual(FLOOR);
97 });
98
99 test("`/` compressed, as HEAD, and as a 304 carries the floor", async () => {
100 const gz = await home({ headers: { "accept-encoding": "gzip, br" } });
101 const etag = gz.headers.get("etag");
102 await gz.arrayBuffer();
103 expect(gz.status).toBe(200);
104 expect(floorOf(gz)).toEqual(FLOOR);
105
106 const head = await home({ method: "HEAD" });
107 expect(head.status).toBe(200);
108 expect(floorOf(head)).toEqual(FLOOR);
109
110 expect(etag).not.toBeNull();
111 const again = await home({ headers: { "if-none-match": etag! } });
112 await again.arrayBuffer();
113 expect(again.status).toBe(304);
114 expect(floorOf(again)).toEqual(FLOOR);
115 });
116
117 test("`/` of an app that is not deployed, starting, or stopped answering carries the floor", async () => {
118 const record = await appNamed("lab", "public");
119 // A port nothing listens on: the proxy's fetch fails, and the holding
120 // page goes back through `relayAnswer`.
outdated

Only two probes are asserted, so issue box 3 (v1-proven passing 20 times) is not met. Make the full proof pass here, or get the box reworded.

121 const dead = Bun.serve({ port: 0, fetch: () => new Response("") });
122 const deadPort = dead.port!;
123 dead.stop(true);
124 const base = { env: "production", commitOid: "0".repeat(40), url: "" } as const;
125 const cases: [EnvStatus | undefined, number][] = [
126 [undefined, 502],
127 [{ ...base, state: "deploying" } as EnvStatus, 503],
128 [{ ...base, state: "running", port: deadPort } as EnvStatus, 502],
129 ];
130 for (const [status, code] of cases) {
131 statuses.set(record.slug, status);
132 const res = await appHome(record.slug);
133 const body = await res.text();
134 expect(res.status).toBe(code);
135 expect(body).toContain("lab");
136 expect(floorOf(res)).toEqual(FLOOR);
137 // Its inline style still applies, and nothing in it can run.
138 const csp = res.headers.get("content-security-policy")!;
139 expect(csp).toContain("style-src 'unsafe-inline'");
140 expect(csp).not.toContain("script-src");
141 expect(body).not.toContain("<script");
142 }
143 statuses.delete(record.slug);
144 });
145
146 test("`/` of an archived app carries the floor", async () => {
147 const record = await appNamed("shelved", "public");
148 forge.app.db.run("UPDATE repo SET archived_at = ? WHERE id = ?", [
149 new Date().toISOString(),
150 record.repoId,
151 ]);
152 const res = await appHome(record.slug);
153 expect(await res.text()).toContain("is archived");
154 expect(res.status).toBe(503);
155 expect(floorOf(res)).toEqual(FLOOR);
156 });
157
158 test("`/` of a private app, as a redirect to sign in and as a 404, carries the floor", async () => {
159 const record = await appNamed("hush", "private");
160 const person = await appHome(record.slug);
161 await person.arrayBuffer();
162 expect(person.status).toBe(303);
163 expect(person.headers.get("location")).toContain("/apps/open");
164 expect(floorOf(person)).toEqual(FLOOR);
165
166 const script = await appHome(record.slug, { accept: "application/json" });
167 await script.arrayBuffer();
168 expect(script.status).toBe(404);
169 expect(floorOf(script)).toEqual(FLOOR);
170 });
171
172 test("the proxy's 304 of an app's page keeps the headers the app's 200 left with", async () => {
173 const record = await appNamed("plain", "public");
174 upstream = Bun.serve({
175 port: 0,
176 fetch: () =>
177 new Response("<!doctype html><html><body><h1>hi</h1></body></html>", {
178 headers: {
179 "content-type": "text/html; charset=utf-8",
180 etag: '"v1"',
181 "content-security-policy": "default-src 'self'",
182 "x-content-type-options": "nosniff",
183 "referrer-policy": "no-referrer",
184 },
185 }),
186 });
187 statuses.set(record.slug, {
188 env: "production",
189 state: "running",
190 commitOid: "0".repeat(40),
191 url: "",
192 port: upstream.port,
193 } as EnvStatus);
194 const first = await appHome(record.slug);
195 await first.arrayBuffer();
196 expect(first.status).toBe(200);
197 expect(floorOf(first)).toEqual(FLOOR);
198 const etag = first.headers.get("etag");
199 expect(etag).not.toBeNull();
200
201 const again = await appHome(record.slug, { "if-none-match": etag! });
202 await again.arrayBuffer();
203 expect(again.status).toBe(304);
204 expect(floorOf(again)).toEqual(FLOOR);
205 expect(again.headers.get("content-security-policy")).toBe(
206 first.headers.get("content-security-policy"),
207 );
208 statuses.delete(record.slug);
209 });
210
211 test("`bun scripts/v1-proven.ts`'s proof passes 20 times in a row against a booted forge", async () => {
212 const failed: string[] = [];
213 for (let run = 0; run < 20; run++)
214 for (const r of await prove(forge.servers.url, fetch))
215 if (r.status !== "pass") failed.push(`run ${run}: ${line(r)}`);
216 expect(failed).toEqual([]);
217 }, 60_000);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs