Skip to content

createMcpExpressApp / createMcpHonoApp: non-loopback host silently skips Host/Origin checks (only 0.0.0.0/:: warn) | #2843

Open

Description

@tiagovilasboas

Static review of public source at commit 603217008710. No traffic was sent to any MCP environment.

createMcpExpressApp (and the matching Hono helper) auto-arm Host + Origin validation only for loopback hosts. Binding to 0.0.0.0 / :: logs a warning, but any other non-loopback host (LAN IP, container hostname, public DNS name) gets neither middleware nor that warning:

packages/middleware/express/src/express.ts (same ladder in packages/middleware/hono/src/hono.ts):

const localhostHosts = ['127.0.0.1', 'localhost', '::1'];
if (localhostHosts.includes(host)) {
    app.use(localhostHostValidation());
} else if (host === '0.0.0.0' || host === '::') {
    console.warn(
        `Warning: Server is binding to ${host} without DNS rebinding protection. ` +
            'Consider using the allowedHosts option...'
    );
}
// Origin: only armed for loopback or explicit allowedOrigins

So createMcpExpressApp({ host: '192.168.1.10' }) or { host: 'mcp.internal' } serves without Host/Origin checks and without the operator nudge that 0.0.0.0 already gets. DNS rebinding / browser-origin abuse is exactly what those middleware exist for on HTTP MCP endpoints.

Suggested change:

  • Treat every non-loopback host like 0.0.0.0: require allowedHosts (and ideally allowedOrigins), or at least emit the same warning.
  • Or fail closed: refuse to construct the app for non-loopback binds unless allowlists are provided.
  • Keep the explicit opt-out path for demos that truly need an open bind.

Severity: medium as insecure default / defense-in-depth for non-loopback HTTP mounts; not claiming a working exploit against a specific deployment. No proof-of-concept.

Happy to send a focused PR if this direction is useful.

Activity

  1. added
    v1Issues / PRs related to v1.x
    v2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes
    on Sep 23, 2026
  2. iamhuman-cheolheelee commented on Sep 24, 2026

    @iamhuman-cheolheelee

    Confirmed on main (7f7a94c). The same ladder exists in all three factories, not just Express/Hono:

    • packages/middleware/express/src/express.ts (createMcpExpressApp)
    • packages/middleware/hono/src/hono.ts:92-98
    • packages/middleware/fastify/src/fastify.ts:81-86 (uses app.log.warn)

    Any host that is neither loopback nor 0.0.0.0/:: falls through both branches, so there's no Host check and no warning. Equivalent wildcard spellings like 0:0:0:0:0:0:0:0 or [::] also skip the warning.

    A backwards-compatible fix I'd suggest: make the warning branch the else (any non-loopback bind without allowedHosts warns), and apply it the same way in all three factories, with one test per factory. Fail-closed would break existing LAN/container deployments that sit behind their own auth or proxy, so I think it belongs in a separate, explicitly breaking change if you want it at all.

    This also overlaps with #2852, which proposes removing the default Origin arming from the same code. It's probably worth settling both in one place so the Host/Origin defaults don't get changed twice.

    @tiagovilasboas, you offered a PR. If you're not planning to send one, I'm happy to put up the warning-only version for all three factories once a maintainer confirms that direction.

  3. imran-siddique commented on Sep 25, 2026

    @imran-siddique

    The same ladder is in the published v1.x line. createMcpExpressApp in @modelcontextprotocol/sdk 1.30.1, released the day after this was filed, installs no Host validation for a host outside the three loopback names when allowedHosts is unset, and only warns for 0.0.0.0 and ::. 1.30.1 also added a comment above the parser saying a disallowed Host is answered 403 unread, which on those binds has nothing to run after. A fix on main would need a v1.x backport, the way #2698 got #2717.

  4. A7mdMe commented on Nov 14, 2026

    @A7mdMe

    Great analysis by @tiagovilasboas and @mactsouk regarding the non-loopback bind ladder across Express, Hono, and Fastify adapters.

    When running MCP servers over HTTP or SSE in Docker containers, Kubernetes pods, or LAN environments, binding to 0.0.0.0 or a service hostname without strict Host and Origin validation creates an acute vulnerability: a browser executing untrusted JavaScript can perform a DNS rebinding attack (resolving an external hostname to the local or container IP), sending requests with an external Host header directly to the unauthenticated or weakly authenticated MCP server.

    In designing production guardrails for MCP servers, we found that addressing this requires three concrete architectural patterns:

    1. Robust IPv6 Bracket-Aware Host Header Parsing

    A naive host.split(':') fails on bracketed IPv6 literals (e.g. [::1]:3000 or [2001:db8::1]) and doesn't handle port normalization:

    export function parseHostHeader(rawHost: string): { hostname: string; port: number | null } {
      const trimmed = rawHost.trim().toLowerCase();
    
      if (trimmed.startsWith('[')) {
        const closingIdx = trimmed.indexOf(']');
        if (closingIdx === -1) throw new Error('Malformed IPv6 host header: missing closing bracket');
        const hostname = trimmed.substring(0, closingIdx + 1);
        const remainder = trimmed.substring(closingIdx + 1);
        if (remainder.startsWith(':')) {
          const port = parseInt(remainder.substring(1), 10);
          if (isNaN(port) || port < 1 || port > 65535) throw new Error('Invalid port');
          return { hostname, port };
        }
        return { hostname, port: null };
      }
    
      const parts = trimmed.split(':');
      if (parts.length === 1) return { hostname: parts[0], port: null };
      if (parts.length === 2) {
        const port = parseInt(parts[1], 10);
        if (isNaN(port) || port < 1 || port > 65535) throw new Error('Invalid port');
        return { hostname: parts[0], port };
      }
      throw new Error('Malformed host header: unbracketed IPv6 or excessive colons');
    }

    2. Dual-Layer Origin & Wildcard Host Whitelisting

    To allow legitimate developer workflows while stopping browser pivots:

    • Wildcard Rejection: Client requests presenting Host: 0.0.0.0 or :: must be rejected immediately (wildcard IPs are bind addresses, never valid client target hosts).
    • Native Desktop Permissiveness: Native MCP clients (Claude Desktop, Cursor, CLI stdio-to-HTTP bridges) do not send an Origin header. The middleware should allow missing Origin headers by default (allowNoOrigin: true), but strictly block untrusted browser origins and Origin: null (sandboxed iframe exploits).
    • Explicit Domain & Subdomain Allowlisting: Support exact hostnames (mcp.internal) and wildcard subdomains (*.corp.net).

    3. Modern JSON-RPC Error Envelopes (Avoiding Legacy -32000)

    As noted in recent spec discussions (e.g. #3185), transport-level Host/Origin rejections on modern MCP paths should avoid legacy -32000 and return standard JSON-RPC 2.0 -32600 (Invalid Request) accompanied by HTTP 400 (malformed header) or 403 (unauthorized host/origin):

    // Standard JSON-RPC 2.0 response for rejected Host / Origin
    {
      "jsonrpc": "2.0",
      "id": null,
      "error": {
        "code": -32600,
        "message": "Forbidden: Host header not permitted",
        "data": {
          "reason": "DNS Rebinding protection: host not allowed",
          "rejectedHeader": "Host",
          "value": rawHost
        }
      }
    }

    This prevents silent bypasses on containerized binds while ensuring clients receive actionable, spec-compliant diagnostic responses rather than opaque drops.


    Disclosure: Maintainer of mcp-production-kit (open-source TypeScript kit for MCP servers with automated DNS rebinding protection, secret redaction, audit logging, and stdio stream isolation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    v1Issues / PRs related to v1.xv2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions