~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
"""Cross-user settings isolation tests.
Covers the single-setting ``PUT``/``GET /settings/api/{key}`` routes and the
full-snapshot ``GET /settings/api`` route on the live FastAPI settings router
(``src/local_deep_research/web/routers/settings.py``). Every one of them opens
``get_user_db_session(<authenticated username>)`` -- the caller's own per-user
encrypted SQLCipher database -- so isolation here is structural rather than a
per-route ownership check.
Note on what the provider-secret tests can and cannot prove. ``llm.openai.api_key``
is redacted by key name, not by value: ``DataSanitizer.redact_value`` returns
``"[REDACTED]"`` for that key whichever row it was handed. So on those tests the
response-body assertions pin the *redaction* behaviour only -- a read that served
the wrong user's row would still render ``"[REDACTED]"`` and still omit
``USER_A_SECRET``. What pins the write path there is ``_stored_provider_secret``,
which bypasses HTTP and reads each user's encrypted database directly with that
user's own credentials.
The read path is pinned by
``test_non_secret_setting_read_uses_authenticated_user_database`` (single-key
route) and by the ``UNREDACTED_SETTING_KEY`` assertions inside
``test_provider_secret_absent_from_foreign_full_snapshot`` (full-snapshot
route), both of which use ``search.fetch.mode`` -- an unredacted key whose
value differs per user, so the response reveals which database the GET
actually read from.
This supersedes the PR's original Flask-era version (``tests/security/
test_cross_user_settings.py`` against ``web/routes/settings_routes.py``, a
module the FastAPI migration, #3299, removed) -- ported onto the live FastAPI
route paths (``/settings/api``, ``/settings/api/{key}``) and TestClient.
"""
from typing import Final
import pytest
from local_deep_research.database.models import Setting
from local_deep_research.database.session_context import get_user_db_session
from .auth_fixtures import AuthenticatedUser
pytestmark = pytest.mark.real_session_check
SETTING_KEY: Final = "llm.openai.api_key"
USER_A_SECRET: Final = "sk-a-6ff08c43-1607-43e8-b6f8-21ad5729738e"
USER_B_SECRET: Final = "sk-b-2f3405da-f3e7-42c7-9056-a7559d913841"
UNREDACTED_SETTING_KEY: Final = "search.fetch.mode"
USER_A_MODE: Final = "summary_focus"
USER_B_MODE: Final = "disabled"
def _set_provider_secret(user: AuthenticatedUser, secret: str) -> None:
response = user.client.put(
f"/settings/api/{SETTING_KEY}", json={"value": secret}
)
assert response.status_code == 200, response.text[:300]
def _stored_provider_secret(user: AuthenticatedUser) -> str:
with get_user_db_session(user.username, user.password) as db_session:
setting = db_session.query(Setting).filter_by(key=SETTING_KEY).one()
return str(setting.value)
def test_provider_secret_isolated_on_single_setting_read(
two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
) -> None:
# Given
user_a, user_b = two_authenticated_users
_set_provider_secret(user_a, USER_A_SECRET)
_set_provider_secret(user_b, USER_B_SECRET)
# When
response = user_b.client.get(f"/settings/api/{SETTING_KEY}")
# Then
assert response.status_code == 200
data = response.json()
assert data["key"] == SETTING_KEY
assert data["value"] == "[REDACTED]"
assert USER_A_SECRET not in response.text
# Then
survivor = user_a.client.get(f"/settings/api/{SETTING_KEY}")
assert survivor.status_code == 200
survivor_data = survivor.json()
assert survivor_data["key"] == SETTING_KEY
assert survivor_data["value"] == "[REDACTED]"
assert _stored_provider_secret(user_a) == USER_A_SECRET
assert _stored_provider_secret(user_b) == USER_B_SECRET
def test_provider_secret_absent_from_foreign_full_snapshot(
two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
) -> None:
"""Pin both redaction AND cross-user read isolation on ``GET /settings/api``.
The redaction assertions below (``SETTING_KEY`` reads ``"[REDACTED]"``,
``USER_A_SECRET`` absent from the raw response body) hold for ANY user's
row, because ``DataSanitizer.redact_settings_snapshot`` redacts by
setting NAME, not by row ownership -- they would still pass even if this
route served user A's entire settings dict to user B. They pin the
redaction behaviour only, same caveat as
``test_provider_secret_isolated_on_single_setting_read`` above.
The ``UNREDACTED_SETTING_KEY`` assertions are the actual isolation
discriminator: ``search.fetch.mode`` is not in
``DataSanitizer.DEFAULT_SENSITIVE_KEYS`` (its leaf is ``mode``), so it
ships unredacted in the snapshot with a value that differs per user. If
``GET /settings/api`` leaked user A's snapshot to user B, then
``data["settings"][UNREDACTED_SETTING_KEY]["value"]`` would read
``USER_A_MODE`` instead of the asserted ``USER_B_MODE`` -- so THIS
assertion is the one that fails on a leak, not the redaction ones above.
"""
# Given
user_a, user_b = two_authenticated_users
_set_provider_secret(user_a, USER_A_SECRET)
_set_provider_secret(user_b, USER_B_SECRET)
put_a = user_a.client.put(
f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_A_MODE}
)
put_b = user_b.client.put(
f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_B_MODE}
)
assert put_a.status_code == 200, put_a.text[:300]
assert put_b.status_code == 200, put_b.text[:300]
# When
response = user_b.client.get("/settings/api")
# Then
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert isinstance(data["settings"], dict)
assert data["settings"][SETTING_KEY]["value"] == "[REDACTED]"
assert USER_A_SECRET not in response.text
assert data["settings"][UNREDACTED_SETTING_KEY]["value"] == USER_B_MODE
# Then
survivor = user_a.client.get("/settings/api")
assert survivor.status_code == 200
survivor_settings = survivor.json()["settings"]
assert survivor_settings[SETTING_KEY]["value"] == "[REDACTED]"
assert survivor_settings[UNREDACTED_SETTING_KEY]["value"] == USER_A_MODE
assert _stored_provider_secret(user_a) == USER_A_SECRET
assert _stored_provider_secret(user_b) == USER_B_SECRET
def test_non_secret_setting_read_uses_authenticated_user_database(
two_authenticated_users: tuple[AuthenticatedUser, AuthenticatedUser],
) -> None:
# Given
user_a, user_b = two_authenticated_users
response_a = user_a.client.put(
f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_A_MODE}
)
response_b = user_b.client.put(
f"/settings/api/{UNREDACTED_SETTING_KEY}", json={"value": USER_B_MODE}
)
assert response_a.status_code == 200, response_a.text[:300]
assert response_b.status_code == 200, response_b.text[:300]
# When
read_b = user_b.client.get(f"/settings/api/{UNREDACTED_SETTING_KEY}")
read_a = user_a.client.get(f"/settings/api/{UNREDACTED_SETTING_KEY}")
# Then
assert read_b.status_code == 200
assert read_a.status_code == 200
assert read_b.json()["value"] == USER_B_MODE
assert read_a.json()["value"] == USER_A_MODE