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.

37.7 KB
Raw Blame
"""Host classification accepts only canonical host spellings (#6869 follow-up).

Host classification -- the require-local LLM / embeddings checks, the
Elasticsearch / Paperless endpoint fail-up, the ``llm.allowed_local_hostnames``
override and that setting's save-time validator -- reads a host through ONE
canonicaliser, ``policy._canonical_host``: a host containing both ``:`` and
``%`` as written (an IPv6 zone ID, encoded or not) is refused before any
decoding; any other host is percent-decoded exactly once, case folded, one
trailing root dot dropped. The result is classified only if it is a plain DNS
name or an IP literal ``ipaddress`` parses as written. Every other spelling,
and every cloud-metadata name or address (wrapped in an IPv6 form or not), is
unclassified: never local, never eligible as a public endpoint, and ignored
when listed in ``llm.allowed_local_hostnames``.

The earlier design enumerated metadata spellings instead, and each review
round found one more that slipped through (most recently a zone ID encoded
twice, which callers decoded once and the metadata check decoded again). The
property tests below run a generated corpus of spellings through every
classification entry point instead of listing cases one by one:

* no non-canonical spelling is ever classified local or eligible as public;
* no canonical metadata name or address (or an IPv6 wrap of one) is;
* canonical private / public names and addresses keep their classification
  (regression control), whichever canonical-preserving spelling is used.

``evaluate_url`` (the fetch-time check) keeps its broader normalisation; the
corpus also pins that it refuses every spelling of a metadata address a
single-decoding resolver or HTTP client would connect to.
"""

import ipaddress
import socket
from collections import namedtuple
from unittest.mock import patch
from urllib.parse import urlsplit

import pytest

from local_deep_research.security.egress import validators
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
    _canonical_host,
    _classify_host,
    evaluate_embeddings,
    evaluate_engine,
    evaluate_llm_endpoint,
    evaluate_url,
    host_not_classified_public,
    url_host_is_declared_local,
)
from local_deep_research.security.ssrf_validator import (
    ALWAYS_BLOCKED_METADATA_HOSTNAMES,
    ALWAYS_BLOCKED_METADATA_IPS,
)

# ---------------------------------------------------------------------------
# Corpus generator (pure: standard library only)
# ---------------------------------------------------------------------------

# host: the spelling as urlsplit / a configured value would carry it.
# canonical: what _canonical_host must return for it (None: unclassified
#   spelling). An IPv6 wrap of an IPv4 address is a canonical literal.
# metadata: the destination is a cloud-metadata endpoint.
# fetch_metadata: evaluate_url must refuse it as blocked_metadata_ip, because
#   a single-decoding resolver or HTTP client reaches a metadata endpoint.
Spelling = namedtuple(
    "Spelling", "host base canonical metadata fetch_metadata transform"
)

# Non-metadata bases. Their canonical spellings are the regression controls.
CONTROL_IPV4 = ["10.0.0.42", "192.168.1.5", "127.0.0.1", "93.184.216.34"]
CONTROL_IPV6 = ["fd12::1", "::1", "2606:4700:4700::1111"]
CONTROL_NAMES = ["gpu-box.lan", "search.example.com", "ollama_service"]

_FULLWIDTH = {
    **{c: chr(ord(c) - ord("0") + 0xFF10) for c in "0123456789"},
    **{
        c: chr(ord(c) - ord("a") + 0xFF41) for c in "abcdefghijklmnopqrstuvwxyz"
    },
    **{
        c: chr(ord(c) - ord("A") + 0xFF21) for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    },
    ":": ":",
}


def _fullwidth(text):
    return "".join(_FULLWIDTH.get(c, c) for c in text)


def _percent_all(text):
    return "".join(f"%{b:02x}" for b in text.encode())


# Control and whitespace characters a configured value or a parser
# differential could carry into a host.
_CONTROL_SUFFIXES = ["\x00", "\t", "\n", "\x7f", "\x1f"]

# IPv6 zone identifiers, as written in the host: numeric, hex-looking,
# alphabetic, the RFC 6874 "%25" form, and twice-encoded forms whose single
# decode leaves a residual "%" (a second decode would turn "%10" into a
# control character, the round-3 case). Also zone IDs that are two hex digits
# or contain an encoded colon, whose single decode splices them into a
# different, valid address ("fd00:ec2::254%31" decodes to fd00:ec2::2541,
# "%41" to a trailing "A", "%3a1" to a further ":1" group), while an HTTP
# client reads them as the zone of the address written before the "%".
_ZONES = [
    "%1",
    "%10",
    "%a",
    "%eth0",
    "%25eth0",
    "%2510",
    "%251",
    "%252510",
    "%31",
    "%41",
    "%3a1",
    "%3A1",
]


def _ipv6_from_bytes(data):
    return str(ipaddress.IPv6Address(bytes(data)))


