import { test, expect } from "@playwright/test"; import * as urls from "./urls"; // EVERY API WRITE IS REFUSED CROSS-SITE (umtool/proxy.ts, lib/report/writeGuard.mjs): the proxy, under the server // the suite runs (`next start` by default), answers another site's write 403 before the route reads its body -- // here on /api/projects/new, a route with no per-route check of its own. `{}` names no kind, so a write that does // get through is refused by the route (400) and writes nothing. const said = async (r: { json(): Promise }) => ((await r.json()) as { error?: string }).error ?? ""; test("another site's write is refused by the proxy; a terminal's, a read and the app's own page are not", async ({ page, request }) => { // A text/plain POST, the kind no browser preflights. const plain = { "content-type": "text/plain" }; let r = await request.post("/api/projects/new", { data: "{}", headers: { ...plain, origin: "https://evil.example" } }); expect(r.status()).toBe(403); expect(await said(r)).toContain("evil.example"); r = await request.post("/api/projects/new", { data: "{}", headers: { ...plain, "sec-fetch-site": "cross-site" } }); expect(r.status()).toBe(403); expect(await said(r)).toContain("cross-site"); r = await request.post("/api/projects/new", { data: "{}", headers: { ...plain, "sec-fetch-site": "same-site", origin: "http://localhost:3001" } }); expect(r.status(), "the editor's page").toBe(403); for (const method of ["put", "patch", "delete"] as const) { r = await request[method]("/api/projects/new", { data: "{}", headers: { ...plain, origin: "https://evil.example" } }); expect(r.status(), method).toBe(403); } // DNS rebinding: the page's own origin, but a name that is not this machine. r = await request.post("/api/projects/new", { data: "{}", headers: { ...plain, origin: "http://rebind.example", "x-forwarded-host": "rebind.example", "sec-fetch-site": "same-origin" } }); expect(r.status()).toBe(403); expect(await said(r)).toContain("UMTOOL_ALLOWED_HOSTS"); // Another spelling of the path never reaches the route unjudged. for (const p of ["/%61pi/projects/new", "/api/projects/new/", "/api//projects/new", "/api/projects/%6eew"]) { r = await request.post(p, { data: "{}", headers: { ...plain, origin: "https://evil.example" }, maxRedirects: 0 }); expect([403, 404, 307, 308], `${p}: ${r.status()} ${await r.text()}`).toContain(r.status()); } // A terminal (no Origin, no Sec-Fetch-Site): the route answers. r = await request.post("/api/projects/new", { data: "{}", headers: plain }); expect(r.status()).toBe(400); expect(await said(r)).toContain("unknown kind"); // A JSON body over Next's 10 MB default and under the proxy's cap (16 MB, next.config.ts proxyClientMaxBodySize) // arrives whole through the proxy: cut, it would not parse ("bad json"). r = await request.post("/api/projects/new", { data: JSON.stringify({ kind: "", pad: "x".repeat(12 * 1024 * 1024) }), headers: plain }); expect(r.status()).toBe(400); expect(await said(r)).toContain("unknown kind"); // Over the cap: answered 413 with its sentence (writeGuard.mjs bodyRefusal), never handed to the route cut. r = await request.post("/api/projects/new", { data: JSON.stringify({ kind: "", pad: "x".repeat(20 * 1024 * 1024) }), headers: plain }); expect(r.status()).toBe(413); expect(await said(r)).toContain("over the 16777216 (16 MB)"); // A read from anywhere is not the proxy's business. r = await request.get("/api/projects/new?from=", { headers: { origin: "https://evil.example", "sec-fetch-site": "cross-site" } }); expect(r.status()).not.toBe(403); // The app's own page: a real browser's same-origin write carries Origin and Sec-Fetch-Site, and passes. await page.goto(urls.homeHref()); const fromPage = await page.evaluate(async () => { const res = await fetch("/api/projects/new", { method: "POST", headers: { "content-type": "application/json" }, body: "{}" }); return { status: res.status, body: (await res.json()) as { error?: string } }; }); expect(fromPage.status).toBe(400); expect(fromPage.body.error).toContain("unknown kind"); }); // THE UPLOADS ARE OUTSIDE THE PROXY (writeGuard.mjs EXEMPT): a body the proxy matches is held in memory and cut at its // cap, so a recording's routes are left out of the matcher and refuse another site themselves, before the body. const EXEMPT = ["/api/report/voices", "/api/report/post-narration", "/api/report/post-narration/record"]; const NO_PROJECT = "reports/no-such-project"; test("the upload routes are outside the proxy: another site is refused by the route itself, and a 20 MB recording arrives whole", async ({ request }) => { for (const p of EXEMPT) { let r = await request.post(p, { data: "{}", headers: { "content-type": "text/plain", origin: "https://evil.example" } }); expect(r.status(), p).toBe(403); expect(await said(r), p).toContain("evil.example"); // The attack's own shape: a multipart form, which no browser preflights either. r = await request.post(p, { multipart: { project: NO_PROJECT, part: { name: "p1.webm", mimeType: "audio/webm", buffer: Buffer.alloc(1024) } }, headers: { "sec-fetch-site": "cross-site" } }); expect(r.status(), `${p} multipart`).toBe(403); } // 20 MB, over the proxy's 16 MB cap: through the proxy it would reach the route cut, and not parse ("bad form"). const big = Buffer.alloc(20 * 1024 * 1024, 1); const r = await request.post("/api/report/voices", { multipart: { project: NO_PROJECT, op: "recording", item: "x", file: { name: "take.wav", mimeType: "audio/wav", buffer: big } }, }); expect(r.status()).toBe(404); expect(await said(r)).toContain("no such project"); }); test("a GET that writes the manifest (a TTS job followed to done) is refused cross-site; the page's own poll is not", async ({ request }) => { const q = `/api/report/post-narration?project=${encodeURIComponent("reports/post-narration-fixture")}&id=no-such-post&job=no-such-job&file=x`; let r = await request.get(q, { headers: { "sec-fetch-site": "cross-site" } }); expect(r.status()).toBe(403); r = await request.get(q, { headers: { "sec-fetch-site": "same-origin" } }); expect(r.status()).toBe(404); expect(await said(r)).toContain("no post item"); });