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.

8.4 KB
Raw Blame
"""Unit tests for the new-setting namespace guard (FastAPI re-port).

Main (Flask) gated setting CREATION through the web API behind
_is_allowed_new_setting_key: a frozen allow-list of known prefixes, a
blocking list (auth., bootstrap., db_config., security., server.,
testing.) that wins over the allow-list, and rejection of path-traversal
style '..' keys. The FastAPI port dropped the guard at all three write
routes and additionally accepted caller-supplied 'visible'/'editable'
metadata on creation. These tests re-port main's deleted
test_settings_routes_security coverage against the FastAPI sync helpers.
"""

import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch

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

S = "local_deep_research.web.routers.settings"


def _patched_db(existing_settings=None):
    """Patch get_user_db_session with a session whose Setting query returns
    ``existing_settings`` (none by default → every key is a creation)."""
    query = MagicMock()
    query.all.return_value = existing_settings or []
    query.first.return_value = None
    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
    )


class TestIsAllowedNewSettingKey:
    def test_rejects_non_string(self):
        assert _is_allowed_new_setting_key(None) is False
        assert _is_allowed_new_setting_key(123) is False
        assert _is_allowed_new_setting_key(["llm.model"]) is False

    def test_rejects_empty(self):
        assert _is_allowed_new_setting_key("") is False

    def test_rejects_double_dot(self):
        assert _is_allowed_new_setting_key("llm..model") is False
        assert _is_allowed_new_setting_key("llm.foo..bar") is False

    def test_block_list_wins(self):
        for key in (
            "auth.bypass",
            "bootstrap.admin",
            "db_config.path",
            "security.csrf_enabled",
            "server.port",
            "testing.flag",
        ):
            assert _is_allowed_new_setting_key(key) is False, key

    def test_case_insensitive_prefix_match(self):
        assert _is_allowed_new_setting_key("SECURITY.evil") is False
        assert _is_allowed_new_setting_key("LLM.custom_thing") is True

    def test_allows_known_prefixes(self):
        for key in (
            "llm.custom.endpoint",
            "search.engine.web.foo",
            "app.some_flag",
            "notifications.channel",
        ):
            assert _is_allowed_new_setting_key(key) is True, key

    def test_rejects_unknown_prefix(self):
        assert _is_allowed_new_setting_key("wild.namespace") is False
        assert _is_allowed_new_setting_key("x") is False


class TestPutCreateGuard:
    """PUT /settings/api/{key} creation branch (_api_update_setting_sync)."""

    def _call(self, key, data):
        from local_deep_research.web.routers.settings import (
            _api_update_setting_sync,
        )

        _, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.create_or_update_setting") as create,
        ):
            resp = _api_update_setting_sync(data, key, "alice")
        return resp, create

    def test_blocked_prefix_returns_400_without_creating(self):
        resp, create = self._call("security.evil", {"value": "x"})

        assert resp.status_code == 400
        assert "not allowed" in json.loads(resp.body)["error"]
        create.assert_not_called()

    def test_unknown_prefix_returns_400_without_creating(self):
        resp, create = self._call("wild.key", {"value": "x"})

        assert resp.status_code == 400
        create.assert_not_called()

    def test_allowed_prefix_creates_with_201(self):
        created = Mock()
        created.key = "llm.custom_option"
        created.value = "x"
        created.type = Mock(value="LLM")
        created.name = "Custom Option"

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

        _, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.create_or_update_setting", return_value=created),
        ):
            resp = _api_update_setting_sync(
                {"value": "x"}, "llm.custom_option", "alice"
            )

        assert resp.status_code == 201

    def test_visible_and_editable_are_system_controlled(self):
        """Caller-supplied 'visible'/'editable' must NOT reach the created
        setting — main explicitly excluded them as system-controlled."""
        from local_deep_research.web.routers.settings import (
            _api_update_setting_sync,
        )

        _, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.create_or_update_setting", return_value=None) as create,
        ):
            _api_update_setting_sync(
                {"value": "x", "visible": False, "editable": False},
                "llm.custom_option",
                "alice",
            )

        create.assert_called_once()
        setting_dict = create.call_args[0][0]
        assert "visible" not in setting_dict
        assert "editable" not in setting_dict


class TestSaveAllSettingsGuard:
    """JSON bulk save (_save_all_settings_sync) creation branch."""

    def _call(self, form_data):
        from local_deep_research.web.routers.settings import (
            _save_all_settings_sync,
        )

        session, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.create_or_update_setting") as create,
            patch(f"{S}.set_setting"),
        ):
            resp = _save_all_settings_sync(form_data, "alice")
        return resp, session, create

    def test_blocked_prefix_rejected_with_validation_error(self):
        resp, session, create = self._call({"security.evil": "x"})

        assert resp.status_code == 400
        errors = json.loads(resp.body)["errors"]
        assert any(e["key"] == "security.evil" for e in errors)
        create.assert_not_called()
        session.rollback.assert_called()

    def test_unknown_prefix_rejected(self):
        resp, _, create = self._call({"wild.key": "x"})

        assert resp.status_code == 400
        create.assert_not_called()

    def test_allowed_prefix_is_created(self):
        created = Mock()
        created.type = "LLM"
        from local_deep_research.web.routers.settings import (
            _save_all_settings_sync,
        )

        session, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.create_or_update_setting", return_value=created) as c,
        ):
            _save_all_settings_sync({"llm.custom_option": "x"}, "alice")

        c.assert_called_once()
        session.commit.assert_called()


class TestSaveSettingsFormGuard:
    """Form-POST fallback (_save_settings_sync): new keys outside allowed
    namespaces are skipped; existing keys update regardless."""

    def _run(self, form_data, existing=None):
        from local_deep_research.web.routers.settings import (
            _save_settings_sync,
        )

        _, db_patch = _patched_db(existing)
        sm = Mock()
        sm.set_setting.return_value = True
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=sm),
        ):
            _save_settings_sync(form_data, "alice")
        return sm

    def test_blocked_prefix_new_key_is_skipped(self):
        sm = self._run({"security.evil": "x"})
        sm.set_setting.assert_not_called()

    def test_unknown_prefix_new_key_is_skipped(self):
        sm = self._run({"wild.key": "x"})
        sm.set_setting.assert_not_called()

    def test_existing_key_updates_even_in_blocked_namespace(self):
        """Updates bypass the guard — it gates creation only."""
        existing = Mock()
        existing.key = "security.csrf_enabled"
        existing.editable = True
        existing.ui_element = "checkbox"
        sm = self._run({"security.csrf_enabled": "true"}, existing=[existing])
        sm.set_setting.assert_called_once()

    def test_allowed_prefix_new_key_is_saved(self):
        sm = self._run({"llm.custom_option": "x"})
        sm.set_setting.assert_called_once_with(
            "llm.custom_option", "x", commit=False
        )