commit d1da939c66c0bb492f419c7af392fca03c32fe92
parent 337a345ac315783e91d6793340c7ce091e744f1a
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 9 Nov 2026 20:06:32 -0500
umtool: writes from a page only to this machine's names -- DNS rebinding refused (L8a review)
crossSiteRefusal (POST /api/cli, Save version, Open in draft) refuses a Host, X-Forwarded-Host or Origin that is not localhost, *.localhost, 127.x or ::1, unless UMTOOL_ALLOWED_HOSTS lists it.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 50 insertions(+), 4 deletions(-)
diff --git a/umtool/lib/report/sameOrigin.mjs b/umtool/lib/report/sameOrigin.mjs
@@ -6,11 +6,44 @@
//
// 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.
+ * 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<string, string | undefined>} [env]
*/
-export function crossSiteRefusal(headers) {
+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();
@@ -23,5 +56,6 @@ export function crossSiteRefusal(headers) {
} catch {
return "refused: the request's origin does not parse";
}
- return host && from !== host ? `refused: a request from ${from} may not write to ${host}` : null;
+ if (host && from !== host) return `refused: a request from ${from} may not write to ${host}`;
+ return hostAllowed(from, env) ? null : notHere(from);
}
diff --git a/umtool/lib/report/sameOrigin.test.mjs b/umtool/lib/report/sameOrigin.test.mjs
@@ -13,5 +13,17 @@ test("crossSiteRefusal: same-origin and non-browser requests pass; another 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" })), null, "behind the proxy");
+ 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");
});