~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
"""Regression coverage identified during the Flask -> FastAPI review.
ADR-0010 records the historical measurement. At that snapshot, these guards
survived in ``src/`` without direct assertions; this file is their committed
behavioral evidence.
COVERAGE AREA 1 -- follow-up LLM-endpoint SSRF pre-flight
At the ADR-0010 snapshot,
``utilities/url_utils.is_safe_custom_llm_endpoint`` had no direct unit test,
and the only route-boundary suite for it
(``tests/web/routers/test_start_research_ssrf.py``) covers
``POST /api/start_research``. ``POST /api/followup/start`` is the second
LLM-endpoint entry point (``web/routers/followup.py:280-297``) and likewise
had no direct test at that snapshot. Both halves are covered below: the
helper directly, and the route wiring that must reject BEFORE the
``ResearchHistory`` / ``UserActiveResearch`` rows are written.
READ THIS BEFORE CHANGING THE ENDPOINT LISTS. Private IPs and localhost
are ALLOWED ON PURPOSE -- that is how a user points the app at Ollama /
LM Studio / vLLM, and the helper's docstring says so. What the guard
targets is cloud-metadata addresses (``ALWAYS_BLOCKED_METADATA_IPS``:
169.254.169.254, 169.254.170.2, 100.100.100.200, fd00:ec2::254 ...) and
non-HTTP schemes (``file://``, ``gopher://``, ``ftp://``). A suite that
started rejecting ``http://127.0.0.1:11434`` would not harden the feature,
it would delete it -- so every rejection case here is paired with an
acceptance case, and the negative control was run in BOTH directions
(guard neutered -> rejections fail; ``allow_private_ips`` flipped to
False -> acceptances fail).
COVERAGE AREA 2 -- #3800 provider credential selection
``routers/settings.py:353`` ``_get_setting_from_session(None, ...)`` must
short-circuit to the default. ``SettingsManager.get_setting`` treats
``key=None`` as "return EVERY setting" (``settings/manager.py:610``:
``__query_settings(None)`` drops the key filter), so without the
short-circuit a provider that declares ``api_key_setting = None``
(LM Studio, Llama.cpp) receives the whole settings dict where an
``api_key`` string is expected -- i.e. every other provider's stored
credential, handed to ``list_models_for_api`` and from there to a
provider endpoint. The assertion below is on the value actually passed
downstream, not on the call merely returning.
COVERAGE AREA 3 -- queued-submission attribution
``routers/research.py:1050`` ``reserved_metadata_keys``. Client-supplied
``metadata.system``, ``metadata.submission``, ``metadata.submission_overrides``
and ``metadata.settings_snapshot`` must not survive into the persisted
research metadata: ``system.user`` is the attribution field, so a caller
that could set it would have work recorded as another user's. The route
IGNORES the reserved keys (filters them out) rather than rejecting the
request -- that is what is pinned here, because that is what the code
does. Both dispatch paths are covered: the direct one (assertion on the
stored ``ResearchHistory.research_meta``) and the queued one (assertion on
the stored ``QueuedResearch.settings_snapshot`` AND on the snapshot handed
to ``queue_processor.notify_research_queued``).
HARNESS
-------
The ``live_app`` fixture and its helpers follow
``tests/security/test_research_password_gate_fastapi.py``: the real assembled
FastAPI app on a temp data dir, a genuinely registered + logged-in throwaway
user per test, ``TestClient(app, raise_server_exceptions=False)``, and a real
CSRF token (CSRF is ASGI-middleware-enforced -- a bare POST 403s before any
dependency runs, which would test CSRF rather than the guard). Client peers
come from a MONOTONIC counter, not random addresses: rate limiting buckets on
client IP and random peers collide across a long session, producing 429s that
look like guard failures.
Nothing at or above a guard is patched. The only patched seams are BELOW the
guard under test -- the thread-spawning entry point, the queue processor, and
(for coverage area 2) the provider registry whose model-listing call receives
the selected credential. Every negative assertion is paired with a positive
control in the same state, so "the endpoint was not called with the secret"
can never pass because the endpoint was never called at all.
"""
from __future__ import annotations
import asyncio
import itertools
import threading
import uuid
from contextlib import ExitStack, contextmanager
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import httpx
import pytest
from local_deep_research.constants import ResearchStatus
from local_deep_research.database.models import (
QueuedResearch,
ResearchHistory,
UserActiveResearch,
)
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.database.session_passwords import (
session_password_store,
)
from local_deep_research.utilities.url_utils import is_safe_custom_llm_endpoint
# These tests log in for real; opt out of the suite's autouse
# ``_legacy_bare_username_auth`` shim so the real server-side session gate runs.
pytestmark = pytest.mark.real_session_check
RESEARCH_ROUTER = "local_deep_research.web.routers.research"
SETTINGS_ROUTER = "local_deep_research.web.routers.settings"
RESEARCH_SERVICE = "local_deep_research.web.services.research_service"
PROVIDERS = "local_deep_research.llm.providers"
QUEUE_PROCESSOR = "local_deep_research.web.queue.processor_v2.queue_processor"
START_RESEARCH_PATH = "/api/start_research"
FOLLOWUP_START_PATH = "/api/followup/start"
AVAILABLE_MODELS_PATH = "/settings/api/available-models"
USER_PW = "Guarded-Correct-Horse-3!" # noqa: S105 — test-only credential
# The stored credential of an UNRELATED provider. If the #3800 short-circuit
# is removed this string reaches a provider that declares no api_key setting.
CANARY_CREDENTIAL = "canary-openai-credential-must-not-travel" # noqa: S105
CANARY_SETTING = "llm.openai.api_key"
# The follow-up route takes no endpoint field in its body: it reads
# ``llm.openai_endpoint.url`` out of the settings snapshot
# (``followup.py:285-288``), so the guard is driven by writing that setting.
ENDPOINT_SETTING = "llm.openai_endpoint.url"
# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------
_peer_counter = itertools.count(1)
@pytest.fixture
def live_app(tmp_path, monkeypatch):
"""The real assembled app on a temp data dir.
The routes read module-level singletons (``db_manager``,
``session_password_store``), so the app has to run against those exact
instances and the data dir must be repointed on the singleton itself.
Store entries for users created here are dropped afterwards --
``session_password_store`` is process-wide and ``reset_all_singletons``
does not touch it.
"""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
monkeypatch.setenv("LDR_DISABLE_RATE_LIMITING", "true")
monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
from local_deep_research.database.auth_db import init_auth_database
from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.web.fastapi_app import app as fastapi_app
import local_deep_research.web.routers.auth as auth_routes
original_data_dir = db_manager.data_dir
created_users: list[str] = []
try:
db_manager.data_dir = tmp_path / "encrypted_databases"
init_auth_database()
# Keep the synchronous test off the real post-login worker threads.
monkeypatch.setattr(
auth_routes,
"_perform_post_login_tasks",
lambda _u, _p, _sid=None: None,
)
yield SimpleNamespace(app=fastapi_app, created_users=created_users)
finally:
for username in created_users:
session_password_store.clear_all_for_user(username)
db_manager.close_all_databases()
db_manager.data_dir = original_data_dir
def _client(app):
"""A TestClient with its own, monotonically assigned peer address."""
from fastapi.testclient import TestClient
peer = next(_peer_counter)
client = TestClient(app, raise_server_exceptions=False)
client.headers.update(
{"X-Forwarded-For": f"10.{peer // 254 % 254 + 1}.{peer % 254 + 1}.9"}
)
return client
def _csrf(client):
"""A CSRF token bound to this client's session (middleware-enforced)."""
client.get("/auth/login")
resp = client.get("/auth/csrf-token")
return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""
def _new_user(harness, prefix):
"""Register + settle a fresh user; return ``(client, username)``."""
username = f"{prefix}_{uuid.uuid4().hex[:8]}"
harness.created_users.append(username)
client = _client(harness.app)
token = _csrf(client)
resp = client.post(
"/auth/register",
data={
"username": username,
"password": USER_PW,
"confirm_password": USER_PW,
"acknowledge": "true",
"csrf_token": token,
},
headers={"X-CSRFToken": token},
follow_redirects=False,
)
assert resp.status_code in (200, 302), (
f"registration failed: {resp.status_code} / {resp.text[:400]}"
)
# Consume the one-shot post-login temp-auth token so later requests
# resolve their password through the session store, as in production.
assert client.get("/auth/check").status_code == 200, (
"the client must be authenticated after registration"
)
return client, username
def _post_json(client, path, body):
token = _csrf(client)
return client.post(path, json=body, headers={"X-CSRFToken": token})
def _set_setting(username, key, value):
"""Write a setting into the user's encrypted settings DB."""
from local_deep_research.settings import SettingsManager
with get_user_db_session(username, password=USER_PW) as db_session:
assert SettingsManager(db_session).set_setting(key, value), (
f"could not seed setting {key!r} — the test cannot drive the "
"guard without it"
)
def _run_record_counts(username):
"""Every table a started/queued research writes a row into."""
with get_user_db_session(username, password=USER_PW) as db_session:
return {
"history": db_session.query(ResearchHistory).count(),
"active": db_session.query(UserActiveResearch).count(),
"queued": db_session.query(QueuedResearch).count(),
}
def _research_meta(username, research_id):
with get_user_db_session(username, password=USER_PW) as db_session:
row = (
db_session.query(ResearchHistory)
.filter_by(id=research_id)
.one_or_none()
)
assert row is not None, f"no ResearchHistory row for {research_id}"
return dict(row.research_meta or {})
def _queued_snapshot(username, research_id):
with get_user_db_session(username, password=USER_PW) as db_session:
row = (
db_session.query(QueuedResearch)
.filter_by(research_id=research_id)
.one_or_none()
)
assert row is not None, f"no QueuedResearch row for {research_id}"
return dict(row.settings_snapshot or {})
def _seed_parent_research(username):
"""Write a completed ``ResearchHistory`` row owned by ``username`` and
return its id.
``web/routers/followup.py``'s ownership gate (hand-ported from main's
#5600 cross-user isolation fix, landed 2026-08-26 — after this file's
SSRF guard was written) 404s before the SSRF check runs whenever
``service.load_parent_research(parent_id)`` comes back empty, which it
always does for an id with no matching row in the caller's own DB. A
random/nonexistent id therefore no longer reaches the guard this test
targets; a real, owned parent row is required to get past the ownership
check and exercise the SSRF pre-flight underneath it.
"""
now = datetime.now(timezone.utc).isoformat()
research_id = str(uuid.uuid4())
with get_user_db_session(username, password=USER_PW) as db_session:
db_session.add(
ResearchHistory(
id=research_id,
query="ssrf pre-flight parent",
mode="quick_summary",
status="completed",
created_at=now,
completed_at=now,
duration_seconds=1,
progress=100,
title="ssrf pre-flight parent",
report_content="body",
)
)
db_session.commit()
return research_id
def _followup_body(parent_research_id):
return {
"parent_research_id": parent_research_id,
"question": "ssrf pre-flight follow-up probe",
}
def _configure_followup_settings(username, endpoint):
"""``llm.model`` (checked one branch ABOVE the SSRF guard) plus the
endpoint the guard reads."""
_set_setting(username, "llm.model", "test-model")
_set_setting(username, ENDPOINT_SETTING, endpoint)
# ===========================================================================
# COVERAGE AREA 1 -- is_safe_custom_llm_endpoint, the helper itself
# ===========================================================================
# Cloud-metadata targets and non-HTTP schemes. Deterministic by construction:
# every host here is an IP literal on ``ALWAYS_BLOCKED_METADATA_IPS`` or a
# scheme outside ``ALLOWED_SCHEMES``, so no DNS resolution is involved and the
# outcome cannot depend on the machine running the suite.
REJECTED_ENDPOINTS = [
# AWS / GCP / Azure / DigitalOcean IMDS.
"http://169.254.169.254/latest/meta-data/",
"https://169.254.169.254/latest/meta-data/iam/security-credentials/",
"169.254.169.254:80",
# ECS task-role credentials.
"http://169.254.170.2/v2/credentials/",
# AlibabaCloud IMDS. Load-bearing: 100.64.0.0/10 is CGNAT and is
# ALLOWED under allow_private_ips (Podman/rootless), so this proves the
# metadata denylist outranks the private-IP allowance.
"http://100.100.100.200/latest/meta-data/",
# IPv6 IMDS.
"http://[fd00:ec2::254]/latest/meta-data/",
"http://[fd00:ec2::254]:80/latest/",
# Credentials in userinfo must not smuggle the host past the check.
"http://user:[email protected]/",
# Non-HTTP schemes.
"file:///etc/passwd",
"gopher://127.0.0.1:11211/_stats",
"ftp://169.254.169.254/",
"javascript:alert(1)",
"data:text/html,<script>x</script>",
# Unparseable garbage.
"not a url at all",
]
# ALLOWED ON PURPOSE -- this is how users reach Ollama / LM Studio / vLLM.
# A change that starts rejecting these removes the feature rather than
# hardening it. Keep in lockstep with is_safe_custom_llm_endpoint's docstring.
ACCEPTED_ENDPOINTS = [
"http://127.0.0.1:11434",
"http://localhost:11434/v1",
"http://[::1]:11434/v1",
"http://192.168.1.10:8000",
"http://10.0.0.5:1234/v1",
"http://172.16.3.4:8080/v1",
# CGNAT — rootless Podman / container bridge addressing.
"http://100.64.1.5:11434",
# Scheme-less: normalized exactly as the provider normalizes it.
"localhost:11434",
"192.168.1.10:8000",
# Ordinary public endpoints stay usable too.
"https://api.openai.com/v1",
"https://openrouter.ai/api/v1",
]
class TestIsSafeCustomLlmEndpointHelper:
"""Direct regression contract for ``is_safe_custom_llm_endpoint``."""
@pytest.mark.parametrize("endpoint", REJECTED_ENDPOINTS)
def test_metadata_and_non_http_endpoints_are_rejected(self, endpoint):
assert is_safe_custom_llm_endpoint(endpoint) is False, (
f"{endpoint!r} must be rejected: it is a cloud-metadata target, a "
"non-HTTP scheme or unparseable, and the value is later handed to "
"httpx as an LLM base_url"
)
@pytest.mark.parametrize("endpoint", ACCEPTED_ENDPOINTS)
def test_local_and_public_endpoints_are_accepted(self, endpoint):
assert is_safe_custom_llm_endpoint(endpoint) is True, (
f"{endpoint!r} must be ACCEPTED. Private IPs and localhost are "
"deliberately allowed — that is how users point the app at "
"Ollama / LM Studio / vLLM. Rejecting them does not harden the "
"guard, it removes the feature"
)
@pytest.mark.parametrize("endpoint", ["", " ", None])
def test_unset_endpoint_is_safe(self, endpoint):
"""No endpoint means nothing to send anywhere. The follow-up route
relies on this: ``llm.openai_endpoint.url`` is unset for every user
who is not using an OpenAI-compatible provider, and they must not be
blocked from starting research."""
assert is_safe_custom_llm_endpoint(endpoint) is True
# ===========================================================================
# COVERAGE AREA 1 -- route wiring on POST /api/followup/start
# ===========================================================================
class TestFollowupCustomEndpointSsrfPreflight:
"""``web/routers/followup.py:280-297``.
``start_research_process`` is imported INSIDE ``_start_followup_sync``, so
the patch target is the service module the name is looked up on, not the
router. It is the boundary directly below the guard: patching it leaves
the guard executing for real while no research thread is ever created.
"""
SPAWN_TARGET = f"{RESEARCH_SERVICE}.start_research_process"
@pytest.mark.parametrize(
"endpoint",
[
"http://169.254.169.254/latest/meta-data/",
"http://[fd00:ec2::254]/latest/meta-data/",
"http://100.100.100.200/latest/meta-data/",
"file:///etc/passwd",
"gopher://127.0.0.1:11211/_stats",
],
)
def test_metadata_or_non_http_endpoint_is_rejected_before_any_db_write(
self, live_app, endpoint
):
client, username = _new_user(live_app, "fup_ssrf")
_configure_followup_settings(username, endpoint)
parent_id = _seed_parent_research(username)
before = _run_record_counts(username)
with patch(self.SPAWN_TARGET) as spawn:
spawn.return_value = MagicMock(ident=5150)
resp = _post_json(
client, FOLLOWUP_START_PATH, _followup_body(parent_id)
)
assert resp.status_code == 400, (
f"{endpoint!r} must be refused at the follow-up request boundary. "
f"Got {resp.status_code}: {resp.text[:400]}"
)
body = resp.json()
assert body["success"] is False
assert body["error"] == "Invalid custom endpoint URL"
spawn.assert_not_called()
assert _run_record_counts(username) == before, (
"the SSRF pre-flight runs before the ResearchHistory / "
"UserActiveResearch inserts — a refusal must leave no row behind"
)
@pytest.mark.parametrize(
"endpoint",
[
"http://127.0.0.1:11434",
"http://192.168.1.10:8000",
"localhost:11434",
"https://api.openai.com/v1",
],
)
def test_local_and_private_endpoints_still_start_the_follow_up(
self, live_app, endpoint
):
"""Positive control, and the anti-over-hardening one.
Without this, a route that rejected EVERY endpoint would satisfy the
refusal test above while making local LLMs unusable — the exact
mistake this guard must not be "fixed" into.
"""
client, username = _new_user(live_app, "fup_ok")
_configure_followup_settings(username, endpoint)
parent_id = _seed_parent_research(username)
before = _run_record_counts(username)
with patch(self.SPAWN_TARGET) as spawn:
spawn.return_value = MagicMock(ident=5151)
resp = _post_json(
client, FOLLOWUP_START_PATH, _followup_body(parent_id)
)
assert resp.status_code == 200, (
f"{endpoint!r} must be accepted: {resp.text[:400]}"
)
body = resp.json()
assert body["success"] is True
assert body["research_id"]
assert spawn.called, "the research thread entry point was never reached"
assert _run_record_counts(username)["history"] == before["history"] + 1
# ===========================================================================
# COVERAGE AREA 2 -- #3800 provider credential selection
# ===========================================================================
def _provider_double(api_key_setting):
"""A discovered-provider double whose ``list_models_for_api`` is the sink
a leaked credential would travel into."""
list_models = MagicMock(return_value=[{"value": "m1", "label": "Model 1"}])
provider_class = SimpleNamespace(
api_key_setting=api_key_setting,
url_setting=None,
list_models_for_api=list_models,
)
return provider_class, list_models
def _discovery_probe(client, api_key_setting):
"""Force-refresh model discovery with one provider and every outbound leg
stubbed. Returns ``(response, list_models_mock)``.
``_get_setting_from_session`` is deliberately NOT patched — it is the
function under test. Only ``discover_providers`` and
``get_discovered_provider_options`` (which decide WHICH provider classes
the loop sees) are replaced.
"""
provider_class, list_models = _provider_double(api_key_setting)
discovered = {
"LMSTUDIO": SimpleNamespace(
provider_class=provider_class, provider_name="LM Studio"
)
}
with ExitStack() as stack:
stack.enter_context(
patch(
f"{PROVIDERS}.get_discovered_provider_options",
return_value=[{"value": "LMSTUDIO", "label": "LM Studio"}],
)
)
stack.enter_context(
patch(f"{PROVIDERS}.discover_providers", return_value=discovered)
)
response = client.post(
f"{AVAILABLE_MODELS_PATH}?force_refresh=true",
headers={"X-CSRFToken": _csrf(client)},
)
return response, list_models
class TestCrossProviderCredentialLeak:
"""``routers/settings.py:353`` — ``_get_setting_from_session(None, ...)``."""
def test_none_key_returns_the_default_without_opening_a_db_session(
self, live_app
):
"""The helper directly. ``get_user_db_session`` is asserted unused:
the short-circuit must happen before any settings read, not merely
filter the result afterwards."""
from local_deep_research.web.routers import settings as settings_router
_client_unused, username = _new_user(live_app, "gs_none")
with patch(f"{SETTINGS_ROUTER}.get_user_db_session") as db_session:
assert (
settings_router._get_setting_from_session(None, username, "")
== ""
)
sentinel = object()
assert (
settings_router._get_setting_from_session(
None, username, sentinel
)
is sentinel
)
db_session.assert_not_called()
def test_named_key_still_reads_the_stored_value(self, live_app):
"""Positive control for the row above: the helper really does read the
DB for a real key, so ``""`` for a ``None`` key means "short-circuited",
not "settings unreadable"."""
from local_deep_research.web.routers import settings as settings_router
_client_unused, username = _new_user(live_app, "gs_named")
_set_setting(username, CANARY_SETTING, CANARY_CREDENTIAL)
assert (
settings_router._get_setting_from_session(
CANARY_SETTING, username, ""
)
== CANARY_CREDENTIAL
)
def test_provider_without_api_key_setting_gets_empty_string_not_settings(
self, live_app
):
"""``POST /settings/api/available-models`` — the consumer-side half.
A provider declaring ``api_key_setting = None`` (LM Studio,
Llama.cpp) must be handed ``""``. Without the short-circuit it is
handed a dict of EVERY setting, so an unrelated provider's stored key
is sent to this endpoint as its ``Authorization`` credential.
"""
client, username = _new_user(live_app, "leak_none")
_set_setting(username, CANARY_SETTING, CANARY_CREDENTIAL)
resp, list_models = _discovery_probe(client, api_key_setting=None)
assert resp.status_code == 200, resp.text[:400]
assert list_models.called, (
"the provider's model-listing call was never reached — an "
"assertion about what it received would be vacuous"
)
api_key = list_models.call_args.args[0]
# Asserted FIRST because it is the actual security claim: an
# unrelated provider's stored credential must not travel into this
# call. The shape assertions below explain HOW it would.
assert CANARY_CREDENTIAL not in repr(list_models.call_args), (
"another provider's stored credential travelled into the "
"model-listing call for a provider that declares no api_key "
"setting — this is #3800"
)
assert not isinstance(api_key, dict), (
"the whole settings dict was passed where an api_key string was "
f"expected: {str(api_key)[:200]}"
)
assert api_key == "", (
"a provider with no api_key setting must receive the empty "
f"default, got {type(api_key).__name__}: {str(api_key)[:200]}"
)
assert CANARY_CREDENTIAL not in resp.text
def test_provider_with_an_api_key_setting_gets_that_stored_credential(
self, live_app
):
"""Positive control at the route. Proves the credential read works at
all, so ``""`` above is the short-circuit rather than a broken read —
and pins that the short-circuit did not disable legitimate key
forwarding."""
client, username = _new_user(live_app, "leak_named")
_set_setting(username, CANARY_SETTING, CANARY_CREDENTIAL)
resp, list_models = _discovery_probe(
client, api_key_setting=CANARY_SETTING
)
assert resp.status_code == 200, resp.text[:400]
assert list_models.called
assert list_models.call_args.args[0] == CANARY_CREDENTIAL
def _provider_cache_rows(username):
"""Fresh SQL snapshots of every column, including IDs and timestamps."""
from sqlalchemy import select
from local_deep_research.database.models.providers import ProviderModel
with get_user_db_session(username, password=USER_PW) as session:
return [
dict(row)
for row in session.execute(
select(*ProviderModel.__table__.columns).order_by(
ProviderModel.id
)
).mappings()
]
def _seed_provider_cache(username, *, expired=False):
from local_deep_research.database.models.providers import ProviderModel
with get_user_db_session(username, password=USER_PW) as session:
session.add(
ProviderModel(
provider="OPENAI",
model_key="cached-model",
model_label="Cached model",
model_metadata={"retained": ["all", "columns"]},
last_updated=datetime.now(timezone.utc)
- timedelta(hours=25 if expired else 1),
)
)
session.commit()
@contextmanager
def _observe_provider_cache_sql(*, fail_reads=False):
"""Observe real SQL, optionally fail the actual cache SELECT boundary."""
from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlalchemy.exc import OperationalError
observed = SimpleNamespace(reads=[], writes=[])
def before_execute(_conn, _cursor, statement, parameters, _ctx, _many):
sql = statement.lstrip().lower()
if "provider_models" not in sql:
return
if sql.startswith("select"):
observed.reads.append(statement)
if fail_reads:
raise OperationalError(
statement,
parameters,
RuntimeError("private-cache-diagnostic-must-not-escape"),
)
elif sql.startswith(("insert", "update", "delete", "replace")):
observed.writes.append(statement)
event.listen(Engine, "before_cursor_execute", before_execute)
try:
yield observed
finally:
event.remove(Engine, "before_cursor_execute", before_execute)
@pytest.fixture
def model_cache_client(live_app):
"""Keep auth, CSRF and storage real; replace only discovery dependencies.
Policy behavior has separate route-boundary tests. This fixture isolates
the cache-method contract with a known public provider, so a cold GET
cannot pass just because an unrelated policy refusal stopped the route.
"""
from local_deep_research.security.egress.policy import (
EgressContext,
EgressScope,
)
client, username = _new_user(live_app, "models_cache")
token = _csrf(client)
assert token
list_models = MagicMock(
return_value=[{"value": "fresh-model", "label": "Fresh model"}]
)
provider = SimpleNamespace(
api_key_setting="llm.openai.api_key",
url_setting=None,
list_models_for_api=list_models,
)
policy_context = EgressContext(
scope=EgressScope.PUBLIC_ONLY,
primary_engine="wikipedia",
require_local_llm=False,
require_local_embeddings=False,
)
with (
patch(
f"{SETTINGS_ROUTER}._resolve_model_discovery_policy",
return_value=(
policy_context,
{"policy.egress_scope": "public_only"},
),
) as policy,
patch(
f"{PROVIDERS}.get_discovered_provider_options",
return_value=[{"value": "OPENAI", "label": "OpenAI"}],
) as options,
patch(
f"{PROVIDERS}.discover_providers",
return_value={
"OPENAI": SimpleNamespace(
provider_class=provider, provider_name="OpenAI"
)
},
) as discovery,
patch(
f"{SETTINGS_ROUTER}._get_setting_from_session",
return_value="cache-test-key",
) as credentials,
):
# Store the key the stubbed read returns. Since main's #6578 the cache
# write re-reads each provider's stored key/URL settings and skips a
# provider whose inputs changed while discovery ran, so a stub that
# disagreed with the stored (default empty) key made every POST look
# superseded and left the cache unwritten.
_set_setting(
username, provider.api_key_setting, credentials.return_value
)
yield SimpleNamespace(
client=client,
username=username,
token=token,
policy=policy,
options=options,
discovery=discovery,
credentials=credentials,
list_models=list_models,
)
class TestModelCacheMethodContract:
"""GET is observational; both cache-populating POST modes require CSRF."""
@pytest.mark.asyncio
@pytest.mark.parametrize(
"forced", [False, True], ids=["ordinary", "forced"]
)
async def test_cancelled_authenticated_post_preserves_encrypted_cache(
self, model_cache_client, monkeypatch, forced
):
from local_deep_research.database import thread_local_session
probe = model_cache_client
_seed_provider_cache(probe.username, expired=True)
before = _provider_cache_rows(probe.username)
started = threading.Event()
release = threading.Event()
finished = threading.Event()
owner = set()
original_cleanup = thread_local_session.cleanup_current_thread
def cleanup():
try:
return original_cleanup()
finally:
if threading.get_ident() in owner:
finished.set()
def list_models(*_args):
owner.add(threading.get_ident())
started.set()
assert release.wait(10), "test did not release the provider"
return [{"value": "fresh-model", "label": "Fresh model"}]
monkeypatch.setattr(
thread_local_session, "cleanup_current_thread", cleanup
)
probe.list_models.side_effect = list_models
transport = httpx.ASGITransport(app=probe.client.app)
with _observe_provider_cache_sql() as sql:
async with httpx.AsyncClient(
transport=transport,
base_url="http://testserver",
# TestClient may use httpx2; share its standard CookieJar.
cookies=probe.client.cookies.jar,
) as client:
request = asyncio.create_task(
client.post(
AVAILABLE_MODELS_PATH,
params={"force_refresh": str(forced).lower()},
headers={"X-CSRFToken": probe.token},
)
)
try:
provider_started = await asyncio.to_thread(started.wait, 10)
if not provider_started and request.done():
response = await request
pytest.fail(
"discovery returned before reaching the provider: "
f"{response.status_code} {response.text[:200]}"
)
assert provider_started, (
"authenticated CSRF-protected discovery did not start"
)
request.cancel()
with pytest.raises(asyncio.CancelledError):
await request
finally:
release.set()
if not request.done():
request.cancel()
await asyncio.gather(request, return_exceptions=True)
if owner:
assert await asyncio.to_thread(finished.wait, 10), (
"discovery worker did not clean up"
)
assert owner and threading.get_ident() not in owner
probe.list_models.assert_called_once_with("cache-test-key", None)
assert sql.writes == []
assert _provider_cache_rows(probe.username) == before
@pytest.mark.parametrize("state", ["cold", "warm", "expired", "filtered"])
def test_get_never_discovers_or_rewrites_cache(
self, model_cache_client, state
):
probe = model_cache_client
if state != "cold":
_seed_provider_cache(probe.username, expired=state == "expired")
if state == "filtered":
from local_deep_research.security.egress.policy import (
EgressContext,
EgressScope,
)
probe.policy.return_value = (
EgressContext(
scope=EgressScope.PRIVATE_ONLY,
primary_engine="library",
require_local_llm=True,
require_local_embeddings=True,
),
{"policy.egress_scope": "private_only"},
)
before = _provider_cache_rows(probe.username)
with _observe_provider_cache_sql() as sql:
response = probe.client.get(AVAILABLE_MODELS_PATH)
assert response.status_code == 200, response.text
body = response.json()
assert set(body) == {
"providers",
"provider_options",
"benchmark_provider_options",
}
if state == "warm":
assert body["providers"]["openai_models"] == [
{
"value": "cached-model",
"label": "Cached model",
"provider": "OPENAI",
}
]
else:
assert body["providers"] == {}
if state == "filtered":
assert body["provider_options"][0]["disabled"] is True
assert sql.reads, "The real cache SELECT must run"
assert sql.writes == []
assert _provider_cache_rows(probe.username) == before
probe.discovery.assert_not_called()
probe.credentials.assert_not_called()
probe.list_models.assert_not_called()
def test_get_cache_query_error_cannot_fall_through_to_discovery(
self, model_cache_client
):
probe = model_cache_client
_seed_provider_cache(probe.username)
before = _provider_cache_rows(probe.username)
with _observe_provider_cache_sql(fail_reads=True) as sql:
response = probe.client.get(AVAILABLE_MODELS_PATH)
assert sql.reads, "The failure must be injected into an actual SELECT"
assert response.status_code == 500, response.text
assert response.json() == {
"status": "error",
"message": "Failed to retrieve available models",
}
assert "private-cache-diagnostic" not in response.text
assert sql.writes == []
assert _provider_cache_rows(probe.username) == before
probe.discovery.assert_not_called()
probe.credentials.assert_not_called()
probe.list_models.assert_not_called()
@pytest.mark.parametrize("state", ["cold", "warm", "expired"])
def test_ordinary_post_is_cache_first(self, model_cache_client, state):
probe = model_cache_client
if state != "cold":
_seed_provider_cache(probe.username, expired=state == "expired")
before = _provider_cache_rows(probe.username)
with _observe_provider_cache_sql() as sql:
response = probe.client.post(
AVAILABLE_MODELS_PATH, headers={"X-CSRFToken": probe.token}
)
assert response.status_code == 200, response.text
expected = "cached-model" if state == "warm" else "fresh-model"
assert [
m["value"] for m in response.json()["providers"]["openai_models"]
] == [expected]
after = _provider_cache_rows(probe.username)
if state == "warm":
assert after == before
assert sql.writes == []
probe.discovery.assert_not_called()
probe.credentials.assert_not_called()
probe.list_models.assert_not_called()
else:
assert after != before
assert [row["model_key"] for row in after] == ["fresh-model"]
assert sql.writes
probe.discovery.assert_called_once()
probe.credentials.assert_called_once_with(
"llm.openai.api_key", probe.username, ""
)
probe.list_models.assert_called_once_with("cache-test-key", None)
def test_forced_post_bypasses_a_fresh_cache(self, model_cache_client):
probe = model_cache_client
_seed_provider_cache(probe.username)
before = _provider_cache_rows(probe.username)
with _observe_provider_cache_sql() as sql:
response = probe.client.post(
f"{AVAILABLE_MODELS_PATH}?force_refresh=true",
headers={"X-CSRFToken": probe.token},
)
assert response.status_code == 200, response.text
assert [
m["value"] for m in response.json()["providers"]["openai_models"]
] == ["fresh-model"]
after = _provider_cache_rows(probe.username)
assert after != before
assert [row["model_key"] for row in after] == ["fresh-model"]
assert sql.writes
probe.discovery.assert_called_once()
probe.credentials.assert_called_once_with(
"llm.openai.api_key", probe.username, ""
)
probe.list_models.assert_called_once_with("cache-test-key", None)
@pytest.mark.parametrize(
"forced", [False, True], ids=["ordinary", "forced"]
)
@pytest.mark.parametrize(
"token", [None, "wrong-session-token"], ids=["missing", "bad"]
)
def test_post_without_csrf_cannot_read_or_mutate_cache(
self, model_cache_client, forced, token
):
probe = model_cache_client
_seed_provider_cache(probe.username)
before = _provider_cache_rows(probe.username)
url = AVAILABLE_MODELS_PATH + ("?force_refresh=true" if forced else "")
headers = {} if token is None else {"X-CSRFToken": token}
with _observe_provider_cache_sql() as sql:
response = probe.client.post(url, headers=headers)
assert response.status_code == 403, response.text
assert response.headers.get("X-LDR-CSRF-Rejected") == "1"
assert sql.reads == sql.writes == []
assert _provider_cache_rows(probe.username) == before
probe.policy.assert_not_called()
probe.options.assert_not_called()
probe.discovery.assert_not_called()
probe.credentials.assert_not_called()
probe.list_models.assert_not_called()
@pytest.mark.parametrize("warm", [False, True], ids=["cold", "warm"])
def test_legacy_forced_get_refuses_before_any_cache_work(
self, model_cache_client, warm
):
probe = model_cache_client
if warm:
_seed_provider_cache(probe.username)
before = _provider_cache_rows(probe.username)
with _observe_provider_cache_sql() as sql:
response = probe.client.get(
f"{AVAILABLE_MODELS_PATH}?force_refresh=true"
)
assert response.status_code == 405, response.text
assert response.headers["Allow"] == "POST"
assert sql.reads == sql.writes == []
assert _provider_cache_rows(probe.username) == before
probe.policy.assert_not_called()
probe.options.assert_not_called()
probe.discovery.assert_not_called()
probe.credentials.assert_not_called()
probe.list_models.assert_not_called()
# ===========================================================================
# COVERAGE AREA 3 -- queued-submission attribution
# ===========================================================================
# Everything a caller might try to plant. ``system.user`` is the attribution
# field; the rest pin arbitrary per-run overrides into the persisted snapshot.
SPOOFED_METADATA = {
"system": {
"user": "victim-account",
"timestamp": "1999-01-01T00:00:00+00:00",
"version": "666",
"server_url": "http://attacker.invalid/",
},
"submission": {
"model_provider": "attacker_provider",
"model": "attacker-model",
"custom_endpoint": "http://169.254.169.254/",
"strategy": "attacker-strategy",
},
"submission_overrides": ["model_provider", "model", "custom_endpoint"],
"settings_snapshot": {"llm.openai.api_key": {"value": "attacker-planted"}},
# Positive control: an ordinary, non-reserved key must round-trip.
"client_note": "ordinary metadata survives",
}
# Only ``query`` is required; the rest are supplied so the request never
# depends on whatever defaults are in the user's settings DB. ``model`` and
# ``strategy`` are the fields the spoofed ``submission`` above tries to
# overwrite, so the resolved values are distinguishable from the planted ones.
SPOOF_BODY = {
"query": "attribution spoofing probe",
"model_provider": "ollama",
"model": "resolved-model",
"search_engine": "wikipedia",
"iterations": 1,
"questions_per_iteration": 1,
"strategy": "source-based",
"metadata": SPOOFED_METADATA,
}
def _assert_attribution_is_the_caller(meta, username):
"""The shared post-condition for both dispatch paths."""
assert meta["system"]["user"] == username, (
"the persisted attribution must be the AUTHENTICATED user; a caller "
"able to set metadata.system.user submits work recorded as someone "
f"else's. Got {meta['system']['user']!r}"
)
assert meta["system"]["timestamp"] != "1999-01-01T00:00:00+00:00"
assert meta["system"]["version"] == "1.0"
assert meta["system"]["server_url"] != "http://attacker.invalid/"
assert meta["submission"]["model"] == "resolved-model", (
"submission must be rebuilt from the resolved request parameters, "
f"not taken from metadata.submission. Got {meta['submission']!r}"
)
assert meta["submission"]["model_provider"] == "ollama"
assert meta["submission"]["strategy"] == "source-based"
assert meta["submission"]["custom_endpoint"] != "http://169.254.169.254/"
# Rebuilt from the fields actually supplied in the body — note
# ``custom_endpoint`` was NOT supplied, so a planted list is detectable.
assert meta["submission_overrides"] == [
"model_provider",
"model",
"search_engine",
"iterations",
"questions_per_iteration",
"strategy",
], f"submission_overrides was not rebuilt: {meta['submission_overrides']!r}"
assert meta["settings_snapshot"].get("llm.openai.api_key") != {
"value": "attacker-planted"
}
assert len(meta["settings_snapshot"]) > 10, (
"settings_snapshot must be the server-captured snapshot, not the "
"client-supplied stub"
)
# Positive control: without it, a route that dropped ALL client metadata
# would satisfy every assertion above while silently losing a feature.
assert meta["client_note"] == "ordinary metadata survives", (
"unreserved metadata keys must still round-trip; the guard is a "
"reserved-key filter, not a blanket drop"
)
class TestQueuedSubmissionAttributionSpoofing:
"""``routers/research.py:1050`` — ``reserved_metadata_keys``.
Pinned behaviour is IGNORE, not reject: the route answers 200 and filters
the reserved keys out of the merge. That is what the code does
(``research.py:1056-1062``), so that is what is asserted.
"""
SPAWN_TARGET = f"{RESEARCH_ROUTER}.start_research_process"
def test_direct_dispatch_ignores_reserved_metadata_keys(self, live_app):
client, username = _new_user(live_app, "meta_direct")
with patch(self.SPAWN_TARGET) as spawn:
spawn.return_value = MagicMock(ident=6060)
resp = _post_json(client, START_RESEARCH_PATH, SPOOF_BODY)
assert resp.status_code == 200, resp.text[:400]
body = resp.json()
assert body["status"] == "success", body
assert spawn.called, (
"the run never started — every 'was not spoofed' assertion below "
"would be vacuous"
)
_assert_attribution_is_the_caller(
_research_meta(username, body["research_id"]), username
)
def test_queued_dispatch_ignores_reserved_metadata_keys(self, live_app):
"""The queued path persists the same metadata into
``QueuedResearch.settings_snapshot`` AND forwards it to the queue
processor, so both sinks are asserted.
``clamp_user_max_concurrent`` is patched to 0 purely to select the
queue branch (``active_count >= max`` with no active researches). It
sits above and independently of the metadata construction, so the
guard still runs for real.
"""
client, username = _new_user(live_app, "meta_queued")
with (
patch(
f"{RESEARCH_ROUTER}.clamp_user_max_concurrent", return_value=0
),
patch(QUEUE_PROCESSOR) as queue_processor,
patch(self.SPAWN_TARGET) as spawn,
):
resp = _post_json(client, START_RESEARCH_PATH, SPOOF_BODY)
assert resp.status_code == 200, resp.text[:400]
body = resp.json()
assert body["status"] == ResearchStatus.QUEUED.value, body
spawn.assert_not_called()
queue_processor.notify_research_queued.assert_called_once()
research_id = body["research_id"]
# 1. what was persisted for the queue processor to pick up later
_assert_attribution_is_the_caller(
_queued_snapshot(username, research_id), username
)
# 2. what was handed to the processor in-process
forwarded = queue_processor.notify_research_queued.call_args.kwargs[
"settings_snapshot"
]
_assert_attribution_is_the_caller(forwarded, username)
# 3. and the history row the UI attributes the run from
_assert_attribution_is_the_caller(
_research_meta(username, research_id), username
)