def _ipv6_wraps(v4):
    """Canonical IPv6 literals that carry ``v4`` where an IPv4-embedding
    mechanism puts the address it reaches."""
    o = list(ipaddress.IPv4Address(v4).packed)
    hi, lo = (o[0] << 8) | o[1], (o[2] << 8) | o[3]
    inverted = [b ^ 0xFF for b in o]
    return {
        "mapped": f"::ffff:{v4}",
        "mapped_hex": f"::ffff:{hi:x}:{lo:x}",
        "nat64_wkp": f"64:ff9b::{v4}",
        # RFC 6052 /48 layout: octets 6-7, then 9-10 around the zero octet 8.
        "nat64_local_use": _ipv6_from_bytes(
            [0, 0x64, 0xFF, 0x9B, 0, 1, o[0], o[1], 0, o[2], o[3]] + [0] * 5
        ),
        # A network-specific NAT64 prefix (RFC 6052 section 2.2) in every
        # layout: /96 and /64, and /56, /40 and /32, where the address is
        # split around the zero octet 8 or sits in octets 4-7.
        "nat64_nsp_96": f"2001:db8:64::{v4}",
        "nat64_nsp_64": _ipv6_from_bytes(
            [0x20, 0x01, 0x0D, 0xB8, 0, 0x64, 0, 0, 0] + o + [0, 0, 0]
        ),
        "nat64_nsp_56": _ipv6_from_bytes(
            [0x20, 0x01, 0x0D, 0xB8, 0, 0x64, 0x01, o[0], 0] + o[1:] + [0] * 4
        ),
        "nat64_nsp_40": _ipv6_from_bytes(
            [0x20, 0x01, 0x0D, 0xB8, 0x64] + o[:3] + [0, o[3]] + [0] * 6
        ),
        "nat64_nsp_32": _ipv6_from_bytes(
            [0x20, 0x01, 0x0D, 0xB8] + o + [0] * 8
        ),
        "compatible": f"::{v4}",
        "translated": f"::ffff:0:{v4}",
        "6to4": _ipv6_from_bytes([0x20, 0x02] + o + [0] * 9 + [1]),
        # Teredo: server IPv4 in octets 4-7, client IPv4 inverted in 12-15.
        "teredo_client": _ipv6_from_bytes(
            [0x20, 0x01, 0, 0, 203, 0, 113, 1, 0x80, 0, 0xFF, 0xFF] + inverted
        ),
        "teredo_server": _ipv6_from_bytes(
            [0x20, 0x01, 0, 0]
            + o
            + [0x80, 0, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 0xFE]
        ),
        "isatap": f"fe80::5efe:{v4}",
    }


def _ipv4_spellings(v4, metadata):
    o = list(ipaddress.IPv4Address(v4).packed)
    n = int(ipaddress.IPv4Address(v4))
    preserving = {
        "as_is": v4,
        "upper": v4.upper(),
        "trailing_dot": v4 + ".",
        "pct_dots": v4.replace(".", "%2e"),
        "pct_dots_upper": v4.replace(".", "%2E"),
        "pct_all": _percent_all(v4),
    }
    # Spellings a resolver may still read as ``v4`` (evaluate_url refuses a
    # metadata one), but which classification refuses to interpret.
    resolver_forms = {
        "integer": str(n),
        "integer_trailing_dot": f"{n}.",
        "hex": f"0x{n:08x}",
        "hex_upper": f"0X{n:08X}",
        "octal_dotted": ".".join(f"0{b:o}" for b in o),
        "hex_dotted": ".".join(f"0x{b:x}" for b in o),
        "short_3": f"{o[0]}.{o[1]}.{(o[2] << 8) | o[3]}",
        "short_2": f"{o[0]}.{(o[1] << 16) | (o[2] << 8) | o[3]}",
        "double_trailing_dot": v4 + "..",
        "fullwidth": _fullwidth(v4),
        "ideographic_dots": v4.replace(".", "。"),
        "fullwidth_dots": v4.replace(".", "."),
        "trailing_ideographic_dot": v4 + "。",
        "pct_hex": _percent_all(f"0x{n:08x}"),
    }
    # Spellings no resolver reads as ``v4``.
    other_forms = {
        # Zero-padded octets read as octal, so not as ``v4``.
        "padded": ".".join(f"{b:04d}" for b in o),
        "double_pct": v4.replace(".", "%252e"),
        "bracketed": f"[{v4}]",
        "zone_rfc6874": v4 + "%25eth0",
        "zone_raw": v4 + "%eth0",
        "pct_nul": v4 + "%00",
        "leading_space": " " + v4,
    }
    for i, suffix in enumerate(_CONTROL_SUFFIXES):
        other_forms[f"control_{i}"] = v4 + suffix
    out = [
        Spelling(host, v4, v4, metadata, metadata, f"v4:{name}")
        for name, host in preserving.items()
    ]
    out += [
        Spelling(host, v4, None, metadata, metadata, f"v4:{name}")
        for name, host in resolver_forms.items()
    ]
    out += [
        Spelling(host, v4, None, metadata, False, f"v4:{name}")
        for name, host in other_forms.items()
    ]
    if metadata:
        for name, wrap in _ipv6_wraps(v4).items():
            canonical = str(ipaddress.IPv6Address(wrap))
            for host in (wrap, f"[{wrap.upper()}]"):
                out.append(
                    Spelling(host, v4, canonical, True, True, f"wrap:{name}")
                )
    return out


def _ipv6_spellings(v6, metadata):
    ip = ipaddress.IPv6Address(v6)
    canonical = str(ip)
    preserving = {
        "as_is": v6,
        "bracketed": f"[{v6}]",
        "upper": v6.upper(),
        "exploded": ip.exploded,
        "bracketed_exploded_upper": f"[{ip.exploded.upper()}]",
        "pct_colons": v6.replace(":", "%3a"),
    }
    zoned = {f"zone{zone}": v6 + zone for zone in _ZONES}
    zoned["bracketed_zone"] = f"[{v6}%25eth0]"
    zoned["bracketed_raw_zone"] = f"[{v6}%10]"
    zoned["exploded_zone"] = ip.exploded.upper() + "%1"
    other_forms = {
        "trailing_dot": v6 + ".",
        "bracketed_trailing_dot": f"[{v6}].",
        "double_pct_colons": v6.replace(":", "%253a"),
        "fullwidth": _fullwidth(v6),
        "leading_space": " " + v6,
    }
    for i, suffix in enumerate(_CONTROL_SUFFIXES):
        other_forms[f"control_{i}"] = v6 + suffix
    out = [
        Spelling(host, v6, canonical, metadata, metadata, f"v6:{name}")
        for name, host in preserving.items()
    ]
    # A zone-scoped literal reaches the address on that interface; the
    # fetch check refuses a metadata one, classification refuses them all.
    out += [
        Spelling(host, v6, None, metadata, metadata, f"v6:{name}")
        for name, host in zoned.items()
    ]
    out += [
        Spelling(host, v6, None, metadata, False, f"v6:{name}")
        for name, host in other_forms.items()
    ]
    return out


def _name_spellings(name, metadata):
    preserving = {
        "as_is": name,
        "upper": name.upper(),
        "title": name.title(),
        "trailing_dot": name + ".",
        "pct_dots": name.replace(".", "%2e"),
        "pct_first": f"%{ord(name[0]):02x}" + name[1:],
    }
    # Spellings the resolver's IDNA encoding folds back to ``name``.
    idna_forms = {
        "fullwidth": _fullwidth(name),
        "ideographic_dots": name.replace(".", "。"),
        "fullwidth_dots": name.replace(".", "."),
        "halfwidth_dots": name.replace(".", "。"),
        "trailing_ideographic_dot": name + "。",
        "soft_hyphen": name[:2] + "­" + name[2:],
        "double_trailing_dot": name + "..",
    }
    other_forms = {
        "double_pct": name.replace(".", "%252e"),
        "pct_nul": name + "%00",
        "zone": name + "%25eth0",
        "bracketed": f"[{name}]",
        "leading_space": " " + name,
        "non_ascii_suffix": name + "é",
    }
    for i, suffix in enumerate(_CONTROL_SUFFIXES):
        other_forms[f"control_{i}"] = name + suffix
    # A dot-replacing transform is a no-op on a single-label name.
    idna_forms = {k: v for k, v in idna_forms.items() if v != name}
    other_forms = {k: v for k, v in other_forms.items() if v != name}
    out = [
        Spelling(host, name, name, metadata, metadata, f"name:{label}")
        for label, host in preserving.items()
    ]
    out += [
        Spelling(host, name, None, metadata, metadata, f"name:{label}")
        for label, host in idna_forms.items()
    ]
    out += [
        Spelling(host, name, None, metadata, False, f"name:{label}")
        for label, host in other_forms.items()
    ]
    return out


def spelling_corpus(metadata_ips, metadata_names):
    """Every spelling of every metadata and control base."""
    corpus = []
    for address in sorted(metadata_ips):
        if ipaddress.ip_address(address).version == 4:
            corpus += _ipv4_spellings(address, True)
        else:
            corpus += _ipv6_spellings(address, True)
    for name in sorted(metadata_names):
        corpus += _name_spellings(name, True)
    for address in CONTROL_IPV4:
        corpus += _ipv4_spellings(address, False)
    for address in CONTROL_IPV6:
        corpus += _ipv6_spellings(address, False)
    for name in CONTROL_NAMES:
        corpus += _name_spellings(name, False)
    return corpus


def url_for(host):
    """An http URL whose urlsplit hostname is ``host`` (case folded), or
    None when the spelling cannot round-trip through a URL (urlsplit drops
    tabs and newlines, and rejects some bracketed forms)."""
    authority = host
    if ":" in host and not host.startswith("["):
        authority = f"[{host}]"
    url = f"http://{authority}:8000/v1"
    try:
        parsed = urlsplit(url).hostname
    except ValueError:
        return None
    expected = authority.lower()
    if expected.startswith("[") and expected.endswith("]"):
        expected = expected[1:-1]
    return url if parsed == expected else None


CORPUS = spelling_corpus(
    ALWAYS_BLOCKED_METADATA_IPS, ALWAYS_BLOCKED_METADATA_HOSTNAMES
)
UNCLASSIFIED = [s for s in CORPUS if s.canonical is None or s.metadata]
CANONICAL_CONTROLS = [s for s in CORPUS if s.canonical and not s.metadata]

# ---------------------------------------------------------------------------
# Fixtures and helpers
# ---------------------------------------------------------------------------

PRIVATE_ANSWER = [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("10.0.0.42", 0))]
PUBLIC_ANSWER = [
    (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0))
]
PROTECTED_SCOPES = [
    EgressScope.PUBLIC_ONLY,
    EgressScope.BOTH,
    EgressScope.PRIVATE_ONLY,
    EgressScope.STRICT,
]
ENGINE_SETTINGS = {
    "paperless": "search.engine.web.paperless.default_params.api_url",
    "elasticsearch": "search.engine.web.elasticsearch.default_params.hosts",
}
LOCAL_HOSTNAMES_KEY = "llm.allowed_local_hostnames"


@pytest.fixture(autouse=True)
def _canonical_corpus_without_active_context():
    """The fetch-check loops below make thousands of evaluate_url calls.
    Its denial quota follows the thread's active run context when one is
    armed, so make sure none leaks in from elsewhere; the loops also pass
    record_denial=False and a fresh context per call."""
    from local_deep_research.security.egress.audit_hook import (
        clear_active_context,
    )

    clear_active_context()
    yield
    clear_active_context()


def _resolver(answer):
    """A resolver stub that answers ``answer`` for any name and, as
    getaddrinfo does, an IP literal with itself."""

    def resolve(host):
        try:
            address = ipaddress.ip_address(host)
        except ValueError:
            return answer
        family = socket.AF_INET6 if address.version == 6 else socket.AF_INET
        return [(family, socket.SOCK_STREAM, 6, "", (str(address), 0))]

    return resolve


def _dns(answer):
    return patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        side_effect=_resolver(answer),
    )


def _ctx(scope=EgressScope.PRIVATE_ONLY, *, local=(), engine="paperless"):
    return EgressContext(
        scope=scope,
        primary_engine=engine,
        require_local_llm=True,
        require_local_embeddings=True,
        local_hostnames=tuple(local),
    )


def _engine_values(engine, spelling):
    """The setting values that configure ``engine`` with this host."""
    values = []
    url = url_for(spelling.host)
    if url is not None:
        values.append(url if engine == "paperless" else [url])
    if engine == "elasticsearch":
        values.append([{"host": spelling.host, "port": 9200, "scheme": "http"}])
    return values


def _expected_control(base, answer):
    """Main's classification of a canonical control base."""
    try:
        address = ipaddress.ip_address(base)
    except ValueError:
        return answer is PRIVATE_ANSWER  # a name: its DNS answer decides
    return address.is_private or address.is_loopback


# ---------------------------------------------------------------------------
# Corpus properties
# ---------------------------------------------------------------------------


def test_corpus_covers_every_kind_of_spelling():
    """Sanity: the generator produced each family the properties rely on."""
    kinds = {s.transform.split(":")[1] for s in CORPUS}
    for family in (
        "double_pct",
        "integer",
        "hex",
        "octal_dotted",
        "short_3",
        "fullwidth",
        "ideographic_dots",
        "zone%2510",
        "zone%eth0",
        "zone%31",
        "zone%3a1",
        "trailing_dot",
        "bracketed",
        "mapped",
        "nat64_wkp",
        "nat64_nsp_96",
        "nat64_nsp_64",
        "nat64_nsp_56",
        "nat64_nsp_40",
        "nat64_nsp_32",
        "compatible",
        "6to4",
        "teredo_client",
        "control_0",
    ):
        assert family in kinds, family
    assert len(UNCLASSIFIED) > 400
    assert len(CANONICAL_CONTROLS) > 50


def test_corpus_canonical_form_is_exactly_the_expected_one():
    """_canonical_host accepts only plain DNS names and IP literals that
    ipaddress parses as written, after one percent-decode, case folding and
    one trailing dot; it returns None for every other spelling."""
    wrong = [
        (s.transform, s.host, _canonical_host(s.host), s.canonical)
        for s in CORPUS
        if _canonical_host(s.host) != s.canonical
    ]
    assert not wrong


@pytest.mark.parametrize("answer", [PRIVATE_ANSWER, PUBLIC_ANSWER])
def test_corpus_unclassified_host_is_never_local_even_when_listed(answer):
    """A non-canonical spelling or a metadata host is not local -- also when
    it is itself listed in llm.allowed_local_hostnames, and whatever DNS
    answers -- and is never declared local at the DNS-pinning step."""
    local = []
    with _dns(answer):
        for s in UNCLASSIFIED:
            ctx = _ctx(local=[s.host])
            if _classify_host(s.host, ctx) is not False:
                local.append((s.transform, s.host, "classify_host"))
            url = url_for(s.host)
            if url is not None and url_host_is_declared_local(url, ctx):
                local.append((s.transform, s.host, "declared_local"))
    assert not local


@pytest.mark.parametrize("answer", [PRIVATE_ANSWER, PUBLIC_ANSWER])
@pytest.mark.parametrize("engine", list(ENGINE_SETTINGS))
def test_corpus_unclassified_engine_endpoint_is_refused_in_every_scope(
    engine, answer
):
    """An Elasticsearch or Paperless endpoint with such a host is neither
    local nor failed up to public: evaluate_engine refuses it as
    unclassified under every protected scope. The private answer is what
    would make it local; the public answer is what would fail it up."""
    admitted = []
    with _dns(answer):
        for s in UNCLASSIFIED:
            for value in _engine_values(engine, s):
                for scope in PROTECTED_SCOPES:
                    decision = evaluate_engine(
                        engine,
                        _ctx(scope, local=[s.host], engine=engine),
                        settings_snapshot={ENGINE_SETTINGS[engine]: value},
                    )
                    if decision.allowed or decision.reason != "unclassified":
                        admitted.append(
                            (s.transform, s.host, scope.value, decision.reason)
                        )
    assert not admitted


def test_corpus_require_local_inference_refuses_unclassified_host():
    """require-local LLM and embeddings checks never accept such a host as
    local, even listed in llm.allowed_local_hostnames with a private DNS
    answer."""
    accepted = []
    with _dns(PRIVATE_ANSWER):
        for s in UNCLASSIFIED:
            url = url_for(s.host)
            if url is None:
                continue
            ctx = _ctx(local=[s.host])
            llm = evaluate_llm_endpoint(
                "ollama", ctx, settings_snapshot={"llm.ollama.url": url}
            )
            embeddings = evaluate_embeddings(
                "openai",
                ctx,
                settings_snapshot={"embeddings.openai.base_url": url},
            )
            ollama_embeddings = evaluate_embeddings(
                "ollama",
                ctx,
                settings_snapshot={"embeddings.ollama.url": url},
            )
            for check, decision in (
                ("llm", llm),
                ("embeddings_openai", embeddings),
                ("embeddings_ollama", ollama_embeddings),
            ):
                if decision.allowed:
                    accepted.append((s.transform, s.host, check))
    assert not accepted


def test_corpus_local_hostnames_validator_refuses_unclassified_entry():
    """The save-time validator refuses every such entry, never raises, and
    calls an entry a cloud-metadata endpoint only when it is written in a
    canonical spelling without ``%``; a non-canonical or percent-encoded
    spelling is reported as not a valid hostname for the list."""
    wrong = []
    with (
        _dns(PRIVATE_ANSWER),
        patch.object(
            validators,
            "_resolve_with_timeout",
            side_effect=_resolver(PRIVATE_ANSWER),
        ),
    ):
        for s in UNCLASSIFIED:
            result = validators.validate_allowed_local_hostnames(
                {LOCAL_HOSTNAMES_KEY: [s.host]}, {}
            )
            error = (result or {}).get("error", "")
            if s.canonical is None or "%" in s.host:
                ok = (
                    "not valid hostnames" in error
                    and "cloud-metadata" not in error
                )
            else:
                ok = "cloud-metadata" in error and "not valid" not in error
            if not ok:
                wrong.append((s.transform, s.host, error))
    assert not wrong


@pytest.mark.parametrize("answer", [PRIVATE_ANSWER, PUBLIC_ANSWER])
def test_corpus_canonical_controls_keep_their_classification(answer):
    """Regression control: a canonical private / public name or address, in
    any canonical-preserving spelling (case, one trailing dot, single
    percent-encoding, IPv6 brackets or exploded form), classifies as before
    -- local iff private (a name: iff its DNS answer is), and a Paperless
    endpoint on it is local or fails up to public accordingly."""
    wrong = []
    with _dns(answer):
        for s in CANONICAL_CONTROLS:
            expected = _expected_control(s.base, answer)
            got = _classify_host(s.host, _ctx())
            if got is not expected:
                wrong.append((s.transform, s.host, "classify_host", got))
            url = url_for(s.host)
            if url is None:
                continue
            setting = {ENGINE_SETTINGS["paperless"]: url}
            private = evaluate_engine(
                "paperless",
                _ctx(EgressScope.PRIVATE_ONLY),
                settings_snapshot=setting,
            )
            public = evaluate_engine(
                "paperless",
                _ctx(EgressScope.PUBLIC_ONLY),
                settings_snapshot=setting,
            )
            if (private.allowed, public.allowed) != (expected, not expected):
                wrong.append((s.transform, s.host, "paperless", expected))
            llm = evaluate_llm_endpoint(
                "ollama", _ctx(), settings_snapshot={"llm.ollama.url": url}
            )
            if llm.allowed is not expected:
                wrong.append((s.transform, s.host, "llm", llm.reason))
    assert not wrong


def test_corpus_fetch_check_refuses_every_reachable_metadata_spelling():
    """evaluate_url keeps its broader normalisation: every spelling a
    single-decoding resolver or client connects to a metadata endpoint with
    -- canonical, alternate IPv4 notation, IDNA-folded, zone-scoped, IPv6
    wrapped -- is refused as blocked_metadata_ip in every scope, before any
    scope or local_hostnames logic. The resolver answers public, so no other
    denial reason can stand in for it."""
    allowed = []
    with _dns(PUBLIC_ANSWER):
        for s in CORPUS:
            url = url_for(s.host)
            if not s.fetch_metadata or url is None:
                continue
            for scope in EgressScope:
                decision = evaluate_url(
                    url, _ctx(scope, local=[s.host]), record_denial=False
                )
                if decision.reason != "blocked_metadata_ip":
                    allowed.append((s.transform, s.host, scope.value))
    assert not allowed


@pytest.mark.parametrize("answer", [PRIVATE_ANSWER, PUBLIC_ANSWER])
def test_corpus_uncounted_prefilter_agrees_with_the_fetch_check(answer):
    """host_not_classified_public (full_search's uncounted pre-filter) is
    True exactly when evaluate_url under public_only refuses the URL as
    scope_mismatch_public_only, for every spelling in the corpus, listed in
    local_hostnames or not; anything evaluate_url refuses for another
    reason first (metadata, malformed host) is left to it."""
    disagree = []
    with _dns(answer):
        for s in CORPUS:
            url = url_for(s.host)
            if url is None:
                continue
            for local in ((), (s.host,)):
                refused = host_not_classified_public(url, _ctx(local=local))
                decision = evaluate_url(
                    url,
                    _ctx(EgressScope.PUBLIC_ONLY, local=local),
                    record_denial=False,
                )
                expected = decision.reason == "scope_mismatch_public_only"
                if refused is not expected:
                    disagree.append((s.transform, s.host, decision.reason))
    assert not disagree


# ---------------------------------------------------------------------------
# Targeted: the round-3 zone case
# ---------------------------------------------------------------------------
#
# ``fd00:ec2::254%2510`` is the AWS IPv6 metadata address with its zone ID
# encoded twice. Callers used to percent-decode the host and the metadata
# check decoded it again, so it saw "fd00:ec2::254" followed by a control
# character, did not recognise it, and the host fell through to the
# address checks (which read the zone after ONE decode and call the address
# not local). The engine fail-up then made Paperless eligible as public, and
# a listed local_hostnames entry made it local.

ROUND3_URLS = [
    "http://[fd00:ec2::254%2510]:8000",
    "http://[fe80::a9fe:a9fe%2510]:8000",
]


@pytest.mark.parametrize("scope", PROTECTED_SCOPES)
@pytest.mark.parametrize("url", ROUND3_URLS)
def test_round3_double_encoded_zone_paperless_is_refused(url, scope):
    with _dns(PUBLIC_ANSWER):
        decision = evaluate_engine(
            "paperless",
            _ctx(scope),
            settings_snapshot={ENGINE_SETTINGS["paperless"]: url},
        )
    assert not decision.allowed
    assert decision.reason == "unclassified"


@pytest.mark.parametrize("scope", PROTECTED_SCOPES)
@pytest.mark.parametrize(
    "hosts",
    [
        [f"{ROUND3_URLS[0]}"],
        f"http://10.0.0.42:9200,{ROUND3_URLS[0]}",
        [{"host": "fd00:ec2::254%10", "port": 9200, "scheme": "http"}],
        [{"host": "fd00:ec2::254%2510", "port": 9200, "scheme": "http"}],
    ],
)
def test_round3_double_encoded_zone_elasticsearch_is_refused(hosts, scope):
    with _dns(PUBLIC_ANSWER):
        decision = evaluate_engine(
            "elasticsearch",
            _ctx(scope, engine="elasticsearch"),
            settings_snapshot={ENGINE_SETTINGS["elasticsearch"]: hosts},
        )
    assert not decision.allowed
    assert decision.reason == "unclassified"


@pytest.mark.parametrize(
    "entry", ["fd00:ec2::254%10", "fe80::a9fe:a9fe%10", "fd12::1%10"]
)
def test_round3_zone_scoped_local_hostnames_entry_is_ignored(entry):
    """A zone-scoped entry (metadata or not) is not a canonical spelling, so
    it is ignored: the host it names is not local, and a require-local LLM
    endpoint on it is refused even with a private DNS answer."""
    url = f"http://[{entry.replace('%', '%25')}]:11434"
    ctx = _ctx(local=[entry])
    with _dns(PRIVATE_ANSWER):
        assert _classify_host(entry, ctx) is False
        assert _classify_host(urlsplit(url).hostname, ctx) is False
        decision = evaluate_llm_endpoint(
            "ollama", ctx, settings_snapshot={"llm.ollama.url": url}
        )
    assert not decision.allowed
    assert decision.reason == "provider_remote"


@pytest.mark.parametrize(
    "host",
    ["fd00:ec2::254%31", "fd12::1%31", "fd12::1%41", "[fd12::1%3a1]"],
)
def test_encoded_zone_id_is_not_read_as_part_of_the_address(host):
    """``%`` is both the IPv6 zone delimiter and the escape character. An
    HTTP client reads ``fd12::1%31`` as fd12::1 on interface "31", while
    one decode reads the different address fd12::11 (and
    ``fd00:ec2::254%31``, the AWS metadata address on an interface, as
    fd00:ec2::2541). Such a host has no canonical spelling: it is not
    local, and a require-local LLM endpoint on it is refused even with a
    private DNS answer."""
    assert _canonical_host(host) is None
    url = f"http://[{host.strip('[]')}]:11434"
    with _dns(PRIVATE_ANSWER):
        assert _classify_host(host, _ctx()) is False
        decision = evaluate_llm_endpoint(
            "ollama", _ctx(), settings_snapshot={"llm.ollama.url": url}
        )
    assert not decision.allowed
    assert decision.reason == "provider_remote"


def test_round3_zone_scoped_entry_is_rejected_at_save_without_metadata_claim():
    with patch.object(
        validators, "_resolve_with_timeout", return_value=PRIVATE_ANSWER
    ):
        result = validators.validate_allowed_local_hostnames(
            {LOCAL_HOSTNAMES_KEY: ["fd12::1%10"]}, {}
        )
    assert result is not None
    assert "not valid hostnames" in result["error"]
    assert "metadata" not in result["error"]


# ---------------------------------------------------------------------------
# The fetch check (evaluate_url) is never looser than before
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("scope", list(EgressScope))
@pytest.mark.parametrize(
    "host", ["169%252e254%252e169%252e254", "fd00%253aec2%253a%253a254"]
)
def test_fetch_check_refuses_a_metadata_address_encoded_twice(host, scope):
    """No HTTP client decodes twice, but a host that only reaches a metadata
    address after a second decode is never legitimate; the broad fetch check
    refuses it as one in every scope."""
    url = f"http://{host}/latest"
    with _dns(PRIVATE_ANSWER):
        decision = evaluate_url(url, _ctx(scope, local=[host]))
    assert decision.reason == "blocked_metadata_ip"


@pytest.mark.parametrize("scope", list(EgressScope))
@pytest.mark.parametrize(
    "host",
    [
        "api%25252eservice%25252esoftlayer%25252ecom",
        "metadata%25252egoogle%25252einternal",
    ],
)
def test_fetch_check_refuses_a_metadata_name_encoded_three_times(host, scope):
    """Every spelling the fetch check derives, the host decoded until stable
    included, is matched against the metadata hostnames, not only the
    once-decoded one. The private DNS answer would otherwise admit the host
    under PRIVATE_ONLY and STRICT."""
    url = f"http://{host}/latest"
    with _dns(PRIVATE_ANSWER):
        decision = evaluate_url(url, _ctx(scope, local=[host]))
    assert decision.reason == "blocked_metadata_ip"


@pytest.mark.parametrize("scope", list(EgressScope))
@pytest.mark.parametrize(
    "host",
    [
        "0.0.0.0",
        "0%2e0%2e0%2e0",
        "[::]",
        # 6to4 and Teredo addresses carrying 127.0.0.1.
        "[2002:7f00:1::]",
        "[2001:0:4136:e378:8000:63bf:80ff:fffe]",
    ],
)
def test_fetch_check_refuses_other_always_blocked_addresses(host, scope):
    """Not metadata, but in the SSRF validator's blocked ranges and not one
    of the private ranges it can allow: refused by the is_ip_blocked check
    over every spelling, under the same reason, before classification (which
    would call them public)."""
    with _dns(PUBLIC_ANSWER):
        decision = evaluate_url(f"http://{host}:8000/", _ctx(scope))
    assert decision.reason == "blocked_metadata_ip"


@pytest.mark.parametrize("scope", list(EgressScope))
@pytest.mark.parametrize(
    "host", ["gpu-box.lan%00", "gpu-box.lan%09", "gpu-box.lan%0a"]
)
def test_fetch_check_refuses_a_host_with_control_characters(host, scope):
    """A host that decodes to a control character reaches nothing (the
    resolver refuses an embedded NUL); it is refused as malformed in every
    scope instead of being classified, and is not counted as a policy
    denial."""
    with _dns(PRIVATE_ANSWER):
        decision = evaluate_url(f"http://{host}/", _ctx(scope))
    assert not decision.allowed
    assert decision.reason == "url_malformed"


@pytest.mark.parametrize(
    "scope", [EgressScope.PUBLIC_ONLY, EgressScope.BOTH, EgressScope.STRICT]
)
@pytest.mark.parametrize("host", ["fd12::1%2d", "fd12::1%10"])
def test_fetch_check_zone_id_read_two_ways_is_not_admitted(host, scope):
    """An HTTP client reads ``fd12::1%2d`` as the private fd12::1 on
    interface "2d", while the once-decoded text ``fd12::1-`` is no address
    and classifies public after a lookup. The two readings disagree, so the
    fetch check leaves the host unclassified rather than admit it under
    PUBLIC_ONLY. (``fd12::1%10`` decodes to a control character and is
    refused as malformed.)"""
    with _dns(PUBLIC_ANSWER):
        decision = evaluate_url(f"http://[{host}]/", _ctx(scope))
    assert not decision.allowed


@pytest.mark.parametrize(
    "entry", ["0x0a00002a", "b\u00fccherei.lan", "fd12::1%eth0"]
)
@pytest.mark.parametrize(
    "scope",
    [EgressScope.PUBLIC_ONLY, EgressScope.PRIVATE_ONLY, EgressScope.STRICT],
)
def test_fetch_check_ignored_local_hostnames_entry_cannot_admit_its_host(
    entry, scope
):
    """A non-canonical llm.allowed_local_hostnames entry is ignored, but a
    URL host matching it exactly must not become admissible because of
    that: it is unclassified at fetch time (it used to be local), so with a
    public DNS answer it is still refused under PUBLIC_ONLY, and it is not
    admitted as local under PRIVATE_ONLY / STRICT."""
    url = url_for(entry)
    assert url is not None
    with _dns(PUBLIC_ANSWER):
        decision = evaluate_url(url, _ctx(scope, local=[entry]))
    assert not decision.allowed


# ---------------------------------------------------------------------------
# Declared local hostnames are compared in canonical form
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "entry", ["GPU-Box.LAN", "gpu-box.lan.", "gpu%2dbox.lan"]
)
@pytest.mark.parametrize(
    "url", ["http://gpu-box.lan:11434", "http://GPU-BOX.lan.:11434"]
)
def test_declared_local_hostname_matches_in_canonical_form(entry, url):
    """An entry and a URL host that are the same canonical name match
    whatever their case or trailing dot, so the declaration takes effect
    (and holds at the DNS-pinning step) even though DNS answers public."""
    ctx = _ctx(local=[entry])
    with _dns(PUBLIC_ANSWER):
        decision = evaluate_llm_endpoint(
            "ollama", ctx, settings_snapshot={"llm.ollama.url": url}
        )
    assert decision.allowed, decision.reason
    assert url_host_is_declared_local(url, ctx)


@pytest.mark.parametrize(
    "host", ["\u212a8s.lan", "%E2%84%AA8s.lan", "%e2%84%aa8s.lan"]
)
def test_non_ascii_letter_that_lowercases_to_ascii_is_unclassified(host):
    """U+212A KELVIN SIGN lower-cases to an ASCII "k". The ASCII test runs on
    the decoded host before case folding, so this is not read as the
    declared ``k8s.lan``, and such an entry is refused at save."""
    assert _canonical_host(host) is None
    with _dns(PRIVATE_ANSWER):
        assert _classify_host(host, _ctx(local=["k8s.lan"])) is False
        assert _classify_host(host, _ctx(local=[host])) is False
    with patch.object(
        validators,
        "_resolve_with_timeout",
        side_effect=_resolver(PRIVATE_ANSWER),
    ):
        result = validators.validate_allowed_local_hostnames(
            {LOCAL_HOSTNAMES_KEY: [host]}, {}
        )
    assert result is not None
    assert "not valid hostnames" in result["error"]


@pytest.mark.parametrize(
    "entry", ["gpu%2dbox.lan", "10%2e0%2e0%2e42", "%67pu-box.lan", "fd12::1%25"]
)
def test_local_hostnames_validator_refuses_a_percent_encoded_entry(entry):
    """A new entry with ``%`` is refused at save even when one decode would
    make it a plain private name: no hostname needs percent-encoding, and
    the saved text then always is the name it matches."""
    with patch.object(
        validators,
        "_resolve_with_timeout",
        side_effect=_resolver(PRIVATE_ANSWER),
    ):
        result = validators.validate_allowed_local_hostnames(
            {LOCAL_HOSTNAMES_KEY: [entry]}, {}
        )
    assert result is not None
    assert "not valid hostnames" in result["error"]


# ---------------------------------------------------------------------------
# The settings validator never raises
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "value",
    [
        ["a\x00b"],
        ["\x00"],
        ["%00"],
        ["169.254.169.254\x00"],
        ["0x" + "f" * 4096],
        ["a" * 10000],
        ["[" * 50],
        ["%"],
        [12, None, {"host": "x"}, ["nested"]],
        '["\\u0000"]',
    ],
)
def test_local_hostnames_validator_never_raises(value):
    with patch.object(
        validators, "_resolve_with_timeout", return_value=PRIVATE_ANSWER
    ):
        result = validators.validate_allowed_local_hostnames(
            {LOCAL_HOSTNAMES_KEY: value}, {}
        )
    assert result is None or set(result) == {"key", "error"}