|
1 | 1 | from typing import Annotated, Tuple |
2 | 2 | from urllib.parse import urlparse, urlunparse |
| 3 | +import ipaddress |
| 4 | +import socket |
3 | 5 |
|
4 | 6 | import markdownify |
5 | 7 | import readabilipy.simple_json |
|
23 | 25 | DEFAULT_USER_AGENT_AUTONOMOUS = "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)" |
24 | 26 | DEFAULT_USER_AGENT_MANUAL = "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)" |
25 | 27 |
|
| 28 | +# Default blocked IP ranges (local/internal networks) |
| 29 | +DEFAULT_BLOCKED_IP_RANGES = [ |
| 30 | + "127.0.0.0/8", # Loopback |
| 31 | + "10.0.0.0/8", # Private Class A |
| 32 | + "172.16.0.0/12", # Private Class B |
| 33 | + "192.168.0.0/16", # Private Class C |
| 34 | + "169.254.0.0/16", # Link-local |
| 35 | + "224.0.0.0/4", # Multicast |
| 36 | + "::1/128", # IPv6 loopback |
| 37 | + "fc00::/7", # IPv6 unique local |
| 38 | + "fe80::/10", # IPv6 link-local |
| 39 | +] |
| 40 | + |
| 41 | + |
| 42 | +def is_ip_in_ranges(ip_str: str, ranges: list[str]) -> bool: |
| 43 | + """Check if an IP address is within any of the specified CIDR ranges. |
| 44 | + |
| 45 | + Args: |
| 46 | + ip_str: IP address string to check |
| 47 | + ranges: List of CIDR range strings |
| 48 | + |
| 49 | + Returns: |
| 50 | + True if IP is in any of the ranges, False otherwise |
| 51 | + """ |
| 52 | + try: |
| 53 | + ip = ipaddress.ip_address(ip_str) |
| 54 | + for range_str in ranges: |
| 55 | + try: |
| 56 | + network = ipaddress.ip_network(range_str, strict=False) |
| 57 | + if ip in network: |
| 58 | + return True |
| 59 | + except ValueError: |
| 60 | + continue |
| 61 | + return False |
| 62 | + except ValueError: |
| 63 | + return False |
| 64 | + |
| 65 | + |
| 66 | +def resolve_hostname_to_ips(hostname: str) -> list[str]: |
| 67 | + """Resolve a hostname to its IP addresses. |
| 68 | + |
| 69 | + Args: |
| 70 | + hostname: Hostname to resolve |
| 71 | + |
| 72 | + Returns: |
| 73 | + List of IP addresses as strings |
| 74 | + |
| 75 | + Raises: |
| 76 | + socket.gaierror: If hostname cannot be resolved |
| 77 | + """ |
| 78 | + try: |
| 79 | + # Get both IPv4 and IPv6 addresses |
| 80 | + addr_info = socket.getaddrinfo(hostname, None, family=socket.AF_UNSPEC, type=socket.SOCK_STREAM) |
| 81 | + ips = list(set(info[4][0] for info in addr_info)) |
| 82 | + return ips |
| 83 | + except socket.gaierror: |
| 84 | + raise |
| 85 | + |
| 86 | + |
| 87 | +async def validate_url_against_allowlist( |
| 88 | + url: str, |
| 89 | + allowed_hosts: list[str] | None = None, |
| 90 | + blocked_ip_ranges: list[str] | None = None, |
| 91 | + allow_private_ips: bool = False |
| 92 | +) -> None: |
| 93 | + """Validate a URL against host allowlist and IP range restrictions. |
| 94 | + |
| 95 | + Args: |
| 96 | + url: URL to validate |
| 97 | + allowed_hosts: List of allowed hostnames/domains. If None, all hosts allowed by IP rules |
| 98 | + blocked_ip_ranges: List of CIDR ranges to block. If None, uses DEFAULT_BLOCKED_IP_RANGES |
| 99 | + allow_private_ips: If True, allows access to private/internal IP ranges |
| 100 | + |
| 101 | + Raises: |
| 102 | + McpError: If URL is not allowed |
| 103 | + """ |
| 104 | + if blocked_ip_ranges is None: |
| 105 | + blocked_ip_ranges = DEFAULT_BLOCKED_IP_RANGES if not allow_private_ips else [] |
| 106 | + |
| 107 | + parsed = urlparse(url) |
| 108 | + hostname = parsed.hostname |
| 109 | + |
| 110 | + if not hostname: |
| 111 | + raise McpError(ErrorData( |
| 112 | + code=INVALID_PARAMS, |
| 113 | + message="Invalid URL: no hostname found" |
| 114 | + )) |
| 115 | + |
| 116 | + # Check against allowed hosts list if provided |
| 117 | + if allowed_hosts is not None: |
| 118 | + host_allowed = False |
| 119 | + for allowed_host in allowed_hosts: |
| 120 | + if allowed_host.startswith('*.'): |
| 121 | + # Wildcard domain matching |
| 122 | + domain_suffix = allowed_host[2:] |
| 123 | + if hostname == domain_suffix or hostname.endswith('.' + domain_suffix): |
| 124 | + host_allowed = True |
| 125 | + break |
| 126 | + elif hostname == allowed_host: |
| 127 | + host_allowed = True |
| 128 | + break |
| 129 | + |
| 130 | + if not host_allowed: |
| 131 | + raise McpError(ErrorData( |
| 132 | + code=INTERNAL_ERROR, |
| 133 | + message=f"Host '{hostname}' is not in the allowed hosts list" |
| 134 | + )) |
| 135 | + |
| 136 | + # Resolve hostname to IP addresses and check against blocked ranges |
| 137 | + if blocked_ip_ranges: |
| 138 | + try: |
| 139 | + ips = resolve_hostname_to_ips(hostname) |
| 140 | + except socket.gaierror as e: |
| 141 | + raise McpError(ErrorData( |
| 142 | + code=INTERNAL_ERROR, |
| 143 | + message=f"Failed to resolve hostname '{hostname}': {e}" |
| 144 | + )) |
| 145 | + |
| 146 | + for ip in ips: |
| 147 | + if is_ip_in_ranges(ip, blocked_ip_ranges): |
| 148 | + raise McpError(ErrorData( |
| 149 | + code=INTERNAL_ERROR, |
| 150 | + message=f"Access to IP address '{ip}' (resolved from '{hostname}') is blocked as it falls within restricted IP ranges" |
| 151 | + )) |
| 152 | + |
26 | 153 |
|
27 | 154 | def extract_content_from_html(html: str) -> str: |
28 | 155 | """Extract and convert HTML content to Markdown format. |
@@ -182,13 +309,19 @@ async def serve( |
182 | 309 | custom_user_agent: str | None = None, |
183 | 310 | ignore_robots_txt: bool = False, |
184 | 311 | proxy_url: str | None = None, |
| 312 | + allowed_hosts: list[str] | None = None, |
| 313 | + allow_private_ips: bool = False, |
| 314 | + blocked_ip_ranges: list[str] | None = None, |
185 | 315 | ) -> None: |
186 | 316 | """Run the fetch MCP server. |
187 | 317 |
|
188 | 318 | Args: |
189 | 319 | custom_user_agent: Optional custom User-Agent string to use for requests |
190 | 320 | ignore_robots_txt: Whether to ignore robots.txt restrictions |
191 | 321 | proxy_url: Optional proxy URL to use for requests |
| 322 | + allowed_hosts: Optional list of allowed hostnames/domains (supports wildcards like *.example.com) |
| 323 | + allow_private_ips: Whether to allow access to private/internal IP ranges |
| 324 | + blocked_ip_ranges: Optional list of custom CIDR ranges to block |
192 | 325 | """ |
193 | 326 | server = Server("mcp-fetch") |
194 | 327 | user_agent_autonomous = custom_user_agent or DEFAULT_USER_AGENT_AUTONOMOUS |
@@ -231,6 +364,11 @@ async def call_tool(name, arguments: dict) -> list[TextContent]: |
231 | 364 | if not url: |
232 | 365 | raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required")) |
233 | 366 |
|
| 367 | + # Validate URL against allowlist and IP restrictions |
| 368 | + await validate_url_against_allowlist( |
| 369 | + url, allowed_hosts, blocked_ip_ranges, allow_private_ips |
| 370 | + ) |
| 371 | + |
234 | 372 | if not ignore_robots_txt: |
235 | 373 | await check_may_autonomously_fetch_url(url, user_agent_autonomous, proxy_url) |
236 | 374 |
|
@@ -262,6 +400,11 @@ async def get_prompt(name: str, arguments: dict | None) -> GetPromptResult: |
262 | 400 | url = arguments["url"] |
263 | 401 |
|
264 | 402 | try: |
| 403 | + # Validate URL against allowlist and IP restrictions |
| 404 | + await validate_url_against_allowlist( |
| 405 | + url, allowed_hosts, blocked_ip_ranges, allow_private_ips |
| 406 | + ) |
| 407 | + |
265 | 408 | content, prefix = await fetch_url(url, user_agent_manual, proxy_url=proxy_url) |
266 | 409 | # TODO: after SDK bug is addressed, don't catch the exception |
267 | 410 | except McpError as e: |
|
0 commit comments