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.

17.6 KB
Raw Blame
"""Regression tests: the operator-gated "unprotected" escape hatch must not
be offered by the UI unless ``LDR_POLICY_ALLOW_UNPROTECTED_EGRESS=true``.

Ports the user-facing half of 87537d9ec ("fix(security): operator-gate
unprotected egress and harden policy-sensitive consumers", #5148) onto the
FastAPI migration branch. On main, the ``security/egress/policy.py`` helpers
``unprotected_egress_allowed()`` and ``effective_scope_for_display()`` were
wired into two Flask files:

  - ``web/app_factory.py``: the ``index()`` route (research-page context)
    and the ``inject_frontend_constants`` context processor.
  - ``web/routes/settings_routes.py``: ``_shape_egress_scope_metadata()``,
    applied to every settings-metadata response.

Those Flask files were deleted on this branch (they're now unresolved
modify/delete merge artifacts, not live code) — main's enforcement helpers
merged fine into ``security/egress/policy.py``, but nothing wired them into
the FastAPI equivalents, so the settings UI kept offering "Unprotected" as a
selectable egress scope even when the operator never set
``LDR_POLICY_ALLOW_UNPROTECTED_EGRESS``. This file pins the fix wired into:

  - ``web/routers/settings.py::_shape_egress_scope_setting`` (new helper),
    applied in ``api_get_all_settings``, ``_save_all_settings_sync``'s
    response echo, and ``api_get_db_setting`` — every settings-API read path
    that serves the egress-scope setting's ``options``/``value`` to the
    settings dashboard.
  - ``web/fastapi_app.py``'s ``index()`` route, which feeds
    ``pages/research.html``'s inline privacy panel (the ``<select
    id="policy_egress_scope">`` already gates
    ``{% if settings.allow_unprotected_egress %}`` — that template merged
    cleanly; only the Python context that populates ``allow_unprotected_egress``
    was missing).

Every test below drives the REAL functions (env-var monkeypatching, not
mocking ``unprotected_egress_allowed``/``effective_scope_for_display``
themselves) and pins both directions: the option/value must be ABSENT when
the operator has not enabled the escape hatch, and PRESENT when they have.
"""

from __future__ import annotations

from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, patch

import pytest

from local_deep_research.settings.manager import SettingsManager
from local_deep_research.web.routers.settings import (
    _save_all_settings_sync,
    _shape_egress_scope_setting,
    api_get_all_settings,
    api_get_db_setting,
)

S = "local_deep_research.web.routers.settings"
ENV_VAR = "LDR_POLICY_ALLOW_UNPROTECTED_EGRESS"


def _real_egress_scope_metadata() -> dict:
    """The real, current ``policy.egress_scope`` metadata from
    ``defaults/default_settings.json`` — used instead of a hand-rolled
    fixture so these tests fail if the registered options ever drift."""
    return dict(SettingsManager().default_settings["policy.egress_scope"])


def _patched_db(existing_settings=None, first=None):
    """Mirrors the ``_patched_db`` helper used by the other
    ``tests/web/routers/test_settings_*`` files: a MagicMock DB session
    whose ``Setting`` query returns ``existing_settings`` / ``first``."""
    query = MagicMock()
    query.all.return_value = existing_settings or []
    query.first.return_value = first
    query.filter.return_value = query
    query.filter_by.return_value = query

    session = MagicMock()
    session.query.return_value = query

    @contextmanager
    def fake_db_session(*_a, **_kw):
        yield session

    return session, patch(
        f"{S}.get_user_db_session", side_effect=fake_db_session
    )


# ---------------------------------------------------------------------------
# _shape_egress_scope_setting — the core new helper, unit-level
# ---------------------------------------------------------------------------


