{"record":{"id":"13b5cfc4a657ecf5","repo":"BerriAI/litellm","slug":"origin-mismatch-on-scheme","errorCode":null,"errorMessage":"Origin mismatch on scheme","messagePattern":"Origin mismatch on scheme","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":349,"sourceCode":"\n    Hostnames are compared case-insensitively. Default ports are made\n    explicit (HTTP→80, HTTPS→443) so ``https://api.example.com:443/...``\n    and ``https://api.example.com/...`` are treated as the same origin.\n\n    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.\"\"\"","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L331-L367","documentation":"SSRFError raised when a redirect changes the scheme relative to the original URL — the check is strict equality between candidate.scheme and expected.scheme, so both https-to-http downgrades and http-to-https upgrades are rejected. safe_get validates every hop against the initial origin to prevent redirect-driven SSRF bypass.","triggerScenarios":"Starting a fetch at http://example.com/file when the server 301s to https://example.com/file (very common), or the reverse https-to-http downgrade redirect.","commonSituations":"Users supplying http:// URLs for sites that force HTTPS; any fetch where the canonical URL differs in scheme from the entered one.","solutions":["Normalize the initial URL to https:// before fetching when the site is known to serve HTTPS.","If you control the redirect chain, keep the scheme consistent.","Catch SSRFError and retry once with the https form of the initial URL.","As a last resort in trusted environments: litellm.user_url_validation = False."],"exampleFix":"# before\nurl = \"http://example.com/report.pdf\"  # server 301s to https://\nresp = safe_get(client, url)            # SSRFError: Origin mismatch on scheme\n\n# after\nurl = \"https://example.com/report.pdf\"\nresp = safe_get(client, url)","handlingStrategy":"try-catch","validationCode":"url = user_url if user_url.startswith(\"https://\") else \"https://\" + user_url.removeprefix(\"http://\")\n# starting on https avoids the common http->https redirect mismatch","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 scheme\" in str(e) and url.startswith(\"http://\"):\n        resp = safe_get(client, \"https://\" + url[len(\"http://\"):])  # one retry on the https origin\n    else:\n        raise","preventionTips":["Canonicalize user URLs to https:// before fetching since most sites force HTTPS anyway.","Remember the origin check is strict: even http->https upgrades are rejected, so always start on the final scheme."],"tags":["ssrf","redirect","https","origin"],"backgroundTag":"redirect-origin-mismatch","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}