docs/python-sdk/fastmcp-server-auth-ssrf.mdx
fastmcp.server.auth.ssrfSSRF-safe HTTP utilities for FastMCP.
This module provides SSRF-protected HTTP fetching with:
When 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.
format_ip_for_url <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L55" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>format_ip_for_url(ip_str: str) -> str
Format IP address for use in URL (bracket IPv6 addresses).
IPv6 addresses must be bracketed in URLs to distinguish the address from the port separator. For example: https://[2001:db8::1]:443/path
Args:
ip_str: IP address stringReturns:
is_ip_allowed <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L137" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>is_ip_allowed(ip_str: str) -> bool
Check if an IP address is allowed (must be globally routable unicast).
Uses ip.is_global which catches:
Additionally blocks multicast addresses (not caught by is_global).
Args:
ip_str: IP address string to checkReturns:
resolve_hostname <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L175" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>resolve_hostname(hostname: str, port: int = 443) -> list[str]
Resolve hostname to IP addresses using DNS.
Args:
hostname: Hostname to resolveport: Port number (used for getaddrinfo)Returns:
Raises:
SSRFError: If resolution failsvalidate_url <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L274" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>validate_url(url: str, require_path: bool = False) -> ValidatedURL
Validate URL for SSRF and resolve to IPs.
Args:
url: URL to validaterequire_path: If True, require non-root path (for CIMD)Returns:
Raises:
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 <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L370" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>ssrf_safe_fetch(url: str) -> bytes
Fetch URL with comprehensive SSRF protection and DNS pinning.
Security measures:
Args:
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 operationReturns:
Raises:
SSRFError: If SSRF validation failsSSRFFetchError: If fetch failsssrf_safe_fetch_response <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L413" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>ssrf_safe_fetch_response(url: str) -> SSRFFetchResponse
Fetch URL with SSRF protection and return response metadata.
This is equivalent to :func:ssrf_safe_fetch but returns response headers
and status code, and supports conditional request headers.
SSRFError <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L96" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Raised when an SSRF protection check fails.
SSRFFetchError <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L100" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Raised when SSRF-safe fetch fails.
ValidatedURL <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L205" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>A URL that has been validated for SSRF with resolved IPs.
SSRFFetchResponse <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/server/auth/ssrf.py#L217" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Response payload from an SSRF-safe fetch.