{"record":{"id":"88d476c4d8523514","repo":"BerriAI/litellm","slug":"redirect-response-has-no-location-header","errorCode":null,"errorMessage":"Redirect response has no Location header","messagePattern":"Redirect response has no Location header","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":370,"sourceCode":"    expected_host: Final = _normalize_host(expected.hostname or \"\")\n    if not candidate_host or candidate_host != expected_host:\n        raise SSRFError(\"Origin mismatch on host\")\n\n    default_port: Final = 443 if candidate.scheme == \"https\" else 80\n    candidate_port: Final = candidate.port if candidate.port is not None else default_port\n    expected_port: Final = expected.port if expected.port is not None else default_port\n    if candidate_port != expected_port:\n        raise SSRFError(\"Origin mismatch on port\")\n\n\n_MAX_REDIRECTS: Final = 10\n\n\ndef _extract_redirect_url(response: Any, request_url: str) -> str:\n    \"\"\"Extract and resolve the redirect target from a response's Location header.\"\"\"\n    location: Final = response.headers.get(\"location\")\n    if not isinstance(location, str) or not location:\n        raise SSRFError(\"Redirect response has no Location header\")\n    # Resolve relative URLs against the request URL\n    return str(httpx.URL(request_url).join(location))\n\n\ndef safe_get(client: Any, url: str, **kwargs: Any) -> Any:\n    \"\"\"\n    Fetch a user-supplied URL with SSRF protection on every redirect hop.\n\n    Validates the initial URL and each redirect target before making the\n    request. No DNS rebinding (resolve-and-rewrite). No redirect bypass\n    (each hop validated). No breaking change for legitimate CDN redirects.\n\n    When ``litellm.user_url_validation`` is False, validation is bypassed\n    and this function delegates to ``client.get(url, follow_redirects=True)``.\n\n    Args:\n        client: An httpx.Client (sync).\n        url: The user-supplied URL.","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L352-L388","documentation":"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.","triggerScenarios":"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.","commonSituations":"Misconfigured backend services; intermediate proxies that strip redirect headers; rare in practice with httpx because is_redirect usually requires a location.","solutions":["Treat it as an upstream server bug: fetch without redirects and inspect the raw 3xx response.","Report the malformed redirect to the origin server's operator.","Catch SSRFError and return a 502-style error noting the malformed redirect."],"exampleFix":"# before\nresp = safe_get(client, url)  # 3xx without Location -> unhandled SSRFError\n\n# after\ntry:\n    resp = safe_get(client, url)\nexcept SSRFError as e:\n    if \"no Location header\" in str(e):\n        raise HTTPException(status_code=502, detail=\"Upstream sent a redirect without a Location header\")\n    raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.litellm_core_utils.url_utils import SSRFError\n\ntry:\n    resp = safe_get(client, url)\nexcept SSRFError as e:\n    if \"no Location header\" in str(e):\n        raise HTTPException(status_code=502, detail=\"Upstream sent a redirect without a Location header\")\n    raise","preventionTips":["Expect malformed 3xx responses from hand-rolled servers; map them to 502 with a clear message.","Smoke-test user-facing fetch endpoints against servers known to send odd redirects."],"tags":["ssrf","redirect","http","malformed-response"],"backgroundTag":"invalid-redirect-response","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}