{"record":{"id":"3cd678ae356bc3b9","repo":"pypa/pip","slug":"domain-too-long","errorCode":null,"errorMessage":"Domain too long","messagePattern":"Domain too long","errorType":"exception","errorClass":"IDNAError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/idna/core.py","lineNumber":625,"sourceCode":"        rendered as Unicode. Matches the per-label recovery prescribed\n        by UTS #46 §4 and the WHATWG URL \"domain to Unicode\" algorithm.\n    :returns: The decoded domain as a Unicode string.\n    :raises IDNAError: If the input is not valid ASCII, contains an\n        invalid label, or is empty.\n    \"\"\"\n    if not isinstance(s, str):\n        try:\n            s = str(s, \"ascii\")\n        except (UnicodeDecodeError, TypeError) as err:\n            raise IDNAError(\"Invalid ASCII in A-label\") from err\n    if len(s) > _max_input_length:\n        raise IDNAError(\"Domain too long\")\n    if uts46:\n        s = uts46_remap(s, std3_rules, False)\n    # Reject inputs that exceed the maximum DNS domain length up-front\n    # to avoid expensive computation on long inputs.\n    if not valid_string_length(s, trailing_dot=True):\n        raise IDNAError(\"Domain too long\")\n    trailing_dot = False\n    result = []\n    labels = s.split(\".\") if strict else _unicode_dots_re.split(s)\n    if not labels or labels == [\"\"]:\n        raise IDNAError(\"Empty domain\")\n    if not labels[-1]:\n        del labels[-1]\n        trailing_dot = True\n    for label in labels:\n        try:\n            u = ulabel(label)\n        except IDNAError:\n            if display and label[:4].lower() == \"xn--\":\n                u = label.lower()\n            else:\n                raise\n        if u:\n            result.append(u)","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/idna/core.py#L607-L643","documentation":"Raised by idna.encode and idna.decode when the input domain string exceeds the maximum allowed length. The check has two layers: a fast guard at _max_input_length (1024 chars) to reject absurdly long strings early, and a standards-based check via valid_string_length enforcing RFC 1035's 253-octet (254 with trailing dot) DNS limit. Domains exceeding these limits cannot be valid DNS names.","triggerScenarios":"Calling idna.encode() or idna.decode() with a string longer than 253 octets (or 254 with trailing dot), or longer than 1024 characters. Also raised after UTS#46 remapping if the result exceeds limits.","commonSituations":"User-supplied or generated domain strings that are unbounded; accidentally passing a full URL or path instead of just the hostname; concatenated subdomains; test data with very long random strings.","solutions":["Validate the hostname length before calling encode/decode: reject if len(host) > 253.","Ensure you are passing only the hostname, not the full URL path or query string.","Truncate or reject oversized input at the data-entry boundary.","Use urllib.parse.urlsplit to extract just the hostname before IDNA processing."],"exampleFix":"# before\nidna.encode('a' * 300 + '.example.com')  # raises Domain too long\n\n# after\nhost = 'a' * 300 + '.example.com'\nif len(host) > 253:\n    raise ValueError(f'Hostname too long: {len(host)} > 253')\nidna.encode(host)","handlingStrategy":"validation","validationCode":"MAX_DNS_LENGTH = 253\n\ndef is_valid_domain_length(domain: str) -> bool:\n    trailing = domain.endswith('.')\n    effective = domain.rstrip('.') if trailing else domain\n    max_len = 254 if trailing else 253\n    return len(effective) <= max_len and len(domain) <= 1024","typeGuard":"def is_valid_domain_length(domain: str) -> bool:\n    return 0 < len(domain) <= 253 or (0 < len(domain) <= 254 and domain.endswith('.'))","tryCatchPattern":"from idna.core import IDNAError\ntry:\n    result = idna.encode(domain)\nexcept IDNAError as e:\n    if 'too long' in str(e).lower():\n        raise ValueError(f'Domain exceeds DNS length limit: {len(domain)}')\n    raise","preventionTips":["Validate hostname length (<=253 chars) before IDNA processing.","Extract only the hostname from URLs before encoding — not the full URL.","Cap user-supplied domain input at the application boundary."],"tags":["idna","dns","validation","length-limit"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}