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.

9.0 KB
Raw Blame
"""Provider construction and content fetching agree on metadata exclusions."""

import ipaddress
import socket
from unittest.mock import Mock

import pytest

from local_deep_research.security import ssrf_validator as ssrf
from local_deep_research.security.egress.fetch import policy_aware_validate_url
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
    _classify_host,
    evaluate_url,
)


def _answers(addresses):
    return [
        (
            socket.AF_INET6
            if ipaddress.ip_address(ip).version == 6
            else socket.AF_INET,
            socket.SOCK_STREAM,
            socket.IPPROTO_TCP,
            "",
            (ip, 80),
        )
        for ip in addresses
    ]


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


@pytest.mark.parametrize(
    "address",
    [
        "168.63.129.16",
        "fd20:ce::254",
        "169.254.42.42",
        "fd00:42::42",
        "fd00:a9fe:a9fe::1",
        "fd00:c1::a9fe:a9fe",
        "fe80::a9fe:a9fe",
        "fe80::a9fe:a9fe%25eth0",
        "192.0.0.192",
        "::ffff:192.0.0.192",
        "fd00:ec2::23",
        "FD00:0EC2:0000:0000:0000:0000:0000:0023",
    ],
)
def test_provider_guard_rejects_metadata_literals(monkeypatch, address):
    # Any resolver call answers with a public address, so the literal must
    # be refused by the metadata check itself: neither a DNS answer nor a
    # local resolver failure (e.g. a missing ``eth0`` zone) can produce the
    # refusal asserted below.
    resolver = Mock(return_value=_answers(["93.184.216.34"]))
    monkeypatch.setattr(socket, "getaddrinfo", resolver)
    host = f"[{address}]" if ":" in address else address
    with pytest.raises(ValueError, match="refusing to send") as excinfo:
        ssrf.assert_base_url_safe(
            f"http://{host}/v1", setting_key="llm.openai_endpoint.url"
        )
    assert "could not resolve" not in str(excinfo.value)
    resolver.assert_not_called()


@pytest.mark.parametrize(
    "addresses",
    [
        ["168.63.129.16"],
        ["192.0.0.192"],
        ["fd00:ec2::23"],
        ["fd20:ce::254"],
        ["169.254.42.42"],
        ["fe80::a9fe:a9fe"],
        ["10.0.0.5", "169.254.42.42"],
        ["93.184.216.34", "168.63.129.16"],
        ["93.184.216.34", "192.0.0.192"],
        ["93.184.216.34", "fd00:ec2::23"],
    ],
)
def test_provider_and_fetch_guards_reject_dns_metadata(monkeypatch, addresses):
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers(addresses)
    )
    url = "http://metadata-fixture.invalid/v1"
    with pytest.raises(ValueError, match="failed SSRF validation"):
        ssrf.assert_base_url_safe(url, setting_key="llm.openai_endpoint.url")
    # The PRIVATE_ONLY fetch gate refuses the same answers. (It does not
    # read local_hostnames; the allowlisted end-to-end path is covered by
    # test_allowlisted_metadata_dns_never_reaches_fetch_transport.)
    assert policy_aware_validate_url(url, _ctx()) is False


@pytest.mark.parametrize(
    "address", ["127.0.0.1", "10.0.0.5", "192.168.1.10", "::1", "fd12::1"]
)
def test_self_hosted_backends_remain_usable(monkeypatch, address):
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers([address])
    )
    url = "http://self-hosted-fixture.invalid/v1"
    assert (
        ssrf.assert_base_url_safe(url, setting_key="llm.openai_endpoint.url")
        == url
    )
    assert policy_aware_validate_url(url, _ctx()) is True


def test_public_provider_remains_usable(monkeypatch):
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers(["93.184.216.34"])
    )
    url = "https://provider-fixture.invalid/v1"
    assert (
        ssrf.assert_base_url_safe(url, setting_key="llm.openai_endpoint.url")
        == url
    )


@pytest.mark.parametrize("scope", list(EgressScope))
def test_legacy_metadata_literal_cannot_be_allowlisted(scope):
    context = _ctx(scope, local_hostnames=("192.0.0.192",))
    assert evaluate_url("http://192.0.0.192/", context).allowed is False
    assert policy_aware_validate_url("http://192.0.0.192/", context) is False


