From 45f9f629d7d7fc7f82a4c6c9052a8723f3316029 Mon Sep 17 00:00:00 2001 From: Dmitry Ryskin Date: Fri, 30 Oct 2026 22:12:31 +0100 Subject: [PATCH] [request-guard] enable bypassing host checks --- docker/Dockerfile.web | 2 + docs/configuration.md | 1 + packages/apps/web/env.example | 3 + packages/apps/web/src/server/request-guard.ts | 67 ++++++++++++------- packages/apps/web/test/request-guard.test.mjs | 36 +++++++++- 5 files changed, 85 insertions(+), 24 deletions(-) diff --git a/docker/Dockerfile.web b/docker/Dockerfile.web index 466b96b..72ac4db 100644 --- a/docker/Dockerfile.web +++ b/docker/Dockerfile.web @@ -13,6 +13,8 @@ # APP_PUBLIC_URL optional expected browser origin for same-origin checks # APP_ALLOWED_HOSTS optional comma-separated extra Host values, for proxies # that rewrite Host +# APP_ALLOW_UNTRUSTED_HOSTS true bypasses Host checks (unsafe: disables +# DNS-rebinding protection); write Origin checks remain # PORT HTTP listen port (default 3000) # HOSTNAME listen address (default 0.0.0.0) # diff --git a/docs/configuration.md b/docs/configuration.md index 0b4ab1a..05a8e9a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -113,3 +113,4 @@ export EXAMPLE_MCP_TOKEN=your-server-token | `RESTATE_AUTH_TOKEN` | Bearer token for an authenticated ingress | | `APP_PUBLIC_URL` | Browser origin when the UI sits behind a proxy; its host becomes the only accepted Host | | `APP_ALLOWED_HOSTS` | Comma-separated extra Host values to accept, for proxies that rewrite Host | +| `APP_ALLOW_UNTRUSTED_HOSTS` | Set exactly `true` to bypass Host restrictions (default off). Disables DNS-rebinding protection; same-origin write checks still apply. Prefer `APP_PUBLIC_URL` / `APP_ALLOWED_HOSTS`. | diff --git a/packages/apps/web/env.example b/packages/apps/web/env.example index 27c7a9c..ffd969f 100644 --- a/packages/apps/web/env.example +++ b/packages/apps/web/env.example @@ -9,4 +9,7 @@ RESTATE_AUTH_TOKEN= # Optional comma-separated Host values also accepted, for proxies that rewrite # Host to an internal name. Only list names an attacker's DNS cannot claim. # APP_ALLOWED_HOSTS=web.internal:3000 +# Unsafe: set exactly true to accept any Host, disabling DNS-rebinding protection. +# Same-origin write checks still apply. Prefer APP_PUBLIC_URL / APP_ALLOWED_HOSTS. +# APP_ALLOW_UNTRUSTED_HOSTS=false PORT=3000 diff --git a/packages/apps/web/src/server/request-guard.ts b/packages/apps/web/src/server/request-guard.ts index 03c5337..1ae10da 100644 --- a/packages/apps/web/src/server/request-guard.ts +++ b/packages/apps/web/src/server/request-guard.ts @@ -30,42 +30,63 @@ function allowedProxyHosts() { .filter(Boolean); } +function allowUntrustedHosts() { + return process.env.APP_ALLOW_UNTRUSTED_HOSTS === "true"; +} + +function validateAllowedHost(expectedUrl: string, request: Request) { + const expected = new URL(expectedUrl); + const allowedHosts = [expected.host, ...allowedProxyHosts()]; + + const {host} = parseHost( + request.headers.get("host") ?? "", + expected.protocol, + ); + + if (!allowUntrustedHosts() && !allowedHosts.includes(host)) { + throw new UiRequestError(403, "Untrusted UI host"); + } + + return expected.origin; +} + +function validateLoopback(request: Request) { + const actual = new URL(request.url); + // Next.js can reconstruct request.url with localhost even when the browser + // uses 127.0.0.1. Host retains the address the browser actually requested. + const parsed = parseHost( + request.headers.get("host") ?? actual.host, + actual.protocol, + ); + + if (!allowUntrustedHosts() && !LOOPBACK_HOSTNAMES.includes(parsed.hostname)) { + throw new UiRequestError(403, "Untrusted local UI host"); + } + + return parsed.origin; +} + /** * The origin this UI is served from: APP_PUBLIC_URL when set, otherwise the - * loopback address the browser used. + * request's Host and protocol (restricted to loopback by default). * * Every route, reads included, must pass this check. A DNS-rebinding page * (attacker.example re-resolved to our address) is same-origin with itself, * so the browser lets it read responses and sends no cross-origin Origin * header on GET. Its Host header still names the attacker's domain, which is * what this rejects: behind a proxy the Host must be the public one (or one - * listed in APP_ALLOWED_HOSTS, for proxies that rewrite it), locally it must - * be a loopback name. + * listed in APP_ALLOWED_HOSTS, for proxies that rewrite it); locally it must + * be a loopback name. APP_ALLOW_UNTRUSTED_HOSTS=true bypasses these Host + * restrictions (disabling DNS-rebinding protection), not write Origin checks. */ export function trustedOrigin(request: Request): string { const publicUrl = process.env.APP_PUBLIC_URL; + if (publicUrl) { - const expected = new URL(publicUrl); - const host = parseHost( - request.headers.get("host") ?? "", - expected.protocol, - ).host; - if (host !== expected.host && !allowedProxyHosts().includes(host)) { - throw new UiRequestError(403, "Untrusted UI host"); - } - return expected.origin; + return validateAllowedHost(publicUrl, request); } - // Next.js can reconstruct request.url with localhost even when the browser - // uses 127.0.0.1. Host retains the address the browser actually requested. - const fallback = new URL(request.url); - const actual = parseHost( - request.headers.get("host") ?? fallback.host, - fallback.protocol, - ); - if (!LOOPBACK_HOSTNAMES.includes(actual.hostname)) { - throw new UiRequestError(403, "Untrusted local UI host"); - } - return actual.origin; + + return validateLoopback(request); } /** The UI is a local operator tool. Reject cross-origin browser writes. */ diff --git a/packages/apps/web/test/request-guard.test.mjs b/packages/apps/web/test/request-guard.test.mjs index 95c86ff..a601b8b 100644 --- a/packages/apps/web/test/request-guard.test.mjs +++ b/packages/apps/web/test/request-guard.test.mjs @@ -35,7 +35,11 @@ function assertForbidden(check, pattern) { /** Sets the guard's environment for one test and restores it afterwards. */ function useEnv(t, env) { const saved = {}; - for (const key of ["APP_PUBLIC_URL", "APP_ALLOWED_HOSTS"]) { + for (const key of [ + "APP_PUBLIC_URL", + "APP_ALLOWED_HOSTS", + "APP_ALLOW_UNTRUSTED_HOSTS", + ]) { saved[key] = process.env[key]; if (env[key] === undefined) { delete process.env[key]; @@ -118,6 +122,36 @@ test("behind a proxy, reads through a rebound Host are rejected", (t) => { } }); +for (const publicUrl of [undefined, PUBLIC_URL]) { + test(`APP_ALLOW_UNTRUSTED_HOSTS bypasses Host checks with APP_PUBLIC_URL=${publicUrl}`, (t) => { + useEnv(t, { + APP_PUBLIC_URL: publicUrl, + APP_ALLOW_UNTRUSTED_HOSTS: "true", + }); + const host = "web.internal:3000"; + const origin = publicUrl ?? `http://${host}`; + + assert.equal(trustedOrigin(read(host)), origin); + requireSameOrigin(write(host, origin)); + assertForbidden(() => requireSameOrigin(write(host)), /Cross-origin/); + assertForbidden( + () => requireSameOrigin(write(host, "http://evil.example")), + /Cross-origin/, + ); + }); + + test(`Host bypass requires exactly true with APP_PUBLIC_URL=${publicUrl}`, (t) => { + useEnv(t, {APP_PUBLIC_URL: publicUrl}); + for (const value of ["", "false", "1", "TRUE"]) { + process.env.APP_ALLOW_UNTRUSTED_HOSTS = value; + assertForbidden( + () => trustedOrigin(read("attacker.example")), + /Untrusted/, + ); + } + }); +} + test("APP_ALLOWED_HOSTS admits the Host a rewriting proxy sends", (t) => { useEnv(t, { APP_PUBLIC_URL: PUBLIC_URL,