{"record":{"id":"9bf6e728d2a213f4","repo":"BerriAI/litellm","slug":"url-scheme-is-not-allowed","errorCode":null,"errorMessage":"URL scheme is not allowed","messagePattern":"URL scheme is not allowed","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":346,"sourceCode":"    configured ``api_base``, but the URL it hands back must actually point\n    back at the same origin or we'd be blindly forwarding credentials\n    wherever the upstream told us to.\n\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","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L328-L364","documentation":"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.","triggerScenarios":"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://.","commonSituations":"Legacy servers that redirect to ftp mirrors; misconfigured Location headers; penetration tests chaining redirects to forbidden schemes.","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."],"exampleFix":"# before\nresp = safe_get(client, url)  # 3xx Location: ftp://mirror/file -> unhandled SSRFError\n\n# after\ntry:\n    resp = safe_get(client, url)\nexcept SSRFError as e:\n    raise HTTPException(status_code=400, detail=f\"URL fetch refused: {e}\")","handlingStrategy":"try-catch","validationCode":"from urllib.parse import urlparse\n\ndef initial_url_is_http(url: str) -> bool:\n    try:\n        return urlparse(url).scheme in (\"http\", \"https\")\n    except ValueError:\n        return False","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    raise HTTPException(status_code=400, detail=f\"URL fetch refused: {e}\")","preventionTips":["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."],"tags":["ssrf","redirect","scheme","security"],"backgroundTag":"url-scheme-not-allowed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}