// Writes from another site are refused (sameOrigin.mjs): the Open in draft form and Save version. import assert from "node:assert/strict"; import test from "node:test"; import { crossSiteRefusal } from "./sameOrigin.mjs"; const h = (o) => ({ get: (k) => o[k.toLowerCase()] ?? null }); test("crossSiteRefusal: same-origin and non-browser requests pass; another site, a sibling site and an opaque origin do not", () => { assert.equal(crossSiteRefusal(h({})), null, "curl, the CLI, Playwright's request"); assert.equal(crossSiteRefusal(h({ "sec-fetch-site": "same-origin", origin: "http://localhost:3050", host: "localhost:3050" })), null); assert.equal(crossSiteRefusal(h({ "sec-fetch-site": "none" })), null, "typed into the address bar"); assert.match(String(crossSiteRefusal(h({ "sec-fetch-site": "cross-site", origin: "https://evil.example", host: "localhost:3050" }))), /cross-site/); assert.match(String(crossSiteRefusal(h({ "sec-fetch-site": "same-site", origin: "http://localhost:3001", host: "localhost:3050" }))), /same-site/); assert.match(String(crossSiteRefusal(h({ origin: "https://evil.example", host: "localhost:3050" }))), /evil\.example/); assert.match(String(crossSiteRefusal(h({ origin: "null", host: "localhost:3050" }))), /opaque/); assert.equal(crossSiteRefusal(h({ origin: "https://umtool.lan", host: "127.0.0.1:3050", "x-forwarded-host": "umtool.lan" }), { UMTOOL_ALLOWED_HOSTS: "umtool.lan" }), null, "behind the proxy, its name listed"); }); test("crossSiteRefusal: DNS rebinding -- a host that is not this machine, or listed, is refused, Origin or not", () => { const env = {}; assert.match(String(crossSiteRefusal(h({ host: "evil.example:3050", origin: "http://evil.example:3050", "sec-fetch-site": "same-origin" }), env)), /evil\.example/); assert.match(String(crossSiteRefusal(h({ host: "evil.example:3050" }), env)), /evil\.example/, "no Origin: the Host alone"); assert.match(String(crossSiteRefusal(h({ host: "localhost:3050", "x-forwarded-host": "evil.example:3050", origin: "http://evil.example:3050" }), env)), /evil\.example/, "a forwarded host is checked too"); assert.match(String(crossSiteRefusal(h({ origin: "https://umtool.lan", host: "127.0.0.1:3050", "x-forwarded-host": "umtool.lan" }), env)), /umtool\.lan/, "a proxy's name, unlisted"); for (const host of ["localhost:3050", "127.0.0.1:3050", "[::1]:3050", "umtool.localhost:3050"]) { assert.equal(crossSiteRefusal(h({ host, origin: `http://${host}`, "sec-fetch-site": "same-origin" }), env), null, host); } assert.equal(crossSiteRefusal(h({ host: "box.lan:3050", origin: "http://box.lan:3050" }), { UMTOOL_ALLOWED_HOSTS: "other, BOX.lan" }), null, "listed, any case, ports aside"); });