~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(chat): guard chat research against missing DB password (#4457) (#4571)
LearningCircuit7c358b4676f3
diff --git a/changelog.d/4571.bugfix.md b/changelog.d/4571.bugfix.mdnew file mode 100644index 0000000000..a65aa40743--- /dev/null+++ b/changelog.d/4571.bugfix.md@@ -0,0 +1 @@+Fixed chat-initiated research running without the user's database password after the in-memory session-password store expired (24h TTL) or was cleared by a server/container restart while the session cookie was still valid. In that state the research completed (so the user saw results) but every background metric write was silently dropped, leaving the metrics dashboard empty (part of #4457). The chat `send_message` route now applies the same encryption-aware password guard as the direct and follow-up research routes, returning a clear "session expired — log out and log back in" message before any rows are created. Unencrypted installs are unaffected.diff --git a/src/local_deep_research/chat/routes.py b/src/local_deep_research/chat/routes.pyindex 5ad04943f2..6c640a6d1e 100644--- a/src/local_deep_research/chat/routes.py+++ b/src/local_deep_research/chat/routes.py@@ -39,6 +39,7 @@ from ..settings.manager import SettingsManagerfrom ..web.auth.decorators import login_requiredfrom ..web.utils.templates import render_template_with_defaultsfrom ..web.auth.password_utils import get_user_password+from ..database.encrypted_db import db_managerfrom ..web.routes.globals import (cleanup_research,is_research_thread_alive,@@ -853,6 +854,29 @@ def send_message(session_id):# Always quick mode in chat (intentional v1 scope).research_mode = "quick"+ # Verify the DB password is available BEFORE creating any rows+ # or spawning a worker. Chat-triggered research runs on a+ # background thread that writes token/search metrics to the+ # user's encrypted database; without the password every metric+ # write is silently dropped, leaving the metrics dashboard empty+ # while the research still completes (issue #4457). The direct+ # (/start_research) and follow-up routes apply the same guard.+ # Trigger: password-store TTL expiry or a server/container+ # restart while the session cookie is still valid.+ if db_manager.has_encryption and not get_user_password(username):+ logger.error(+ f"No password available for user {username} with "+ "encrypted database - cannot start chat research "+ "(session password expired or lost after server restart)"+ )+ return jsonify(+ {+ "success": False,+ "error": "Your session has expired. Please log out "+ "and log back in to continue.",+ }+ ), 401+# ---- Concurrency guards (per-session + global per-user) ----# Both guards run in one transaction so a stale-row reclaim# is visible to the count check below it.diff --git a/tests/chat/test_chat_research_triggering.py b/tests/chat/test_chat_research_triggering.pyindex 4e04a486d0..662febba9e 100644--- a/tests/chat/test_chat_research_triggering.py+++ b/tests/chat/test_chat_research_triggering.py@@ -71,6 +71,95 @@ class TestResearchTriggering:# Verify research process was startedassert mock_start_research.called+ def test_send_message_blocks_research_when_password_missing_encrypted(+ self, authenticated_client+ ):+ """Encrypted DB + no DB password -> 401, no research spawned (#4457).++ Mirrors the guard on /start_research and the follow-up route: a+ chat-triggered research must not run passwordless, because its+ background metric writes (token/search) would be silently dropped,+ leaving the metrics dashboard empty while the research completes.+ Trigger in production: session-password TTL expiry or a server+ restart while the session cookie is still valid.+ """+ create_response = authenticated_client.post(+ "/api/chat/sessions",+ json={"initial_query": "Test query"},+ content_type="application/json",+ )+ session_id = json.loads(create_response.data)["session_id"]++ # Patch only the chat-route references: get_user_password returns+ # None (password lost) and db_manager reports an encrypted DB. The+ # session lookup above used the real stored password and is+ # unaffected (get_user_db_session imports its own references).+ with (+ patch(+ "local_deep_research.chat.routes.get_user_password",+ return_value=None,+ ),+ patch("local_deep_research.chat.routes.db_manager") as mock_db,+ patch(+ "local_deep_research.chat.routes.start_research_process"+ ) as mock_start_research,+ ):+ mock_db.has_encryption = True+ response = authenticated_client.post(+ f"/api/chat/sessions/{session_id}/messages",+ json={+ "content": "What is quantum computing?",+ "trigger_research": True,+ },+ content_type="application/json",+ )++ assert response.status_code == 401+ data = json.loads(response.data)+ assert data["success"] is False+ assert "log out" in data["error"].lower()+ # No worker thread spawned — the run is refused before side effects.+ mock_start_research.assert_not_called()++ def test_send_message_unencrypted_db_allows_research_without_password(+ self, authenticated_client+ ):+ """Non-encrypted DB must NOT be blocked by the password guard.++ The guard is encryption-aware: unencrypted installs have no+ encryption key requirement, so a missing get_user_password() value+ must not 401 the chat research path.+ """+ create_response = authenticated_client.post(+ "/api/chat/sessions",+ json={"initial_query": "Test query"},+ content_type="application/json",+ )+ session_id = json.loads(create_response.data)["session_id"]++ with (+ patch(+ "local_deep_research.chat.routes.get_user_password",+ return_value=None,+ ),+ patch("local_deep_research.chat.routes.db_manager") as mock_db,+ patch(+ "local_deep_research.chat.routes.start_research_process"+ ) as mock_start_research,+ ):+ mock_db.has_encryption = False+ response = authenticated_client.post(+ f"/api/chat/sessions/{session_id}/messages",+ json={+ "content": "What is quantum computing?",+ "trigger_research": True,+ },+ content_type="application/json",+ )++ assert response.status_code == 200+ assert mock_start_research.called+def test_send_message_uses_quick_mode_by_default(self, authenticated_client):