// ONE GUARD IN FRONT OF EVERY WRITE (umtool redesign E4). A route that writes used to refuse another site's page // only when it called crossSiteRefusal itself, and on 10 Nov about 80 of the 141 route files that export a write // did not: a page on any site the operator has open could send a `text/plain` POST (no preflight; the routes read // `request.json()` whatever the type) to localhost:3050 and create or delete projects, start renders, write // manifests. `umtool/proxy.ts` now asks `writeRefusal` before any write under /api/ reaches its handler (EXEMPT's // uploads aside, which refuse cross-site themselves); the per-route calls stay (belt and braces). The proxy's matcher is GUARD_MATCHER, and writeGuard.test.mjs sweeps every // app/**/route.ts that exports a write and checks the proxy covers it. // // WHO IS ASKED. Only a request a browser sent: a browser puts `Origin` on every write (the Fetch standard appends it // to any method but GET/HEAD) and `Sec-Fetch-Site` on every request, and a page can set neither. One carrying // either is judged by crossSiteRefusal in full -- another site, a sibling site (the editor's page on :3001), an // opaque origin, and a Host / X-Forwarded-Host / Origin that is not this machine (DNS rebinding) are refused. A // request with NEITHER (curl, the CLI, an agent, the editor's server with its Bearer) passes here: who may write is // lib/report/caller.ts's business, and its Host is whatever it chose to send, so checking it protects nothing -- // while refusing it would refuse the editor in docker (`UMTOOL_URL=http://umtool:3050`) and an agent on the LAN. // // Plain ESM so `node --test` reads it. import { crossSiteRefusal } from "./sameOrigin.mjs"; // THE UPLOADS ARE OUTSIDE THE PROXY. For a request the proxy matches, Next holds the body twice in memory before // the proxy runs and hands the route a copy cut off at proxyClientMaxBodySize (next.config.ts, kept small so a // cross-site tab cannot make the server buffer gigabytes). A recording uploaded from a page is bigger than any such // cap, so these routes stay out of the matcher. Each one calls crossSiteRefusal ITSELF, as the first thing its write // handler does, before it reads the body (writeGuard.test.mjs checks the order in the source) -- on EVERY request, // not only a browser's as writeRefusal does: one naming a Host or X-Forwarded-Host that is not this machine (nor in // UMTOOL_ALLOWED_HOSTS) is refused even from curl or the editor's server (`Host: umtool:3050` in docker), and so is // a browser's from another site. export const EXEMPT = ["/api/report/voices", "/api/report/post-narration", "/api/report/post-narration/record"]; /** * The proxy's body cap: next.config.ts `proxyClientMaxBodySize: "16mb"` (Next's bytes: 16 × 1024 × 1024), which * writeGuard.test.mjs holds equal. A matched body over it reaches its route CUT at the cap, so it never parses. */ export const BODY_CAP = 16 * 1024 * 1024; /** * The sentence to refuse a write with (413) when its declared length is over BODY_CAP, or null. A body with no * `content-length` (chunked) is not judged here: the route sees it cut, and refuses it as it does any bad body. * @param {string} method * @param {string} pathname * @param {{ get(name: string): string | null }} headers */ export function bodyRefusal(method, pathname, headers) { if (!guardedPath(method, pathname)) return null; const said = (headers.get("content-length") ?? "").trim(); const n = /^\d+$/.test(said) ? Number(said) : NaN; if (!(n > BODY_CAP)) return null; return `the body is ${n} bytes, over the ${BODY_CAP} (16 MB) a write to umtool's API takes -- an upload goes to its own route (a voice, a narration recording)`; } /** * The proxy's matcher: everything under /api/ except EXEMPT. umtool/proxy.ts must say exactly this string, because * Next reads a matcher at build time and it cannot be imported (the sweep test checks). */ export const GUARD_MATCHER = `/api/((?!${EXEMPT.map((p) => `${p.slice("/api/".length)}$`).join("|")}).*)`; const READS = new Set(["GET", "HEAD", "OPTIONS"]); /** Whether a request is a write the guard judges: any method but GET/HEAD/OPTIONS, to /api or below. */ export function guardedPath(method, pathname) { const m = String(method ?? "").toUpperCase(); if (READS.has(m)) return false; // Judged as decoded and lower case, so `/%61pi/…` or `/API/…` is a write too, whatever Next routes it to. let p = String(pathname ?? ""); try { p = decodeURIComponent(p); } catch { /* undecodable: judged as sent */ } p = p.toLowerCase(); return p === "/api" || p.startsWith("/api/"); } /** Whether a request carries what only a browser sends (and a page cannot forge). */ export function fromBrowser(headers) { return Boolean((headers.get("origin") ?? "").trim() || (headers.get("sec-fetch-site") ?? "").trim()); } /** * The sentence to refuse a request with (403), or null to let it through. * @param {string} method * @param {string} pathname * @param {{ get(name: string): string | null }} headers * @param {Record} [env] */ export function writeRefusal(method, pathname, headers, env = process.env) { if (!guardedPath(method, pathname)) return null; if (!fromBrowser(headers)) return null; return crossSiteRefusal(headers, env); }