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.8 KB
Raw Blame
"""Egress validation must apply to setting CREATION, not just updates.

`PUT /settings/api/{key}` has two branches: update an existing row, or create
a new one. The update branch validates egress-governed values; the create
branch did not. That gap is reachable in one step, because the delete endpoint
gates only on `editable`:

    PUT    llm.allowed_local_hostnames = ["8.8.8.8"]   -> 400 (rejected)
    DELETE llm.allowed_local_hostnames                 -> 200
    PUT    llm.allowed_local_hostnames = ["8.8.8.8"]   -> 201 (CREATED)

`llm.allowed_local_hostnames` is shipped `editable`, and `llm.` is an allowed
prefix, so both steps pass their own checks. The value is then read into
``EgressContext.local_hostnames`` and any host listed there is classified
LOCAL — laundering an attacker-chosen public address past ``private_only``
and ``require_local_llm``.

Flask ran this validation at four sites; the FastAPI port carried three, and
the update branch's own comment claimed the hole was closed. It was closed for
update only. These tests pin the delete-then-create path specifically, because
a test that only exercises update passes either way.
"""

import pytest

from local_deep_research.web.routers.settings import (
    first_egress_validation_error,
)

GOVERNED_KEY = "llm.allowed_local_hostnames"
PUBLIC_VALUE = ["8.8.8.8"]


def test_validator_rejects_a_public_host_for_this_key():
    """Guards the premise: the validator does consider this value bad.

    If this stops holding, the endpoint tests below would pass vacuously.
    """
    err = first_egress_validation_error({GOVERNED_KEY: PUBLIC_VALUE}, {})
    assert err is not None, (
        f"{GOVERNED_KEY}={PUBLIC_VALUE} is no longer rejected by the "
        f"validator; the endpoint assertions below no longer prove anything"
    )


class TestEgressValidationCoversCreate:
    def test_update_rejects_public_host(self, authenticated_client):
        """The branch that already had the guard."""
        resp = authenticated_client.put(
            f"/settings/api/{GOVERNED_KEY}", json={"value": PUBLIC_VALUE}
        )
        assert resp.status_code == 400, (
            f"update accepted a public host: {resp.status_code} {resp.text[:200]}"
        )

    def test_delete_then_create_still_rejects_public_host(
        self, authenticated_client
    ):
        """The regression: recreate after delete must not bypass the guard."""
        authenticated_client.delete(f"/settings/api/{GOVERNED_KEY}")

        resp = authenticated_client.put(
            f"/settings/api/{GOVERNED_KEY}", json={"value": PUBLIC_VALUE}
        )

        assert resp.status_code == 400, (
            f"DELETE followed by PUT recreated {GOVERNED_KEY} with a public "
            f"host and no egress validation (got {resp.status_code}: "
            f"{resp.text[:300]}). The create branch is missing the guard the "
            f"update branch has."
        )

    def test_create_still_allows_a_legitimate_local_value(
        self, authenticated_client
    ):
        """The guard must not be so broad it blocks valid configuration."""
        authenticated_client.delete(f"/settings/api/{GOVERNED_KEY}")

        resp = authenticated_client.put(
            f"/settings/api/{GOVERNED_KEY}", json={"value": ["localhost"]}
        )

        assert resp.status_code < 400, (
            f"create rejected a legitimate local hostname: "
            f"{resp.status_code} {resp.text[:200]}"
        )


@pytest.mark.parametrize("value", [["8.8.8.8"], ["1.1.1.1"], ["example.com"]])
def test_create_rejects_various_public_hosts(authenticated_client, value):
    authenticated_client.delete(f"/settings/api/{GOVERNED_KEY}")
    resp = authenticated_client.put(
        f"/settings/api/{GOVERNED_KEY}", json={"value": value}
    )
    assert resp.status_code == 400, (
        f"create accepted public host {value!r}: {resp.status_code}"
    )