Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker/Dockerfile.web
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. |
3 changes: 3 additions & 0 deletions packages/apps/web/env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
67 changes: 44 additions & 23 deletions packages/apps/web/src/server/request-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
36 changes: 35 additions & 1 deletion packages/apps/web/test/request-guard.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand Down
Loading