Repository navigation
fix(fetch): refuse --allowed-hosts wildcards on IP addresses - #5562
Conversation
A wildcard entry was only checked as a host name, so `*.0.0.1` was accepted and then matched, as a suffix, every IPv4 literal ending .0.0.1 (10.0.0.1 and 127.0.0.1 alike). normalize_allowed_host now rejects a wildcard whose suffix ends in a number (an address or a numeric tail of one), and host_is_allowed never matches a wildcard entry against an IP-literal host. Closes #5521 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change restricting allowed-host wildcards to domain names.
No open issues 🎉
What changed in this PR
Fixes #5521 in the fetch reference server by preventing --allowed-hosts wildcards from matching IP addresses, while preserving domain wildcards and exact address entries.
Changes:
- Rejects wildcard entries with numeric address suffixes at startup.
- Prevents wildcard matching against IP-literal hosts, including when entries bypass normalization.
- Adds regression tests and documents the restriction.
| File | Description |
|---|---|
src/fetch/tests/test_allowed_hosts_wildcard_ip.py |
Tests rejected entries, address matching, valid entries, and server startup refusal. |
src/fetch/src/mcp_server_fetch/allowlist.py |
Adds numeric-suffix validation and excludes IP addresses from wildcard matching. |
src/fetch/README.md |
Clarifies that wildcards apply only to domains and addresses require exact entries. |
Review effort: Balanced
|
| Tools | Prompts | Resources | Resource templates | |
|---|---|---|---|---|
| Base | 1 | 1 | 0 | 0 |
| Head | 1 | 1 | 0 | 0 |
Updated by this run for 10aa0343eda2b18f9b52d9778f05eeecd946eb35.
|
Copilot round 1: clean. The review reported no open issues and left no inline or suppressed comments, so the loop ends here on the first clean round. Nothing was declined and nothing was pushed in response. |
Closes #5521
Description
--allowed-hostsdocuments two entry forms: an exact host (a name or an IP literal) and*.<domain>. A wildcard entry was only checked as a host name, so*.0.0.1or*.10.0.0.1was accepted and then matched, as a plain suffix, every IPv4 literal with that tail:*.0.0.1allowed10.0.0.1and127.0.0.1alike.Both halves the issue asks for, in
src/fetch/src/mcp_server_fetch/allowlist.py:normalize_allowed_hostrejects a wildcard on an address. A wildcard whose suffix ends in a numeric label (decimal digits or0xhex, the test URL parsers use to read a host as IPv4) raisesValueError:*.10.0.0.1,*.0.0.1,*.1,*.0x7f.1.*.[::1]was already refused by the character check and is now pinned by a test. Through the flag this is an argparse usage error at startup; throughcreate_server(allowed_hosts=...)it is theValueError.host_is_allowednever matches a wildcard entry against an IP-literal host, so the suffix match cannot widen an address even for a list that did not come through the parser. An address is matched by exact entries only, as before.Domain wildcards (
*.example.com,*.1.example.com,*.xn--p1ai) and exact address entries are unchanged.Server Details
--allowed-hostsoption's entry validation and matching; README (Customization - Allowed hosts); tests. No tool, prompt or schema change.Motivation and Context
#5521. An operator's allowlist entry was accepted in a form the documented entry forms exclude, and then admitted more hosts than it named.
How Has This Been Tested?
Automated: new
src/fetch/tests/test_allowed_hosts_wildcard_ip.py(rejected entries, entries still accepted, wildcard-vs-address matching for IPv4 and IPv6, exact address entries still matching,create_serverrefusing the list). Full fetch suite: 573 passed;allowlist.pyat 100% lines and branches;npm run local:gateexit 0.Clients, against a page served on
http://127.0.0.1:38521/, server started with--allowed-hosts <entries> --allow-private-ips --ignore-robots-txt, tool callfetchwithurl=http://127.0.0.1:38521/.Inspector CLI 2.9.0 (
--method tools/call --tool-name fetch --tool-arg url=http://127.0.0.1:38521/ --format json), both eras:v2/main*.0.0.1legacy(2025)Contents of http://127.0.0.1:38521/: … hello from the loopback pageConnection closed)*.0.0.1modern(2026-07-28)_metacarriesio.modelcontextprotocol/serverInfoConnection closed during the version negotiation probe)*.example.com 127.0.0.1legacy*.example.com 127.0.0.1modernserverInfoin_meta*.example.com 10.0.0.1legacyisError: true:Refused to fetch http://127.0.0.1:38521/: 127.0.0.1 is not one of the hosts this server is allowed to fetch from (--allowed-hosts).*.example.com 10.0.0.1modernserverInfoin_metaThe startup refusal on this branch, on the server's stderr:
LLM client: Claude Code 2.1.327, headless (
claude -p --mcp-config … --strict-mcp-config --allowedTools mcp__fetch__fetch), prompt "Use the fetch tool of the fetch server to fetch http://127.0.0.1:38521/ and reply with exactly the text the tool returned. If the tool is unavailable or fails, say so and quote the error." Claude Code has no era switch, so the era could not be selected.v2/main,*.0.0.1Contents of http://127.0.0.1:38521/: # t hello from the loopback page(the loopback address fetched through the wildcard)*.0.0.1CONNECTION_CLOSED): it refused to start127.0.0.1Contents of http://127.0.0.1:38521/: # t hello from the loopback pageBreaking Changes
None for a configuration that follows the documented entry forms. A configuration holding a wildcard on an address (
*.0.0.1) now fails at startup with a message naming the entry; list the address exactly instead.Types of changes
Checklist
npm run changeset) if this changes what a TypeScript server publishes (not applicable: fetch is a Python server)Additional context
The issue's reporter (
SalyyS1) prototyped a fix on a fork; this PR is written independently against the issue, per the repository's contribution policy.🤖 Generated with Claude Code