feat(web): configurable public addresses for the MCP Apps listeners (#1862)

Add MCP_SANDBOX_FULL_ADDRESS and MCP_APP_ORIGIN_FULL_ADDRESS so a reverse
proxy or ingress deployment can advertise the sandbox proxy URL and the
_meta.ui.domain app origin the browser actually reaches, instead of the
bind-derived http://localhost:<port> ones. The process still listens on
MCP_SANDBOX_PORT / MCP_APP_ORIGIN_PORT.

Both values are validated in public-address.ts and refused (warned, bind-
derived address kept) when malformed or when their origin collapses onto
the Inspector UI's (or, for the app origin, the sandbox's) — the MCP Apps
spec requires host != sandbox origin, and the single-hostname proxy layout
is exactly the one that breaks it. Plain http under an https UI is warned.

The sandbox proxy page's referrer check was a hardcoded
http://localhost|127.0.0.1 pattern no https embedder could match. It now
compares the referrer's parsed origin against an exact list injected by the
controller, derived from the same filtered allow-list as its frame-ancestors
header, with the old loopback check kept only as the no-list fallback.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HzuzRPJv7M8wGe3wyw5sSg
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-09-14 00:10:34 -04:00
commit 12544d251c

View file

@ -21,6 +21,12 @@ import { readInspectorVersionSafe } from "../../../core/node/version.ts";
import { resolveSandboxPort } from "./sandbox-controller.js";
import { resolveAppOriginPort } from "./app-origin-controller.js";
import { resolveBindHostname } from "./resolve-bind-host.js";
import {
APP_ORIGIN_FULL_ADDRESS_ENV,
resolveAppOriginPublicOrigin,
resolveSandboxPublicUrl,
SANDBOX_FULL_ADDRESS_ENV,
} from "./public-address.js";
import {
canonicalUrlHost,
isAllInterfacesHost,
@ -74,6 +80,17 @@ export interface WebServerConfig {
* address the sandbox uses.
*/
appOriginPort: number;
/**
* Public sandbox proxy URL advertised in place of the bind-derived one
* (`MCP_SANDBOX_FULL_ADDRESS`, #1862), or undefined when unset or refused.
* See `public-address.ts` for what is refused and why.
*/
sandboxPublicUrl?: string;
/**
* Public origin published app documents are served under
* (`MCP_APP_ORIGIN_FULL_ADDRESS`, #1862), or undefined when unset or refused.
*/
appOriginPublicOrigin?: string;
logger: Logger | undefined;
/** When true, open browser after server starts. */
autoOpen: boolean;
@ -460,6 +477,22 @@ export function buildWebServerConfig(
}
})
.filter((o): o is string => o !== null);
const allowedOrigins = configuredOrigins?.length
? configuredOrigins
: defaultAllowedOrigins(hostname, port);
// Resolved after the allow-list because both are checked against it: neither
// listener may share an origin with the Inspector page (#1862). The sandbox
// goes first so the app origin can be checked against it too.
const sandboxPublicUrl = resolveSandboxPublicUrl(
process.env[SANDBOX_FULL_ADDRESS_ENV],
allowedOrigins,
);
const appOriginPublicOrigin = resolveAppOriginPublicOrigin(
process.env[APP_ORIGIN_FULL_ADDRESS_ENV],
allowedOrigins,
sandboxPublicUrl,
);
return {
port,
@ -471,12 +504,12 @@ export function buildWebServerConfig(
writable,
initialServers,
storageDir: process.env.MCP_STORAGE_DIR,
allowedOrigins: configuredOrigins?.length
? configuredOrigins
: defaultAllowedOrigins(hostname, port),
allowedOrigins,
sandboxPort,
sandboxHost: hostname,
appOriginPort,
sandboxPublicUrl,
appOriginPublicOrigin,
logger,
autoOpen: resolveAutoOpen(),
};