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.

21.6 KB
Raw Blame
"""Port of the deleted ``tests/web/routes/test_settings_routes_security.py``.

Original (on ``origin/main``): ``tests/web/routes/test_settings_routes_security.py``
— 18 test functions across four classes, driving the Flask ``settings_bp``
through ``app.test_client()``.  Source under test moved
``web/routes/settings_routes.py`` -> ``web/routers/settings.py``.

WHAT IS **NOT** RE-PORTED HERE (already superseded on the branch, verified by
reading the successor's assertions rather than its name):

* ``TestNewSettingNamespaceGate::test_put_api_rejects_trailing_dot_under_allowed_prefix_with_400``
  -> ``tests/web/routers/test_settings_api.py::TestSettingsAPI::
  test_put_rejects_trailing_dot_under_allowed_prefix_with_400`` is a
  line-for-line copy of the original, including the ``"malformed"`` message
  assertion.
* ``TestSaveSettingsPasswordNoop`` (both tests) ->
  ``tests/security/test_settings_egress_and_secrets_fastapi.py::
  TestBulkSecretWriteBackAndEcho::test_no_js_form_post_secret_write_back_is_a_noop``
  is parametrised over ``("", "[REDACTED]")``, asserts 302, asserts the
  STORED row still holds the secret, and carries a positive control proving
  the same form POST did write a sibling key.  Strictly stronger than the
  original, which only checked ``set_setting`` call args on a mock.
* Most of ``TestIsAllowedNewSettingKey`` ->
  ``tests/web/routers/test_settings_namespace_guard.py::TestIsAllowedNewSettingKey``
  (non-string, empty, double-dot, block-list-wins, case-insensitive) plus
  ``tests/web/routers/test_settings_api.py::TestIsAllowedNewSettingKey::
  test_rejects_trailing_and_leading_dot`` (the #4840 shapes).  The two rows
  those two files between them do NOT cover are re-ported below.

The namespace-gate HTTP tests ARE re-ported.  ``test_settings_namespace_guard.py``
drives the ``_save_all_settings_sync`` / ``_save_settings_sync`` /
``_api_update_setting_sync`` helpers directly, so it cannot catch an async
wrapper that stops calling them, loses the request body, or answers before the
guard runs.  The original was an HTTP suite and stays one.

HARNESS.  ``TestClient`` against the real FastAPI app with ``require_auth``
overridden (the idiom in ``test_unified_search_keyword_fallback.py`` /
``test_report_api_contract.py``), a real CSRF token from ``/auth/csrf-token``
because CSRF is ASGI-middleware-enforced and cannot be switched off with a
config flag, and ``follow_redirects=False`` on the form POST — httpx follows
redirects by default where Flask's client did not, which would silently turn
the original's ``== 302`` into a ``200``.
"""

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

import pytest
from fastapi.testclient import TestClient

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

S = "local_deep_research.web.routers.settings"


@pytest.fixture
def client():
    """Authenticated, CSRF-armed client for the settings router."""
    from local_deep_research.web.dependencies.auth import require_auth
    from local_deep_research.web.fastapi_app import app

    app.dependency_overrides[require_auth] = lambda: "testuser"
    c = TestClient(app, raise_server_exceptions=False)
    token = c.get("/auth/csrf-token").json()["csrf_token"]
    c.headers.update({"X-CSRFToken": token})
    try:
        yield c
    finally:
        app.dependency_overrides.pop(require_auth, None)


@contextmanager
def _db(existing_settings=None):
    """Patch ``get_user_db_session`` so the ``Setting`` query returns
    ``existing_settings`` (empty by default -> every key is a CREATE)."""
    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_session(*args, **kwargs):
        yield session

    with patch(f"{S}.get_user_db_session", side_effect=_fake_session):
        yield session


# ---------------------------------------------------------------------------
# save_all_settings body validation
# ---------------------------------------------------------------------------


