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
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
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 | ||
| 66 | 66 | |
| 67 | 67 | 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. |
| 68 | 68 | |
| 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 | ||
| 69 | 73 | ## The whole tab (#18259) |
| 70 | 74 | |
| 71 | 75 | 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 @@ | ||
| 4 | 4 | * With that page closed there is nobody to ask, and it says so: the forge has stopped. |
| 5 | 5 | */ |
| 6 | 6 | 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"; | |
| 8 | 8 | |
| 9 | 9 | interface Client { |
| 10 | 10 | readonly url: string; |
| @@ -29,7 +29,7 @@ async function answer(request: Request): Promise<Response> { | ||
| 29 | 29 | const url = new URL(request.url); |
| 30 | 30 | let form: Record<string, string> = {}; |
| 31 | 31 | 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 }); | |
| 33 | 33 | const read = await readForm(request); |
| 34 | 34 | if (read === null) return replyOf({ status: 413, html: "Too large." }); |
| 35 | 35 | form = read; |
src/tab/wire.ts +11
| @@ -10,6 +10,17 @@ export const PREFIX = "/tab/f"; | ||
| 10 | 10 | |
| 11 | 11 | const CSP = "default-src 'none'; style-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'self'"; |
| 12 | 12 | |
| 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"); | |
| 21 | return (sent === null || sent === "null") && headers.get("sec-fetch-site") === "same-origin"; | |
| 22 | } | |
| 23 | ||
| 13 | 24 | /** The reply as a Response: a page, a redirect, or a file to keep. */ |
| 14 | 25 | export function replyOf(r: TabReply): Response { |
| 15 | 26 | 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 | ||
| 15 | 15 | clients: { matchAll: async (o: { includeUncontrolled?: boolean }) => (o.includeUncontrolled === true ? [host] : []), claim: async () => {} }, |
| 16 | 16 | skipWaiting: async () => {}, |
| 17 | 17 | addEventListener(type: string, l: any) { |
| 18 | if (type === "fetch") listener = l; | |
| 18 | if (type === "fetch") (globalThis as any).self.__fetch = listener = l; | |
| 19 | 19 | }, |
| 20 | 20 | }; |
| 21 | 21 | await import("../src/tab/sw.ts"); |
| @@ -34,3 +34,18 @@ test("activated settles when the worker activates, without a controlled page", a | ||
| 34 | 34 | await done; |
| 35 | 35 | await expect(activated({ installing: null, waiting: null, active: { state: "activated", addEventListener() {} } })).resolves.toBeUndefined(); |
| 36 | 36 | }); |
| 37 | ||
| 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"; | ||
| 11 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 12 | 12 | import { serve, type Servers } from "../src/http/server.ts"; |
| 13 | 13 | 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"; | |
| 15 | 15 | import { RECORDS_CAP } from "../src/tab/spec.ts"; |
| 16 | 16 | import { TAB_FACTS, TAB_MOVE, tabLine } from "../src/tab/words.ts"; |
| 17 | 17 | import { Session } from "../src/tui/session.ts"; |
| @@ -234,3 +234,28 @@ describe("what the server holds of it", () => { | ||
| 234 | 234 | expect((await fetch(`${servers.url}/tab/nothing.js`)).status).toBe(404); |
| 235 | 235 | }); |
| 236 | 236 | }); |
| 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 | }); |
new above
offline
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.