BerriAI/litellm · error · SSRFError
Origin mismatch on scheme
Error message
Origin mismatch on scheme
What it means
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.
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.
Example fix
# before url = "http://example.com/report.pdf" # server 301s to https:// resp = safe_get(client, url) # SSRFError: Origin mismatch on scheme # after url = "https://example.com/report.pdf" resp = safe_get(client, url)
Defensive patterns
Strategy: try-catch
Validate before calling
url = user_url if user_url.startswith("https://") else "https://" + user_url.removeprefix("http://")
# starting on https avoids the common http->https redirect mismatch 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 scheme" in str(e) and url.startswith("http://"):
resp = safe_get(client, "https://" + url[len("http://"):]) # one retry on the https origin
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Users supplying http:// URLs for sites that force HTTPS; any fetch where the canonical URL differs in scheme from the entered one.
Related errors
- Origin mismatch on host
- Origin mismatch on port
- Mavvrik FOCUS destination
- Redirect response has no Location header
- Too many redirects
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/13b5cfc4a657ecf5.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/url_utils.py:349
Hostnames are compared case-insensitively. Default ports are made
explicit (HTTP→80, HTTPS→443) so ``https://api.example.com:443/...``
and ``https://api.example.com/...`` are treated as the same origin.
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."""View on GitHub (pinned to 77b7c6c40c)