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.

11.7 KB
Raw Blame
"""Fake-IP DNS must not make public destinations look private to egress."""

from __future__ import annotations

import socket
from unittest.mock import patch

import pytest

from local_deep_research.security.egress.audit_hook import (
    _audit_hook,
    clear_active_context,
    set_active_context,
)
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
    PolicyDeniedError,
    _classify_host,
    classify_engine,
    context_from_snapshot,
    evaluate_engine,
    evaluate_url,
    host_not_classified_public,
)
from local_deep_research.security.egress.validators import (
    validate_allowed_local_hostnames,
)
from local_deep_research.security.ssrf_validator import validate_url


def _ctx(scope: EgressScope, **kwargs) -> EgressContext:
    return EgressContext(
        scope=scope,
        primary_engine="paperless",
        require_local_llm=False,
        require_local_embeddings=False,
        **kwargs,
    )


def _fake_ip_answers(*addresses: str) -> list[tuple]:
    return [
        (socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, 0))
        for address in addresses
    ]


@pytest.mark.parametrize(
    "host", ["198.18.0.7", "198.19.255.254", "::ffff:198.18.0.7"]
)
def test_fake_ip_literal_is_unknown_not_private(host):
    ctx = _ctx(EgressScope.PRIVATE_ONLY)
    assert _classify_host(host, ctx) is None
    assert host in ctx._dns_cache


def test_fake_ip_dns_and_mixed_private_answer_remain_unknown():
    ctx = _ctx(EgressScope.PRIVATE_ONLY)
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=_fake_ip_answers("10.0.0.42", "198.18.3.4"),
    ):
        assert _classify_host("news.fakeip.test", ctx) is None
    assert ctx._dns_cache["news.fakeip.test"] is None


@pytest.mark.parametrize("public_first", [False, True])
@pytest.mark.parametrize(
    "scope",
    [
        EgressScope.STRICT,
        EgressScope.PRIVATE_ONLY,
        EgressScope.PUBLIC_ONLY,
        EgressScope.BOTH,
    ],
)
def test_fake_ip_mixed_public_dns_stays_unknown_and_refused(
    scope, public_first
):
    ctx = _ctx(scope)
    addresses = ["198.18.3.4", "8.8.8.8"]
    if public_first:
        addresses.reverse()
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=_fake_ip_answers(*addresses),
    ):
        assert _classify_host("mixed.fakeip.test", ctx) is None
        assert not evaluate_url("https://mixed.fakeip.test/story", ctx).allowed
    assert ctx._dns_cache["mixed.fakeip.test"] is None


@pytest.mark.parametrize(
    "scope",
    [
        EgressScope.STRICT,
        EgressScope.PRIVATE_ONLY,
        EgressScope.PUBLIC_ONLY,
        EgressScope.BOTH,
    ],
)
def test_fake_ip_dns_url_refused_in_every_protected_scope(scope):
    ctx = _ctx(scope)
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=_fake_ip_answers("198.18.3.4"),
    ):
        assert not evaluate_url("https://news.fakeip.test/story", ctx).allowed


@pytest.mark.parametrize(
    "scope", [EgressScope.STRICT, EgressScope.PRIVATE_ONLY]
)
def test_fake_ip_socket_refused_under_local_only_scopes(scope):
    ctx = _ctx(scope)
    set_active_context(ctx)
    try:
        with pytest.raises(PolicyDeniedError):
            _audit_hook("socket.connect", (_IPv4Socket(), ("198.18.3.4", 443)))
    finally:
        clear_active_context()


def test_fake_ip_endpoint_does_not_make_private_engine_eligible():
    setting = "search.engine.web.paperless.default_params.api_url"
    snapshot = {
        "policy.egress_scope": "adaptive",
        setting: "https://paper.fakeip.test/api",
    }
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=_fake_ip_answers("198.18.3.4"),
    ):
        ctx = context_from_snapshot(snapshot, "paperless")
        classification = classify_engine(
            "paperless", ctx, settings_snapshot=snapshot
        )
        decision = evaluate_engine("paperless", ctx, settings_snapshot=snapshot)

    assert ctx.scope == EgressScope.PRIVATE_ONLY
    assert ctx.require_local_llm and ctx.require_local_embeddings
    assert classification.is_public is False
    assert classification.is_local is False
    assert not decision.allowed


