{"record":{"id":"6102d2c0ca38ecc8","repo":"BerriAI/litellm","slug":"origin-mismatch-on-host","errorCode":null,"errorMessage":"Origin mismatch on host","messagePattern":"Origin mismatch on host","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":354,"sourceCode":"    Error messages identify *which* component mismatched but never echo\n    the operator's ``expected`` host or the candidate's hostname back to\n    the caller — in the SSRF threat model the caller is the attacker,\n    and reflecting host info would be a secondary leak of operator\n    infrastructure details.\n    \"\"\"\n    candidate: Final = urlparse(candidate_url)\n    expected: Final = urlparse(expected_url)\n\n    if candidate.scheme not in _ALLOWED_SCHEMES:\n        raise SSRFError(\"URL scheme is not allowed\")\n\n    if candidate.scheme != expected.scheme:\n        raise SSRFError(\"Origin mismatch on scheme\")\n\n    candidate_host: Final = _normalize_host(candidate.hostname or \"\")\n    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))","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L336-L372","documentation":"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.","triggerScenarios":"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.","commonSituations":"Fetching user-supplied attachments/documents whose storage redirects to a signing or CDN host; marketing sites that canonicalize to www.","solutions":["Pre-resolve the redirect chain yourself (one manual hop) and pass the final URL as the initial URL to safe_get.","If the alternate host is expected, fetch it directly by using its URL as the starting URL.","Catch SSRFError and surface 'redirects to another host' as a 4xx.","For trusted environments only: disable litellm.user_url_validation."],"exampleFix":"# before\nurl = \"https://app.example.com/files/123\"  # 302 -> https://cdn.example.net/files/123\nresp = safe_get(client, url)                 # SSRFError: Origin mismatch on host\n\n# after — use the final host as the initial URL\nurl = \"https://cdn.example.net/files/123\"\nresp = safe_get(client, url)","handlingStrategy":"try-catch","validationCode":"# Pre-resolve one hop yourself, then hand safe_get the final origin\nraw = client.get(url, follow_redirects=False)\nif raw.is_redirect:\n    from urllib.parse import urljoin\n    final = urljoin(url, raw.headers[\"location\"])\nelse:\n    final = url\nresp = safe_get(client, final)","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 \"Origin mismatch on host\" in str(e):\n        raise HTTPException(status_code=400, detail=\"URL redirects to a different host; supply the final URL directly\")\n    raise","preventionTips":["When you know a URL redirects (shorteners, presigned storage), resolve the final URL first and fetch that with safe_get.","Do not wrap safe_get in a loop that blindly follows Location — that defeats the origin pinning."],"tags":["ssrf","redirect","hostname","origin","cdn"],"backgroundTag":"redirect-origin-mismatch","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}