class TestSaveAllSettingsValidation:
    """HTTP tests for the save_all_settings endpoint."""

    def test_empty_json_body_returns_400(self, client):
        """POST with an empty JSON object triggers 'No settings data provided'.

        Branch guard: ``web/routers/settings.py:1001-1005``.  Nothing else
        under ``tests/`` asserts this message.
        """
        with _db():
            resp = client.post("/settings/save_all_settings", json={})

        assert resp.status_code == 400
        data = resp.json()
        assert data["status"] == "error"
        assert "No settings data" in data["message"]


# ---------------------------------------------------------------------------
# Namespace validation gate (new-key creation) — over HTTP
# ---------------------------------------------------------------------------


class TestNewSettingNamespaceGate:
    """The three write routes must reject new keys outside allowed namespaces."""

    def test_put_api_rejects_blocked_prefix_with_400(self, client):
        """PUT to a new key under a blocked prefix returns 400, not 403/201."""
        with _db(), patch(f"{S}.create_or_update_setting") as create:
            resp = client.put(
                "/settings/api/security.evil", json={"value": "bad"}
            )

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

    def test_put_api_rejects_unknown_prefix_with_400(self, client):
        """Unknown prefixes (neither allow nor block) also return 400."""
        with _db(), patch(f"{S}.create_or_update_setting") as create:
            resp = client.put("/settings/api/custom.foo", json={"value": "x"})

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

    def test_save_all_settings_rejects_blocked_prefix(self, client):
        """save_all_settings rejects new keys in blocked namespaces via
        ``validation_errors`` — and the error text names the reason."""
        with _db(), patch(f"{S}.create_or_update_setting") as create:
            resp = client.post(
                "/settings/save_all_settings",
                json={"security.admin_override": True},
            )

        assert resp.status_code == 400
        data = resp.json()
        assert data["status"] == "error"
        assert any(
            e["key"] == "security.admin_override"
            and "not allowed" in e["error"]
            for e in data["errors"]
        )
        create.assert_not_called()

    def test_save_all_settings_rejects_unknown_prefix(self, client):
        """save_all_settings rejects new keys with unknown prefixes."""
        with _db(), patch(f"{S}.create_or_update_setting") as create:
            resp = client.post(
                "/settings/save_all_settings", json={"custom.injected": 1}
            )

        assert resp.status_code == 400
        assert any(e["key"] == "custom.injected" for e in resp.json()["errors"])
        create.assert_not_called()

    def test_save_settings_form_post_rejects_blocked_prefix(self, client):
        """save_settings (non-JS form-POST fallback) rejects blocked namespaces.

        This is the bypass path the original PR #3088 left unguarded — an
        attacker switching from AJAX to form POST must not be able to inject
        ``security.*`` / ``auth.*`` / ``bootstrap.*`` keys.
        ``set_setting`` must not be called for rejected keys.
        """
        sm = Mock()
        sm.set_setting.return_value = True
        with _db(), patch(f"{S}.get_settings_manager", return_value=sm):
            resp = client.post(
                "/settings/save_settings",
                data={"security.admin_override": "true"},
                follow_redirects=False,
            )

        # The route redirects; the rejection itself is signalled via flash.
        assert resp.status_code == 302
        for call in sm.set_setting.call_args_list:
            assert call.args[0] != "security.admin_override", (
                f"Blocked key reached set_setting: {call}"
            )

    def test_save_settings_form_post_rejects_unknown_prefix(self, client):
        """save_settings rejects unknown (non-allow-listed) prefixes too."""
        sm = Mock()
        sm.set_setting.return_value = True
        with _db(), patch(f"{S}.get_settings_manager", return_value=sm):
            resp = client.post(
                "/settings/save_settings",
                data={"custom.injected": "x"},
                follow_redirects=False,
            )

        assert resp.status_code == 302
        for call in sm.set_setting.call_args_list:
            assert call.args[0] != "custom.injected", (
                f"Unknown-prefix key reached set_setting: {call}"
            )

    def test_save_settings_form_post_allows_known_prefix(self, client):
        """save_settings still writes legitimate keys in the allow-list.

        Positive control: without it, a route that rejected EVERYTHING would
        score green on the two tests above.
        """
        sm = Mock()
        sm.set_setting.return_value = True
        with _db(), patch(f"{S}.get_settings_manager", return_value=sm):
            resp = client.post(
                "/settings/save_settings",
                data={"llm.new_temperature": "0.5"},
                follow_redirects=False,
            )

        assert resp.status_code == 302
        assert any(
            call.args[0] == "llm.new_temperature"
            for call in sm.set_setting.call_args_list
        ), "Legitimate allow-listed key did not reach set_setting"


# ---------------------------------------------------------------------------
# _is_allowed_new_setting_key — the rows no branch test covers
# ---------------------------------------------------------------------------


class TestIsAllowedNewSettingKey:
    """Only the guards NOT already pinned by
    ``test_settings_namespace_guard.py::TestIsAllowedNewSettingKey`` or
    ``test_settings_api.py::TestIsAllowedNewSettingKey``."""

    def test_rejects_whitespace_only(self):
        """``""`` is covered upstream; whitespace-only is not.  A blank-looking
        key must not match any allowed prefix, and ``is_valid_setting_key``
        rejects whitespace inside a segment outright."""
        assert _is_allowed_new_setting_key("   ") is False
        assert _is_allowed_new_setting_key("\t") is False

    def test_allows_known_prefixes(self):
        """The original's allow-list row.  ``backup.``, ``rag.`` and
        ``embeddings.`` appear in ``ALLOWED_SETTING_PREFIXES`` but in no
        branch test, so dropping any of them from the frozenset is currently
        invisible."""
        for key in (
            "app.flag",
            "backup.destination",
            "llm.model",
            "search.tool",
            "rag.chunk_size",
            "embeddings.ollama.url",
        ):
            assert _is_allowed_new_setting_key(key) is True, key


# ===========================================================================
# RECOVERED — not from either deleted file, but from a suite that is DEAD on
# this branch.
#
# ``tests/security/test_settings_secret_redaction_isolation.py`` (added by
# 69eca236c, #5602) is the only place that ever pinned main's
# ``_is_secret_empty_noop`` / ``_redaction_sentinel_error`` /
# ``_embeds_redaction_sentinel`` family.  On this branch it does not run: its
# ``logged_in_client`` fixture imports ``local_deep_research.web.app_factory``
# and two tests import ``local_deep_research.web.routes.settings_routes``, both
# deleted by the migration.  Running it gives **2 failed, 3 passed, 18
# ERRORS** — and an error lands in pytest's separate errors bucket, so the loss
# below has been invisible.
#
# Two distinct regressions, verified against the branch, both across all THREE
# write routes:
#
# (1) EMBEDDED SENTINEL.  main returned 400 for a sensitive value that
#     *embeds* (but does not equal) ``"[REDACTED]"`` — the shape a stale tab
#     produces when a user edits a redacted comma-separated Apprise URL list,
#     e.g. ``"[REDACTED],discord://webhook/tok"``.
#     main: ``web/routes/settings_routes.py`` ``_embeds_redaction_sentinel``
#     :256-278, enforced at :631 (save_all existing-key), :809 (save_all
#     new-key), :1085 + :1168 (save_settings form, with a flash built from
#     ``_redaction_sentinel_error``) and :1404 (api_update_setting).
#     branch: no equivalent anywhere in ``src/`` — the corrupt value is stored
#     verbatim and silently breaks every notification.
#
# (2) CLEARING A NON-PASSWORD SECRET.  main's ``_is_secret_empty_noop``
#     (settings_routes.py:212-224) treated ``value == ""`` as a no-op ONLY for
#     ``ui_element == "password"`` (those inputs render blank, so an untouched
#     form must not wipe them).  The branch replaced it with
#     ``value in ("", DataSanitizer.REDACTION_TEXT)`` for ANY sensitive
#     setting — settings.py:625-635, :1163-1174, :3325-3339.
#     ``notifications.service_url`` is sensitive by leaf name
#     (``DataSanitizer.DEFAULT_SENSITIVE_KEYS`` contains ``"service_url"``) but
#     renders as a *textarea*, so a deliberate "clear my notification URL" is
#     now silently dropped — and the PUT route even answers
#     ``"unchanged (empty password ignored)"`` for a setting that is not a
#     password.  main's own ``_redaction_sentinel_error`` docstring names this
#     asymmetry as load-bearing.
#
# EVERY TEST IN THIS SECTION IS EXPECTED TO FAIL.  They are left red per the
# porting rules.
# ===========================================================================


