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.

9.5 KB
Raw Blame
"""
Regression tests for 5 behaviours that were lost when main's
``web/routes/settings_routes.py`` (Flask) was ported to
``web/routers/settings.py`` (FastAPI) and did not survive the port,
each traced to a specific main commit:

1. Single-key PUT (``api_update_setting``) only ran cross-field
   egress-policy validation for a 4-key allowlist instead of
   unconditionally for every key (main 87537d9ec), letting an
   SSRF-shaped value through on a key the allowlist omitted — notably
   ``search.engine.web.searxng.default_params.instance_url``.
2. The operator-gated "filesystem" PDF-storage option was not hidden
   from the settings-API ``options`` metadata when the gate is off
   (main fb49985aa, ``_shape_pdf_storage_mode_metadata`` /
   ``filesystem_pdf_storage_allowed``).
3. ``embeddings.openai.chunk_size`` validation did not reject booleans
   or non-integer floats (main bf66e67da, ``is_openai_chunk_size``).
4. ``zotero.`` was missing from ``ALLOWED_SETTING_PREFIXES`` (main
   a9bc2f307), so a NEW ``zotero.*`` key could not be created via PUT.
5. The DELETE-then-PUT-recreate path built ``setting_dict`` from
   caller-supplied request-body fields instead of trusted
   ``SettingsManager().default_settings`` metadata (main 87537d9ec),
   silently dropping min/max bounds and degrading ``ui_element`` from
   "number" to "text" on recreate.
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated test client (mirrors test_settings_api.py's fixture)."""
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

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

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

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Login bootstrap failed: expected 302, got {resp.status_code}: "
            f"{resp.text[:500]}"
        )

    csrf_resp = c.get("/auth/csrf-token")
    if csrf_resp.status_code == 200:
        token = csrf_resp.json().get("csrf_token")
        if token:
            c.headers.update({"X-CSRFToken": token})

    yield c

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


def _option_values(options):
    return [
        option.get("value") if isinstance(option, dict) else option
        for option in (options or [])
    ]


@pytest.mark.timeout(120)
class TestSingleKeyPutEgressValidation:
    """#1: api_update_setting must run cross-field egress validation for
    EVERY key, not just a 4-key allowlist (main 87537d9ec)."""

    KEY = "search.engine.web.searxng.default_params.instance_url"

    def test_metadata_ip_rejected_and_not_persisted(self, auth_client):
        original = (
            auth_client.get(f"/settings/api/{self.KEY}").json().get("value")
        )
        try:
            resp = auth_client.put(
                f"/settings/api/{self.KEY}",
                json={"value": "http://169.254.169.254/latest/meta-data/"},
            )
            assert resp.status_code == 400, resp.text
            assert "metadata" in resp.json().get("error", "").lower()

            after = (
                auth_client.get(f"/settings/api/{self.KEY}").json().get("value")
            )
            assert after == original, (
                "SSRF-shaped instance_url must NOT persist after a 400"
            )
        finally:
            if original is not None:
                auth_client.put(
                    f"/settings/api/{self.KEY}", json={"value": original}
                )

    def test_save_all_settings_already_rejects_the_same_value(
        self, auth_client
    ):
        """Sanity check on the proof from the audit: the bulk save route
        already 400s the same value — only the single-key PUT had the gap."""
        resp = auth_client.post(
            "/settings/save_all_settings",
            json={self.KEY: "http://169.254.169.254/latest/meta-data/"},
        )
        assert resp.status_code == 400, resp.text


