{"record":{"id":"afbfc2822c3a705b","repo":"BerriAI/litellm","slug":"origin-mismatch-on-port","errorCode":null,"errorMessage":"Origin mismatch on port","messagePattern":"Origin mismatch on port","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":360,"sourceCode":"    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))\n\n\ndef safe_get(client: Any, url: str, **kwargs: Any) -> Any:\n    \"\"\"\n    Fetch a user-supplied URL with SSRF protection on every redirect hop.\n","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L342-L378","documentation":"SSRFError raised when a redirect target's effective port differs from the original URL's, with 80/443 defaults applied per scheme. A hop from https://host (443) to https://host:8443, or http://host to http://host:8080, is an origin change and is rejected like any other cross-origin redirect.","triggerScenarios":"Servers that redirect the same host to a different port (admin listeners on :8443, port-forcing proxies); URLs that omit the port initially but whose redirect Location adds one.","commonSituations":"Internal tools behind port-rewriting proxies; containerized services that redirect from a gateway port to a service port.","solutions":["Use the exact final URL (host and port) as the initial fetch URL.","Fix the upstream redirect to preserve the port.","Catch SSRFError and report the mismatch to the caller as a 4xx."],"exampleFix":"# before\nresp = safe_get(client, \"https://gw.example.com/dl\")  # 302 -> https://gw.example.com:8443/dl\n# SSRFError: Origin mismatch on port\n\n# after\nresp = safe_get(client, \"https://gw.example.com:8443/dl\")","handlingStrategy":"try-catch","validationCode":"from urllib.parse import urlparse\n\n# start with the exact final host:port when the chain is known to hop ports\np = urlparse(url)\nif p.port is None and known_redirect_port(p.hostname):\n    url = url.replace(p.netloc, f\"{p.netloc}:{known_redirect_port(p.hostname)}\", 1)","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 port\" in str(e):\n        raise HTTPException(status_code=400, detail=\"URL redirects to a different port; supply the final URL\")\n    raise","preventionTips":["Supply the final URL including its explicit port when the upstream is known to redirect across ports.","Treat port-mismatch errors as configuration information about the target, not transient failures."],"tags":["ssrf","redirect","port","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"}