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.

7.3 KB
Raw Blame
"""Mutating an environment-locked setting must 403, not 500.

Regression: on origin/main, the Flask settings handler had an explicit
early guard -- ``check_env_setting(key) is not None`` -- returning
``403 {"error": "Setting <key> is environment-locked"}`` before touching
the database, whenever a client tried to write a setting pinned by an
``LDR_*`` env var override. The FastAPI port
(``PUT``/``DELETE /settings/api/{key}``) dropped that guard: the write is
still correctly blocked deeper down by
``SettingsManager.set_setting()``/``delete_setting()``'s own
``_is_environment_locked()`` check, but a ``False`` return from either was
indistinguishable from any other write failure and fell through to a
generic ``500 {"error": "Failed to update/delete setting <key>"}``. Lost
diagnostic + wrong status code -- the write was never unsafe, just
misreported.

Fixed in ``src/local_deep_research/web/routers/settings.py``:
``_api_update_setting_sync`` and ``api_delete_setting`` now call
``SettingsManager._is_environment_locked()`` up front (matching main's
"checked before anything else" contract) and return the explicit 403 only
for that specific cause, so a genuine, unrelated write failure still
reports 500.

A fresh client logs in from scratch inside EVERY test (rather than once in
a shared fixture) because ``tests/conftest.py``'s autouse
``cleanup_database_connections`` fixture closes every open user database
before each test function runs -- a session established by an
outer-scoped fixture would already be disconnected by the time the test
body executed (verified against this same failure mode in
test_auth_disconnected_db_gate.py).
"""

import uuid

import pytest
from fastapi.testclient import TestClient

# app.debug is deliberately non-editable (see default_settings.json), which
# is exactly why it's a good probe here: the env-lock guard must fire
# BEFORE the router's separate "not editable" 403 check, matching main's
# ordering (env-lock checked first, existence/editability checked after).
LOCKED_KEY = "app.debug"
LOCKED_ENV_VAR = "LDR_APP_DEBUG"

# A normal, editable, non-locked setting used for the "genuine failure"
# tests, where the write's rejection has nothing to do with env-lock.
PLAIN_KEY = "llm.model"


@pytest.fixture(scope="module")
def app():
    from local_deep_research.web.fastapi_app import app

    return app


def _unique_ip() -> str:
    return f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.4"


def _new_client(app) -> TestClient:
    c = TestClient(app, raise_server_exceptions=False)
    c.headers.update({"X-Forwarded-For": _unique_ip()})
    return c


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


def _login(client: TestClient, user: str, pw: str):
    return client.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf(client)},
        follow_redirects=False,
    )


@pytest.fixture(scope="module")
def registered_user(app):
    """Register one real user for the whole module; yield (username, pw)."""
    c = _new_client(app)
    user = f"test_envlock_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

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

    token = c.get("/auth/csrf-token").json().get("csrf_token", "")
    c.post(
        "/auth/logout",
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )
    return user, pw


@pytest.fixture
def auth_client(app, registered_user):
    """A newly logged-in client with a CSRF header ready for PUT/DELETE,
    established fresh inside this test's own execution window (see module
    docstring)."""
    user, pw = registered_user
    client = _new_client(app)
    resp = _login(client, user, pw)
    assert resp.status_code == 302, f"Login failed: {resp.status_code}"

    csrf_resp = client.get("/auth/csrf-token")
    token = (
        csrf_resp.json().get("csrf_token")
        if csrf_resp.status_code == 200
        else None
    )
    assert token, "Could not obtain CSRF token for authenticated client"
    client.headers.update({"X-CSRFToken": token})
    return client


class TestEnvLockedSettingReturns403:
    def test_put_env_locked_setting_returns_403(self, auth_client, monkeypatch):
        monkeypatch.setenv(LOCKED_ENV_VAR, "true")

        resp = auth_client.put(
            f"/settings/api/{LOCKED_KEY}", json={"value": True}
        )

        assert resp.status_code == 403, (
            f"env-locked PUT did not 403: {resp.status_code} {resp.text[:200]}"
        )
        assert "environment-locked" in resp.json().get("error", "")

    def test_delete_env_locked_setting_returns_403(
        self, auth_client, monkeypatch
    ):
        monkeypatch.setenv(LOCKED_ENV_VAR, "true")

        resp = auth_client.delete(f"/settings/api/{LOCKED_KEY}")

        assert resp.status_code == 403, (
            f"env-locked DELETE did not 403: {resp.status_code} "
            f"{resp.text[:200]}"
        )
        assert "environment-locked" in resp.json().get("error", "")

    def test_put_without_env_lock_is_unaffected(self, auth_client):
        """Sanity/counterpart: the same key, without the env var set,
        must NOT 403 for env-lock reasons (it may still 403 for the
        pre-existing 'not editable' reason -- app.debug is non-editable --
        but never with an environment-locked message)."""
        resp = auth_client.put(
            f"/settings/api/{LOCKED_KEY}", json={"value": True}
        )
        assert "environment-locked" not in resp.json().get("error", "")


class TestGenuineFailureStillReturns500:
    """A write rejection that is NOT env-lock related must keep reporting
    500 -- only env-locks get the new 403, per the fix's own contract."""

    def test_put_genuine_failure_returns_500_not_403(
        self, auth_client, monkeypatch
    ):
        monkeypatch.setattr(
            "local_deep_research.web.routers.settings.set_setting",
            lambda *a, **k: False,
        )

        resp = auth_client.put(
            f"/settings/api/{PLAIN_KEY}", json={"value": "unused"}
        )

        assert resp.status_code == 500, (
            f"non-env-lock write failure should still 500, got "
            f"{resp.status_code} {resp.text[:200]}"
        )
        assert "environment-locked" not in resp.json().get("error", "")

    def test_delete_genuine_failure_returns_500_not_403(
        self, auth_client, monkeypatch
    ):
        monkeypatch.setattr(
            "local_deep_research.settings.manager.SettingsManager."
            "delete_setting",
            lambda self, key, commit=True: False,
        )

        resp = auth_client.delete(f"/settings/api/{PLAIN_KEY}")

        assert resp.status_code == 500, (
            f"non-env-lock delete failure should still 500, got "
            f"{resp.status_code} {resp.text[:200]}"
        )
        assert "environment-locked" not in resp.json().get("error", "")