@pytest.mark.parametrize("allow_private", [False, True])
@pytest.mark.parametrize("allow_localhost", [False, True])
@pytest.mark.parametrize("allow_nat64", [False, True])
def test_legacy_metadata_blocks_all_allow_flags(
    allow_private, allow_localhost, allow_nat64
):
    for address in (
        "192.0.0.192",
        "::ffff:192.0.0.192",
        "64:ff9b::c000:c0",
        "64:ff9b:1:c000:0:c000::",
    ):
        assert (
            ssrf.is_ip_blocked(
                address,
                allow_private_ips=allow_private,
                allow_localhost=allow_localhost,
                allow_nat64=allow_nat64,
            )
            is True
        )


@pytest.mark.parametrize("allow_private", [False, True])
@pytest.mark.parametrize("allow_localhost", [False, True])
@pytest.mark.parametrize("allow_nat64", [False, True])
def test_eks_ipv6_metadata_blocks_all_allow_flags(
    allow_private, allow_localhost, allow_nat64
):
    for address in (
        "fd00:ec2::23",
        "FD00:EC2::23",
        "fd00:0ec2:0000:0000:0000:0000:0000:0023",
    ):
        assert ssrf.is_ip_blocked(
            address,
            allow_private_ips=allow_private,
            allow_localhost=allow_localhost,
            allow_nat64=allow_nat64,
        )


@pytest.mark.parametrize("scope", list(EgressScope))
@pytest.mark.parametrize(
    "address",
    [
        "fd00:ec2::23",
        "FD00:EC2::23",
        "fd00:0ec2:0000:0000:0000:0000:0000:0023",
    ],
)
def test_eks_ipv6_metadata_cannot_be_local_or_allowlisted(scope, address):
    assert _classify_host(address, _ctx(scope), allow_dns=False) is False
    context = _ctx(scope, local_hostnames=(address,))
    url = f"http://[{address}]/v1/credentials"
    assert evaluate_url(url, context).allowed is False
    assert policy_aware_validate_url(url, context) is False


@pytest.mark.parametrize(
    "address",
    [
        "169.254.1.2",  # rootless Podman 5.3+ host.containers.internal
        "169.254.10.5",
        "fe80::1",
    ],
)
def test_provider_guard_keeps_link_local_backends_usable(monkeypatch, address):
    # The provider guard deliberately does not set block_link_local: rootless
    # Podman maps host.containers.internal (and host-gateway) to 169.254.1.2,
    # so link-local is a real self-hosted LLM destination. Only the
    # always-blocked metadata literals are refused here.
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers([address])
    )
    host = f"[{address}]" if ":" in address else address
    url = f"http://{host}/v1"
    assert ssrf.assert_base_url_safe(url, setting_key="llm.ollama.url") == url
    url = "http://host.containers.internal:11434"
    assert ssrf.assert_base_url_safe(url, setting_key="llm.ollama.url") == url


@pytest.mark.parametrize(
    "addresses",
    [["169.254.10.5"], ["fe80::1"], ["93.184.216.34", "169.254.1.1"]],
)
def test_private_fetch_guard_still_rejects_link_local_dns(
    monkeypatch, addresses
):
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers(addresses)
    )
    url = "http://link-local-fixture.invalid/v1"
    assert policy_aware_validate_url(url, _ctx()) is False


def test_provider_guard_blocks_nat64_metadata_even_with_operator_opt_in(
    monkeypatch,
):
    monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
    for address in ("64:ff9b::a83f:8110", "64:ff9b::a9fe:a9fe"):
        with pytest.raises(ValueError, match="failed SSRF validation"):
            ssrf.assert_base_url_safe(
                f"http://[{address}]/v1",
                setting_key="llm.openai_endpoint.url",
            )


def test_nat64_private_backend_remains_usable_when_operator_enabled(
    monkeypatch,
):
    address = "64:ff9b::a00:5"
    monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers([address])
    )
    url = f"http://[{address}]/v1"
    assert (
        ssrf.assert_base_url_safe(url, setting_key="llm.openai_endpoint.url")
        == url
    )


def test_private_service_allowlist_is_not_blanket_denied():
    context = _ctx(local_hostnames=("10.0.0.5",))
    assert evaluate_url("http://10.0.0.5/", context).allowed is True


def test_generic_private_validation_retains_explicit_link_local_opt_in(
    monkeypatch,
):
    # Narrow the provider constructor, not the generic validator's existing
    # explicit allow-private contract for callers outside that boundary.
    monkeypatch.setattr(
        socket, "getaddrinfo", lambda *a, **kw: _answers(["169.254.1.1"])
    )
    assert (
        ssrf.validate_url("http://169.254.1.1/", allow_private_ips=True) is True
    )
    assert (
        ssrf.validate_url(
            "http://169.254.1.1/", allow_private_ips=True, block_link_local=True
        )
        is False
    )