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.

4.0 KB
Raw Blame
"""Bulk settings rewrites must not clobber env-locked settings.

``SettingsManager.load_from_defaults_file`` takes
``preserve_environment_locked``, which defaults to **False**. Both bulk-rewrite
routes — ``POST /settings/reset_to_defaults`` and ``POST /settings/api/import``
— must pass it explicitly.

This matters because ``import_settings`` writes rows in BULK rather than
through the per-key setters, so it bypasses the ``_is_environment_locked``
guard that ``set_setting`` / ``create_or_update_setting`` / ``delete_setting``
all apply. Without the flag, a reset silently overwrites the stored value of a
setting the operator locked via an ``LDR_*`` environment variable, and drops
the ``policy_audit`` warning that would have recorded the attempt.

The damage is latent rather than immediate: reads prefer the env var, so
nothing looks wrong until the operator removes it — at which point the stored
value is whatever the reset wrote, not what they had configured.

Both call sites passed the flag on the pre-migration Flask routes
(``web/routes/settings_routes.py``); the port dropped the argument at both.
These tests pin the call contract, which is the property that regressed.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.settings.manager import SettingsManager


def test_manager_default_is_unsafe_for_bulk_rewrites():
    """Guards the premise: the effective default really is False.

    ``load_from_defaults_file`` names ``preserve_environment_locked``
    explicitly (no ``**kwargs`` passthrough since #5841 — that passthrough
    is what exposed the ``override_locked`` lock bypass), and the unsafe
    ``False`` default lives in ``import_settings``. A named parameter is
    what the bulk-rewrite routes rely on: dropping it from the wrapper's
    signature would raise ``TypeError`` at those call sites instead of
    silently discarding the flag.

    If this default ever flips to True the call-site assertions below stop
    proving anything and should be revisited rather than kept green.
    """
    import inspect

    sig = inspect.signature(SettingsManager.import_settings)
    assert sig.parameters["preserve_environment_locked"].default is False, (
        "default changed — the call-site assertions below are now vacuous"
    )
    # And confirm the parameter path the routes actually rely on.
    fwd = inspect.signature(SettingsManager.load_from_defaults_file)
    assert "preserve_environment_locked" in fwd.parameters, (
        "load_from_defaults_file no longer names "
        "preserve_environment_locked; the routes' explicit argument "
        "would now fail to reach import_settings"
    )


class TestBulkRewriteRoutesPreserveEnvLocks:
    @pytest.mark.parametrize(
        "route,method",
        [
            ("/settings/reset_to_defaults", "post"),
            ("/settings/api/import", "post"),
        ],
    )
    def test_route_passes_preserve_environment_locked(
        self, authenticated_client, route, method
    ):
        """Both bulk-rewrite routes must pass the flag explicitly."""
        fake_manager = MagicMock()
        fake_manager.load_from_defaults_file = MagicMock()
        fake_manager.get_all_settings.return_value = {}

        with patch(
            "local_deep_research.web.routers.settings.get_settings_manager",
            return_value=fake_manager,
        ):
            getattr(authenticated_client, method)(route)

        if not fake_manager.load_from_defaults_file.called:
            pytest.skip(
                f"{route} did not reach load_from_defaults_file in this "
                f"configuration; nothing to assert"
            )

        _args, kwargs = fake_manager.load_from_defaults_file.call_args
        assert kwargs.get("preserve_environment_locked") is True, (
            f"{route} called load_from_defaults_file without "
            f"preserve_environment_locked=True, so a bulk rewrite would "
            f"overwrite settings the operator locked via LDR_* env vars "
            f"(got kwargs={kwargs!r})"
        )