~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
"""Two FastAPI-port regressions in
``src/local_deep_research/web/routers/settings.py`` vs. ``origin/main``'s
Flask ``web/routes/settings_routes.py``.
1. ``POST /settings/save_settings`` (``_save_settings_sync``) -- the no-JS
form-POST fallback -- coerced values and wrote them straight to the DB
without ever calling ``validate_setting()``, so ``min_value``/
``max_value``/``options`` constraints went unenforced on this path while
the JSON POST (``save_all_settings``) and the single-key PUT
(``api_update_setting``) both enforce them. Main had this exact guard
(``fb49985aa8``: "this JS-disabled POST fallback wrote values
unchecked") but it was not carried into the FastAPI rewrite.
Fixed by adding the same ``validate_setting()`` call the sibling paths
use, preserving the route's best-effort semantics: invalid keys are
skipped and counted in ``failed_count``, the rest of the batch still
validates, saves, and commits (not converted to all-or-nothing).
2. ``GET /settings/api/{key}`` (``api_get_db_setting``) returned the raw DB
value with ``editable=True`` even when an ``LDR_*`` env var overrides the
setting, while ``GET /settings/api`` and ``GET /settings/api/bulk`` both
apply that overlay (via ``SettingsManager.get_all_settings()``). Main
applied it on the single-key route too, via
``_shape_single_effective_metadata``.
Fixed by overlaying ``SettingsManager.get_all_settings()`` -- the same
overlay the bulk endpoints already use -- onto both return branches of
``api_get_db_setting``, via the new ``_apply_env_override()`` helper.
This is a read/display fix only: writes to an env-locked setting were
already rejected server-side by
``SettingsManager._is_environment_locked`` regardless of this bug.
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 (same failure mode documented in
``test_settings_env_lock_403.py`` and ``test_auth_disconnected_db_gate.py``).
"""
import uuid
import pytest
from fastapi.testclient import TestClient
@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}.9"
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_settingsgap_{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/POST,
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
# search.iterations: ui_element="number", min_value=1, default 3.
NUMERIC_KEY = "search.iterations"
# app.theme: ui_element="select" with a fixed options list, default "dark",
# and NOT in DYNAMIC_SETTINGS (whose options validation is intentionally
# skipped) -- so an out-of-options value must be rejected.
SELECT_KEY = "app.theme"
# llm.temperature: ui_element="range", 0.0-1.0, default 0.7. Used as the
# "valid key in the same batch" that must still save.
VALID_KEY = "llm.temperature"
SUMMARY_INPUT_KEY = "search.fetch.summary_input_max_length"
@pytest.mark.parametrize("route", ["put", "bulk"])
def test_summary_input_budget_errors_hide_parser_details(
auth_client, monkeypatch, route
):
"""Client validation errors must not depend on parser exception text."""
from local_deep_research.web.routers import settings
endpoint = f"/settings/api/{SUMMARY_INPUT_KEY}"
original = auth_client.get(endpoint).json()["value"]
def fail_conversion(value):
raise ValueError("internal-parser-diagnostic")
monkeypatch.setattr(settings, "to_positive_int", fail_conversion)
if route == "put":
response = auth_client.put(endpoint, json={"value": 600001})
else:
response = auth_client.post(
"/settings/save_all_settings",
json={SUMMARY_INPUT_KEY: 600001},
)
assert response.status_code == 400, response.text
if route == "put":
assert response.json() == {
"error": f"Invalid value for setting {SUMMARY_INPUT_KEY}"
}
else:
assert response.json()["errors"] == [
{
"key": SUMMARY_INPUT_KEY,
"name": "Fetch Summary Input Max Length",
"error": "Value must be a positive integer",
}
]
assert "internal-parser-diagnostic" not in response.text
assert auth_client.get(endpoint).json()["value"] == original
@pytest.mark.parametrize("route", ["put", "bulk", "form"])
@pytest.mark.parametrize("recreate", [False, True])
def test_summary_input_budget_saves_require_positive_integers(
auth_client, route, recreate
):
"""Rejected edits must not leave a saved value that breaks summary fetches."""
endpoint = f"/settings/api/{SUMMARY_INPUT_KEY}"
original = auth_client.get(endpoint).json()["value"]
if recreate:
deleted = auth_client.delete(endpoint)
assert deleted.status_code == 200, deleted.text
def save(value):
if route == "put":
return auth_client.put(endpoint, json={"value": value})
if route == "bulk":
return auth_client.post(
"/settings/save_all_settings", json={SUMMARY_INPUT_KEY: value}
)
csrf = auth_client.get("/auth/csrf-token").json()["csrf_token"]
return auth_client.post(
"/settings/save_settings",
data={
SUMMARY_INPUT_KEY: "" if value is None else str(value),
"csrf_token": csrf,
},
follow_redirects=False,
)
try:
for value in (1.5, None):
response = save(value)
if route == "form":
assert response.status_code in (302, 303), response.text
else:
assert response.status_code == 400, response.text
current = auth_client.get(endpoint)
# Defaults may be materialized again on a read after DELETE.
if current.status_code == 200:
assert current.json()["value"] == original
else:
assert recreate and current.status_code == 404, current.text
response = save(600001)
expected_statuses = (302, 303) if route == "form" else (200, 201)
assert response.status_code in expected_statuses, response.text
assert auth_client.get(endpoint).json()["value"] == 600001
finally:
restored = auth_client.put(endpoint, json={"value": original})
assert restored.status_code in (200, 201), restored.text
@pytest.mark.timeout(120)
class TestSaveSettingsFormPostValidation:
"""FIX 1: POST /settings/save_settings must enforce validate_setting()
per field, best-effort -- skip and report invalid keys, still save the
rest of the batch -- matching save_all_settings / api_update_setting.
"""
def test_out_of_bounds_and_out_of_options_not_persisted_valid_key_saved(
self, auth_client
):
original_valid = auth_client.get(f"/settings/api/{VALID_KEY}").json()[
"value"
]
try:
csrf = auth_client.get("/auth/csrf-token").json()["csrf_token"]
resp = auth_client.post(
"/settings/save_settings",
data={
NUMERIC_KEY: "-5", # below min_value=1
SELECT_KEY: "not-a-real-theme", # not in options
VALID_KEY: "0.42", # valid, in the SAME batch
"csrf_token": csrf,
},
follow_redirects=False,
)
assert resp.status_code in (302, 303), resp.text
# Invalid values must NOT be persisted.
got_iterations = auth_client.get(
f"/settings/api/{NUMERIC_KEY}"
).json()["value"]
assert got_iterations != -5, (
"out-of-bounds numeric was persisted through the no-JS "
f"form-POST fallback (min_value=1): got {got_iterations!r}"
)
got_theme = auth_client.get(f"/settings/api/{SELECT_KEY}").json()[
"value"
]
assert got_theme != "not-a-real-theme", (
"out-of-options select value was persisted through the "
f"no-JS form-POST fallback: got {got_theme!r}"
)
# A valid key in the SAME batch must still save -- proving
# best-effort (not all-or-nothing) semantics survived the fix.
got_valid = auth_client.get(f"/settings/api/{VALID_KEY}").json()[
"value"
]
assert got_valid == 0.42, (
"a valid key in the same batch as invalid ones was not "
f"saved: got {got_valid!r}"
)
finally:
# Restore via the (independently correct) single-key PUT path,
# not save_settings again.
auth_client.put(
f"/settings/api/{VALID_KEY}",
json={"value": original_valid},
)
def test_normal_save_still_works(self, auth_client):
"""Sanity/counterpart: a fully valid batch still saves via the
no-JS fallback -- the fix must not turn this into an all-or-nothing
route or break the happy path."""
original = auth_client.get(f"/settings/api/{VALID_KEY}").json()["value"]
try:
csrf = auth_client.get("/auth/csrf-token").json()["csrf_token"]
resp = auth_client.post(
"/settings/save_settings",
data={VALID_KEY: "0.33", "csrf_token": csrf},
follow_redirects=False,
)
assert resp.status_code in (302, 303), resp.text
got = auth_client.get(f"/settings/api/{VALID_KEY}").json()["value"]
assert got == 0.33
finally:
auth_client.put(
f"/settings/api/{VALID_KEY}", json={"value": original}
)
@pytest.mark.timeout(120)
class TestSingleKeyGetEnvOverlay:
"""FIX 2: GET /settings/api/{key} must apply the same LDR_* env-var
overlay (effective value + editable=False) that GET /settings/api
(bulk) applies, for the same key."""
def test_single_key_matches_bulk_under_env_override(
self, auth_client, monkeypatch
):
monkeypatch.setenv("LDR_SEARCH_ITERATIONS", "42")
single = auth_client.get(f"/settings/api/{NUMERIC_KEY}").json()
bulk = auth_client.get("/settings/api").json()
bulk_entry = bulk["settings"][NUMERIC_KEY]
assert single["value"] == 42, (
f"single-key GET ignored the env override: {single!r}"
)
assert single["editable"] is False, (
f"single-key GET reported editable=True under an env "
f"override: {single!r}"
)
assert single["value"] == bulk_entry["value"], (
"single-key and bulk endpoints disagree on the effective "
f"value: {single['value']!r} vs {bulk_entry['value']!r}"
)
assert single["editable"] == bulk_entry["editable"], (
"single-key and bulk endpoints disagree on editable: "
f"{single['editable']!r} vs {bulk_entry['editable']!r}"
)
def test_single_key_get_unaffected_without_env_var(self, auth_client):
"""Sanity/counterpart: without the env var set, the same key is
editable and reflects the (typed) DB value -- the fix must not
make every setting look env-locked."""
resp = auth_client.get(f"/settings/api/{NUMERIC_KEY}")
assert resp.status_code == 200
data = resp.json()
assert data["editable"] is True
assert isinstance(data["value"], int)