class TestShapeEgressScopeSetting:
    def test_hides_unprotected_option_when_operator_has_not_enabled_it(
        self, monkeypatch
    ):
        monkeypatch.delenv(ENV_VAR, raising=False)
        shaped = _shape_egress_scope_setting(
            "policy.egress_scope", _real_egress_scope_metadata()
        )
        values = {opt["value"] for opt in shaped["options"]}
        assert "unprotected" not in values
        # The rest of the catalog must survive untouched.
        assert values == {"adaptive", "public_only", "private_only", "strict"}

    def test_offers_unprotected_option_when_operator_has_enabled_it(
        self, monkeypatch
    ):
        monkeypatch.setenv(ENV_VAR, "true")
        shaped = _shape_egress_scope_setting(
            "policy.egress_scope", _real_egress_scope_metadata()
        )
        values = {opt["value"] for opt in shaped["options"]}
        assert "unprotected" in values

    def test_normalises_a_stale_unprotected_value_when_disabled(
        self, monkeypatch
    ):
        monkeypatch.delenv(ENV_VAR, raising=False)
        metadata = _real_egress_scope_metadata()
        metadata["value"] = "unprotected"  # e.g. saved before the operator
        # disabled the hatch.
        shaped = _shape_egress_scope_setting("policy.egress_scope", metadata)
        assert shaped["value"] == "adaptive"

    def test_preserves_unprotected_value_when_enabled(self, monkeypatch):
        monkeypatch.setenv(ENV_VAR, "true")
        metadata = _real_egress_scope_metadata()
        metadata["value"] = "unprotected"
        shaped = _shape_egress_scope_setting("policy.egress_scope", metadata)
        assert shaped["value"] == "unprotected"

    def test_ignores_unrelated_settings(self, monkeypatch):
        monkeypatch.delenv(ENV_VAR, raising=False)
        other = {"value": "ollama", "options": [{"label": "x", "value": "y"}]}
        assert _shape_egress_scope_setting("llm.provider", other) is other

    def test_tolerates_non_dict_metadata(self):
        assert _shape_egress_scope_setting("policy.egress_scope", None) is None
        assert (
            _shape_egress_scope_setting("policy.egress_scope", "weird")
            == "weird"
        )


# ---------------------------------------------------------------------------
# GET /settings/api  (api_get_all_settings) — the settings dashboard's
# primary data source (URLS.SETTINGS_API.BASE in settings.js).
# ---------------------------------------------------------------------------


class TestApiGetAllSettingsGate:
    def _call(self, monkeypatch, allow_unprotected: bool):
        if allow_unprotected:
            monkeypatch.setenv(ENV_VAR, "true")
        else:
            monkeypatch.delenv(ENV_VAR, raising=False)

        fake_settings_manager = Mock()
        fake_settings_manager.get_all_settings.return_value = {
            "policy.egress_scope": _real_egress_scope_metadata(),
            "llm.provider": {"value": "ollama", "options": None},
        }
        _, db_patch = _patched_db()
        request = SimpleNamespace(
            query_params=SimpleNamespace(get=lambda *_a, **_kw: None)
        )
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager", return_value=fake_settings_manager
            ),
        ):
            return api_get_all_settings(request, username="alice")

    def test_hides_unprotected_when_disabled(self, monkeypatch):
        resp = self._call(monkeypatch, allow_unprotected=False)
        options = resp["settings"]["policy.egress_scope"]["options"]
        values = {opt["value"] for opt in options}
        assert "unprotected" not in values

    def test_offers_unprotected_when_enabled(self, monkeypatch):
        resp = self._call(monkeypatch, allow_unprotected=True)
        options = resp["settings"]["policy.egress_scope"]["options"]
        values = {opt["value"] for opt in options}
        assert "unprotected" in values


# ---------------------------------------------------------------------------
# POST /settings/save_all_settings (_save_all_settings_sync) — the
# settings-dashboard save response settings.js uses to refresh its local
# cache (and thus redraw the <select>) after a save.
# ---------------------------------------------------------------------------


class TestSaveAllSettingsEchoGate:
    def _egress_row(self):
        meta = _real_egress_scope_metadata()
        row = Mock()
        row.key = "policy.egress_scope"
        row.editable = True
        row.value = meta["value"]
        row.name = meta["name"]
        row.description = meta["description"]
        row.type = "SEARCH"
        row.category = "policy"
        row.ui_element = meta["ui_element"]
        row.options = meta["options"]
        row.visible = True
        row.min_value = None
        row.max_value = None
        row.step = None
        return row

    def _call(self, monkeypatch, allow_unprotected: bool):
        if allow_unprotected:
            monkeypatch.setenv(ENV_VAR, "true")
        else:
            monkeypatch.delenv(ENV_VAR, raising=False)

        _, db_patch = _patched_db(existing_settings=[self._egress_row()])
        settings_manager = Mock(settings_locked=False, default_settings={})
        created = Mock()
        created.type = "LLM"
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=settings_manager),
            patch(f"{S}.create_or_update_setting", return_value=created),
            patch(f"{S}.invalidate_settings_caches"),
        ):
            # A save that only touches an unrelated new key — the point is
            # to observe how policy.egress_scope is ECHOED back, not to
            # exercise the update path for it.
            return _save_all_settings_sync(
                {"llm.custom_option_probe": "hello"}, "alice"
            )

    def test_echoed_options_hide_unprotected_when_disabled(self, monkeypatch):
        resp = self._call(monkeypatch, allow_unprotected=False)
        options = resp["settings"]["policy.egress_scope"]["options"]
        values = {opt["value"] for opt in options}
        assert "unprotected" not in values

    def test_echoed_options_offer_unprotected_when_enabled(self, monkeypatch):
        resp = self._call(monkeypatch, allow_unprotected=True)
        options = resp["settings"]["policy.egress_scope"]["options"]
        values = {opt["value"] for opt in options}
        assert "unprotected" in values


# ---------------------------------------------------------------------------
# GET /settings/api/{key}  (api_get_db_setting) — single-setting fetch,
# both the DB-backed row branch and the defaults-fallback branch.
# ---------------------------------------------------------------------------


class TestApiGetDbSettingGate:
    def test_db_branch_hides_unprotected_when_disabled(self, monkeypatch):
        monkeypatch.delenv(ENV_VAR, raising=False)
        meta = _real_egress_scope_metadata()
        row = Mock()
        row.key = "policy.egress_scope"
        row.value = "unprotected"  # stale saved value
        row.type = "SEARCH"
        row.name = meta["name"]
        row.description = meta["description"]
        row.category = "policy"
        row.ui_element = meta["ui_element"]
        row.options = meta["options"]
        row.min_value = None
        row.max_value = None
        row.step = None
        row.visible = True
        row.editable = True

        _, db_patch = _patched_db(first=row)
        with db_patch:
            resp = api_get_db_setting(
                SimpleNamespace(), "policy.egress_scope", username="alice"
            )

        values = {opt["value"] for opt in resp["options"]}
        assert "unprotected" not in values
        # Display normalisation: the stale "unprotected" value must not
        # leak through as the shown value once the hatch is disabled.
        assert resp["value"] == "adaptive"

    def test_default_branch_hides_unprotected_when_disabled(self, monkeypatch):
        monkeypatch.delenv(ENV_VAR, raising=False)
        _, db_patch = _patched_db(first=None)
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=SettingsManager()),
        ):
            resp = api_get_db_setting(
                SimpleNamespace(), "policy.egress_scope", username="alice"
            )
        values = {opt["value"] for opt in resp["options"]}
        assert "unprotected" not in values

    def test_default_branch_offers_unprotected_when_enabled(self, monkeypatch):
        monkeypatch.setenv(ENV_VAR, "true")
        _, db_patch = _patched_db(first=None)
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=SettingsManager()),
        ):
            resp = api_get_db_setting(
                SimpleNamespace(), "policy.egress_scope", username="alice"
            )
        values = {opt["value"] for opt in resp["options"]}
        assert "unprotected" in values

    def test_other_settings_options_are_unaffected(self, monkeypatch):
        """Sanity check: the gate is scoped to policy.egress_scope only —
        an unrelated select-type setting's options must pass through as-is."""
        monkeypatch.delenv(ENV_VAR, raising=False)
        row = Mock()
        row.key = "app.theme"
        row.value = "dark"
        row.type = "APP"
        row.name = "Theme"
        row.description = "d"
        row.category = "app"
        row.ui_element = "select"
        row.options = [
            {"label": "Dark", "value": "dark"},
            {"label": "Light", "value": "light"},
        ]
        row.min_value = None
        row.max_value = None
        row.step = None
        row.visible = True
        row.editable = True

        _, db_patch = _patched_db(first=row)
        with db_patch:
            resp = api_get_db_setting(
                SimpleNamespace(), "app.theme", username="alice"
            )
        assert {opt["value"] for opt in resp["options"]} == {"dark", "light"}


