Public: anyone can read this; the crew can change it.
Every route that changes state refuses a cross-origin request, proven by one table-driven test over the route table #13500
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13480-every-route-that-changes-state-refuses-a → main 2 commits fork
3 files
changed +48
−14
src/http/context.ts +15 −7
| @@ -21,7 +21,10 @@ export interface Auth { | ||
| 21 | 21 | readonly via: "session" | "edge" | "token" | "password" | "none"; |
| 22 | 22 | /** Which signed-in browser asked, when `via` is `session`. */ |
| 23 | 23 | readonly sessionId?: string; |
| 24 | /** A notice from an admin waits for this person; `page()` says it and marks it said (`core/access_link.ts`). */ | |
| 24 | /** | |
| 25 | * A notice from an admin waits for this person; `page()` says it and marks it said | |
| 26 | * (`core/access_link.ts`). | |
| 27 | */ | |
| 25 | 28 | readonly noticeWaiting?: true; |
| 26 | 29 | /** Set for a token: the name its owner gave it, never its secret. */ |
| 27 | 30 | readonly tokenName?: string; |
| @@ -102,7 +105,9 @@ export function publicUrl(request: Request, base: string | undefined): URL { | ||
| 102 | 105 | /** |
| 103 | 106 | * Browser CSRF: a session-cookie mutation must originate from this forge. |
| 104 | 107 | * Browsers always send Origin on cross-site requests; a missing Origin |
| 105 | * means a non-browser client, which cookies alone cannot impersonate. | |
| 108 | * means a non-browser client, which cookies alone cannot impersonate — | |
| 109 | * unless the browser's own `Sec-Fetch-Site` (which a page cannot set) says | |
| 110 | * the request came from another site, which is refused too (#13480). | |
| 106 | 111 | * |
| 107 | 112 | * A mismatch is logged — one line, the two origins, nothing else — because |
| 108 | 113 | * the usual cause is `FORGE_URL` naming somewhere other than where the |
| @@ -111,17 +116,20 @@ export function publicUrl(request: Request, base: string | undefined): URL { | ||
| 111 | 116 | */ |
| 112 | 117 | export function crossOrigin(request: Request, url: URL, log?: Log): boolean { |
| 113 | 118 | const origin = request.headers.get("origin"); |
| 114 | // `Origin: null` with the browser's own `Sec-Fetch-Site: same-origin` is one of our pages under a strict | |
| 115 | // referrer policy; a page cannot set that header, so it stays a cross-site refusal everywhere else. | |
| 119 | // `Origin: null` with the browser's own `Sec-Fetch-Site: same-origin` is one of our pages under a | |
| 120 | // strict referrer policy; a page cannot set that header, so it stays a cross-site refusal | |
| 121 | // everywhere else. | |
| 116 | 122 | const sameSite = origin === "null" && request.headers.get("sec-fetch-site") === "same-origin"; |
| 117 | const mismatch = origin !== null && origin !== url.origin && !sameSite; | |
| 123 | const site = request.headers.get("sec-fetch-site"); | |
| 124 | const foreignBrowser = origin === null && (site === "cross-site" || site === "same-site"); | |
| 125 | const mismatch = foreignBrowser || (origin !== null && origin !== url.origin && !sameSite); | |
| 118 | 126 | if (mismatch) |
| 119 | 127 | log?.warn("refused cross-origin mutation", { |
| 120 | origin, | |
| 128 | origin: origin ?? "-", | |
| 121 | 129 | expected: url.origin, |
| 122 | 130 | method: request.method, |
| 123 | 131 | path: url.pathname, |
| 124 | site: request.headers.get("sec-fetch-site") ?? "-", | |
| 132 | site: site ?? "-", | |
| 125 | 133 | }); |
| 126 | 134 | return mismatch; |
| 127 | 135 | } |
test/consent-origin.test.ts +10 −2
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | * 2026-10-27: every OAuth consent was refused. The consent page sent `Referrer-Policy: no-referrer`, so the | |
| 3 | * browser posted its own form with `Origin: null`, and the CSRF gate read null as another site. | |
| 2 | * 2026-10-27: every OAuth consent was refused. The consent page sent | |
| 3 | * `Referrer-Policy: no-referrer`, so the browser posted its own form with `Origin: null`, and the | |
| 4 | * CSRF gate read null as another site. | |
| 4 | 5 | */ |
| 5 | 6 | import { expect, test } from "bun:test"; |
| 6 | 7 | import { crossOrigin } from "../src/http/context.ts"; |
| @@ -23,6 +24,13 @@ test("another site's origin is refused and our own passes", () => { | ||
| 23 | 24 | expect(crossOrigin(post({}), url)).toBe(false); |
| 24 | 25 | }); |
| 25 | 26 | |
| 27 | test("no Origin from a browser that says another site sent it is refused; from our own page or a tool it passes", () => { | |
| 28 | expect(crossOrigin(post({ "sec-fetch-site": "cross-site" }), url)).toBe(true); | |
| 29 | expect(crossOrigin(post({ "sec-fetch-site": "same-site" }), url)).toBe(true); | |
| 30 | expect(crossOrigin(post({ "sec-fetch-site": "same-origin" }), url)).toBe(false); | |
| 31 | expect(crossOrigin(post({ "sec-fetch-site": "none" }), url)).toBe(false); | |
| 32 | }); | |
| 33 | ||
| 26 | 34 | test("the consent page asks for a same-origin referrer policy, never no-referrer", async () => { |
| 27 | 35 | const src = await Bun.file(new URL("../src/web/pages/oauth.ts", import.meta.url)).text(); |
| 28 | 36 | expect(src).toContain('"referrer-policy", "same-origin"'); |
test/origin-every-mutation.test.ts +23 −5
| @@ -55,7 +55,10 @@ const mutations = (): { method: string; pattern: string }[] => | ||
| 55 | 55 | [...buildRouter().routes(), ...buildApiRouter().routes()].filter((r) => MUTATING.has(r.method)); |
| 56 | 56 | |
| 57 | 57 | let dataDir = "", app: App, cookie = ""; |
| 58 | /** How many times an origin check has refused a request, read from the log line `crossOrigin` writes. */ | |
| 58 | /** | |
| 59 | * How many times an origin check has refused a request, read from the log line `crossOrigin` | |
| 60 | * writes. | |
| 61 | */ | |
| 59 | 62 | let originRefusals = 0; |
| 60 | 63 | const cells = process.env["FORGE_CELLS"]; |
| 61 | 64 | |
| @@ -94,8 +97,12 @@ afterAll(async () => { | ||
| 94 | 97 | |
| 95 | 98 | const events = (): number => (app.db.query("select count(*) as n from event").get() as { n: number }).n; |
| 96 | 99 | |
| 97 | async function send(r: { method: string; pattern: string }, origin: string | undefined): Promise<number> { | |
| 98 | const headers: Record<string, string> = { cookie, "content-type": "application/json" }; | |
| 100 | async function send( | |
| 101 | r: { method: string; pattern: string }, | |
| 102 | origin: string | undefined, | |
| 103 | extra: Record<string, string> = {}, | |
| 104 | ): Promise<number> { | |
| 105 | const headers: Record<string, string> = { cookie, "content-type": "application/json", ...extra }; | |
| 99 | 106 | if (origin !== undefined) headers["origin"] = origin; |
| 100 | 107 | const res = await forgeHttp(app).handle( |
| 101 | 108 | new Request(`http://forge.test${fill(r.pattern)}`, { method: r.method, headers, body: "{}" }), |
| @@ -104,7 +111,18 @@ async function send(r: { method: string; pattern: string }, origin: string | und | ||
| 104 | 111 | return res.status; |
| 105 | 112 | } |
| 106 | 113 | |
| 107 | test("every mutating route refuses a signed-in request from a foreign Origin, writing nothing", async () => { | |
| 114 | /** | |
| 115 | * The two shapes a forged browser request takes: a foreign Origin, and no | |
| 116 | * Origin at all with the browser's own `Sec-Fetch-Site: cross-site` (a header | |
| 117 | * a page cannot set or drop). A route added to either router is walked here | |
| 118 | * the moment it is registered, so a new unlisted mutation without a check fails. | |
| 119 | */ | |
| 120 | const FORGED: [string, string | undefined, Record<string, string>][] = [ | |
| 121 | ["a foreign Origin", "https://evil.example", {}], | |
| 122 | ["no Origin, from another site's page", undefined, { "sec-fetch-site": "cross-site" }], | |
| 123 | ]; | |
| 124 | ||
| 125 | test.each(FORGED)("every mutating route refuses a signed-in request with %s, writing nothing", async (_, origin, extra) => { | |
| 108 | 126 | const routes = mutations(); |
| 109 | 127 | expect(routes.length).toBeGreaterThan(100); |
| 110 | 128 | const wrong: string[] = []; |
| @@ -112,7 +130,7 @@ test("every mutating route refuses a signed-in request from a foreign Origin, wr | ||
| 112 | 130 | if (key(r) in EXEMPT || key(r) in UNCALLABLE) continue; |
| 113 | 131 | const before = events(); |
| 114 | 132 | const refusedBefore = originRefusals; |
| 115 | const status = await send(r, "https://evil.example"); | |
| 133 | const status = await send(r, origin, extra); | |
| 116 | 134 | const checked = originRefusals > refusedBefore; |
| 117 | 135 | if (status !== 403 || events() !== before || !checked) |
| 118 | 136 | wrong.push(`${key(r)} → ${status}, ${events() - before} events${checked ? "" : ", no origin check"}`); |
new above
offline