@pytest.mark.parametrize("other_host", ["10.0.0.42", "8.8.8.8"])
def test_fake_ip_endpoint_in_engine_host_list_is_not_ignored(other_host):
    snapshot = {
        "search.engine.web.elasticsearch.default_params.hosts": [
            f"http://{other_host}:9200",
            "http://198.18.3.4:9200",
        ]
    }
    ctx = _ctx(EgressScope.PRIVATE_ONLY)

    classification = classify_engine(
        "elasticsearch", ctx, settings_snapshot=snapshot
    )
    decision = evaluate_engine("elasticsearch", ctx, settings_snapshot=snapshot)

    assert classification.is_local is False
    assert classification.is_public is False
    assert not decision.allowed


class _IPv4Socket:
    family = socket.AF_INET


def _connect_through_socket_guard(ctx: EgressContext, address: str) -> None:
    set_active_context(ctx)
    try:
        _audit_hook("socket.connect", (_IPv4Socket(), (address, 8000)))
    finally:
        clear_active_context()


@pytest.mark.parametrize("scope", ["strict", "private_only"])
def test_declared_address_admits_a_real_local_service_at_the_socket_guard(
    scope,
):
    """The documented remedy for a real local service at a fixed address in
    198.18.0.0/15: declare that address, and the hostname it is configured
    by. The socket guard sees only the address a connection goes to."""
    snapshot = {
        "policy.egress_scope": scope,
        "llm.allowed_local_hostnames": ["paper.lab.test", "198.18.3.4"],
        "search.engine.web.paperless.default_params.api_url": (
            "http://paper.lab.test:8000/api"
        ),
    }
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=_fake_ip_answers("198.18.3.4"),
    ):
        ctx = context_from_snapshot(snapshot, "paperless")
        assert ctx.scope.value == scope
        assert evaluate_engine(
            "paperless", ctx, settings_snapshot=snapshot
        ).allowed
        assert evaluate_url("http://paper.lab.test:8000/api", ctx).allowed
        assert evaluate_url("http://198.18.3.4:8000/api", ctx).allowed
        _connect_through_socket_guard(ctx, "198.18.3.4")


@pytest.mark.parametrize("scope", ["strict", "private_only"])
def test_declared_hostname_alone_is_refused_at_the_socket_guard(scope):
    """A declared name stays local at URL checks, but under Strict and
    Private only the socket guard checks the address it resolves to. That
    address must be declared too (docs/egress-modes.md)."""
    ctx = context_from_snapshot(
        {
            "policy.egress_scope": scope,
            "llm.allowed_local_hostnames": ["paper.fakeip.test"],
        },
        "paperless",
    )
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=_fake_ip_answers("198.18.3.4"),
    ):
        assert evaluate_url("https://paper.fakeip.test/api", ctx).allowed
        with pytest.raises(PolicyDeniedError):
            _connect_through_socket_guard(ctx, "198.18.3.4")


def test_allowed_local_hostnames_accepts_an_address_in_the_fake_ip_range():
    def answer_itself(host):
        return _fake_ip_answers(host)

    key = "llm.allowed_local_hostnames"
    with (
        patch(
            "local_deep_research.security.egress.validators._resolve_with_timeout",
            side_effect=answer_itself,
        ),
        patch(
            "local_deep_research.security.egress.policy._resolve_with_timeout",
            side_effect=answer_itself,
        ),
    ):
        assert (
            validate_allowed_local_hostnames({key: ["198.18.3.4"]}, {}) is None
        )
        rejected = validate_allowed_local_hostnames({key: ["8.8.8.8"]}, {})
    assert rejected is not None
    assert "8.8.8.8" in rejected["error"]


def test_unprotected_still_allows_fake_ip_range():
    ctx = _ctx(EgressScope.UNPROTECTED)
    assert evaluate_url("http://198.18.0.7/", ctx).allowed


