~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
harden(settings): strip override_locked passthrough from load_from_defaults_file (#6796)
LearningCircuita519008bb172
diff --git a/changelog.d/5738.bugfix.md b/changelog.d/5738.bugfix.mdnew file mode 100644index 0000000000..9c78d5312d--- /dev/null+++ b/changelog.d/5738.bugfix.md@@ -0,0 +1 @@+Added the `override_locked` parameter to `ISettingsManager.delete_setting` and `ISettingsManager.import_settings` on the abstract base class, and to `InMemorySettingsManager`'s implementations of both (accepted and ignored there — the in-memory store has no settings lock). Previously only the concrete, database-backed `SettingsManager` accepted `override_locked`; code written against the `ISettingsManager` interface that called `import_settings(..., override_locked=True)` would raise `TypeError` against `InMemorySettingsManager`, even though the interface's own concrete implementation supported it. The contract tests' known-extra-parameters inventory is now empty for both methods.diff --git a/changelog.d/5841.security.md b/changelog.d/5841.security.mdnew file mode 100644index 0000000000..74f979ec44--- /dev/null+++ b/changelog.d/5841.security.md@@ -0,0 +1 @@+security hardening: `SettingsManager.load_from_defaults_file` no longer forwards arbitrary keyword arguments to `import_settings` — the wrapper now names only its two safe parameters, `commit` and `preserve_environment_locked`, on the `ISettingsManager` ABC and both implementations, and any other keyword (including `override_locked`) raises `TypeError` instead of being forwarded. This closes an opaque passthrough: a caller that splatted user-controlled kwargs into `load_from_defaults_file` could previously reach `override_locked=True` and bypass the settings lock. `import_settings` and `delete_setting` are unaffected and still take an explicit `override_locked` parameter for trusted callers; only `import_settings`' bypass is exercised today, by the three bootstrap/migration call sites (database initializer, first-run seeding, post-login migration), which now spend it through direct `import_settings(..., override_locked=True)` calls (behavior unchanged, including the post-login `commit=False` atomicity contract).diff --git a/src/local_deep_research/api/settings_utils.py b/src/local_deep_research/api/settings_utils.pyindex 25ae9b8a78..cc60e2568d 100644--- a/src/local_deep_research/api/settings_utils.py+++ b/src/local_deep_research/api/settings_utils.py@@ -162,10 +162,17 @@ class InMemorySettingsManager(ISettingsManager):return resultdef load_from_defaults_file(- self, commit: bool = True, **kwargs: Any+ self,+ commit: bool = True,+ preserve_environment_locked: bool = False,) -> None:- """Reload defaults while honoring environment-lock preservation."""- preserve_locked = bool(kwargs.get("preserve_environment_locked", False))+ """Reload defaults while honoring environment-lock preservation.++ No ``**kwargs`` passthrough (see the ``ISettingsManager`` contract,+ #5841): the in-memory store has no settings lock, so the+ ``override_locked`` bypass is meaningless here — and now also+ structurally rejected.+ """preserved_values = ({key: copy.deepcopy(value["value"])@@ -174,7 +181,7 @@ class InMemorySettingsManager(ISettingsManager):and "value" in valueand check_env_setting(key) is not None}- if preserve_locked+ if preserve_environment_lockedelse {})self._settings.clear()@@ -198,8 +205,15 @@ class InMemorySettingsManager(ISettingsManager):return settingreturn None- def delete_setting(self, key: str, commit: bool = True) -> bool:- """Delete a setting (in memory only)."""+ def delete_setting(+ self, key: str, commit: bool = True, override_locked: bool = False+ ) -> bool:+ """Delete a setting (in memory only).++ ``override_locked`` is accepted to satisfy the ``ISettingsManager``+ contract (#5738) and otherwise ignored: the in-memory store has no+ settings lock, so there is nothing for it to override.+ """if key in self._settings:del self._settings[key]return True@@ -230,8 +244,14 @@ class InMemorySettingsManager(ISettingsManager):overwrite: bool = True,delete_extra: bool = False,preserve_environment_locked: bool = False,+ override_locked: bool = False,) -> None:- """Import settings from a dictionary."""+ """Import settings from a dictionary.++ ``override_locked`` is accepted to satisfy the ``ISettingsManager``+ contract (#5738) and otherwise ignored: the in-memory store has no+ settings lock, so there is nothing for it to override.+ """# Schema-aware import (#5589) — validates untrusted file values# against the current schema; see validate_imported_setting_value's# docstring for the trusted-vs-untrusted rationale (values retaineddiff --git a/src/local_deep_research/database/initialize.py b/src/local_deep_research/database/initialize.pyindex 5eb5506e0f..7531e5863a 100644--- a/src/local_deep_research/database/initialize.py+++ b/src/local_deep_research/database/initialize.py@@ -5,6 +5,7 @@ This module provides a single entry point for database initialization,using Alembic for schema migrations and version control."""+import timefrom typing import Any, Optionalfrom loguru import loggerfrom sqlalchemy import Engine, inspect@@ -75,9 +76,23 @@ def _initialize_default_settings(db_session: Session) -> None:logger.info("Loading default settings into database")# Load settings from defaults file- # This will not overwrite existing settings but will add new ones- settings_mgr.load_from_defaults_file(- overwrite=False, delete_extra=True, override_locked=True+ # This will not overwrite existing settings but will add new ones.+ # Direct import_settings call: the load_from_defaults_file wrapper+ # no longer accepts override_locked (#5841), and this trusted+ # bootstrap must bypass the lock so a locked account still+ # receives settings a later release ships. Time it directly too:+ # routing through the wrapper used to log this for free.+ start = time.perf_counter()+ row_count = len(settings_mgr.default_settings)+ settings_mgr.import_settings(+ settings_mgr.default_settings,+ overwrite=False,+ delete_extra=True,+ override_locked=True,+ )+ elapsed_ms = (time.perf_counter() - start) * 1000+ logger.info(+ f"Loaded {row_count} default settings in {elapsed_ms:.0f}ms")# Update the saved versiondiff --git a/src/local_deep_research/settings/base.py b/src/local_deep_research/settings/base.pyindex 778e4b0b4e..42a2bb85bb 100644--- a/src/local_deep_research/settings/base.py+++ b/src/local_deep_research/settings/base.py@@ -81,13 +81,16 @@ class ISettingsManager(ABC):pass@abstractmethod- def delete_setting(self, key: str, commit: bool = True) -> bool:+ def delete_setting(+ self, key: str, commit: bool = True, override_locked: bool = False+ ) -> bool:"""Delete a setting.Args:key: Setting keycommit: Whether to commit the change+ override_locked: Delete even when settings are locked.Returns:True if successful, False otherwise@@ -126,14 +129,24 @@ class ISettingsManager(ABC):@abstractmethoddef load_from_defaults_file(- self, commit: bool = True, **kwargs: Any+ self,+ commit: bool = True,+ preserve_environment_locked: bool = False,) -> None:"""Import settings from the defaults settings file.+ Deliberately does not accept ``**kwargs``: forwarding them would+ expose ``import_settings``' settings-lock bypass+ (``override_locked``) through this wrapper (#5841). Trusted+ bootstrap callers spend the bypass via direct ``import_settings``+ calls.+Args:commit: Whether to commit changes to database- **kwargs: Additional arguments for import_settings+ preserve_environment_locked: Preserve stored values for+ settings with active environment overrides while imported+ metadata is refreshed"""pass@@ -145,6 +158,7 @@ class ISettingsManager(ABC):overwrite: bool = True,delete_extra: bool = False,preserve_environment_locked: bool = False,+ override_locked: bool = False,) -> None:"""Import settings from a dictionary.@@ -157,5 +171,6 @@ class ISettingsManager(ABC):preserve_environment_locked: If true, preserve the stored valuefor settings with active environment overrides while importedmetadata may refresh+ override_locked: Import even when settings are locked."""passdiff --git a/src/local_deep_research/settings/manager.py b/src/local_deep_research/settings/manager.pyindex 3ee5528b03..46b80b5628 100644--- a/src/local_deep_research/settings/manager.py+++ b/src/local_deep_research/settings/manager.py@@ -685,8 +685,20 @@ class SettingsManager(ISettingsManager):if settings_count == 0:logger.info("No settings found in database, loading defaults")- self.load_from_defaults_file(commit=True, override_locked=True)- logger.info("Default settings loaded successfully")+ # Trusted first-run seeding bypasses the lock via import_settings+ # directly — the load_from_defaults_file wrapper no longer+ # accepts override_locked (#5841). Time it directly too: routing+ # through the wrapper used to log this for free.+ start = time.perf_counter()+ row_count = len(self.default_settings)+ self.import_settings(+ self.default_settings, commit=True, override_locked=True+ )+ elapsed_ms = (time.perf_counter() - start) * 1000+ logger.info(+ f"Default settings loaded successfully: {row_count} "+ f"settings in {elapsed_ms:.0f}ms"+ )def _check_thread_safety(self):"""Check if this instance is being used in the same thread it was created in."""@@ -1263,10 +1275,10 @@ class SettingsManager(ISettingsManager):## This is deliberate, not a bug: the DB row is a self-contained# snapshot. Schema is NOT read live from JSON on every call; it is- # reconciled from the JSON defaults only at version bump, via- # `import_settings(overwrite=False)` (see `load_from_defaults_file`- # and its callers in `database/initialize.py` and post-login in- # `web/auth/routes.py`). Every release bumps the package version, so+ # reconciled from the JSON defaults only at version bump, via the+ # direct `import_settings(overwrite=False, override_locked=True)`+ # bootstrap calls in `database/initialize.py` and post-login in+ # `web/routers/auth.py`). Every release bumps the package version, so# JSON metadata changes reach a user on their next login after# upgrade. Do NOT change this to overlay defaults schema on every# read — it bypasses that version gate and breaks the snapshot@@ -1532,26 +1544,44 @@ class SettingsManager(ISettingsManager):return Falsedef load_from_defaults_file(- self, commit: bool = True, **kwargs: Any+ self,+ commit: bool = True,+ preserve_environment_locked: bool = False,) -> None:"""Import settings from the defaults settings file.+ Deliberately does NOT forward ``**kwargs`` to ``import_settings``:+ the passthrough exposed the settings-lock bypass+ (``override_locked=True``) through this wrapper, so a future+ request-side caller splatting user-controlled kwargs+ (``load_from_defaults_file(**payload)``) could have imported+ settings on a locked account (#5841). Only the two safe parameters+ are enumerated; any other keyword — ``override_locked`` included —+ raises ``TypeError``. The bootstrap/migration call sites that must+ bypass the lock call ``import_settings(..., override_locked=True)``+ directly (pinned by ``test_bootstrap_call_sites_pass_override_locked``).+Args:commit: Whether to commit changes to database. The post-login- atomic block in `web/auth/routes.py` passes ``commit=False``- and combines this call with ``update_db_version(commit=False)``- under a single terminal ``db_session.commit()`` — preserving- the all-or-nothing invariant is what prevents the sticky-loop- bug where `app.version` is missing after a partial write.- **kwargs: Will be passed to `import_settings`, including- ``override_locked`` when the caller has to run while the- settings lock is set.-+ atomic block in `web/routers/auth.py` performs its bulk+ default import through a direct ``import_settings(commit=False,+ override_locked=True)`` call and combines it with+ ``update_db_version(commit=False)`` under a single terminal+ ``db_session.commit()`` — preserving the all-or-nothing+ invariant is what prevents the sticky-loop bug where+ `app.version` is missing after a partial write.+ preserve_environment_locked: Passed to `import_settings` so a+ bulk reset/import cannot overwrite the stored values of+ settings with active ``LDR_*`` environment overrides."""start = time.perf_counter()row_count = len(self.default_settings)- self.import_settings(self.default_settings, commit=commit, **kwargs)+ self.import_settings(+ self.default_settings,+ commit=commit,+ preserve_environment_locked=preserve_environment_locked,+ )elapsed_ms = (time.perf_counter() - start) * 1000if elapsed_ms > 100:logger.info(diff --git a/src/local_deep_research/web/routers/auth.py b/src/local_deep_research/web/routers/auth.pyindex 805e22c6c4..3982c49188 100644--- a/src/local_deep_research/web/routers/auth.py+++ b/src/local_deep_research/web/routers/auth.py@@ -352,8 +352,8 @@ def _perform_post_login_tasks_body(# subsequent login re-runs the ~498-row bulk insert (app.version is# not in default_settings.json, only `update_db_version()` writes# it). Do not factor these calls into separate sessions or allow- # `load_from_defaults_file`/`update_db_version` to commit internally- # here — both must be called with `commit=False`.+ # the `import_settings`/`update_db_version` calls to commit+ # internally here — both must be called with `commit=False`.step_start = time.perf_counter()try:from ...settings.manager import SettingsManager@@ -367,9 +367,15 @@ def _perform_post_login_tasks_body("- loading missing default settings")# override_locked: this only adds keys the upgrade- # introduced, so a locked account still needs it.- settings_manager.load_from_defaults_file(- commit=False, overwrite=False, override_locked=True+ # introduced, so a locked account still needs it. Spent via+ # a direct import_settings call — the+ # load_from_defaults_file wrapper no longer accepts+ # override_locked (#5841).+ settings_manager.import_settings(+ settings_manager.default_settings,+ commit=False,+ overwrite=False,+ override_locked=True,)settings_manager.update_db_version(commit=False)db_session.commit()diff --git a/tests/api/test_settings_utils.py b/tests/api/test_settings_utils.pyindex 4f68736cb3..c5d05a37f4 100644--- a/tests/api/test_settings_utils.py+++ b/tests/api/test_settings_utils.py@@ -2,6 +2,8 @@from unittest.mock import PropertyMock, patch+import pytest+from local_deep_research.api.settings_utils import (InMemorySettingsManager,get_default_settings_snapshot,@@ -256,6 +258,25 @@ class TestInMemorySettingsManager:# Should be back to default# (exact value depends on defaults file)+ def test_load_from_defaults_file_refuses_the_override_locked_bypass(self):+ """``load_from_defaults_file`` deliberately does not accept+ ``**kwargs`` (see the ``ISettingsManager`` contract, #5841): the+ in-memory store has no settings lock, so the ``override_locked``+ bypass is meaningless here -- and now also structurally rejected,+ mirroring ``SettingsManager.load_from_defaults_file``'s equivalent+ refusal pinned by+ ``test_load_from_defaults_file_refuses_the_override_locked_bypass``+ in ``tests/settings/test_settings_manager.py``. This is #5738's+ ``override_locked`` landing on ``delete_setting`` and+ ``import_settings``, not on this wrapper -- a regression here+ would let a caller typed on ``ISettingsManager`` believe it can+ smuggle the bypass through the wrapper again.+ """+ manager = InMemorySettingsManager()++ with pytest.raises(TypeError):+ manager.load_from_defaults_file(**{"override_locked": True})+def test_load_from_defaults_preserves_locked_value_and_refreshes_metadata(self, monkeypatch):diff --git a/tests/database/test_initialize_functions.py b/tests/database/test_initialize_functions.pyindex 258e26dbcf..4572448bf5 100644--- a/tests/database/test_initialize_functions.py+++ b/tests/database/test_initialize_functions.py@@ -129,13 +129,22 @@ class TestInitializeDefaultSettings:) as MockSettingsManager:mock_settings_mgr = Mock()mock_settings_mgr.db_version_matches_package.return_value = False+ mock_settings_mgr.default_settings = {"app.theme": {"value": "x"}}MockSettingsManager.return_value = mock_settings_mgr_initialize_default_settings(mock_session)MockSettingsManager.assert_called_once_with(mock_session)mock_settings_mgr.db_version_matches_package.assert_called_once()- mock_settings_mgr.load_from_defaults_file.assert_called_once()+ # Since #5841 the trusted bootstrap spends its lock bypass via+ # a direct import_settings call; the load_from_defaults_file+ # wrapper no longer accepts override_locked.+ mock_settings_mgr.import_settings.assert_called_once_with(+ mock_settings_mgr.default_settings,+ overwrite=False,+ delete_extra=True,+ override_locked=True,+ )mock_settings_mgr.update_db_version.assert_called_once()def test_skips_when_version_matches(self):@@ -155,8 +164,8 @@ class TestInitializeDefaultSettings:_initialize_default_settings(mock_session)- # Should not call load_from_defaults_file- mock_settings_mgr.load_from_defaults_file.assert_not_called()+ # Should not call import_settings+ mock_settings_mgr.import_settings.assert_not_called()def test_handles_errors_gracefully(self):"""_initialize_default_settings swallows SettingsManager errors.diff --git a/tests/database/test_post_login_settings_atomicity.py b/tests/database/test_post_login_settings_atomicity.pyindex b87a74e155..d3ef9a121f 100644--- a/tests/database/test_post_login_settings_atomicity.py+++ b/tests/database/test_post_login_settings_atomicity.py@@ -108,7 +108,7 @@ def _run_post_login_atomic_block(engine, raise_after_stage: bool = False):Args:engine: SQLCipher engine for the user DB.- raise_after_stage: If True, raise after load_from_defaults_file+ raise_after_stage: If True, raise after import_settingshas staged rows but before the terminal commit. This modelsa mid-write crash / thread death / dispose-triggered errorwhose rollback must leave the DB in a clean pre-write state.@@ -116,7 +116,14 @@ def _run_post_login_atomic_block(engine, raise_after_stage: bool = False):Session = sessionmaker(bind=engine)with Session() as db_session:settings_manager = SettingsManager(db_session)- settings_manager.load_from_defaults_file(commit=False, overwrite=False)+ # Mirrors the migrated auth block (#5841): the trusted bootstrap+ # spends its lock bypass through a direct import_settings call.+ settings_manager.import_settings(+ settings_manager.default_settings,+ commit=False,+ overwrite=False,+ override_locked=True,+ )if raise_after_stage:raise RuntimeError("simulated mid-write failure")settings_manager.update_db_version(commit=False)@@ -189,33 +196,78 @@ class TestPostLoginSettingsAtomicity:def test_post_login_routes_uses_commit_false_for_both_calls(self):"""Structural guard: lock in that the post-login task body calls- `load_from_defaults_file` and `update_db_version` with- `commit=False` and emits a single terminal `db_session.commit()`.- Any refactor that regresses to the two-commit form will fail- this test before production sees a sticky loop.+ `import_settings` and `update_db_version` with `commit=False` and+ emits a single terminal `db_session.commit()`. Any refactor that+ regresses to the two-commit form will fail this test before+ production sees a sticky loop. (The bulk default import goes+ through a direct `import_settings` call since #5841 — the+ `load_from_defaults_file` wrapper no longer accepts+ `override_locked`.)Inspects `_perform_post_login_tasks_body` (not the decoratedwrapper) because #3489 split the function into a thintry/except wrapper + body for daemon-thread exception logging.The atomic block lives in the body."""+ import ast # noqa: E402import inspect # noqa: E402+ import textwrap # noqa: E402from local_deep_research.web.routers import auth as routes # noqa: E402- src = inspect.getsource(routes._perform_post_login_tasks_body)+ src = textwrap.dedent(+ inspect.getsource(routes._perform_post_login_tasks_body)+ )+ tree = ast.parse(src)++ def _find_call(name):+ # A char-window check here (e.g. "commit=False" in src[i:i+N])+ # is satisfied by the *next* line's call sharing the same+ # kwarg spelling -- parse the call instead of pattern-matching+ # nearby text.+ calls = [+ node+ for node in ast.walk(tree)+ if isinstance(node, ast.Call)+ and (+ (isinstance(node.func, ast.Name) and node.func.id == name)+ or (+ isinstance(node.func, ast.Attribute)+ and node.func.attr == name+ )+ )+ ]+ assert len(calls) == 1, (+ f"expected exactly one {name}(...) call, found {len(calls)}"+ )+ return calls[0]++ def _kwarg_is_constant(call, kwarg_name, expected):+ for kw in call.keywords:+ if kw.arg == kwarg_name:+ return (+ isinstance(kw.value, ast.Constant)+ and kw.value.value is expected+ )+ return False- assert "load_from_defaults_file(" in src- assert "update_db_version(commit=False)" in src, (+ import_call = _find_call("import_settings")+ assert _kwarg_is_constant(import_call, "commit", False), (+ "import_settings must be called with commit=False "+ "so the atomic block controls the terminal commit"+ )+ assert _kwarg_is_constant(import_call, "overwrite", False), (+ "import_settings must be called with overwrite=False: it runs "+ "with override_locked=True on every version bump, so overwriting "+ "would silently reset users' customised settings at login"+ )++ update_call = _find_call("update_db_version")+ assert _kwarg_is_constant(update_call, "commit", False), ("update_db_version must be called with commit=False so the ""atomic block controls the terminal commit")- lfd_idx = src.index("load_from_defaults_file(")- lfd_tail = src[lfd_idx : lfd_idx + 200]- assert "commit=False" in lfd_tail, (- "load_from_defaults_file must be called with commit=False "- "so the atomic block controls the terminal commit"- )+assert "db_session.commit()" in src, ("the atomic block must end with a single db_session.commit()")diff --git a/tests/database/test_upgrade_from_pre_migration_install.py b/tests/database/test_upgrade_from_pre_migration_install.pyindex 31e4c4bd30..6e31a84d2c 100644--- a/tests/database/test_upgrade_from_pre_migration_install.py+++ b/tests/database/test_upgrade_from_pre_migration_install.py@@ -52,10 +52,14 @@ database -- ``initialize_database()``, whose settings step is the whole ofthis PR's diff to ``database/initialize.py``::- settings_mgr.load_from_defaults_file(overwrite=False, delete_extra=True)- + settings_mgr.load_from_defaults_file(+ + settings_mgr.import_settings(settings_mgr.default_settings,+ overwrite=False, delete_extra=True, override_locked=True+ )+ (the direct ``import_settings`` form is the trusted-bootstrap bypass+ since #5841; ``load_from_defaults_file`` no longer accepts+ ``override_locked``)+That step reconciles the shipped defaults into the user's database on aversion bump. This PR changed two stored defaults -- ``app.theme````dark`` -> ``system`` and ``web.host`` ``0.0.0.0`` -> ``127.0.0.1`` -- so itdiff --git a/tests/security/test_env_locked_survives_reset_and_import.py b/tests/security/test_env_locked_survives_reset_and_import.pyindex 9652b44049..d6c33312ca 100644--- a/tests/security/test_env_locked_survives_reset_and_import.py+++ b/tests/security/test_env_locked_survives_reset_and_import.py@@ -31,11 +31,13 @@ from local_deep_research.settings.manager import SettingsManagerdef test_manager_default_is_unsafe_for_bulk_rewrites():"""Guards the premise: the effective default really is False.- ``load_from_defaults_file(commit=True, **kwargs)`` does not name this- parameter — it forwards ``**kwargs`` to ``import_settings``, which is- where the default lives. That indirection is exactly why the dropped- argument was invisible at the call site: passing nothing is silently- valid, and the unsafe default is one function away.+ ``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 stopproving anything and should be revisited rather than kept green.@@ -46,11 +48,12 @@ def test_manager_default_is_unsafe_for_bulk_rewrites():assert sig.parameters["preserve_environment_locked"].default is False, ("default changed — the call-site assertions below are now vacuous")- # And confirm the forwarding path the routes actually rely on.+ # And confirm the parameter path the routes actually rely on.fwd = inspect.signature(SettingsManager.load_from_defaults_file)- assert "kwargs" in fwd.parameters, (- "load_from_defaults_file no longer forwards **kwargs; the routes' "- "preserve_environment_locked argument may now be silently dropped"+ 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")diff --git a/tests/settings/test_base_coverage.py b/tests/settings/test_base_coverage.pyindex 455679114c..7983241744 100644--- a/tests/settings/test_base_coverage.py+++ b/tests/settings/test_base_coverage.py@@ -38,7 +38,7 @@ class ConcreteSettingsManager(ISettingsManager):self._store[setting.get("key", "")] = setting.get("value")return setting- def delete_setting(self, key, commit=True):+ def delete_setting(self, key, commit=True, override_locked=False):if key in self._store:del self._store[key]return True@@ -50,11 +50,19 @@ class ConcreteSettingsManager(ISettingsManager):def get_settings_snapshot(self):return dict(self._store)- def load_from_defaults_file(self, commit=True, **kwargs):+ def load_from_defaults_file(+ self, commit=True, preserve_environment_locked=False+ ):passdef import_settings(- self, settings_data, commit=True, overwrite=True, delete_extra=False+ self,+ settings_data,+ commit=True,+ overwrite=True,+ delete_extra=False,+ preserve_environment_locked=False,+ override_locked=False,):for k, v in settings_data.items():if overwrite or k not in self._store:@@ -175,7 +183,9 @@ class TestISettingsManagerABC:def get_settings_snapshot(self):return {}- def load_from_defaults_file(self, commit=True, **kwargs):+ def load_from_defaults_file(+ self, commit=True, preserve_environment_locked=False+ ):pass# import_settings intentionally omitteddiff --git a/tests/settings/test_settings_manager.py b/tests/settings/test_settings_manager.pyindex 9577ffa15f..da1f281506 100644--- a/tests/settings/test_settings_manager.py+++ b/tests/settings/test_settings_manager.py@@ -273,18 +273,44 @@ class TestSettingsManagerLocking:for c in bound.warning.call_args_list)- def test_load_from_defaults_file_forwards_override_locked(self):- """Test that the kwarg reaches import_settings through the wrapper."""+ def test_load_from_defaults_file_refuses_the_override_locked_bypass(self):+ """The wrapper must not forward import_settings' lock bypass (#5841).++ ``override_locked`` used to ride an opaque ``**kwargs`` passthrough,+ so any future request-side caller splatting user-controlled kwargs+ (``load_from_defaults_file(**payload)``) could import settings on a+ locked account. The wrapper now enumerates only the safe parameters+ (``commit``, ``preserve_environment_locked``); the bypass kwarg+ raises ``TypeError`` instead of being forwarded. The bootstrap and+ migration call sites spend the bypass via direct ``import_settings``+ calls, pinned by ``test_bootstrap_call_sites_pass_override_locked``.+ """+ mock_session = MagicMock()+ mock_session.query.return_value.count.return_value = 1++ manager = SettingsManager(db_session=mock_session)++ with patch.object(manager, "import_settings") as mock_import:+ with pytest.raises(TypeError):+ manager.load_from_defaults_file(**{"override_locked": True})++ mock_import.assert_not_called()++ def test_load_from_defaults_file_forwards_preserve_environment_locked(+ self,+ ):+ """The one intentional passthrough kwarg still reaches import_settings."""mock_session = MagicMock()mock_session.query.return_value.count.return_value = 1manager = SettingsManager(db_session=mock_session)- manager._SettingsManager__settings_locked = Truewith patch.object(manager, "import_settings") as mock_import:- manager.load_from_defaults_file(override_locked=True)+ manager.load_from_defaults_file(preserve_environment_locked=True)- assert mock_import.call_args.kwargs["override_locked"] is True+ assert (+ mock_import.call_args.kwargs["preserve_environment_locked"] is True+ )def test_settings_locked_exception_handling(self):"""Test that settings_locked returns False on error."""@@ -455,16 +481,18 @@ class TestSettingsManagerGetSetting:assert result == "fallback"def test_get_setting_auto_initializes_empty_db(self):- """Test that _ensure_settings_initialized is called for empty DB."""+ """Test that _ensure_settings_initialized is called for empty DB.++ First-run seeding imports defaults via a direct import_settings+ call (the wrapper no longer accepts override_locked, #5841).+ """mock_session = MagicMock()mock_session.query.return_value.count.return_value = 0- with patch.object(- SettingsManager, "load_from_defaults_file"- ) as mock_load:+ with patch.object(SettingsManager, "import_settings") as mock_import:SettingsManager(db_session=mock_session)- mock_load.assert_called_once()+ mock_import.assert_called_once()class TestSettingsManagerSetSetting:@@ -2075,24 +2103,25 @@ class TestEnsureSettingsInitialized:mock_session = MagicMock()mock_session.query.return_value.count.return_value = 42- with patch.object(- SettingsManager, "load_from_defaults_file"- ) as mock_load:+ with patch.object(SettingsManager, "import_settings") as mock_import:SettingsManager(db_session=mock_session)- mock_load.assert_not_called()+ mock_import.assert_not_called()def test_loads_defaults_when_db_empty(self):- """_ensure_settings_initialized loads defaults when DB has 0 settings."""+ """_ensure_settings_initialized loads defaults when DB has 0 settings.++ Seeding goes through a direct import_settings call since #5841+ (the load_from_defaults_file wrapper no longer accepts+ override_locked).+ """mock_session = MagicMock()mock_session.query.return_value.count.return_value = 0- with patch.object(- SettingsManager, "load_from_defaults_file"- ) as mock_load:+ with patch.object(SettingsManager, "import_settings") as mock_import:SettingsManager(db_session=mock_session)- mock_load.assert_called_once()+ mock_import.assert_called_once()class TestNewUiElementTypes:diff --git a/tests/settings/test_settings_manager_contracts.py b/tests/settings/test_settings_manager_contracts.pyindex ab59e2af40..d2578e27a5 100644--- a/tests/settings/test_settings_manager_contracts.py+++ b/tests/settings/test_settings_manager_contracts.py@@ -48,13 +48,14 @@ as a whole rather than one method:DELIBERATELY NOT COVERED -- filed upstream, do not duplicate: #5737(``import_settings``lock refusal is a silent no-op / ``load_from_defaults_file`` logs a false-success), #5738 (``override_locked`` missing from the ABC and from the-in-memory implementation), #5739 (``settings_locked`` fails open, latent-infinite recursion), #5740 (inconsistent lock-vs-env ordering). Where a-scenario here touches the same code it does so from a different angle:-#5738 is about a missing *parameter*; ``locked_*`` scenarios below are-about the in-memory implementation having no lock *enforcement* at all,-which is what makes an in-memory-based lock test prove nothing.+success), #5739 (``settings_locked`` fails open, latent+infinite recursion), #5740 (inconsistent lock-vs-env ordering). #5738+(``override_locked`` missing from the ABC and from the in-memory+implementation) is now fixed -- both ``delete_setting`` and+``import_settings`` declare ``override_locked`` on the ABC and accept it+(as a no-op) on ``InMemorySettingsManager``; ``locked_*`` scenarios below+are still about the in-memory implementation having no lock *enforcement*+at all, which is what makes an in-memory-based lock test prove nothing.The 5-minute TTL cache in ``scheduler/background.py`` already has itssession-lifetime contract pinned by ``tests/web/test_scheduler_job_@@ -637,8 +638,9 @@ SCENARIOS: tuple[Scenario, ...] = ("app.lock_settings has no effect on ""InMemorySettingsManager -- it carries no lock enforcement at ""all, so a lock test written against it proves nothing about "- "production. (Related to but distinct from #5738, which is "- "about the missing override_locked parameter.)"+ "production. (Related to but distinct from #5738, which was "+ "about the override_locked parameter missing from the ABC; "+ "set_setting never declared that parameter at all.)"),),Scenario(@@ -741,13 +743,13 @@ def pytest_generate_tests(metafunc):# Parameters each implementation accepts BEYOND what ISettingsManager# declares. Every entry is a place where code written against the ABC-# cannot reach a behaviour production depends on. The two SettingsManager-# entries are #5738; the inventory exists so the NEXT one fails this test+# cannot reach a behaviour production depends on. #5738 -- the two+# SettingsManager entries this inventory used to carry -- is fixed:+# override_locked is now declared on the ABC itself, so it is no longer+# an "extra" parameter for either implementation. The inventory stays+# empty (not deleted) so the NEXT undeclared parameter fails this test# instead of shipping unnoticed.-_KNOWN_EXTRA_PARAMETERS = {- ("SettingsManager", "delete_setting"): {"override_locked"},- ("SettingsManager", "import_settings"): {"override_locked"},-}+_KNOWN_EXTRA_PARAMETERS = {}# Public methods reachable on the database manager that the ABC does not# declare and the in-memory manager does not provide. Substituting thediff --git a/tests/web/routers/test_settings_lock_bootstrap_paths.py b/tests/web/routers/test_settings_lock_bootstrap_paths.pyindex ffaa21c47c..78ab05d015 100644--- a/tests/web/routers/test_settings_lock_bootstrap_paths.py+++ b/tests/web/routers/test_settings_lock_bootstrap_paths.py@@ -21,11 +21,13 @@ successor anywhere on the branch, and this file recovers them.2. The post-login migration must actually reach a locked account.``test_settings_lock_enforcement.py::test_bootstrap_call_sites_pass_override_locked``pins the keyword by reading the source, and- ``TestStartupIsUnaffectedByTheLock`` drives ``load_from_defaults_file``- directly -- but nothing drives the real call site. A refactor that moved- step 1 of ``_perform_post_login_tasks_body`` behind a differently-named- helper, or dropped it, would leave both of those green while a locked- account silently stopped receiving settings an upgrade shipped.+ ``TestStartupIsUnaffectedByTheLock`` drives the override-locked half+ through a direct ``import_settings(..., override_locked=True)`` call+ (``load_from_defaults_file`` no longer accepts the kwarg, #5841) -- but+ nothing drives the real call site. A refactor that moved step 1 of+ ``_perform_post_login_tasks_body`` behind a differently-named helper, or+ dropped it, would leave both of those green while a locked account+ silently stopped receiving settings an upgrade shipped.Both tests run against a real in-memory database and read the value back out,so the assertion is never made against a mock of itself.@@ -88,13 +90,15 @@ def _value_of(session, key):def test_delete_extra_still_prunes_while_locked(locked_session):"""``delete_extra`` is not routed through the ``delete_setting`` guard.- The database initializer calls ``load_from_defaults_file(overwrite=False,- delete_extra=True, override_locked=True)``. If the pruning branch went- through the public ``delete_setting()`` helper it would hit that helper's- own ``override_locked=False`` default and silently keep stale rows on- locked accounts. It does not: ``import_settings`` prunes with a direct- query, because import needs strict failure semantics and- ``delete_setting()`` swallows SQL errors. This pins that.+ The database initializer calls ``import_settings(..., overwrite=False,+ delete_extra=True, override_locked=True)`` directly (the trusted+ bootstrap bypass, spent at the import call site since #5841). If the+ pruning branch went through the public ``delete_setting()`` helper it+ would hit that helper's own ``override_locked=False`` default and+ silently keep stale rows on locked accounts. It does not:+ ``import_settings`` prunes with a direct query, because import needs+ strict failure semantics and ``delete_setting()`` swallows SQL errors.+ This pins that."""locked_session.add(Setting(diff --git a/tests/web/routers/test_settings_lock_enforcement.py b/tests/web/routers/test_settings_lock_enforcement.pyindex 1a1fbd3ec3..4804a4eec8 100644--- a/tests/web/routers/test_settings_lock_enforcement.py+++ b/tests/web/routers/test_settings_lock_enforcement.py@@ -714,7 +714,9 @@ class TestStartupIsUnaffectedByTheLock:"this is the manager-level half of #5659")- manager.load_from_defaults_file(override_locked=True)+ manager.import_settings(+ manager.default_settings, override_locked=True+ )assert (session.query(Setting).filter(Setting.key == PLAIN_KEY).first()is not None@@ -737,7 +739,11 @@ def test_bootstrap_call_sites_pass_override_locked():pin was the one thing it had that nothing else did.Retargeted for the port: `_perform_post_login_tasks_body` moved from- `web/auth/routes.py` to `web/routers/auth.py`.+ `web/auth/routes.py` to `web/routers/auth.py`. Retargeted again for+ #5841: the trusted bootstrap call sites spend the bypass through direct+ ``import_settings(..., override_locked=True)`` calls because the+ ``load_from_defaults_file`` wrapper no longer accepts the kwarg (it is+ the wrapper's job to be structurally incapable of smuggling it).A behavioural test covers the post-login path; the initializer and themanager's own seeding path are awkward to drive end to end, and this is@@ -746,7 +752,9 @@ def test_bootstrap_call_sites_pass_override_locked():which shows up much later as a missing-key crash rather than as a lockerror."""+ import astimport inspect+ import textwrapfrom local_deep_research.database import initialize as db_initializefrom local_deep_research.web.routers import auth as auth_router@@ -767,11 +775,40 @@ def test_bootstrap_call_sites_pass_override_locked():]for func, label in call_sites:- src = inspect.getsource(func)- idx = src.index("load_from_defaults_file(")- call = src[idx : idx + 200]- assert "override_locked=True" in call, (- f"the {label} calls load_from_defaults_file without "+ # A char-window check here (e.g. "override_locked=True" in+ # src[i:i+200]) can be satisfied by an unrelated call further down+ # the source -- parse the call instead of pattern-matching nearby+ # text.+ src = textwrap.dedent(inspect.getsource(func))+ tree = ast.parse(src)+ calls = [+ node+ for node in ast.walk(tree)+ if isinstance(node, ast.Call)+ and (+ (+ isinstance(node.func, ast.Name)+ and node.func.id == "import_settings"+ )+ or (+ isinstance(node.func, ast.Attribute)+ and node.func.attr == "import_settings"+ )+ )+ ]+ assert len(calls) == 1, (+ f"expected exactly one import_settings(...) call in {label}, "+ f"found {len(calls)}"+ )+ call = calls[0]+ override_locked_true = any(+ kw.arg == "override_locked"+ and isinstance(kw.value, ast.Constant)+ and kw.value.value is True+ for kw in call.keywords+ )+ assert override_locked_true, (+ f"the {label} calls import_settings without ""override_locked=True, so a locked account will silently miss ""settings the upgrade shipped")