Skip to content

fix(fetch): refuse --allowed-hosts wildcards on IP addresses - #5562

Merged
cliffhall merged 1 commit into
v2/mainfrom
v2/fix/5521-fetch-allowed-hosts-wildcard-ip
Nov 18, 2026
Merged

cliffhall merged 1 commit into
v2/mainfrom
v2/fix/5521-fetch-allowed-hosts-wildcard-ip

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5521

Description

--allowed-hosts documents 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.1 or *.10.0.0.1 was accepted and then matched, as a plain suffix, every IPv4 literal with that tail: *.0.0.1 allowed 10.0.0.1 and 127.0.0.1 alike.

Both halves the issue asks for, in src/fetch/src/mcp_server_fetch/allowlist.py:

  • normalize_allowed_host rejects a wildcard on an address. A wildcard whose suffix ends in a numeric label (decimal digits or 0x hex, the test URL parsers use to read a host as IPv4) raises ValueError: *.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; through create_server(allowed_hosts=...) it is the ValueError.
  • host_is_allowed never 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

  • Server: fetch
  • Changes to: the --allowed-hosts option'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_server refusing the list). Full fetch suite: 573 passed; allowlist.py at 100% lines and branches; npm run local:gate exit 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 call fetch with url=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:

Entries Era v2/main This branch
*.0.0.1 legacy (2025) exit 0: Contents of http://127.0.0.1:38521/: … hello from the loopback page server refuses to start; CLI exit 1 (Connection closed)
*.0.0.1 modern (2026-07-28) exit 0, same page, _meta carries io.modelcontextprotocol/serverInfo server refuses to start; CLI exit 1 (Connection closed during the version negotiation probe)
*.example.com 127.0.0.1 legacy exit 0: the page (exact address entry still matches)
*.example.com 127.0.0.1 modern exit 0: the page, with serverInfo in _meta
*.example.com 10.0.0.1 legacy exit 5, isError: 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.1 modern exit 5, same refusal, with serverInfo in _meta

The startup refusal on this branch, on the server's stderr:

mcp-server-fetch: error: argument --allowed-hosts: '*.0.0.1' puts a wildcard on an IP address: *.<domain> takes a domain name, and an address is listed exactly

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.

Config Result
v2/main, *.0.0.1 Contents of http://127.0.0.1:38521/: # t hello from the loopback page (the loopback address fetched through the wildcard)
branch, *.0.0.1 The model reports the fetch server failed to connect (CONNECTION_CLOSED): it refused to start
branch, 127.0.0.1 Contents of http://127.0.0.1:38521/: # t hello from the loopback page

Breaking 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation (not applicable: no protocol surface changes; the change is to a command-line option's validation)
  • My changes follow MCP security best practices
  • I have updated the server's README accordingly
  • I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes (not applicable: fetch is a Python server)
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options

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

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>
@cliffhall cliffhall added the v2 label Nov 18, 2026
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 10aa034

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@github-actions

Copy link
Copy Markdown
Contributor

fetch: MCP interface diff

Base: e1d154ee0370 (Merge pull request #5534 from modelcontextprotocol/v2/feat/5525-memory-write-caps)
Head: the build in this checkout, at 1f67dfaed25e (Merge 10aa034 into e1d154e)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 1 1 0 0
Head 1 1 0 0

Updated by this run for 10aa0343eda2b18f9b52d9778f05eeecd946eb35.

@cliffhall

Copy link
Copy Markdown
Member Author

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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetch: --allowed-hosts accepts wildcard IP entries (e.g. *.0.0.1) and matches any IPv4 ending with that suffix

2 participants