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
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
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 URL is invalid or resolves to blocked IPs
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.

