You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 7464deb
Browse filesBrowse the repository at this point in the historyBrowse files
feat(fetch): add opt-in host allowlisting via --allowed-hosts
Add a host allowlist for the fetch server, addressing the SSRF surface
tracked in modelcontextprotocol#2317 without changing default behavior:
- New --allowed-hosts flag (exact hosts, case-insensitive; *.example.com
wildcards covering the bare domain and any subdomain; IP literals).
- Enforced on the initial request, the robots.txt pre-check, and every
redirect hop: redirects are now followed manually (bounded at 20, same
as httpx's default) and each hop is re-validated, so a redirect from
an allowed host can no longer bounce a fetch to a disallowed host.
- URL validation parses with httpx.URL — the same parser used to
connect — so the validated host is always the host being connected.
- Denials fail closed with an error that does not echo the allowlist.
Default-IP-blocking is intentionally left to the separate default-deny
proposal; with no flag set, behavior is identical to before.
Copy file name to clipboardExpand all lines: src/fetch/README.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -174,6 +174,30 @@ This can be customized by adding the argument `--user-agent=YourUserAgent` to th
174
174
175
175
The server can be configured to use a proxy by using the `--proxy-url` argument.
176
176
177
+
### Customization - Allowed hosts
178
+
179
+
By default the server can fetch any host (see the security caution above). To restrict which hosts the server may connect to, add the `--allowed-hosts` argument with one or more entries to the `args` list in the configuration:
180
+
181
+
-`example.com` allows exactly `example.com` (matching is case-insensitive)
182
+
-`*.example.com` allows `example.com` itself and any subdomain, e.g. `api.example.com`
183
+
184
+
Matching is by hostname only: an entry allows that host on any port, and IPv6 addresses are listed without brackets (e.g. `--allowed-hosts ::1`).
185
+
186
+
The allowlist is enforced on the initial request, on the `robots.txt` pre-check, and on every redirect hop, so a redirect from an allowed host cannot bounce the fetch to a disallowed host. Requests to any other host fail with an error explaining that the host is not allowlisted.
187
+
188
+
Note that the allowlist matches hostnames, not the IP addresses they resolve to: an allowlisted domain whose DNS records point at internal addresses can still be fetched (this is what makes it possible to deliberately allowlist internal hosts).
Copy file name to clipboardExpand all lines: src/fetch/src/mcp_server_fetch/__init__.py
+8-1Lines changed: 8 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,16 @@ def main():
16
16
help="Ignore robots.txt restrictions",
17
17
)
18
18
parser.add_argument("--proxy-url", type=str, help="Proxy URL to use for requests")
19
+
parser.add_argument(
20
+
"--allowed-hosts",
21
+
type=str,
22
+
nargs="+",
23
+
metavar="HOST",
24
+
help="Only allow fetching these hosts (exact names like example.com or wildcards like *.example.com, which also covers example.com itself). Applies to the initial URL and every redirect hop. If omitted, all hosts are allowed.",
The URL is parsed with httpx — the same parser that will be used to
66
+
connect — so the validated host is always the host that gets connected.
67
+
68
+
Raises:
69
+
McpError: If the URL has no hostname or its host is not allowlisted
70
+
"""
71
+
ifallowed_hostsisNone:
72
+
return
73
+
fromhttpximportURL, InvalidURL
74
+
75
+
try:
76
+
hostname=URL(url).host
77
+
exceptInvalidURL:
78
+
hostname=None
79
+
ifnothostname:
80
+
raiseMcpError(ErrorData(
81
+
code=INVALID_PARAMS,
82
+
message=f"Invalid URL: could not determine a hostname for {url}",
83
+
))
84
+
ifnotis_host_allowed(hostname, allowed_hosts):
85
+
raiseMcpError(ErrorData(
86
+
code=INTERNAL_ERROR,
87
+
message=f"Fetching '{hostname}' is not allowed: this server is configured with a host allowlist (--allowed-hosts) and this host is not on it. The user can adjust the server configuration if this host should be accessible.",
88
+
))
89
+
90
+
91
+
asyncdef_get_following_redirects(
92
+
client: "AsyncClient",
93
+
url: str,
94
+
*,
95
+
user_agent: str,
96
+
allowed_hosts: list[str] |None,
97
+
timeout: float|None=None,
98
+
) ->"Response":
99
+
"""GET a URL, following redirects manually and re-validating every hop.
100
+
101
+
Redirects are followed by hand (instead of httpx's follow_redirects) so that
102
+
each redirect target is checked against the allowlist before connecting;
103
+
otherwise a 302 from an allowed host could bounce the fetch to any host.
104
+
105
+
Args:
106
+
client: httpx.AsyncClient to use
107
+
url: Initial URL to fetch
108
+
user_agent: User-Agent header value
109
+
allowed_hosts: Allowlist applied to the initial URL and every redirect hop
110
+
timeout: Optional per-request timeout in seconds (httpx default if None)
111
+
112
+
Returns:
113
+
The final (non-redirect) httpx.Response
114
+
115
+
Raises:
116
+
McpError: If a hop is not allowlisted or the redirect limit is exceeded
0 commit comments