{"record":{"id":"dcf7572c566f8b4a","repo":"BerriAI/litellm","slug":"url-has-no-hostname","errorCode":null,"errorMessage":"URL has no hostname","messagePattern":"URL has no hostname","errorType":"exception","errorClass":"SSRFError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/url_utils.py","lineNumber":269,"sourceCode":"        url: The user-supplied URL to validate.\n\n    Returns:\n        Tuple of (rewritten_url, host_header).\n        The rewritten URL has the hostname replaced with the validated IP.\n        The host_header value should be sent as the Host header.\n\n    Raises:\n        SSRFError: If the URL scheme is invalid or the hostname resolves\n            to a private/internal IP address.\n    \"\"\"\n    parsed: Final = urlparse(url)\n\n    if parsed.scheme not in _ALLOWED_SCHEMES:\n        raise SSRFError(f\"URL scheme '{parsed.scheme}' is not allowed\")\n\n    hostname: Final = parsed.hostname\n    if not hostname:\n        raise SSRFError(\"URL has no hostname\")\n\n    port: Final = parsed.port\n    default_port: Final = _default_port_for_scheme(parsed.scheme)\n    effective_port: Final = port if port is not None else default_port\n    host_header: Final = _format_host_header(hostname, effective_port, default_port)\n\n    is_allowlisted: Final = _is_host_allowlisted(hostname, effective_port)\n\n    # Resolve hostname and validate ALL addresses\n    try:\n        addrinfo: Final = socket.getaddrinfo(hostname, effective_port, proto=socket.IPPROTO_TCP)\n    except socket.gaierror as e:\n        raise SSRFError(f\"DNS resolution failed for '{hostname}': {e}\")\n\n    if not addrinfo:\n        raise SSRFError(f\"No addresses found for '{hostname}'\")\n\n    if not is_allowlisted:","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/url_utils.py#L251-L287","documentation":"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.","triggerScenarios":"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).","commonSituations":"Config typos ('http://' + '' + '/path'); host env variables left unset; users entering just a path or pasting broken links.","solutions":["Validate hostname presence before the call with urlparse(url).hostname.","Fix the URL construction that produced an empty host slot.","Return 400 with a clear message when the user-supplied URL is malformed."],"exampleFix":"# before\nbase, path = os.getenv(\"AGENT_BASE\", \"\"), \"/v1/x\"\nresp = safe_get(client, f\"http://{base}{path}\")  # base empty -> \"http:///v1/x\"\n\n# after\nbase = os.getenv(\"AGENT_BASE\")\nif not base:\n    raise RuntimeError(\"AGENT_BASE is not configured\")\nresp = safe_get(client, f\"http://{base}{path}\")","handlingStrategy":"validation","validationCode":"from urllib.parse import urlparse\n\np = urlparse(user_url)\nif not p.hostname:\n    raise HTTPException(status_code=400, detail=\"URL is missing a hostname\")\nresp = safe_get(client, user_url)","typeGuard":"from urllib.parse import urlparse\n\ndef url_has_hostname(value: str) -> bool:\n    try:\n        return bool(urlparse(value).hostname)\n    except ValueError:\n        return False","tryCatchPattern":"from litellm.litellm_core_utils.url_utils import SSRFError\n\ntry:\n    resp = safe_get(client, url)\nexcept SSRFError as e:\n    if \"no hostname\" in str(e):\n        raise HTTPException(status_code=400, detail=\"Malformed URL: no hostname\")\n    raise","preventionTips":["Validate parsed.hostname is non-empty before any fetch of user-supplied URLs.","Fail fast on empty host env vars/config values used in URL construction."],"tags":["ssrf","url-validation","hostname","malformed-url"],"backgroundTag":"invalid-url-format","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}