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

Tab forge: making an app is refused ("not from this forge's own page") #18267

opened this in answer to

Merged 5h 14m ago, live in production

scout/forge:scout/18258-tab-forge-making-an-app-is-refused-not-f → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/18258-tab-forge-making-an-app-is-refused-not-f was merged into main as b869757.

Review

Sign in to review this change.
5 files changed +59 −4
docs/features/tab.md +4
@@ -66,6 +66,10 @@ Tests: `test/tab-fill.test.ts` runs a stand-in model through a good answer, inva
6666
6767 The runtime is the browser's own Prompt API: it adds no dependency, no weights hosted or proxied by this forge, takes a JSON schema as a constraint, and keeps the prompt on the device. WebLLM and wllama were not chosen: they need their weights and runtime from a third-party host, which this page's policy (`connect-src 'self'`) does not allow and which would be a request leaving the device. **Not measured:** download size, time to first app and memory on a phone-class device. The run that built this had no device, no GPU and no route to a model, and a figure written without one would be invented. The "about 2 GB" on the page is the browser's own figure for its model, not ours. What a person with a phone gets today: most phones lack the Prompt API, so the tier is not offered and the compiler writes the app, with no download and no model memory. Where the API exists, the numbers have to come from a real device before this tier is relied on.
6868
69 ## A change refused in the tab forge
70
71 Every change in the tab forge (new app, edit, issue, settings) is a form post the worker accepts only from the forge's own page. If one is refused it says nothing was saved and to go back to the tab forge and send it from there.
72
6973 ## The whole tab (#18259)
7074
7175 Once started, the forge is the tab: its frame covers the viewport (`html[data-running]`, `src/web/style/content.ts`), with no page around it. Its shell carries one quiet line, *This forge lives in this browser… Export*, and the pages below (`src/tab/whole.ts`, `src/tab/coverage.ts`, words in `TAB_WHOLE`, said again by the terminal's entry, `src/tui/screens/tab.ts`):
src/tab/sw.ts +2 −2
@@ -4,7 +4,7 @@
44 * With that page closed there is nobody to ask, and it says so: the forge has stopped.
55 */
66 import type { TabReply } from "./forge.ts";
7 import { forgeMessage, fromOwnPage, PREFIX, readForm, REFUSED_WRITE, replyOf } from "./wire.ts";
7 import { forgeMessage, ownPage, PREFIX, readForm, REFUSED_WRITE, replyOf } from "./wire.ts";
88
99 interface Client {
1010 readonly url: string;
@@ -29,7 +29,7 @@ async function answer(request: Request): Promise<Response> {
2929 const url = new URL(request.url);
3030 let form: Record<string, string> = {};
3131 if (request.method === "POST") {
32 if (!fromOwnPage(request.headers.get("origin"), request.referrer, sw.location.origin)) return replyOf({ status: 403, html: REFUSED_WRITE });
32 if (!ownPage(request.headers, sw.location.origin, request.referrer)) return replyOf({ status: 403, html: REFUSED_WRITE });
3333 const read = await readForm(request);
3434 if (read === null) return replyOf({ status: 413, html: "Too large." });
3535 form = read;
src/tab/wire.ts +11
@@ -10,6 +10,17 @@ export const PREFIX = "/tab/f";
1010
1111 const CSP = "default-src 'none'; style-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'self'";
1212
13 /**
14 * Whether a write comes from this forge's own page, by either sign a browser gives: the referrer
15 * (see fromOwnPage), or, when Origin is `null` or missing, `Sec-Fetch-Site` (set by the browser,
16 * never by a page), where only `same-origin` passes. An Origin naming another site is refused.
17 */
18 export function ownPage(headers: Headers, origin: string, referrer = ""): boolean {
19 if (fromOwnPage(headers.get("origin"), referrer, origin)) return true;
20 const sent = headers.get("origin");
outdated

Correct as written. Add a test through the worker's real POST path (sw.ts), not only this function, so that dropping the call in sw.ts fails a test.

21 return (sent === null || sent === "null") && headers.get("sec-fetch-site") === "same-origin";
22 }
outdated

Add a real-browser check that a form posted from a tab page reaches this guard with the origin it expects. The unit tests use hand-built Headers and could not have caught the original bug.

23
1324 /** The reply as a Response: a page, a redirect, or a file to keep. */
1425 export function replyOf(r: TabReply): Response {
1526 const headers: Record<string, string> = {
test/tab-host-visible.test.ts +16 −1
@@ -15,7 +15,7 @@ test("the service worker asks windows it does not control, so the /tab host is f
1515 clients: { matchAll: async (o: { includeUncontrolled?: boolean }) => (o.includeUncontrolled === true ? [host] : []), claim: async () => {} },
1616 skipWaiting: async () => {},
1717 addEventListener(type: string, l: any) {
18 if (type === "fetch") listener = l;
18 if (type === "fetch") (globalThis as any).self.__fetch = listener = l;
1919 },
2020 };
2121 await import("../src/tab/sw.ts");
@@ -34,3 +34,18 @@ test("activated settles when the worker activates, without a controlled page", a
3434 await done;
3535 await expect(activated({ installing: null, waiting: null, active: { state: "activated", addEventListener() {} } })).resolves.toBeUndefined();
3636 });
37

This drives the worker's fetch handler with a synthetic Request. It guards the code, but it is not the browser path. Box 4 still needs a real service worker and a real form post.

38 test("the worker's POST path refuses a cross-site write with the new words, and takes the page's own (#18258)", async () => {
39 const listener = (globalThis as any).self.__fetch as (e: { request: Request; respondWith(r: Promise<Response>): void }) => void;
40 const post = async (headers: Record<string, string>): Promise<Response> => {
41 let out: Promise<Response> | undefined;
42 listener({ request: new Request("https://forge.test/tab/f/new", { method: "POST", headers, body: "name=a" }), respondWith: (r) => (out = r) });
43 return out!;
44 };
45 const refused = await post({ origin: "https://evil.test", "sec-fetch-site": "cross-site", "content-type": "application/x-www-form-urlencoded" });
46 expect(refused.status).toBe(403);
47 expect(await refused.text()).toContain("send it from there");
48 expect((await post({ origin: "null", "sec-fetch-site": "cross-site", "content-type": "application/x-www-form-urlencoded" })).status).toBe(403);
49 expect((await post({ origin: "https://forge.test", "content-type": "application/x-www-form-urlencoded" })).status).toBe(200);
50 expect((await post({ origin: "null", "sec-fetch-site": "same-origin", "content-type": "application/x-www-form-urlencoded" })).status).toBe(200);
51 });
test/tab.test.ts +26 −1
@@ -11,7 +11,7 @@ import { join } from "node:path";
1111 import { createApp, type App } from "../src/app.ts";
1212 import { serve, type Servers } from "../src/http/server.ts";
1313 import { handle, migrate, type TabDb } from "../src/tab/forge.ts";
14 import { forgeMessage, readForm, replyOf } from "../src/tab/wire.ts";
14 import { forgeMessage, ownPage, readForm, REFUSED_WRITE, replyOf } from "../src/tab/wire.ts";
1515 import { RECORDS_CAP } from "../src/tab/spec.ts";
1616 import { TAB_FACTS, TAB_MOVE, tabLine } from "../src/tab/words.ts";
1717 import { Session } from "../src/tui/session.ts";
@@ -234,3 +234,28 @@ describe("what the server holds of it", () => {
234234 expect((await fetch(`${servers.url}/tab/nothing.js`)).status).toBe(404);
235235 });
236236 });
237
238 describe("the tab forge's own-page guard (#18258)", () => {
239 const own = "https://forge.test";
240 const h = (o: Record<string, string>) => new Headers(o);
241 test("its own origin passes; another origin never does, whatever Sec-Fetch-Site says", () => {
242 expect(ownPage(h({ origin: own }), own)).toBe(true);
243 expect(ownPage(h({ origin: "https://evil.test", "sec-fetch-site": "same-origin" }), own)).toBe(false);
244 });
245 test("a null or missing Origin passes only when the browser says same-origin", () => {
246 expect(ownPage(h({ origin: "null", "sec-fetch-site": "same-origin" }), own)).toBe(true);
247 expect(ownPage(h({ "sec-fetch-site": "same-origin" }), own)).toBe(true);
248 expect(ownPage(h({ origin: "null", "sec-fetch-site": "cross-site" }), own)).toBe(false);
249 expect(ownPage(h({ origin: "null" }), own)).toBe(false);
250 expect(ownPage(h({}), own)).toBe(false);
251 });
252 test("a null or missing Origin also passes on the forge's own referrer, never on a foreign one", () => {
253 expect(ownPage(h({}), own, `${own}/tab/f/new`)).toBe(true);
254 expect(ownPage(h({ origin: "null" }), own, "https://evil.test/x")).toBe(false);
255 expect(ownPage(h({ origin: "https://evil.test" }), own, `${own}/tab/f/new`)).toBe(false);
256 });
257 test("the pages are sent with a referrer policy that keeps Origin on their own forms, and the refusal says what to do", () => {
258 expect(replyOf({ status: 200, html: "x" }).headers.get("referrer-policy")).toBe("same-origin");
259 expect(REFUSED_WRITE).toContain("send it from there");
260 });
261 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs