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.

4.0 KB
Raw Blame
"""``policy_aware_validate_url`` must exclude link-local under PRIVATE_ONLY.

DEFENSE IN DEPTH, not a fix for a demonstrated exploit. Reaching this gate
with an attacker-chosen address requires the operator to have listed the
host in ``local_hostnames`` (or the attacker to control DNS for a host the
operator already listed), so it is a hardening gap rather than an open door.

What it does close: ``ALWAYS_BLOCKED_METADATA_IPS`` holds known literal
metadata addresses, and the remainder of 169.254.0.0/16 still carries
provider-specific metadata endpoints that are not listed there. Before this, the two egress gates disagreed: ``policy.py``
excluded link-local, ``fetch.py`` did not.

The controls below matter as much as the blocks: PRIVATE_ONLY exists so a
lab box on 192.168.x / 127.0.0.1 stays reachable. A change that blocked
those would break the feature this scope is for.
"""

import socket
from contextlib import contextmanager
from unittest.mock import patch

import pytest

from local_deep_research.security.egress.fetch import (
    policy_aware_validate_url,
)
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
)

HOST = "lab.internal"


@contextmanager
def resolves_to(ip: str):
    """Force every DNS answer for HOST to ``ip``."""

    def fake_getaddrinfo(host, port=None, *args, **kwargs):
        return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (ip, port or 80))]

    with patch.object(socket, "getaddrinfo", fake_getaddrinfo):
        yield


def _ctx(scope=EgressScope.PRIVATE_ONLY):
    return EgressContext(
        scope=scope,
        primary_engine="searxng",
        require_local_llm=False,
        require_local_embeddings=False,
        local_hostnames=(HOST,),
    )


@pytest.mark.parametrize(
    "ip,why",
    [
        ("169.254.10.5", "link-local outside the listed metadata literals"),
        ("169.254.169.254", "the canonical metadata literal"),
        ("169.254.1.1", "generic IPv4 link-local"),
    ],
)
def test_link_local_rejected_under_private_only(ip, why):
    with resolves_to(ip):
        assert not policy_aware_validate_url(f"http://{HOST}/x", _ctx()), why


@pytest.mark.parametrize(
    "ip,why",
    [
        ("192.168.1.10", "RFC1918 lab host -- PRIVATE_ONLY exists for this"),
        ("10.0.0.5", "RFC1918 lab host"),
        ("127.0.0.1", "loopback Ollama, the documented use case"),
    ],
)
def test_private_hosts_still_reachable_under_private_only(ip, why):
    """CONTROL: the hardening must not break what PRIVATE_ONLY is for."""
    with resolves_to(ip):
        assert policy_aware_validate_url(f"http://{HOST}/x", _ctx()), why


def test_link_local_still_rejected_with_no_context():
    """CONTROL: strict default path is unaffected and still rejects."""
    with resolves_to("169.254.10.5"):
        assert not policy_aware_validate_url(f"http://{HOST}/x", None)


def test_gate_agrees_with_policy_classify_host():
    """The two egress gates must not disagree about link-local.

    A validator pair that disagrees is its own hazard: whichever one a
    future caller reaches first silently decides the outcome.
    """
    from local_deep_research.security.egress.policy import evaluate_url

    ctx = _ctx()
    with resolves_to("169.254.10.5"):
        fetch_allows = policy_aware_validate_url(f"http://{HOST}/x", ctx)
        try:
            policy_allows = evaluate_url(f"http://{HOST}/x", ctx).allowed
        except Exception:  # pragma: no cover - policy raising is also a block
            policy_allows = False

    assert not fetch_allows, "fetch.py gate must reject link-local"
    # policy.py reaches its local-hostname short-circuit before IP
    # classification, so it is the weaker of the two here. Pinned as an
    # xfail so the day it is tightened, this test tells us instead of
    # quietly passing.
    if policy_allows:
        pytest.xfail(
            "policy.py::_classify_host short-circuits on local_hostnames "
            "before IP classification, so block_link_local never fires on "
            "that path; fetch.py is the gate that stops it today"
        )