BerriAI/litellm · error · SSRFError

Origin mismatch on host

Error message

Origin mismatch on host

What it means

SSRFError raised when a redirect lands on a different hostname than the original URL (compared after lowercasing and stripping a trailing dot via _normalize_host). safe_get pins every hop to the initial origin, so hops to CDNs, shorteners, or apex/www variants are rejected as potential SSRF.

Solutions

  1. Pre-resolve the redirect chain yourself (one manual hop) and pass the final URL as the initial URL to safe_get.
  2. If the alternate host is expected, fetch it directly by using its URL as the starting URL.
  3. Catch SSRFError and surface 'redirects to another host' as a 4xx.
  4. For trusted environments only: disable litellm.user_url_validation.

Example fix

# before
url = "https://app.example.com/files/123"  # 302 -> https://cdn.example.net/files/123
resp = safe_get(client, url)                 # SSRFError: Origin mismatch on host

# after — use the final host as the initial URL
url = "https://cdn.example.net/files/123"
resp = safe_get(client, url)
Defensive patterns

Strategy: try-catch

Validate before calling

# Pre-resolve one hop yourself, then hand safe_get the final origin
raw = client.get(url, follow_redirects=False)
if raw.is_redirect:
    from urllib.parse import urljoin
    final = urljoin(url, raw.headers["location"])
else:
    final = url
resp = safe_get(client, final)

Try / catch

from litellm.litellm_core_utils.url_utils import SSRFError

try:
    resp = safe_get(client, url)
except SSRFError as e:
    if "Origin mismatch on host" in str(e):
        raise HTTPException(status_code=400, detail="URL redirects to a different host; supply the final URL directly")
    raise

Prevention

When it happens

Trigger: example.com 301 to www.example.com (or the reverse); a file URL redirecting to a CDN such as cdn.example.net, an S3 signing domain, or cloudfront.net; any link-shortener chain.

Common situations: Fetching user-supplied attachments/documents whose storage redirects to a signing or CDN host; marketing sites that canonicalize to www.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/6102d2c0ca38ecc8. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/url_utils.py:354

    Error messages identify *which* component mismatched but never echo
    the operator's ``expected`` host or the candidate's hostname back to
    the caller — in the SSRF threat model the caller is the attacker,
    and reflecting host info would be a secondary leak of operator
    infrastructure details.
    """
    candidate: Final = urlparse(candidate_url)
    expected: Final = urlparse(expected_url)

    if candidate.scheme not in _ALLOWED_SCHEMES:
        raise SSRFError("URL scheme is not allowed")

    if candidate.scheme != expected.scheme:
        raise SSRFError("Origin mismatch on scheme")

    candidate_host: Final = _normalize_host(candidate.hostname or "")
    expected_host: Final = _normalize_host(expected.hostname or "")
    if not candidate_host or candidate_host != expected_host:
        raise SSRFError("Origin mismatch on host")

    default_port: Final = 443 if candidate.scheme == "https" else 80
    candidate_port: Final = candidate.port if candidate.port is not None else default_port
    expected_port: Final = expected.port if expected.port is not None else default_port
    if candidate_port != expected_port:
        raise SSRFError("Origin mismatch on port")


_MAX_REDIRECTS: Final = 10


def _extract_redirect_url(response: Any, request_url: str) -> str:
    """Extract and resolve the redirect target from a response's Location header."""
    location: Final = response.headers.get("location")
    if not isinstance(location, str) or not location:
        raise SSRFError("Redirect response has no Location header")
    # Resolve relative URLs against the request URL
    return str(httpx.URL(request_url).join(location))

View on GitHub (pinned to 77b7c6c40c)