fastmcp.server.auth.ssrf
SSRF-safe HTTP utilities for FastMCP.
This module provides SSRF-protected HTTP fetching with:
- DNS resolution and IP validation before requests
- DNS pinning to prevent rebinding TOCTOU attacks
- Support for both CIMD and JWKS fetches
FASTMCP_SSRF_TRUST_PROXY is set, DNS resolution and the IP blocklist are
skipped and a single request is made to the hostname URL through the configured
HTTPS_PROXY/ALL_PROXY, delegating DNS and egress to that trusted proxy (the scheme
and hostname checks still apply). The proxy URL is read from the environment and
passed to httpx2 explicitly with trust_env disabled, so the request is provably
routed through the proxy rather than predicted to be — NO_PROXY is not evaluated in
this mode. If no proxy is configured, the fetch is refused rather than sent direct
with the blocklist disabled.
Functions
format_ip_for_url
ip_str: IP address string
- IP string suitable for URL (IPv6 addresses are bracketed)
is_ip_allowed
- Private (10.x, 172.16-31.x, 192.168.x)
- Loopback (127.x, ::1)
- Link-local (169.254.x, fe80::) - includes AWS metadata!
- Reserved, unspecified
- RFC6598 Carrier-Grade NAT (100.64.0.0/10) - can point to internal networks
- IPv6 transition forms that embed blocked IPv4 targets
ip_str: IP address string to check
- True if the IP is allowed (public unicast internet), False if blocked
resolve_hostname
hostname: Hostname to resolveport: Port number (used for getaddrinfo)
- List of resolved IP addresses
SSRFError: If resolution fails
validate_url
url: URL to validaterequire_path: If True, require non-root path (for CIMD)
- ValidatedURL with resolved IPs
SSRFError: If the URL is invalid, resolves to blocked IPs, or proxy-trust mode is enabled but no configured proxy will route the request.
ssrf_safe_fetch
- HTTPS only
- DNS resolution with IP validation
- Connects to validated IP directly (DNS pinning prevents rebinding)
- Response size limit
- Redirects disabled
- Overall timeout
url: URL to fetchrequire_path: If True, require non-root pathmax_size: Maximum response size in bytes (default 5KB)timeout: Per-operation timeout in secondsoverall_timeout: Overall timeout for entire operation
- Response body as bytes
SSRFError: If SSRF validation failsSSRFFetchError: If fetch fails
ssrf_safe_fetch_response
ssrf_safe_fetch but returns response headers
and status code, and supports conditional request headers.

