// A WRITE FROM THIS APP'S OWN PAGES, NOT ANOTHER SITE'S (umtool redesign L3 review): a plain form POST (the Open // in draft page's button) carries no token, so a page on another site could post the same form. The browser says // where a request comes from -- `Sec-Fetch-Site`, and `Origin` on a POST -- and a write that says another site is // refused. A terminal or an agent (curl, the CLI, Playwright's request) sends neither and is not refused here // (its Authorization is lib/report/caller.ts's business). // // Plain ESM so `node --test` reads it. // DNS REBINDING (lane palette review): a page on evil.example whose name is re-pointed at 127.0.0.1 is // "same-origin" with itself and sends `Host: evil.example:3050`. So the host the request names -- Host, and // X-Forwarded-Host when a proxy (or the page) sets one -- and the Origin's host must each be THIS machine // (localhost, *.localhost, 127.x, ::1) or a name listed in UMTOOL_ALLOWED_HOSTS (comma-separated host names, // no ports: the names a proxy in front of umtool serves it as; docs/knobs.md, "⌘K commands"). const LOOPBACK = new Set(["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1"]); /** A Host-header value's name, without its port or brackets, lower case. */ export function hostNameOf(value) { const v = String(value ?? "").trim().toLowerCase(); if (v.startsWith("[")) return v.slice(1, v.indexOf("]") > 0 ? v.indexOf("]") : undefined); return v.split(":")[0]; } /** Whether a host name is this machine, or one the operator lists. */ export function hostAllowed(name, env = process.env) { const h = hostNameOf(name); if (!h) return false; if (LOOPBACK.has(h) || h.endsWith(".localhost") || /^127(\.\d{1,3}){3}$/.test(h)) return true; const listed = String(env.UMTOOL_ALLOWED_HOSTS ?? "").split(",").map((x) => hostNameOf(x)).filter(Boolean); return listed.includes(h); } const notHere = (h) => `refused: a request to ${hostNameOf(h)} -- umtool answers as localhost, or a name in UMTOOL_ALLOWED_HOSTS`; /** * The sentence to refuse with, or null for a same-origin (or non-browser) request to a host that is * this machine (or listed). * @param {{ get(name: string): string | null }} headers * @param {Record} [env] */ export function crossSiteRefusal(headers, env = process.env) { for (const k of ["host", "x-forwarded-host"]) { for (const h of String(headers.get(k) ?? "").split(",").map((x) => x.trim()).filter(Boolean)) { if (!hostAllowed(h, env)) return notHere(h); } } const site = (headers.get("sec-fetch-site") ?? "").trim().toLowerCase(); if (site && site !== "same-origin" && site !== "none") return `refused: a ${site} request may not write here`; const origin = (headers.get("origin") ?? "").trim(); if (!origin) return null; if (origin === "null") return "refused: a request from an opaque origin may not write here"; const host = (headers.get("x-forwarded-host") ?? headers.get("host") ?? "").split(",")[0].trim().toLowerCase(); let from = ""; try { from = new URL(origin).host.toLowerCase(); } catch { return "refused: the request's origin does not parse"; } if (host && from !== host) return `refused: a request from ${from} may not write to ${host}`; return hostAllowed(from, env) ? null : notHere(from); }