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.

3.7 KB
Raw Blame
"""Direct link-local custom LLM endpoints are classified as disallowed.

These are regression tests for the submitted endpoint and host-classification
boundaries. They do not exercise redirects, connection-time name resolution,
or describe current exposure.

Two independent layers have to agree, which is why both are asserted here:

1. ``is_safe_custom_llm_endpoint`` -- the HTTP-boundary guard, called by
   ``/api/start_research``, the follow-up route and the news route.
2. ``_classify_host`` in the egress policy -- reached from ``get_llm``, which
   does not consult layer 1. Both layers must retain the direct-host
   classification.

The regression risk runs the other way too, so the localhost/RFC1918 controls
below are not decoration: ``allow_private_ips=True`` is deliberate, because a
self-hosted LLM backend legitimately lives on 127.0.0.1 or a LAN address. A
change that blocks link-local by blocking "private" wholesale would break every
self-hosted user, and would pass a test that only checked the link-local half.
"""

import pytest

from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
    _classify_host,
)
from local_deep_research.utilities.url_utils import (
    is_safe_custom_llm_endpoint,
)

# Representative IPv4 and IPv6 link-local inputs.
LINK_LOCAL_ENDPOINTS = [
    "http://169.254.10.5/v1",
    "http://169.254.1.1:8080/v1",
    "http://[fe80::1]:8080/v1",
]

# Must keep working: this is where self-hosted backends actually live.
SELF_HOSTED_ENDPOINTS = [
    "http://127.0.0.1:11434/v1",  # ollama default
    "http://10.0.0.5:8080/v1",
    "http://192.168.1.50:1234/v1",
]


def _private_only_ctx():
    return EgressContext(
        scope=EgressScope.PRIVATE_ONLY,
        primary_engine="test",
        require_local_llm=True,
        require_local_embeddings=True,
    )


@pytest.mark.parametrize("url", LINK_LOCAL_ENDPOINTS)
def test_link_local_endpoint_is_refused_at_the_http_boundary(url):
    assert is_safe_custom_llm_endpoint(url) is False, (
        f"{url} was accepted despite direct link-local endpoint policy"
    )


@pytest.mark.parametrize("url", SELF_HOSTED_ENDPOINTS)
def test_self_hosted_endpoint_still_accepted(url):
    """CONTROL. Without this, blocking 'private' wholesale would pass above."""
    assert is_safe_custom_llm_endpoint(url) is True, (
        f"{url} was refused; self-hosted LLM backends live on localhost and "
        "RFC1918 and must keep working -- allow_private_ips=True is deliberate"
    )


def test_public_endpoint_still_accepted():
    """CONTROL: the guard has not simply become deny-all."""
    assert is_safe_custom_llm_endpoint("https://api.openai.com/v1") is True


@pytest.mark.parametrize("host", ["169.254.10.5", "169.254.1.1", "fe80::1"])
def test_link_local_does_not_classify_as_local_in_egress_policy(host):
    """Layer 2 refuses a submitted link-local host under PRIVATE_ONLY."""
    assert _classify_host(host, _private_only_ctx()) is False, (
        f"{host} classified as LOCAL under the PRIVATE_ONLY direct-host policy"
    )


@pytest.mark.parametrize("host", ["127.0.0.1", "10.0.0.5", "192.168.1.50"])
def test_self_hosted_still_classifies_as_local(host):
    """CONTROL for layer 2, mirroring the boundary control above."""
    assert _classify_host(host, _private_only_ctx()) is True, (
        f"{host} no longer classifies as local; PRIVATE_ONLY runs against a "
        "self-hosted backend would be refused"
    )


@pytest.mark.parametrize(
    "host", ["169.254.169.254", "metadata.google.internal"]
)
def test_named_metadata_addresses_remain_blocked(host):
    """The pre-existing literal denylist must survive the range change."""
    assert _classify_host(host, _private_only_ctx()) is False