BerriAI/litellm · error · SSRFError

URL has no hostname

Error message

URL has no hostname

What it means

SSRFError raised by validate_url when the URL parses but has no hostname — 'http:///path', 'http://:8080/x', or scheme-only strings. The hostname is mandatory because the next step resolves DNS and checks every returned address against the block policy; without it validation cannot proceed.

Solutions

  1. Validate hostname presence before the call with urlparse(url).hostname.
  2. Fix the URL construction that produced an empty host slot.
  3. Return 400 with a clear message when the user-supplied URL is malformed.

Example fix

# before
base, path = os.getenv("AGENT_BASE", ""), "/v1/x"
resp = safe_get(client, f"http://{base}{path}")  # base empty -> "http:///v1/x"

# after
base = os.getenv("AGENT_BASE")
if not base:
    raise RuntimeError("AGENT_BASE is not configured")
resp = safe_get(client, f"http://{base}{path}")
Defensive patterns

Strategy: validation

Validate before calling

from urllib.parse import urlparse

p = urlparse(user_url)
if not p.hostname:
    raise HTTPException(status_code=400, detail="URL is missing a hostname")
resp = safe_get(client, user_url)

Type guard

from urllib.parse import urlparse

def url_has_hostname(value: str) -> bool:
    try:
        return bool(urlparse(value).hostname)
    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:
    if "no hostname" in str(e):
        raise HTTPException(status_code=400, detail="Malformed URL: no hostname")
    raise

Prevention

When it happens

Trigger: URLs like 'http:///v1/file', 'http://:9999', or a URL assembled by concatenation where the host slot ended up empty (empty env var, blank config field).

Common situations: Config typos ('http://' + '' + '/path'); host env variables left unset; users entering just a path or pasting broken links.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


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

Appendix: source

Thrown at litellm/litellm_core_utils/url_utils.py:269

        url: The user-supplied URL to validate.

    Returns:
        Tuple of (rewritten_url, host_header).
        The rewritten URL has the hostname replaced with the validated IP.
        The host_header value should be sent as the Host header.

    Raises:
        SSRFError: If the URL scheme is invalid or the hostname resolves
            to a private/internal IP address.
    """
    parsed: Final = urlparse(url)

    if parsed.scheme not in _ALLOWED_SCHEMES:
        raise SSRFError(f"URL scheme '{parsed.scheme}' is not allowed")

    hostname: Final = parsed.hostname
    if not hostname:
        raise SSRFError("URL has no hostname")

    port: Final = parsed.port
    default_port: Final = _default_port_for_scheme(parsed.scheme)
    effective_port: Final = port if port is not None else default_port
    host_header: Final = _format_host_header(hostname, effective_port, default_port)

    is_allowlisted: Final = _is_host_allowlisted(hostname, effective_port)

    # Resolve hostname and validate ALL addresses
    try:
        addrinfo: Final = socket.getaddrinfo(hostname, effective_port, proto=socket.IPPROTO_TCP)
    except socket.gaierror as e:
        raise SSRFError(f"DNS resolution failed for '{hostname}': {e}")

    if not addrinfo:
        raise SSRFError(f"No addresses found for '{hostname}'")

    if not is_allowlisted:

View on GitHub (pinned to 77b7c6c40c)