WEBHOOK_KEY = "notifications.service_url"
SECRET_URL = "discord://HOOKID_XYZ/TOKEN_SECRET_abcdefghijklmnop"
REDACTED = "[REDACTED]"
# The three shapes main rejected: sentinel before a real URL, after one, and
# padded with whitespace (which is not an exact match either).
CORRUPT_VALUES = (
    f"{REDACTED},{SECRET_URL}",
    f"{SECRET_URL},{REDACTED}",
    f"  {REDACTED}  ",
)


def _webhook_row():
    """A stored, configured ``notifications.service_url`` — sensitive by key
    leaf, but a ``textarea``, not a password input."""
    s = MagicMock()
    s.key = WEBHOOK_KEY
    s.value = SECRET_URL
    s.ui_element = "textarea"
    s.editable = True
    s.visible = True
    s.type = "APP"
    s.name = WEBHOOK_KEY
    s.description = ""
    s.category = "notifications"
    s.options = None
    s.min_value = None
    s.max_value = None
    s.step = None
    return s


@contextmanager
def _webhook_db():
    """Patch the session so both the single-key lookup and the bulk
    ``.all()`` fetch return the configured webhook row."""
    row = _webhook_row()
    db = MagicMock()
    db.query.return_value.filter.return_value.first.return_value = row
    db.query.return_value.all.return_value = [row]

    @contextmanager
    def _fake_session(*args, **kwargs):
        yield db

    manager = MagicMock()
    manager.settings_locked = False
    manager.get_all_settings.return_value = {}
    manager.default_settings = {}
    manager._is_environment_locked.return_value = False

    with (
        patch(f"{S}.get_user_db_session", side_effect=_fake_session),
        patch(f"{S}.get_settings_manager", return_value=manager),
    ):
        yield row


class TestEmbeddedRedactionSentinelIsRejected:
    """A sensitive value that EMBEDS the sentinel must be a hard 400, not a
    silent write.

    Unlike the exact-match case (a benign untouched-field round-trip, which
    main deliberately swallowed), the user made an edit here — storing it
    verbatim looks like a successful save while breaking the integration, and
    dropping it silently would be just as wrong.  Hence 400, not a no-op.
    """

    @pytest.mark.parametrize("corrupt", CORRUPT_VALUES)
    def test_put_rejects_embedded_sentinel(self, client, corrupt):
        with _webhook_db(), patch(f"{S}.set_setting") as setter:
            resp = client.put(
                f"/settings/api/{WEBHOOK_KEY}", json={"value": corrupt}
            )

        assert resp.status_code == 400, resp.text[:300]
        assert "redaction placeholder" in resp.json()["error"].lower()
        assert "retype the whole value" in resp.json()["error"]
        # And the corrupt value never reached storage.
        for call in setter.call_args_list:
            assert call.args[:2] != (WEBHOOK_KEY, corrupt), (
                f"corrupt value was written: {call}"
            )

    @pytest.mark.parametrize("corrupt", CORRUPT_VALUES)
    def test_save_all_settings_rejects_embedded_sentinel(self, client, corrupt):
        with _webhook_db(), patch(f"{S}.set_setting") as setter:
            resp = client.post(
                "/settings/save_all_settings", json={WEBHOOK_KEY: corrupt}
            )

        assert resp.status_code == 400, resp.text[:300]
        body = resp.json()
        assert body["status"] == "error"
        assert any(
            e["key"] == WEBHOOK_KEY
            and "redaction placeholder" in e["error"].lower()
            for e in body["errors"]
        ), body
        for call in setter.call_args_list:
            assert call.args[:2] != (WEBHOOK_KEY, corrupt), (
                f"corrupt value was written: {call}"
            )

    @pytest.mark.parametrize("corrupt", CORRUPT_VALUES)
    def test_save_settings_form_post_rejects_embedded_sentinel(
        self, client, corrupt
    ):
        """The no-JS form path had the same guard, surfaced through a flash
        built from ``_redaction_sentinel_error`` (main settings_routes.py
        :1155-1170).  The redirect is unchanged; what must not happen is the
        write."""
        sm = Mock()
        sm.set_setting.return_value = True
        with _webhook_db(), patch(f"{S}.get_settings_manager", return_value=sm):
            resp = client.post(
                "/settings/save_settings",
                data={WEBHOOK_KEY: corrupt},
                follow_redirects=False,
            )

        assert resp.status_code == 302
        for call in sm.set_setting.call_args_list:
            assert call.args[:2] != (WEBHOOK_KEY, corrupt), (
                f"corrupt value reached set_setting via save_settings: {call}"
            )