@pytest.mark.timeout(120)
class TestPdfStorageModeOptionShaping:
    """#2: the operator-gated "filesystem" PDF-storage option must be
    stripped from ``options`` metadata when the gate is off, and restored
    when it's on (main fb49985aa)."""

    GATE_TARGET = (
        "local_deep_research.research_library.services.pdf_storage_manager"
        ".filesystem_pdf_storage_allowed"
    )
    KEY = "research_library.pdf_storage_mode"

    def test_filesystem_hidden_when_gate_off(self, auth_client, monkeypatch):
        monkeypatch.setattr(self.GATE_TARGET, lambda: False)

        single = auth_client.get(f"/settings/api/{self.KEY}")
        assert single.status_code == 200
        assert "filesystem" not in _option_values(single.json()["options"])

        bulk = auth_client.get("/settings/api").json()
        bulk_options = bulk["settings"][self.KEY]["options"]
        assert "filesystem" not in _option_values(bulk_options)

    def test_filesystem_shown_when_gate_on(self, auth_client, monkeypatch):
        monkeypatch.setattr(self.GATE_TARGET, lambda: True)

        single = auth_client.get(f"/settings/api/{self.KEY}")
        assert single.status_code == 200
        assert "filesystem" in _option_values(single.json()["options"])

        bulk = auth_client.get("/settings/api").json()
        bulk_options = bulk["settings"][self.KEY]["options"]
        assert "filesystem" in _option_values(bulk_options)


@pytest.mark.timeout(120)
class TestOpenAIChunkSizeValidation:
    """#3: embeddings.openai.chunk_size must reject booleans and
    non-integer floats (main bf66e67da)."""

    KEY = "embeddings.openai.chunk_size"

    def test_rejects_boolean_and_non_integer_float(self, auth_client):
        original = (
            auth_client.get(f"/settings/api/{self.KEY}").json().get("value")
        )
        try:
            bool_resp = auth_client.put(
                f"/settings/api/{self.KEY}", json={"value": True}
            )
            assert bool_resp.status_code == 400, bool_resp.text

            float_resp = auth_client.put(
                f"/settings/api/{self.KEY}", json={"value": 5.7}
            )
            assert float_resp.status_code == 400, float_resp.text

            after = (
                auth_client.get(f"/settings/api/{self.KEY}").json().get("value")
            )
            assert after == original

            # A valid whole number is still accepted.
            ok_resp = auth_client.put(
                f"/settings/api/{self.KEY}", json={"value": 7}
            )
            assert ok_resp.status_code == 200, ok_resp.text
        finally:
            if original is not None:
                auth_client.put(
                    f"/settings/api/{self.KEY}", json={"value": original}
                )


@pytest.mark.timeout(120)
class TestZoteroPrefixAllowed:
    """#4: "zotero." must be an allowed namespace for creating a NEW
    setting key via PUT (main a9bc2f307)."""

    def test_new_zotero_key_can_be_created(self, auth_client):
        key = f"zotero.regression_test_{uuid.uuid4().hex[:8]}"
        try:
            resp = auth_client.put(f"/settings/api/{key}", json={"value": "x"})
            assert resp.status_code == 201, resp.text
        finally:
            auth_client.delete(f"/settings/api/{key}")


@pytest.mark.timeout(120)
class TestDeleteRecreatePreservesValidationMetadata:
    """#5: DELETE followed by PUT-recreate must rebuild the setting from
    trusted default metadata (min/max/ui_element/options), not the
    caller-supplied request body (main 87537d9ec)."""

    KEY = "rag.indexing_batch_size"  # min_value=1, max_value=50, ui_element=number

    def test_round_trip_preserves_min_max_and_ui_element(self, auth_client):
        original = auth_client.get(f"/settings/api/{self.KEY}").json()
        original_value = original.get("value")
        try:
            del_resp = auth_client.delete(f"/settings/api/{self.KEY}")
            assert del_resp.status_code == 200, del_resp.text

            # Recreate with a request body that omits type/options/bounds
            # entirely, and even tries to smuggle a wrong ui_element — a
            # trusted-metadata recreate must ignore this and rebuild from
            # SettingsManager().default_settings instead.
            create_resp = auth_client.put(
                f"/settings/api/{self.KEY}",
                json={"value": 10, "ui_element": "text"},
            )
            assert create_resp.status_code == 201, create_resp.text

            after = auth_client.get(f"/settings/api/{self.KEY}").json()
            assert after["min_value"] == 1
            assert after["max_value"] == 50
            assert after["ui_element"] == "number"

            # Closes the known-open item: the bulk validating path must
            # still reject an out-of-range value after the round trip.
            oob_resp = auth_client.put(
                f"/settings/api/{self.KEY}", json={"value": 9999}
            )
            assert oob_resp.status_code == 400, oob_resp.text
        finally:
            auth_client.put(
                f"/settings/api/{self.KEY}", json={"value": original_value}
            )