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.

6.3 KB
Raw Blame
"""IPv6 zone identifiers must not change the metadata deny decision."""

import json
import os
from pathlib import Path
import socket
import subprocess
import sys
import textwrap
from unittest.mock import Mock

import pytest

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


SCOPED_METADATA_IPS = (
    "fd20:ce::254%1",
    "fd20:ce::254%eth0",
    "fd00:ec2::254%1",
    "fd00:ec2::254%eth0",
    "fd00:ec2::254%25eth0",
    "FD00:0EC2:0000:0000:0000:0000:0000:0254%1",
    "fd00:ec2::23%1",
    "fd00:ec2::23%eth0",
    "fd00:ec2::23%25eth0",
    "FD00:0EC2:0000:0000:0000:0000:0000:0023%1",
)


def _context(scope, local_hostnames=()):
    return EgressContext(
        scope=scope,
        primary_engine="library",
        require_local_llm=False,
        require_local_embeddings=False,
        local_hostnames=local_hostnames,
    )


@pytest.mark.parametrize("address", SCOPED_METADATA_IPS)
@pytest.mark.parametrize("allow_private", [False, True])
@pytest.mark.parametrize("allow_localhost", [False, True])
@pytest.mark.parametrize("allow_nat64", [False, True])
@pytest.mark.parametrize("block_link_local", [False, True])
def test_scoped_metadata_blocks_every_allow_flag_combination(
    address, allow_private, allow_localhost, allow_nat64, block_link_local
):
    assert ssrf.is_ip_blocked(
        address,
        allow_private_ips=allow_private,
        allow_localhost=allow_localhost,
        allow_nat64=allow_nat64,
        block_link_local=block_link_local,
    )


@pytest.mark.parametrize("address", SCOPED_METADATA_IPS)
@pytest.mark.parametrize("scope", list(EgressScope))
@pytest.mark.parametrize("explicit_local", [False, True])
def test_scoped_metadata_is_denied_before_scope_or_local_hostname_allowances(
    address, scope, explicit_local
):
    context = _context(scope, (address,) if explicit_local else ())
    decision = evaluate_url(f"http://[{address}]/v1", context)
    assert decision.allowed is False
    assert decision.reason == "blocked_metadata_ip"


@pytest.mark.parametrize("address", SCOPED_METADATA_IPS)
def test_scoped_metadata_provider_rejection_does_not_depend_on_dns(
    monkeypatch, address
):
    canonical_address = address.partition("%")[0]
    resolver = Mock(
        return_value=[
            (
                socket.AF_INET6,
                socket.SOCK_STREAM,
                socket.IPPROTO_TCP,
                "",
                (canonical_address, 80, 0, 0),
            )
        ]
    )
    monkeypatch.setattr(socket, "getaddrinfo", resolver)
    with pytest.raises(ValueError, match="failed SSRF validation"):
        ssrf.assert_base_url_safe(
            f"http://[{address}]/v1", setting_key="llm.openai_endpoint.url"
        )
    resolver.assert_not_called()


@pytest.mark.parametrize(
    "scope", [EgressScope.PRIVATE_ONLY, EgressScope.STRICT]
)
@pytest.mark.parametrize(
    "address", ["fd12::1%1", "fd12::1%eth0", "::1%1", "::1%lo"]
)
def test_ordinary_scoped_private_and_loopback_destinations_remain_allowed(
    scope, address
):
    assert not ssrf.is_ip_blocked(
        address,
        allow_localhost=True,
        allow_private_ips=True,
        block_link_local=True,
    )
    assert evaluate_url(f"http://[{address}]/v1", _context(scope)).allowed


@pytest.mark.parametrize(
    "scope", [EgressScope.PRIVATE_ONLY, EgressScope.STRICT]
)
def test_installed_socket_hook_rejects_scoped_metadata_before_transport(scope):
    # Audit hooks cannot be removed. Use an isolated child so the final
    # connection-refusal witness does not affect unrelated source tests.
    probe = textwrap.dedent(
        """
        import json, socket, sys
        from pathlib import Path
        from local_deep_research.security import install_audit_hook
        from local_deep_research.security import ssrf_validator
        from local_deep_research.security.egress.audit_hook import active_egress_context
        from local_deep_research.security.egress.policy import EgressContext, EgressScope, PolicyDeniedError

        assert Path(ssrf_validator.__file__).resolve() == Path(sys.argv[2]).resolve()
        install_audit_hook()
        witness = []

        class TransportRefused(RuntimeError):
            pass

        def no_wire(event, args):
            if event == "socket.connect":
                witness.append(args[1][0])
                raise TransportRefused("Stop every admitted connect before network I/O")

        sys.addaudithook(no_wire)
        metadata = ["fd00:ec2::254", "fd00:ec2::254%1", "fd00:ec2::23", "fd00:ec2::23%1"]
        controls = ["fd12::1", "fd12::1%1", "::1", "::1%1"]
        context = EgressContext(scope=EgressScope(sys.argv[1]), primary_engine="library",
                               require_local_llm=False, require_local_embeddings=False)
        results = []
        with active_egress_context(context):
            for address in metadata + controls:
                sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
                try:
                    sock.connect((address, 80, 0, 0))
                    raise AssertionError("The final witness must stop every connect")
                except PolicyDeniedError as exc:
                    results.append({"address": address, "denied": True, "reason": exc.decision.reason})
                except TransportRefused:
                    results.append({"address": address, "denied": False, "reason": None})
                finally:
                    sock.close()
        print(json.dumps({"results": results, "witness": witness}))
        """
    )
    environment = os.environ.copy()
    source_root = Path(ssrf.__file__).resolve().parents[2]
    environment["PYTHONPATH"] = str(source_root)
    environment["PYTHONDONTWRITEBYTECODE"] = "1"
    result = subprocess.run(
        [sys.executable, "-B", "-c", probe, scope.value, ssrf.__file__],
        env=environment,
        text=True,
        capture_output=True,
        check=True,
        timeout=30,
    )
    observed = json.loads(result.stdout)
    assert all(
        item["denied"] and item["reason"] == "blocked_metadata_ip"
        for item in observed["results"][:4]
    )
    assert all(not item["denied"] for item in observed["results"][4:])
    assert observed["witness"] == ["fd12::1", "fd12::1%1", "::1", "::1%1"]