class TestClearingANonPasswordSecretIsNotANoop:
    """Submitting ``""`` for a sensitive setting that is NOT a password input
    is a deliberate clear and must be written.

    The empty-value no-op exists because password inputs render blank, so an
    untouched form must not wipe them.  That reasoning does not extend to a
    textarea the user can see and empty on purpose — and ``GET`` keeps an
    empty ``notifications.service_url`` readable precisely so the UI can tell
    "not configured" from "configured", which is only meaningful if the user
    can reach that state.
    """

    def test_put_empty_value_clears_a_textarea_secret(self, client):
        with _webhook_db() as row, patch(f"{S}.set_setting") as setter:
            resp = client.put(
                f"/settings/api/{WEBHOOK_KEY}", json={"value": ""}
            )

        assert resp.status_code == 200, resp.text[:300]
        # This must NOT be the password no-op response — the setting is a
        # textarea, and the branch's message even calls it a password.
        assert "unchanged" not in (resp.json().get("message") or "").lower()
        assert any(
            call.args[:2] == (WEBHOOK_KEY, "") for call in setter.call_args_list
        ), (
            "clearing a non-password sensitive setting was silently dropped; "
            f"writes seen: {setter.call_args_list}, row still {row.value!r}"
        )

    def test_save_all_settings_empty_value_clears_a_textarea_secret(
        self, client
    ):
        with _webhook_db(), patch(f"{S}.set_setting") as setter:
            resp = client.post(
                "/settings/save_all_settings", json={WEBHOOK_KEY: ""}
            )

        assert resp.status_code == 200, resp.text[:300]
        assert WEBHOOK_KEY in resp.json()["updated"], resp.json()
        assert any(
            call.args[:2] == (WEBHOOK_KEY, "") for call in setter.call_args_list
        ), f"writes seen: {setter.call_args_list}"

    def test_save_settings_form_post_empty_value_clears_a_textarea_secret(
        self, client
    ):
        sm = Mock()
        sm.set_setting.return_value = True
        with _webhook_db(), patch(f"{S}.get_settings_manager", return_value=sm):
            resp = client.post(
                "/settings/save_settings",
                data={WEBHOOK_KEY: ""},
                follow_redirects=False,
            )

        assert resp.status_code == 302
        assert any(
            call.args[:2] == (WEBHOOK_KEY, "")
            for call in sm.set_setting.call_args_list
        ), f"writes seen: {sm.set_setting.call_args_list}"


class TestExactSentinelStaysANoop:
    """Control for the two classes above: the EXACT sentinel is still the
    benign untouched-field round-trip and must remain a silent no-op, not a
    400 and not a write.

    Without this, "reject everything containing the sentinel" would score
    green on ``TestEmbeddedRedactionSentinelIsRejected`` while breaking the
    dashboard's redacted-dump round-trip.  This one PASSES on the branch.
    """

    def test_exact_sentinel_is_not_an_error_and_is_not_written(self, client):
        with _webhook_db() as row, patch(f"{S}.set_setting") as setter:
            resp = client.put(
                f"/settings/api/{WEBHOOK_KEY}", json={"value": REDACTED}
            )

        assert resp.status_code == 200, resp.text[:300]
        for call in setter.call_args_list:
            assert call.args[:2] != (WEBHOOK_KEY, REDACTED), (
                f"the sentinel itself was persisted: {call}"
            )
        assert row.value == SECRET_URL