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

Tab forge: a write from the tab's own page passes the same-origin guard, a cross-site write is still refused #18300

opened this

Merged 6h 8m ago, live in production

scout/forge:scout/18294-tab-forge-a-write-from-the-tab-s-own-pag → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/18294-tab-forge-a-write-from-the-tab-s-own-pag was merged into main as a9db6c9.

Review

Sign in to review this change.
3 files changed +84 −3
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, PREFIX, readForm, replyOf } from "./wire.ts";
7 import { forgeMessage, fromOwnPage, 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 (request.headers.get("origin") !== sw.location.origin) return replyOf({ status: 403, html: "Refused: not from this forge's own page." });
32 if (!fromOwnPage(request.headers.get("origin"), request.referrer, sw.location.origin)) 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 +22 −1
@@ -14,7 +14,7 @@ const CSP = "default-src 'none'; style-src 'self'; form-action 'self'; base-uri
1414 export function replyOf(r: TabReply): Response {
1515 const headers: Record<string, string> = {
1616 "content-type": "text/html; charset=utf-8", "cache-control": "no-store",
17 "content-security-policy": CSP, "x-content-type-options": "nosniff", "referrer-policy": "no-referrer",
17 "content-security-policy": CSP, "x-content-type-options": "nosniff", "referrer-policy": "same-origin",
1818 };
1919 if (r.file !== undefined)
2020 return new Response(r.html, { status: r.status, headers: { ...headers, "content-type": `${r.file.type}; charset=utf-8`, "content-disposition": `attachment; filename="${r.file.name}"` } });
@@ -22,6 +22,27 @@ export function replyOf(r: TabReply): Response {
2222 return new Response(r.html, { status: r.status, headers });
2323 }
2424
25 /**
26 * Whether a write came from one of this forge's own pages. A service worker sees a form's request
27 * before the browser adds its Origin header, so a post from the tab's own page usually arrives
28 * with none (or "null"): then the referrer decides, which the tab's pages send (policy
29 * same-origin) and a foreign page cannot set to this origin. An Origin that names another site is
30 * refused whatever the referrer says.
31 */
32 export function fromOwnPage(origin: string | null, referrer: string, own: string): boolean {
33 if (origin === own) return true;
34 if (origin !== null && origin !== "null") return false;
35 try {
36 return new URL(referrer).origin === own;
37 } catch {
38 return false;

Nit: a referrer from a page that set no-referrer arrives as an empty string or about:client. Both throw or fail the origin compare, so both are refused, as the tests show. A one-line comment would save the next reader the check.

39 }
40 }
41
42 /** The refusal of a write from elsewhere, in words that say what to do. */
43 export const REFUSED_WRITE =
44 "<!doctype html><title>Refused</title><p>Refused: this change was sent from a page outside this tab forge, so nothing was saved. Go back to the tab forge, open the page again and send it from there.</p><p><a href=\"/tab\">Open the tab forge</a></p>";
45
2546 /**
2647 * The message the worker posts to the page: the method, the path under the prefix, the query, the
2748 * form.
test/tab-own-page.test.ts added +60
@@ -0,0 +1,60 @@
1 /**
2 * The tab forge's same-origin guard on writes (#18294): a post from the tab's own page passes, a
3 * cross-site one is still refused, and the refusal says what to do.
4 */
5 import { expect, test } from "bun:test";
6 import { fromOwnPage, REFUSED_WRITE, replyOf } from "../src/tab/wire.ts";
7
8 const OWN = "https://forge.test";
9
10 test("a form posted from the tab's own page passes: the worker sees no Origin, only the referrer", () => {
11 // A service worker sees the navigation before the browser adds Origin: this is the shape the old
12 // `origin !== own` line refused.
13 expect(fromOwnPage(null, `${OWN}/tab/f/new`, OWN)).toBe(true);
14 // A browser that does add it, as "null" under a no-referrer page, or as the origin itself.
15 expect(fromOwnPage("null", `${OWN}/tab/f/new`, OWN)).toBe(true);
16 expect(fromOwnPage(OWN, "", OWN)).toBe(true);
17 });
18
19 test("a cross-site write is still refused", () => {
20 expect(fromOwnPage("https://evil.test", "", OWN)).toBe(false);
21 expect(fromOwnPage("https://evil.test", `${OWN}/tab/f/new`, OWN)).toBe(false);
22 expect(fromOwnPage(null, "https://evil.test/post", OWN)).toBe(false);
23 expect(fromOwnPage("null", "", OWN)).toBe(false);
24 expect(fromOwnPage(null, "", OWN)).toBe(false);
25 expect(fromOwnPage(null, "about:client", OWN)).toBe(false);
26 // A look-alike host is another origin.
27 expect(fromOwnPage(null, "https://forge.test.evil.test/tab/f/", OWN)).toBe(false);
28 });
29
30 test("the tab's pages send their referrer to their own origin only, so the guard can read it", () => {
31 expect(replyOf({ status: 200, html: "ok" }).headers.get("referrer-policy")).toBe("same-origin");
32 });
33
34 test("the refusal tells a person what to do, not an internal reason", () => {
35 expect(REFUSED_WRITE).toContain("nothing was saved");
36 expect(REFUSED_WRITE).toContain("send it from there");
37 expect(REFUSED_WRITE).toContain('href="/tab"');
38 expect(REFUSED_WRITE).not.toContain("not from this forge's own page");
39 });
40
41 test("the worker answers a foreign post with that refusal", async () => {
42 let listener: ((e: { request: Request; respondWith(r: Promise<Response>): void }) => void) | undefined;
43 (globalThis as any).self = {
44 location: { origin: OWN },
45 clients: { matchAll: async () => [], claim: async () => {} },
46 skipWaiting: async () => {},
47 addEventListener(type: string, l: any) {
48 if (type === "fetch") listener = l;
49 },
50 };
51 // A fresh copy: another file in the same run may have loaded the worker under its own `self`.
52 const fresh: string = "../src/tab/sw.ts?own-page";
53 await import(fresh);
54 let out: Promise<Response> | undefined;
55 const request = new Request(`${OWN}/tab/f/new`, { method: "POST", headers: { origin: "https://evil.test", "content-type": "application/x-www-form-urlencoded" }, body: "name=x" });
56 listener!({ request, respondWith: (r) => (out = r) });
57 const res = await out!;
58 expect(res.status).toBe(403);
59 expect(await res.text()).toBe(REFUSED_WRITE);
60 });

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

Keyboard shortcuts

Docs