BerriAI/litellm · error · SSRFError

Redirect response has no Location header

Error message

Redirect response has no Location header

What it means

SSRFError raised by _extract_redirect_url when a response counted as a redirect carries no usable Location header (missing, empty, or non-string). Without a Location there is no next hop to validate, so the fetch aborts rather than guessing; the header value is resolved against the request URL when present.

Solutions

  1. Treat it as an upstream server bug: fetch without redirects and inspect the raw 3xx response.
  2. Report the malformed redirect to the origin server's operator.
  3. Catch SSRFError and return a 502-style error noting the malformed redirect.

Example fix

# before
resp = safe_get(client, url)  # 3xx without Location -> unhandled SSRFError

# after
try:
    resp = safe_get(client, url)
except SSRFError as e:
    if "no Location header" in str(e):
        raise HTTPException(status_code=502, detail="Upstream sent a redirect without a Location header")
    raise
Defensive patterns

Strategy: try-catch

Try / catch

from litellm.litellm_core_utils.url_utils import SSRFError

try:
    resp = safe_get(client, url)
except SSRFError as e:
    if "no Location header" in str(e):
        raise HTTPException(status_code=502, detail="Upstream sent a redirect without a Location header")
    raise

Prevention

When it happens

Trigger: A 3xx response with a missing or blank Location header — hand-rolled servers, some proxies stripping Location, or client objects whose is_redirect is purely status-based.

Common situations: Misconfigured backend services; intermediate proxies that strip redirect headers; rare in practice with httpx because is_redirect usually requires a location.

Related errors


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

Appendix: source

Thrown at litellm/litellm_core_utils/url_utils.py:370

    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))


def safe_get(client: Any, url: str, **kwargs: Any) -> Any:
    """
    Fetch a user-supplied URL with SSRF protection on every redirect hop.

    Validates the initial URL and each redirect target before making the
    request. No DNS rebinding (resolve-and-rewrite). No redirect bypass
    (each hop validated). No breaking change for legitimate CDN redirects.

    When ``litellm.user_url_validation`` is False, validation is bypassed
    and this function delegates to ``client.get(url, follow_redirects=True)``.

    Args:
        client: An httpx.Client (sync).
        url: The user-supplied URL.

View on GitHub (pinned to 77b7c6c40c)