BerriAI/litellm · error · SSRFError
URL scheme is not allowed
Error message
URL scheme is not allowed
What it means
SSRFError raised by the same-origin check safe_get applies to every redirect hop (assert_same_origin): the redirect target's scheme must be http or https. A Location header pointing at ftp://, file://, or any other scheme is rejected before the hop is followed. This variant is specifically about the redirect target — the initial URL's scheme is checked separately by validate_url with a different message.
Solutions
- Do not follow the redirect — treat it as an upstream error and surface it to the caller.
- Avoid or fix the upstream URL whose chain redirects off http(s).
- Catch SSRFError around safe_get and map it to a 4xx.
Example fix
# before
resp = safe_get(client, url) # 3xx Location: ftp://mirror/file -> unhandled SSRFError
# after
try:
resp = safe_get(client, url)
except SSRFError as e:
raise HTTPException(status_code=400, detail=f"URL fetch refused: {e}") Defensive patterns
Strategy: try-catch
Validate before calling
from urllib.parse import urlparse
def initial_url_is_http(url: str) -> bool:
try:
return urlparse(url).scheme in ("http", "https")
except ValueError:
return False Try / catch
from litellm.litellm_core_utils.url_utils import SSRFError
try:
resp = safe_get(client, url)
except SSRFError as e:
raise HTTPException(status_code=400, detail=f"URL fetch refused: {e}") Prevention
- Validate the initial URL's scheme before fetching; most non-http(s) redirect targets come from the same careless sources.
- Treat redirect-refusal errors as upstream data quality problems, not transient failures to retry.
When it happens
Trigger: A fetched URL responds 3xx with Location: ftp://... or another non-http(s) scheme; servers redirecting to protocol handlers; deliberate SSRF attempts hopping to file://.
Common situations: Legacy servers that redirect to ftp mirrors; misconfigured Location headers; penetration tests chaining redirects to forbidden schemes.
Related errors
- URL scheme ' ' is not allowed
- Compresr guardrail api_base
- Origin mismatch on host
- Origin mismatch on port
- Origin mismatch on scheme
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/9bf6e728d2a213f4.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/url_utils.py:346
configured ``api_base``, but the URL it hands back must actually point
back at the same origin or we'd be blindly forwarding credentials
wherever the upstream told us to.
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
View on GitHub (pinned to 77b7c6c40c)