Skip to main content

Module url_safety

Module url_safety 

Source
Expand description

SSRF (Server-Side Request Forgery) protection for URL-fetching tools.

Resolves each URL’s hostname to its IPs and blocks targets that point at private/internal/cloud-metadata addresses. Inspired by the Python url_safety.py in nousresearch/hermes-agent, with Rust-native resolution semantics.

Limitations:

  • DNS rebinding (TOCTOU): an attacker-controlled DNS server with TTL=0 can return a public IP for the pre-flight check and then a private IP on the actual connect. Mitigating this requires connection-level validation (e.g. a socket-bound resolver or an egress proxy). This module performs pre-flight checks only.
  • Redirect-based bypass is caller-responsibility — callers that follow redirects themselves should re-validate each hop.

Functions§

check_raw_url_private_ip
Pre-flight SSRF check for a raw URL string. Convenience wrapper that parses the URL first.
check_url_private_ip
Pre-flight SSRF check for an already-parsed URL. Returns Err(message) when the URL targets an address that should be blocked.
is_blocked_ip
Returns true if the IP address belongs to a range that SSRF guards should reject. Combines stdlib predicates with explicit CGNAT + extra-safe link-local handling.
ssrf_redirect_policy
A reqwest redirect policy that re-validates each hop against the SSRF guard. Use with reqwest::ClientBuilder::redirect(policy) to catch redirects inside third-party SDKs or forwarded requests that would otherwise bypass the manual redirect loop in web_fetch.