Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.5 KB
Raw Blame
"""Recognize ambiguous historical IPv4 host syntax without network access."""

import ipaddress
from typing import Final
from urllib.parse import unquote_to_bytes


AMBIGUOUS_NUMERIC_IPV4_HOST_ERROR: Final = "Ambiguous numeric IPv4 host"
ENCODED_NUMERIC_IPV4_HOST_ERROR: Final = "Encoded numeric IPv4 host"
_COMPONENT_MAXIMUMS: Final[tuple[tuple[int, ...], ...]] = (
    (0xFFFFFFFF,),
    (0xFF, 0xFFFFFF),
    (0xFF, 0xFF, 0xFFFF),
    (0xFF, 0xFF, 0xFF, 0xFF),
)


def is_ambiguous_numeric_ipv4_host(hostname: str) -> bool:
    """Return whether ``hostname`` is a valid noncanonical historical IPv4 form.

    This recognizes the one-to-four-part inet_aton-style grammar using decimal,
    leading-zero octal, and ``0x`` hexadecimal components. It deliberately does
    not perform DNS or platform parsing.
    """
    try:
        _ = ipaddress.ip_address(hostname)
    except ValueError:
        components = hostname.split(".")
    else:
        return False

    if not 1 <= len(components) <= len(_COMPONENT_MAXIMUMS):
        return False

    maximums = _COMPONENT_MAXIMUMS[len(components) - 1]
    return all(
        _parse_component(component, maximum) is not None
        for component, maximum in zip(components, maximums, strict=True)
    )


def is_percent_encoded_numeric_ipv4_host(hostname: str) -> bool:
    """Return whether one-pass percent decoding exposes an IPv4 address form."""
    decoded_hostname = _decode_percent_escapes(hostname)
    if decoded_hostname is None or decoded_hostname == hostname:
        return False
    classification_hostname = decoded_hostname.rstrip(".")

    try:
        parsed_ip = ipaddress.ip_address(classification_hostname)
    except ValueError:
        return is_ambiguous_numeric_ipv4_host(classification_hostname)
    return isinstance(parsed_ip, ipaddress.IPv4Address)


def raw_authority_host(url: str) -> str | None:
    """Return the host text of ``url``'s authority exactly as written.

    The authority is the part between ``//`` and the first ``/``, ``?`` or
    ``#``; userinfo (up to the last ``@``) and a trailing ``:port`` are
    dropped and nothing is decoded or lowercased. Returns ``None`` when the
    URL has no ``//`` authority or the host is a bracketed IP literal.
    """
    start = url.find("//")
    if start < 0:
        return None
    rest = url[start + 2 :]
    end = len(rest)
    for delimiter in "/?#":
        found = rest.find(delimiter)
        if found >= 0:
            end = min(end, found)
    authority = rest[:end].rpartition("@")[2]
    if authority.startswith("["):
        return None
    host, _, port = authority.rpartition(":")
    if not host or not (port == "" or port.isdigit()):
        host = authority
    return host


def has_percent_encoded_numeric_ipv4_authority(url: str) -> bool:
    """Return whether ``url`` spells its host as percent-encoded IPv4.

    Deliberately works on the raw URL text, never on a parser's host: urllib3
    2.8 percent-decodes http(s) hosts (2.7 did not), so a parsed host can no
    longer show that the URL was encoded. Callers must refuse these URLs
    whatever the private-IP policy, since the encoded spelling is what the
    policy checks cannot see through.
    """
    host = raw_authority_host(url)
    return host is not None and is_percent_encoded_numeric_ipv4_host(host)


def has_percent_escape_in_authority_host(url: str) -> bool:
    """Return whether ``url``'s raw authority host contains any ``%``.

    urllib3 2.8 percent-decodes escapes of unreserved characters in http(s)
    hosts (``ev%69l.example`` parses as ``evil.example``), while other HTTP
    clients such as httpx send the escaped text to the resolver unchanged.
    A validator that checks the decoded host therefore checks a different
    name from the one those clients resolve. No real DNS name needs a
    percent escape, so callers refuse these hosts outright. Bracketed IP
    literals (whose IPv6 zone id is written ``%25``) are not covered.
    """
    host = raw_authority_host(url)
    return host is not None and "%" in host


def _decode_percent_escapes(hostname: str) -> str | None:
    """Decode only well-formed ASCII percent escapes without recursive decoding."""
    position = 0
    while (percent := hostname.find("%", position)) >= 0:
        if (
            percent + 2 >= len(hostname)
            or _digit_value(hostname[percent + 1]) is None
            or _digit_value(hostname[percent + 2]) is None
        ):
            return None
        position = percent + 3

    try:
        return unquote_to_bytes(hostname).decode("ascii")
    except UnicodeDecodeError:
        return None


def _parse_component(component: str, maximum: int) -> int | None:
    """Parse one historical IPv4 component without integer overflow."""
    if not component:
        return None

    base = 10
    digits = component
    if component.startswith(("0x", "0X")):
        base = 16
        digits = component[2:]
    elif len(component) > 1 and component.startswith("0"):
        base = 8
        digits = component[1:]

    if not digits:
        return None

    value = 0
    for character in digits:
        digit = _digit_value(character)
        if digit is None or digit >= base or value > (maximum - digit) // base:
            return None
        value = value * base + digit
    return value


def _digit_value(character: str) -> int | None:
    if "0" <= character <= "9":
        return ord(character) - ord("0")
    if "a" <= character <= "f":
        return ord(character) - ord("a") + 10
    if "A" <= character <= "F":
        return ord(character) - ord("A") + 10
    return None