# ---------------------------------------------------------------------------
# GET /  (fastapi_app.py::index) — the research page's inline privacy
# panel. research.html already gates
# "{% if settings.allow_unprotected_egress %}" around the <option
# value="unprotected"> entry (that template merged cleanly); this pins that
# index() actually supplies allow_unprotected_egress / policy_egress_scope.
#
# web/routers/research.py and web/routers/rag.py currently carry unrelated,
# concurrently-being-resolved merge conflicts (unresolved <<<<<<< markers),
# so importing local_deep_research.web.fastapi_app naively fails for a
# reason unconnected to this fix. To exercise the REAL index() route
# end-to-end regardless of that in-progress work, the router import for
# just those two modules is stubbed out — falling back to the real module
# automatically once it imports cleanly. This mirrors the same
# importlib.import_module monkeypatch technique already used by
# tests/web/routers/test_all_routers_load.py::
# test_mount_all_raises_on_router_without_router_attr.
# ---------------------------------------------------------------------------


def _build_isolated_app(monkeypatch):
    """Return the real app.

    This used to evict ``local_deep_research.web.fastapi_app`` from
    sys.modules, re-import it with ``routers.research`` / ``routers.rag``
    stubbed, then evict it again -- a workaround for those two files carrying
    unresolved merge-conflict markers while this test was being written. They
    import cleanly now, so the workaround is obsolete.

    It was also actively harmful. Evicting the module leaves a later importer
    to build a SECOND module object with its own middleware classes, so any
    later test on the same xdist worker that asserts on middleware identity or
    registration order compares against classes from a different import and
    fails. That is what it did: 23 tests failed in CI's full parallel run
    while every one of them passed in isolation -- middleware order, security
    headers, body-size limit, the socket handshake suite. `tests/web` on its
    own was green precisely because the polluter lives in `tests/security`.

    The gate under test is evaluated per request (the env var is toggled
    between requests below), so a rebuilt app was never needed.
    """
    from local_deep_research.web.fastapi_app import app

    return app


@pytest.mark.timeout(120)
def test_index_route_gates_unprotected_option_both_directions(monkeypatch):
    from fastapi.testclient import TestClient

    app = _build_isolated_app(monkeypatch)
    client = TestClient(app, raise_server_exceptions=False)

    import uuid

    user = f"test_egress_ui_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    def _csrf():
        client.get("/auth/login")
        r = client.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    client.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    login_resp = client.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if login_resp.status_code != 302:
        pytest.fail(
            f"Auth bootstrap broken: login returned {login_resp.status_code} "
            f"({login_resp.text[:200]}). Skipping here would hide a broken "
            "auth flow behind a green run."
        )

    monkeypatch.delenv(ENV_VAR, raising=False)
    disabled_page = client.get("/")
    assert disabled_page.status_code == 200
    assert 'id="policy_egress_scope"' in disabled_page.text
    assert '<option value="unprotected"' not in disabled_page.text

    monkeypatch.setenv(ENV_VAR, "true")
    enabled_page = client.get("/")
    assert enabled_page.status_code == 200
    assert '<option value="unprotected"' in enabled_page.text

    client.post("/auth/logout", follow_redirects=False)