# NAT64 literals per RFC 6052: the well-known /96 prefix carries the IPv4 in
# the low 32 bits; the RFC 8215 local-use /48 prefix splits it around the
# reserved byte 8 (bytes 6, 7, 9 and 10).
_NAT64_FAKE_IPS = ["64:ff9b::198.18.0.7", "64:ff9b:1:c612:0:700::"]
_NAT64_RFC1918 = ["64:ff9b::10.0.0.5", "64:ff9b:1:a00:0:500::"]
_NAT64_LOCAL_USE_PUBLIC = "64:ff9b:1:808:8:800::"  # 8.8.8.8
_PROTECTED_SCOPES = [
    EgressScope.STRICT,
    EgressScope.PRIVATE_ONLY,
    EgressScope.PUBLIC_ONLY,
    EgressScope.BOTH,
]


class _IPv6Socket:
    family = socket.AF_INET6


@pytest.fixture
def nat64_allowed(monkeypatch):
    """The DNS64+NAT64 deployment ``LDR_SECURITY_ALLOW_NAT64`` exists for."""
    monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")


@pytest.mark.parametrize("scope", _PROTECTED_SCOPES, ids=lambda s: s.value)
@pytest.mark.parametrize("host", _NAT64_FAKE_IPS)
def test_nat64_wrapped_fake_ip_is_unknown_and_refused(
    nat64_allowed, host, scope
):
    """With NAT64 allowed the SSRF check admits a wrapped fake IP, so the
    policy must keep it as ambiguous as the bare address."""
    url = f"http://[{host}]/story"
    assert validate_url(url, allow_private_ips=False)
    ctx = _ctx(scope)

    assert _classify_host(host, ctx) is None
    assert host_not_classified_public(url, ctx)
    assert not evaluate_url(url, ctx).allowed


@pytest.mark.parametrize("host", _NAT64_RFC1918)
def test_nat64_wrapped_rfc1918_classifies_private(nat64_allowed, host):
    url = f"http://[{host}]/"

    assert _classify_host(host, _ctx(EgressScope.PRIVATE_ONLY)) is True
    assert evaluate_url(url, _ctx(EgressScope.PRIVATE_ONLY)).allowed
    decision = evaluate_url(url, _ctx(EgressScope.PUBLIC_ONLY))
    assert decision.reason == "scope_mismatch_public_only"


@pytest.mark.parametrize(
    "scope",
    [EgressScope.STRICT, EgressScope.PRIVATE_ONLY],
    ids=lambda s: s.value,
)
@pytest.mark.parametrize(
    ("host", "expected"),
    [(_NAT64_FAKE_IPS[1], None), (_NAT64_LOCAL_USE_PUBLIC, False)],
    ids=["fake_ip", "public"],
)
def test_nat64_local_use_prefix_does_not_make_payload_local(
    nat64_allowed, host, expected, scope
):
    """Current Python releases count 64:ff9b:1::/48 as private whatever it
    carries. The embedded IPv4 decides, at the policy check and at the
    socket guard."""
    ctx = _ctx(scope)

    assert _classify_host(host, ctx) is expected
    assert not evaluate_url(f"http://[{host}]/", ctx).allowed
    set_active_context(ctx)
    try:
        with pytest.raises(PolicyDeniedError):
            _audit_hook("socket.connect", (_IPv6Socket(), (host, 443, 0, 0)))
    finally:
        clear_active_context()


@pytest.mark.parametrize(
    ("answer", "expected"),
    [
        ("64:ff9b::198.18.3.4", None),
        ("64:ff9b::10.0.0.42", True),
        (_NAT64_LOCAL_USE_PUBLIC, False),
    ],
    ids=["fake_ip", "rfc1918", "public"],
)
def test_dns64_answers_classify_by_embedded_ipv4(
    nat64_allowed, answer, expected
):
    ctx = _ctx(EgressScope.PRIVATE_ONLY)
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=[
            (socket.AF_INET6, socket.SOCK_STREAM, 6, "", (answer, 0, 0, 0))
        ],
    ):
        assert _classify_host("dns64.example.test", ctx) is expected


@pytest.mark.parametrize("scope", _PROTECTED_SCOPES, ids=lambda s: s.value)
def test_nat64_literals_stay_refused_when_nat64_is_disabled(monkeypatch, scope):
    monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "false")
    for host in [*_NAT64_FAKE_IPS, *_NAT64_RFC1918, _NAT64_LOCAL_USE_PUBLIC]:
        assert not evaluate_url(f"http://[{host}]/", _ctx(scope)).allowed