Skip to content

feat(server): wildcard and callable Host matching, and a loopback-only policy warning - #2015

Merged
bokelley merged 3 commits into
nextfrom
feat/host-policy-wildcards
Dec 5, 2026
Merged

bokelley merged 3 commits into
nextfrom
feat/host-policy-wildcards

Conversation

@bokelley

@bokelley bokelley commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Closes #2010

What changed

Host matching for DNS-rebinding protection is now done by the SDK (HostMatcher in src/adcp/server/http_policy.py) instead of MCP's literal _validate_host. Matching is case-insensitive. Origin matching is unchanged. Nothing waits on modelcontextprotocol/python-sdk#2141.

Wildcards in allowed_hosts

  • *.agents.example.com matches exactly one extra label, the TLS wildcard rule.
  • **.agents.example.com matches one or more labels.
  • Neither matches the apex; list agents.example.com separately.
  • Only a whole leftmost label may be a wildcard. tenant-*.x, a.*.x and a bare * raise ValueError when the server is built.
  • A wildcard must sit under a registrable domain, checked against the SDK's bundled Public Suffix List (ICANN and private sections), or under a dev name. *.com, *.co.uk, **.github.io and *.fly.dev raise; *.example.co.uk, *.acme.github.io and *.localhost are accepted.
  • Entries must be ASCII. A Unicode entry raises with a hint to use punycode, because Python's IDNA 2003 codec maps faß to fass, which is a different domain.

host_validator(hostname) -> bool, on serve, ServeConfig, create_mcp_server, create_a2a_server and build_asgi_app:

  • It is called only when no static entry matches, with the lowercased hostname without the port. Malformed values never reach it.
  • An async def validator, or a callable object with an async __call__, raises TypeError at configuration.
  • At runtime only result is True admits the Host. An awaitable result is closed and rejected, and an exception rejects.
  • adcp.server.HostValidator names the type.

Request Host parsing rejects userinfo, path, query and fragment characters, %, backslash, whitespace, non-ASCII, empty labels, ports that are empty, non-numeric or out of range, and unbracketed IPv6. One trailing dot is stripped from entries and request Hosts alike, so example.com.:8080 still matches an example.com. entry as it did under MCP. Entries the parser cannot structure keep matching their literal value.

Startup warning (fix: commit). The SDK logs a WARNING, once per policy per process, when all of these hold:

  • protection is on,
  • no host_validator is set,
  • every entry is a loopback or dev host,
  • the listener may be public.

serve() judges first, with the address it binds. It stays quiet for a loopback IP or stdio, and its decision covers the factories it calls. create_mcp_server uses its host. create_a2a_server and build_asgi_app don't know where the app will be served, so they warn whenever the policy is loopback-only. A bind address given as a hostname never warns. serve also warns that host_validator is ignored when protection is off.

Docs: handler-authoring.md#http-host-and-origin-policy covers all of the above. It also states that the Origin check applies to protocol POSTs and operational endpoints, not to GET/HEAD discovery reads.

Security review

An independent review found one high and four low issues; all are fixed here, each with tests:

  • High: an async or truthy non-bool validator admitted every Host.
  • Low: IDNA 2003 conversion of Unicode entries.
  • Low: wildcards over a public suffix were accepted.
  • Low: trailing-dot entries with a port regressed.
  • Low: the warning fired only from serve().

Design calls to review

  • * = one label and ** = one or more, rather than * matching any depth.
  • Unknown listener (create_a2a_server, build_asgi_app) means warn. With default settings, test suites that build these apps get one warning per policy per process.
  • Exact matches became case-insensitive, and ports must now be numeric. MCP accepted host:abc for a host:* entry.

Tests

  • tests/test_host_policy_wildcards.py covers wildcard edge cases (apex, deeper subdomains, ports, uppercase, trailing dots, IDN/punycode, userinfo, smuggling characters, public suffixes), validator semantics, all four transports end to end, and the warning paths.
  • The existing transport-policy, serve and testing-helper suites pass, and mypy src/adcp is clean.

🤖 Generated with Claude Code

bokelley and others added 3 commits December 5, 2026 11:58
… in the Host policy

Proxied and multi-tenant sellers had to list every Host value exactly or turn
DNS-rebinding protection off. The SDK's Host check now matches entries
itself, case-insensitively, instead of delegating to MCP's literal matcher:

- `*.agents.example.com` matches exactly one extra label (the TLS wildcard
  rule) and `**.agents.example.com` one or more. Neither matches the apex.
  Only a whole leftmost label may be a wildcard, and it must sit under a
  registrable domain (the bundled Public Suffix List, both sections) or a
  dev name; `*.com`, `*.co.uk` or `tenant-*.example.com` raise ValueError
  when the server is built.
- Request Hosts with userinfo, paths, whitespace, empty labels, a
  non-numeric or out-of-range port, unbracketed IPv6 or raw non-ASCII are
  refused before matching. One trailing dot is stripped from entries and
  request Hosts alike, so `example.com.:8080` still matches an
  `example.com.` entry as it did under MCP's matcher. Entries must be ASCII
  (punycode for IDNs).
- `host_validator(hostname) -> bool` on serve, ServeConfig,
  create_mcp_server, create_a2a_server and build_asgi_app is consulted when
  no allowed_hosts entry matches. It receives the normalized hostname
  without the port. It must be synchronous (an async validator raises
  TypeError at configuration), only a `True` result admits, and an
  exception or awaitable rejects. `adcp.server.HostValidator` names the
  type.

Exact entries and `:*` port patterns keep their meaning. Entries the parser
cannot structure keep matching their literal value.

Refs #2010

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…a possibly public listener

With DNS-rebinding protection on, a server reachable on a public interface
whose allowed_hosts holds only loopback or dev names answers 421 to MCP, A2A
and agent-card discovery for every real hostname, and nothing said so.

The SDK now logs a WARNING, once per policy per process, when protection is
on, no host_validator is set, and every entry is localhost, *.localhost, a
loopback or unspecified IP, or a reserved .test/.example/.invalid name.
serve() judges first against the address it binds (quiet on a loopback IP
and for stdio), so its decision covers the factories it calls.
create_mcp_server uses its host; create_a2a_server and build_asgi_app do not
know the listener and warn for a loopback-only policy. A bind address given
as a hostname is never second-guessed. serve() also warns that
host_validator is ignored when protection is disabled.

Refs #2010

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the Origin check applies

The HTTP Host and Origin policy section now covers leftmost-label wildcards
and their public-suffix rule, ASCII-only entries, trailing-dot handling,
host_validator (synchronous, True-only), the loopback-only warning and who
emits it, and states that the Origin check applies to protocol POSTs and
operational endpoints, not to GET/HEAD discovery reads.

Closes #2010

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bokelley
bokelley merged commit 7e4cee5 into next Dec 5, 2026
39 checks passed
@bokelley
bokelley deleted the feat/host-policy-wildcards branch December 5, 2026 13:10
bokelley added a commit that referenced this pull request Dec 5, 2026
Resolve MIGRATION_v9_to_v10.md and the create_mcp_server signature
against #2014 and #2015, and update the README, migration notes and
3.3 inventory statements that 3.3-rc.3 has no alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant