BerriAI/litellm · error · SSRFError

Origin mismatch on port

Error message

Origin mismatch on port

What it means

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.

Solutions

  1. Use the exact final URL (host and port) as the initial fetch URL.
  2. Fix the upstream redirect to preserve the port.
  3. Catch SSRFError and report the mismatch to the caller as a 4xx.

Example fix

# before
resp = safe_get(client, "https://gw.example.com/dl")  # 302 -> https://gw.example.com:8443/dl
# SSRFError: Origin mismatch on port

# after
resp = safe_get(client, "https://gw.example.com:8443/dl")
Defensive patterns

Strategy: try-catch

Validate before calling

from urllib.parse import urlparse

# start with the exact final host:port when the chain is known to hop ports
p = urlparse(url)
if p.port is None and known_redirect_port(p.hostname):
    url = url.replace(p.netloc, f"{p.netloc}:{known_redirect_port(p.hostname)}", 1)

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 port" in str(e):
        raise HTTPException(status_code=400, detail="URL redirects to a different port; supply the final URL")
    raise

Prevention

When it happens

Trigger: 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.

Common situations: Internal tools behind port-rewriting proxies; containerized services that redirect from a gateway port to a service port.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/afbfc2822c3a705b. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/url_utils.py:360

    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."""
    location: Final = response.headers.get("location")
    if not isinstance(location, str) or not location:
        raise SSRFError("Redirect response has no Location header")
    # Resolve relative URLs against the request URL
    return str(httpx.URL(request_url).join(location))


def safe_get(client: Any, url: str, **kwargs: Any) -> Any:
    """
    Fetch a user-supplied URL with SSRF protection on every redirect hop.

View on GitHub (pinned to 77b7c6c40c)