~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
fix(settings): show bounded validation reasons for form saves (#7663)
Chanhyo Jung98f433f9d0df
diff --git a/changelog.d/7649.bugfix.md b/changelog.d/7649.bugfix.mdnew file mode 100644index 0000000000..08ca9bdde9--- /dev/null+++ b/changelog.d/7649.bugfix.md@@ -0,0 +1 @@+Show bounded per-setting validation feedback when a native settings form submission partially fails.diff --git a/src/local_deep_research/web/routers/settings.py b/src/local_deep_research/web/routers/settings.pyindex 6db1410e59..a33519721a 100644--- a/src/local_deep_research/web/routers/settings.py+++ b/src/local_deep_research/web/routers/settings.py@@ -57,6 +57,7 @@ import asyncioimport jsonimport mathimport platform+import reimport threadingimport timefrom types import SimpleNamespace@@ -2558,6 +2559,42 @@ def reset_to_defaults(}+def _add_form_rejection(+ details: list[str], key: str, reason: str | None+) -> None:+ """Collect at most three trusted rejection summaries, totaling <=600 bytes.++ Keys come from admitted database rows (or the fixed summary key), never+ submitted values. Do not copy option lists, metadata messages or exceptions+ into the session. The aggregate failure count also covers omitted details.+ """+ if len(details) >= 3:+ return+ safe_reason = "Invalid value"+ if reason in (+ "Value must be a positive integer",+ "Value must be a number",+ "Value must be a boolean",+ ):+ safe_reason = reason+ elif isinstance(reason, str):+ if reason.startswith("Value must be one of:"):+ safe_reason = "Value must be an allowed option"+ elif len(reason) <= 80 and re.fullmatch(+ r"Value must be at (?:least|most) -?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?",+ reason,+ ):+ safe_reason = reason+ # Bound each entry while collecting, leaving room for both separators.+ # Decode only complete UTF-8 characters; the ellipsis counts in the cap.+ key = key[:199]+ entry = f"{key}: {safe_reason}"+ if len(entry.encode("utf-8")) > 198:+ key = key.encode("utf-8")[: 198 - len(safe_reason.encode("utf-8")) - 5]+ entry = f"{key.decode('utf-8', errors='ignore')}…: {safe_reason}"+ details.append(entry)++def _save_settings_sync(form_data: dict, username: str) -> dict:"""Sync helper for save_settings — runs the bulk setting writes.@@ -2595,6 +2632,7 @@ def _save_settings_sync(form_data: dict, username: str) -> dict:}failed_count = 0+ rejection_details: list[str] = []rejected_count = 0changed_settings: list[str] = []sentinel_rejected_ui_elements: list[str | None] = []@@ -2612,6 +2650,11 @@ def _save_settings_sync(form_data: dict, username: str) -> dict:try:value = to_positive_int(value)except ValueError:+ _add_form_rejection(+ rejection_details,+ key,+ "Value must be a positive integer",+ )failed_count += 1continue@@ -2791,6 +2834,9 @@ def _save_settings_sync(form_data: dict, username: str) -> dict:f"Validation failed for setting {key}: "f"{error_message}")+ _add_form_rejection(+ rejection_details, db_setting.key, error_message+ )failed_count += 1continue@@ -2834,6 +2880,7 @@ def _save_settings_sync(form_data: dict, username: str) -> dict:"ok": failed_count == 0 and not sentinel_rejected_ui_elements,"policy_error": None,"failed": failed_count,+ "rejection_details": "; ".join(rejection_details),"rejected": rejected_count,"sentinel_rejected": sentinel_rejected_ui_elements,"sentinel_rejected_too_deep": sentinel_rejected_too_deep,@@ -2902,7 +2949,12 @@ async def save_settings(flash(request,f"Saved with {outcome['failed']} setting(s) failing. "- "Check the values and try again.",+ "Check the values and try again."+ + (+ " " + outcome["rejection_details"]+ if outcome.get("rejection_details")+ else ""+ ),"warning",)elif outcome.get("rejected"):diff --git a/tests/ui_tests/test_settings_form_post_fallback_ci.js b/tests/ui_tests/test_settings_form_post_fallback_ci.jsindex 967dd3784e..55788664a5 100644--- a/tests/ui_tests/test_settings_form_post_fallback_ci.js+++ b/tests/ui_tests/test_settings_form_post_fallback_ci.js@@ -49,7 +49,7 @@ async function loadSettingsFormWithFields(page, baseUrl) {async function submitSettingsFormNatively(page) {await Promise.all([page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 30000 }),- page.evaluate(() => document.getElementById('settings-form').submit()),+ page.evaluate(() => window.HTMLFormElement.prototype.submit.call(document.getElementById('settings-form'))),]);}@@ -167,6 +167,35 @@ async function main() {},);+ await run('Form POST fallback', 'Invalid summary shows its constraint while a valid mixed field persists', async () => {+ await loadSettingsFormWithFields(page, baseUrl);+ const summaryKey = 'search.fetch.summary_input_max_length';+ await page.waitForSelector(`#settings-form [name="${summaryKey}"]`);+ await page.waitForSelector('#settings-form [name="llm.temperature"]');+ const before = await page.evaluate(async (key) => (await fetch(`/settings/api/${key}`)).json(), summaryKey);+ await page.evaluate((key) => {+ document.querySelector(`[name="${key}"]`).value = '0';+ document.querySelector('[name="llm.temperature"]').value = '0.55';+ }, summaryKey);+ const postedRequest = page.waitForRequest((request) =>+ request.method() === 'POST' && new URL(request.url()).pathname === '/settings/save_settings');+ await submitSettingsFormNatively(page);+ const posted = new URLSearchParams((await postedRequest).postData());+ const warning = await page.$eval('.ldr-alert-warning[role="alert"]', (el) => el.textContent.trim());+ const after = await page.evaluate(async (key) => ({+ summary: await (await fetch(`/settings/api/${key}`)).json(),+ temperature: await (await fetch('/settings/api/llm.temperature')).json(),+ }), summaryKey);+ const reload = await page.goto(`${baseUrl}/settings/`, { waitUntil: 'domcontentloaded' });+ const html = await reload.text();+ const passed = posted.get(summaryKey) === '0' && posted.get('llm.temperature') === '0.55' &&+ warning.includes('Saved with 1 setting(s) failing.') &&+ warning.includes(`${summaryKey}: Value must be a positive integer`) &&+ !warning.includes('llm.temperature:') && after.temperature.value === 0.55 &&+ after.summary.value === before.value && !html.includes('Value must be a positive integer');+ return { passed, message: `posted summary=${posted.get(summaryKey)}, temperature=${posted.get('llm.temperature')}; alert=${warning}; persisted temperature=${after.temperature.value}; summary unchanged=${after.summary.value === before.value}; flash consumed=${!html.includes('Value must be a positive integer')}` };+ });+// ===============================================================// 3. Flash messages are one-shot: reloading the redirect target// again must NOT re-show the same message (session .pop(), not@@ -194,7 +223,7 @@ async function main() {console.error(error.stack);} finally {results.print();- results.save();+ results.save(ctx.config.resultsDir);await teardownTest(ctx);process.exit(results.exitCode());}diff --git a/tests/web/routers/test_settings_env_lock_write_paths_port.py b/tests/web/routers/test_settings_env_lock_write_paths_port.pyindex 4ef2811157..b176cca599 100644--- a/tests/web/routers/test_settings_env_lock_write_paths_port.py+++ b/tests/web/routers/test_settings_env_lock_write_paths_port.py@@ -158,3 +158,267 @@ class TestNoJsFormSaveCountsEnvLockedKeyAsFailure:page = authenticated_client.get("/settings/", follow_redirects=False)assert "setting(s) failing" not in page.text+++SUMMARY_KEY = "search.fetch.summary_input_max_length"+++def test_native_summary_rejection_feedback(authenticated_client):+ original = authenticated_client.get(f"/settings/api/{SUMMARY_KEY}").json()+ response = authenticated_client.post(+ "/settings/save_settings",+ data={SUMMARY_KEY: "0"},+ follow_redirects=False,+ )+ assert response.status_code == 302+ assert (+ authenticated_client.get(f"/settings/api/{SUMMARY_KEY}").json()+ == original+ )+ page = authenticated_client.get("/settings/")+ assert "Saved with 1 setting(s) failing." in page.text+ assert f"{SUMMARY_KEY}: Value must be a positive integer" in page.text+ assert (+ "Value must be a positive integer"+ not in authenticated_client.get("/settings/").text+ )+++def _native_warning(client, data):+ response = client.post(+ "/settings/save_settings", data=data, follow_redirects=False+ )+ assert response.status_code == 302+ page = client.get("/settings/")+ assert page.status_code == 200+ from bs4 import BeautifulSoup++ warning = BeautifulSoup(page.text, "html.parser").select_one(+ ".ldr-alert-warning[role=alert]"+ )+ assert warning is not None+ return warning.get_text(" ", strip=True), page.text+++def test_native_mixed_validation_and_env_lock(authenticated_client, env_locked):+ before = authenticated_client.get(f"/settings/api/{SUMMARY_KEY}").json()+ warning, _ = _native_warning(+ authenticated_client,+ {+ SUMMARY_KEY: "0",+ "llm.temperature": "0.55",+ ENV_LOCKED_KEY: "ignored",+ },+ )+ assert "Saved with 1 setting(s) failing." in warning+ assert f"{SUMMARY_KEY}: Value must be a positive integer" in warning+ assert "llm.temperature:" not in warning+ assert ENV_LOCKED_KEY not in warning+ assert (+ authenticated_client.get(f"/settings/api/{SUMMARY_KEY}").json()+ == before+ )+ assert (+ authenticated_client.get("/settings/api/llm.temperature").json()[+ "value"+ ]+ == 0.55+ )+++def test_native_normal_numeric_constraint(authenticated_client):+ before = authenticated_client.get("/settings/api/llm.temperature").json()+ warning, _ = _native_warning(+ authenticated_client, {"llm.temperature": "-1"}+ )+ assert "llm.temperature: Value must be at least 0" in warning+ assert (+ authenticated_client.get("/settings/api/llm.temperature").json()+ == before+ )+++def test_native_unsafe_validation_reasons_are_generic(+ authenticated_client, monkeypatch+):+ from local_deep_research.web.routers import settings++ # Inject only the validator boundary; the authenticated route, persistence+ # admission, outcome, flash and rendered HTML remain real.+ monkeypatch.setattr(+ settings,+ "validate_setting",+ lambda *args: (False, "<script>private-token</script>"),+ )+ warning, html = _native_warning(+ authenticated_client, {"llm.temperature": "0.6"}+ )+ assert "llm.temperature: Invalid value" in warning+ assert "private-token" not in html+++def test_native_persistence_failure_remains_generic(+ authenticated_client, monkeypatch+):+ from local_deep_research.settings.manager import SettingsManager++ monkeypatch.setattr(+ SettingsManager, "set_setting", lambda *args, **kwargs: False+ )+ warning, _ = _native_warning(+ authenticated_client, {"llm.temperature": "0.6"}+ )+ assert (+ warning+ == "Saved with 1 setting(s) failing. Check the values and try again."+ )+++ "reason, expected",+ [+ (+ "Value must be one of: secret-token, password",+ "Value must be an allowed option",+ ),+ ("Value must be at least 0<script>secret</script>", "Invalid value"),+ ("Value must be at most 1.5", "Value must be at most 1.5"),+ ("/private/path SQL password", "Invalid value"),+ (None, "Invalid value"),+ ],+)+def test_form_rejection_safe_vocabulary(reason, expected):+ from local_deep_research.web.routers.settings import _add_form_rejection++ details = []+ _add_form_rejection(details, "llm.temperature", reason)+ assert details == [f"llm.temperature: {expected}"]+++def test_form_rejection_utf8_collection_bound():+ from local_deep_research.web.routers.settings import _add_form_rejection++ details = []+ for _ in range(100):+ _add_form_rejection(+ details, "llm." + "界" * 500, "Value must be a number"+ )+ assert len(details) == 3+ assert 590 <= len("; ".join(details).encode("utf-8")) <= 600+ assert all(entry.endswith("…: Value must be a number") for entry in details)+++def test_native_bounded_details_escape_and_suppress_options(+ authenticated_client,+):+ from local_deep_research.database.models.settings import (+ Setting,+ SettingType,+ )+ from local_deep_research.database.session_context import get_user_db_session++ username = authenticated_client.get("/auth/check").json()["username"]+ keys = ["llm.<img src=x onerror=alert(1)>"] + [+ "llm." + "界" * 100 + str(index) for index in range(3)+ ]+ with get_user_db_session(username) as session:+ for key in keys:+ session.add(+ Setting(+ key=key,+ name="Test constraint",+ type=SettingType.LLM,+ value="stored-secret",+ ui_element="select",+ visible=False,+ options=["stored-secret", "option-secret"],+ )+ )+ session.commit()+ warning, html = _native_warning(+ authenticated_client, dict.fromkeys(keys, "submitted-secret")+ )+ prefix = "Saved with 4 setting(s) failing. Check the values and try again. "+ assert warning.startswith(prefix)+ detail = warning.removeprefix(prefix)+ assert detail.count("Value must be an allowed option") == 3+ assert len(detail.encode("utf-8")) <= 600+ assert keys[0] in detail+ assert "<img src=x onerror=alert(1)>" in html+ assert "<img src=x onerror=alert(1)>" not in html+ for secret in ("stored-secret", "option-secret", "submitted-secret"):+ assert secret not in html+ with get_user_db_session(username) as session:+ assert all(+ session.query(Setting).filter_by(key=key).one().value+ == "stored-secret"+ for key in keys+ )+ assert (+ "Value must be an allowed option"+ not in authenticated_client.get("/settings/").text+ )+++def test_native_commit_failure_remains_generic(+ authenticated_client, monkeypatch+):+ from sqlalchemy.orm import Session++ original_commit = Session.commit++ def fail_settings_commit(session):+ # Fail the real settings transaction, leaving unrelated session/auth+ # persistence operational for the redirect and rendered GET.+ if any(+ getattr(row, "key", None) == "llm.temperature"+ for row in session.dirty+ ):+ raise RuntimeError("private-path credential SQL")+ return original_commit(session)++ before = authenticated_client.get("/settings/api/llm.temperature").json()+ with monkeypatch.context() as patch:+ patch.setattr(Session, "commit", fail_settings_commit)+ response = authenticated_client.post(+ "/settings/save_settings",+ data={SUMMARY_KEY: "0", "llm.temperature": "0.55"},+ follow_redirects=False,+ )+ assert response.status_code == 302+ page = authenticated_client.get("/settings/")+ assert (+ "Saved with 2 setting(s) failing. Check the values and try again."+ in page.text+ )+ assert "Value must be a positive integer" not in page.text+ assert "private-path credential SQL" not in page.text+ assert (+ authenticated_client.get("/settings/api/llm.temperature").json()+ == before+ )+++ "extra, expected",+ [+ (+ {"llm.allowed_local_hostnames": "not-json"},+ "must be a JSON array of hostnames",+ ),+ ({"llm.feedback_api_key": "[REDACTED]"}, "Rejected 1 settings:"),+ ],+)+def test_native_priority_messages_survive(+ authenticated_client, extra, expected+):+ response = authenticated_client.post(+ "/settings/save_settings",+ data={SUMMARY_KEY: "0", **extra},+ follow_redirects=False,+ )+ assert response.status_code == 302+ page = authenticated_client.get("/settings/")+ assert expected in page.text+ assert "Value must be a positive integer" not in page.text+ assert "setting(s) failing" not in page.text