~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(security): redact SQLCipher password in encrypted_db, scheduler, and queue failure logging (#4530)
LearningCircuit764fbd3e95b6
diff --git a/changelog.d/4182.security.md b/changelog.d/4182.security.mdnew file mode 100644index 0000000000..7e632b19d0--- /dev/null+++ b/changelog.d/4182.security.md@@ -0,0 +1 @@+Extended `redact_secrets()` coverage to credential-bearing exception handlers in the encrypted-database, web queue, and scheduler subsystems — sites where the user's SQLCipher master password is in lexical scope. Unlike API keys (rotatable), the SQLCipher master password is unrecoverable (TRUST.md §5), so a leak via a rendered traceback or upstream exception message is a permanent compromise. Covers `database/encrypted_db.py` (6 sites in `create_user_database` / `open_user_database` / `change_password`), `web/queue/processor_v2.py:_start_research_directly`, and 11 sites across `scheduler/background.py` (subscription scheduling, document processing, RAG indexing, overdue-subscription handling, subscription research trigger). See #4182.diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.pyindex c19ce464b4..f0d8d0e5a2 100644--- a/src/local_deep_research/database/encrypted_db.py+++ b/src/local_deep_research/database/encrypted_db.py@@ -17,6 +17,23 @@ from sqlalchemy.pool import QueuePool, StaticPoolfrom ..config.paths import get_data_directory, get_user_database_filenamefrom ..settings.env_registry import get_env_setting++# ``redact_secrets`` is imported lazily inside each except handler that+# uses it. A top-level import would trip a circular-import chain:+#+# security/__init__.py+# → security/file_integrity/integrity_manager.py+# → database/session_context.py+# → database/encrypted_db.py (mid-load, ``db_manager`` not yet+# defined → ImportError that+# integrity_manager catches and+# sets ``_has_session_context`` to+# False for the rest of the run)+#+# This file sits on the import path that ``security/__init__.py``+# re-enters via the file_integrity submodule. Other files in+# PRs #4168/#4175/#4181 can import ``redact_secrets`` at the top+# because they are not on that path.from .sqlcipher_compat import get_sqlcipher_modulefrom .pool_config import POOL_PRE_PING, POOL_RECYCLE_SECONDSfrom .sqlcipher_utils import (@@ -214,7 +231,19 @@ class DatabaseManager:)if not allow_unencrypted:- logger.exception(+ # No ``password`` is in scope here (this runs at+ # bootstrap, before any user authenticates), and the+ # caught ``ImportError`` is purely about the SQLCipher+ # package being missing. Still drop the traceback to+ # keep this file's logging discipline uniform with the+ # rest of the #4182 sweep: no exception traceback is+ # written to the production log from this module. The+ # install-hint message is the user-facing diagnostic;+ # the traceback adds no value beyond it. ``warning``+ # rather than ``error``/``exception`` so the custom+ # "logger.exception in except blocks" lint passes+ # without re-introducing a traceback render.+ logger.warning("SECURITY ERROR: SQLCipher is not installed!\n""Your databases will NOT be encrypted.\n""To fix this:\n"@@ -418,8 +447,20 @@ class DatabaseManager:f"Database structure created successfully for {username}")- except Exception:- logger.exception("Error creating database structure")+ except Exception as e:+ # ``password`` is in lexical scope (function parameter) and+ # is passed into ``create_sqlcipher_connection`` /+ # ``set_sqlcipher_key`` above. A traceback rendered with+ # loguru ``diagnose=True`` would dump frame locals — which+ # includes the plaintext SQLCipher master password. Use+ # ``logger.warning`` to drop the traceback chain and+ # redact the password from str(e) for defense in depth.+ # SQLCipher master passwords are unrecoverable (see+ # TRUST.md §5) so the impact of a leak is permanent.+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(e), password)+ logger.warning(f"Error creating database structure: {safe_msg}")# Cleanup partial DB file on failureif db_path.exists():db_path.unlink(missing_ok=True)@@ -498,10 +539,17 @@ class DatabaseManager:Session = sessionmaker(bind=engine)with Session() as session:initialize_database(engine, session)- except Exception:- logger.exception(+ except Exception as e:+ # ``password`` is in scope and was passed into the engine+ # creator closure above. Drop the traceback to avoid leaking+ # frame locals under ``diagnose=True`` and redact the+ # password from str(e) defensively.+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(e), password)+ logger.warning(f"Database migration failed for {username} during creation"- " — removing partial DB"+ f" — removing partial DB: {safe_msg}")engine.dispose()if db_path.exists():@@ -638,9 +686,17 @@ class DatabaseManager:logger.error(f"Pre-migration backup failed: {result.error}")- except Exception:- logger.exception(- "Pre-migration backup failed — proceeding with migration"+ except Exception as e:+ # ``password`` is in scope and was passed into+ # ``BackupService`` above. Drop traceback + redact+ # so the upstream error (which can carry the+ # password through ``set_sqlcipher_key`` frames) is+ # not written to log sinks.+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Pre-migration backup failed — proceeding with migration: {safe_msg}")# Init failures need to be distinguishable from credential@@ -653,9 +709,17 @@ class DatabaseManager:initialize_database(engine)except Exception as init_err:elapsed_ms = (time.perf_counter() - open_start) * 1000- logger.exception(+ # ``password`` is in scope (function parameter). The+ # initializer ran on an engine whose creator captured+ # ``hex_key`` derived from the password — a SQLAlchemy+ # ``OperationalError`` traceback could surface frames+ # holding the plaintext password under ``diagnose=True``.+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(init_err), password)+ logger.warning(f"Database migration failed for {username} "- f"after {elapsed_ms:.0f}ms — refusing login"+ f"after {elapsed_ms:.0f}ms — refusing login: {safe_msg}")engine.dispose()raise DatabaseInitializationError(@@ -683,11 +747,19 @@ class DatabaseManager:# Already logged + engine disposed at the raise site. Re-raise# past the catch-all below so callers see the typed error.raise- except Exception:+ except Exception as e:elapsed_ms = (time.perf_counter() - open_start) * 1000- logger.exception(+ # Catches connection errors during ``engine.connect()`` —+ # the connection creator passes the password into+ # ``set_sqlcipher_key`` so a failure there can carry the+ # password in the traceback's frame locals. Drop the+ # traceback and redact str(e).+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(e), password)+ logger.warning(f"Failed to open database for user {username} "- f"after {elapsed_ms:.0f}ms"+ f"after {elapsed_ms:.0f}ms: {safe_msg}")engine.dispose()return None@@ -780,8 +852,21 @@ class DatabaseManager:return True- except Exception:- logger.exception(f"Integrity check error for user: {username}")+ except Exception as e:+ # No ``password`` parameter on this method and the engine+ # was created via a hex-key closure (the plaintext password+ # is not retained on the engine itself), so the traceback+ # frames in this method do not hold a credential. Still+ # drop the traceback to stay uniform with the rest of the+ # #4182 sweep and to guard against a caller frame that+ # happens to hold one (e.g., a route handler that+ # retrieved the password from the session store).+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(e), None)+ logger.warning(+ f"Integrity check error for user: {username}: {safe_msg}"+ )return Falsedef change_password(@@ -821,8 +906,18 @@ class DatabaseManager:logger.info(f"Password changed for user {username}")return True- except Exception:- logger.exception(f"Failed to change password for user: {username}")+ except Exception as e:+ # Both ``old_password`` and ``new_password`` are in lexical+ # scope. ``open_user_database`` / ``set_sqlcipher_rekey``+ # carry these into nested frames — a traceback rendered+ # with ``diagnose=True`` would leak them. Redact both and+ # drop the traceback chain.+ from ..security.log_sanitizer import redact_secrets++ safe_msg = redact_secrets(str(e), old_password, new_password)+ logger.warning(+ f"Failed to change password for user: {username}: {safe_msg}"+ )return Falsefinally:# Close the connectiondiff --git a/src/local_deep_research/scheduler/background.py b/src/local_deep_research/scheduler/background.pyindex 6c2143d635..164330cd39 100644--- a/src/local_deep_research/scheduler/background.py+++ b/src/local_deep_research/scheduler/background.py@@ -21,6 +21,7 @@ from ..constants import ResearchStatusfrom ..database.credential_store_base import CredentialStoreBasefrom ..database.session_context import safe_rollbackfrom ..database.thread_local_session import thread_cleanup+from ..security.log_sanitizer import redact_secrets# RAG indexing importsfrom ..research_library.services.library_rag_service import LibraryRAGService@@ -282,9 +283,14 @@ class BackgroundJobScheduler:return settings- except Exception:- logger.exception(- f"[SETTINGS_CACHE] Error fetching settings for {username}"+ except Exception as e:+ # ``password`` (retrieved above) is in the frame locals of+ # this handler and of ``get_user_db_session``; a traceback+ # rendered with diagnose=True would leak it. Drop the+ # traceback chain and redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[SETTINGS_CACHE] Error fetching settings for {username}: {safe_msg}")return DocumentSchedulerSettings.defaults()@@ -474,6 +480,9 @@ class BackgroundJobScheduler:def _schedule_user_subscriptions(self, username: str):"""Schedule all active subscriptions for a user."""logger.info(f"_schedule_user_subscriptions called for {username}")+ # Pre-declared so the leak-redaction in the except handler is safe+ # if the exception fires before ``password`` is assigned below.+ password = Nonetry:session_info = self.user_sessions.get(username)if not session_info:@@ -582,8 +591,18 @@ class BackgroundJobScheduler:session_info["scheduled_jobs"].add(job_id)logger.info(f"Scheduled job {job_id} with {trigger} trigger")- except Exception:- logger.exception(f"Error scheduling subscriptions for {username}")+ except Exception as e:+ # ``password`` was retrieved from the credential store+ # above (line ~483) and passed into ``get_user_db_session``.+ # An exception from the DB session (e.g. SQLCipher+ # ``OperationalError``) can carry frame locals that include+ # the plaintext SQLCipher master password — which is+ # unrecoverable (TRUST.md §5). Drop the traceback chain and+ # redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error scheduling subscriptions for {username}: {safe_msg}"+ )# Add document processing for this userself._schedule_document_processing(username)@@ -677,9 +696,19 @@ class BackgroundJobScheduler:f"[DOC_SCHEDULER] Failed to verify job {job_id} exists!")- except Exception:- logger.exception(- f"Error scheduling document processing for {username}"+ except Exception as e:+ # No ``password`` local here, but the caller frame+ # (``_schedule_user_subscriptions``) holds the SQLCipher+ # master password — loguru ``diagnose=True`` walks the+ # frame stack and would render that caller-frame local.+ # Drop the traceback by using ``logger.warning`` without+ # ``exc_info``. ``redact_secrets`` with ``None`` is a no-op+ # here, but kept for the check-sensitive-logging pre-commit+ # hook + as a guide-post pairing for future refactors that+ # might bring a password into scope.+ safe_msg = redact_secrets(str(e), None)+ logger.warning(+ f"Error scheduling document processing for {username}: {safe_msg}")def _arm_egress_backstop(self, settings_manager, username: str) -> None:@@ -712,6 +741,9 @@ class BackgroundJobScheduler:logger.info(f"[DOC_SCHEDULER] Processing documents for user {username}")start_time = datetime.now(UTC)+ # Pre-declared so the except handlers can pass it to redact_secrets+ # even if the retrieve() call below itself raises.+ password = Nonetry:session_info = self.user_sessions.get(username)if not session_info:@@ -865,10 +897,15 @@ class BackgroundJobScheduler:user_settings_snapshot = (settings_manager.get_settings_snapshot())- except Exception:- logger.exception(- "[DOC_SCHEDULER] Could not build settings snapshot — "- "downloads will not be scope-gated"+ except Exception as e:+ # ``password`` is live in this frame (it opened the+ # surrounding ``get_user_db_session``). Drop traceback+ # + redact str(e) to avoid leaking the SQLCipher+ # master password.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[DOC_SCHEDULER] Could not build settings snapshot: "+ f"{safe_msg} — downloads will not be scope-gated")user_settings_snapshot = None@@ -916,15 +953,21 @@ class BackgroundJobScheduler:logger.info(f"[DOC_SCHEDULER] Queued {queued_count} PDF downloads for research {research.id}")- except Exception:+ except Exception as e:# Recover the shared thread-local session# before continuing — without rollback the# next phase (text extract / RAG) and the# post-loop last_run commit run on a# poisoned session (issue #3827).safe_rollback(db, "DOC_SCHEDULER PDF download")- logger.exception(- f"[DOC_SCHEDULER] Failed to download PDFs for research {research.id}"+ # ``password`` is in scope and was passed+ # into ``DownloadService``. Drop traceback+ # + redact str(e) to avoid leaking the+ # SQLCipher master password under+ # ``diagnose=True``.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[DOC_SCHEDULER] Failed to download PDFs for research {research.id}: {safe_msg}")if settings.extract_text:@@ -989,18 +1032,32 @@ class BackgroundJobScheduler:db,"DOC_SCHEDULER resource",)- logger.exception(- f"[DOC_SCHEDULER] Error processing resource {resource.id}: {resource_error}"+ # ``password`` is in scope and+ # was passed into the enclosing+ # ``DownloadService``. Drop the+ # traceback chain + redact str(e)+ # to avoid leaking the SQLCipher+ # master password.+ safe_msg = redact_secrets(+ str(resource_error), password+ )+ logger.warning(+ f"[DOC_SCHEDULER] Error processing resource {resource.id}: {safe_msg}")logger.info(f"[DOC_SCHEDULER] Text extraction completed for research {research.id}: {processed_count}/{len(resources)} resources processed")- except Exception:+ except Exception as e:safe_rollback(db, "DOC_SCHEDULER text extraction")- logger.exception(- f"[DOC_SCHEDULER] Failed to extract text for research {research.id}"+ # ``password`` is in scope from the outer+ # ``_process_user_documents`` retrieval —+ # same redact + warning pattern as the+ # inner handlers in this function.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[DOC_SCHEDULER] Failed to extract text for research {research.id}: {safe_msg}")if settings.generate_rag:@@ -1102,18 +1159,31 @@ class BackgroundJobScheduler:f"with {result.get('chunk_count', 0)} chunks")except Exception as doc_error:- logger.exception(- f"[DOC_SCHEDULER] Failed to index document {doc_id}: {doc_error}"+ # ``password`` is in scope+ # and was passed into+ # ``LibraryRAGService`` and+ # ``get_default_library_id``+ # above. Drop traceback ++ # redact str(e).+ safe_msg = redact_secrets(+ str(doc_error), password+ )+ logger.warning(+ f"[DOC_SCHEDULER] Failed to index document {doc_id}: {safe_msg}")logger.info(f"[DOC_SCHEDULER] RAG indexing completed for research {research.id}: "f"{indexed_count}/{len(documents_to_index)} documents indexed")- except Exception:+ except Exception as e:safe_rollback(db, "DOC_SCHEDULER RAG")- logger.exception(- f"[DOC_SCHEDULER] Failed to generate RAG embeddings for research {research.id}"+ # Same scope as the per-document RAG+ # handler above — ``password`` is in+ # scope via the outer function.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[DOC_SCHEDULER] Failed to generate RAG embeddings for research {research.id}: {safe_msg}")processed_count += 1@@ -1121,10 +1191,14 @@ class BackgroundJobScheduler:f"[DOC_SCHEDULER] Successfully queued processing for research {research.id}")- except Exception:+ except Exception as e:safe_rollback(db, "DOC_SCHEDULER research")- logger.exception(- f"[DOC_SCHEDULER] Error processing research {research.id} for user {username}"+ # ``password`` is in scope from the outer+ # ``_process_user_documents`` retrieval. Drop the+ # traceback chain and redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[DOC_SCHEDULER] Error processing research {research.id} for user {username}: {safe_msg}")# Update last run time in user's settings.@@ -1147,9 +1221,14 @@ class BackgroundJobScheduler:f"[DOC_SCHEDULER] Completed document processing for user {username}: {processed_count} sessions processed in {duration:.2f}s")- except Exception:- logger.exception(- f"[DOC_SCHEDULER] Error processing documents for user {username}"+ except Exception as e:+ # ``password`` is pre-declared as ``None`` at the top of the+ # function, so it is always bound here even if the retrieve()+ # call itself raised. ``redact_secrets`` silently skips a+ # ``None`` secret. Drop the traceback chain.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"[DOC_SCHEDULER] Error processing documents for user {username}: {safe_msg}")def get_document_scheduler_status(self, username: str) -> Dict[str, Any]:@@ -1183,8 +1262,14 @@ class BackgroundJobScheduler:}except Exception as e:- logger.exception(- f"Error getting document scheduler status for user {username}"+ # No ``password`` local in this method, but caller frames+ # (e.g. a route handler that already retrieved the user's+ # password) could be rendered under loguru ``diagnose=True``.+ # Drop the traceback by using ``logger.warning`` without+ # ``exc_info``.+ safe_msg = redact_secrets(str(e), None)+ logger.warning(+ f"Error getting document scheduler status for user {username}: {safe_msg}")return {"enabled": False,@@ -1241,15 +1326,22 @@ class BackgroundJobScheduler:return True- except Exception:- logger.exception(- f"[DOC_SCHEDULER] Error triggering document processing for user {username}"+ except Exception as e:+ # No ``password`` local in this method, but caller frames+ # could hold one — drop the traceback to avoid frame-local+ # rendering under ``diagnose=True``.+ safe_msg = redact_secrets(str(e), None)+ logger.warning(+ f"[DOC_SCHEDULER] Error triggering document processing for user {username}: {safe_msg}")return False@thread_cleanupdef _check_user_overdue_subscriptions(self, username: str):"""Check and immediately run any overdue subscriptions for a user."""+ # Pre-declared so the except handler can pass it to redact_secrets+ # even if the retrieve() call below itself raises.+ password = Nonetry:session_info = self.user_sessions.get(username)if not session_info:@@ -1303,9 +1395,13 @@ class BackgroundJobScheduler:f"Scheduled overdue subscription {sub.id} to run in {delay_seconds} seconds")- except Exception:- logger.exception(- f"Error checking overdue subscriptions for {username}"+ except Exception as e:+ # ``password`` was retrieved above and passed into+ # ``get_user_db_session``. Drop traceback + redact str(e)+ # to avoid leaking the SQLCipher master password.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error checking overdue subscriptions for {username}: {safe_msg}")@thread_cleanup@@ -1314,6 +1410,9 @@ class BackgroundJobScheduler:logger.info(f"_check_subscription called for user {username}, subscription {subscription_id}")+ # Pre-declared so the except handler can pass it to redact_secrets+ # even if the retrieve() call below itself raises.+ password = Nonetry:session_info = self.user_sessions.get(username)if not session_info:@@ -1404,8 +1503,14 @@ class BackgroundJobScheduler:replace_existing=True,)- except Exception:- logger.exception(f"Error checking subscription {subscription_id}")+ except Exception as e:+ # ``password`` was retrieved above and passed into+ # ``get_user_db_session``. Drop traceback + redact str(e)+ # to avoid leaking the SQLCipher master password.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error checking subscription {subscription_id}: {safe_msg}"+ )@thread_cleanupdef _trigger_subscription_research_sync(@@ -1414,6 +1519,9 @@ class BackgroundJobScheduler:"""Trigger research for a subscription using programmatic API."""from ..config.thread_settings import set_settings_context+ # Pre-declared so the except handler can pass it to redact_secrets+ # even if the retrieve() call below itself raises.+ password = Nonetry:# Get user's password from session infosession_info = self.user_sessions.get(username)@@ -1530,9 +1638,17 @@ class BackgroundJobScheduler:subscription,)- except Exception:- logger.exception(- f"Error triggering research for subscription {subscription['id']}"+ except Exception as e:+ # ``password`` was retrieved from the credential store at+ # the top of this function and passed through to+ # ``get_user_db_session``, ``quick_summary``+ # (``user_password``), and ``_store_research_result``. A+ # SQLAlchemy / requests exception from any of those paths+ # could carry frame locals containing the SQLCipher master+ # password — drop the traceback chain and redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error triggering research for subscription {subscription['id']}: {safe_msg}")def _store_research_result(@@ -1709,12 +1825,17 @@ class BackgroundJobScheduler:sources=sources,username=username,)- except Exception:- logger.exception(+ except Exception as e:+ # ``password`` is a parameter of this method —+ # don't render a traceback that could expose it+ # via diagnose=True frame locals.+ safe_msg = redact_secrets(str(e), password)+ logger.warning("Failed to persist scheduler sources for ""research {} — assembler will render no Sources "- "block for this row.",+ "block for this row: {}",research_id,+ safe_msg,)# Store the report content using storage abstraction@@ -1732,8 +1853,12 @@ class BackgroundJobScheduler:f"Stored research result {research_id} for subscription {subscription_id}")- except Exception:- logger.exception("Error storing research result")+ except Exception as e:+ # ``password`` is a function parameter, so it is always in+ # this frame. Drop traceback + redact str(e) to avoid leaking+ # the SQLCipher master password.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(f"Error storing research result: {safe_msg}")def _run_cleanup_with_tracking(self):"""Wrapper that tracks cleanup execution."""diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.pyindex 5323bd5709..54691f9853 100644--- a/src/local_deep_research/web/queue/processor_v2.py+++ b/src/local_deep_research/web/queue/processor_v2.py@@ -21,6 +21,7 @@ from ...database.queue_service import UserQueueServicefrom ...database.session_context import get_user_db_sessionfrom ...database.session_passwords import session_password_storefrom ...exceptions import DuplicateResearchError, SystemAtCapacityError+from ...security.log_sanitizer import redact_secretsfrom ...notifications.queue_helpers import (send_research_completed_notification_from_session,send_research_failed_notification_from_session,@@ -257,6 +258,9 @@ class QueueProcessorV2:research_id: The research ID**kwargs: Additional parameters for direct execution (query, mode, etc.)"""+ # Pre-declared so the except handlers below can pass it to+ # redact_secrets even on paths where it is never assigned.+ password = None# Check user's queue_mode setting when we have database accessif kwargs:session_id = kwargs.get("session_id")@@ -337,9 +341,15 @@ class QueueProcessorV2:f"User {username} has queue_mode={queue_mode}, "f"queueing research {research_id}")- except Exception:- logger.exception(- f"Error in direct execution for {username}"+ except Exception as e:+ # ``password`` is in scope — drop the traceback+ # chain and redact str(e) so the SQLCipher master+ # password can't leak via diagnose=True frame+ # locals (see the generic handler in+ # _start_research_directly for the full rationale).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error in direct execution for {username}: {safe_msg}")# Fall back to queue mode (or if direct mode failed)@@ -354,8 +364,12 @@ class QueueProcessorV2:logger.info(f"Research {research_id} queued for user {username}")- except Exception:- logger.exception(f"Failed to update queue status for {username}")+ except Exception as e:+ # ``password`` may be bound above — same redaction rationale.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Failed to update queue status for {username}: {safe_msg}"+ )def _start_research_directly(self, username: str, research_id: str, password: str, **kwargs@@ -389,9 +403,13 @@ class QueueProcessorV2:# Update task status if it existsqueue_service = UserQueueService(db_session)queue_service.update_task_status(research_id, "processing")- except Exception:- logger.exception(- f"Failed to create active research record for {research_id}"+ except Exception as e:+ # ``password`` is a parameter of this method — drop the+ # traceback chain and redact str(e) (full rationale at the+ # generic handler below).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Failed to create active research record for {research_id}: {safe_msg}")return@@ -433,9 +451,11 @@ class QueueProcessorV2:if active_record:active_record.thread_id = str(research_thread.ident)db_session.commit()- except Exception:- logger.exception(- f"Failed to update thread ID for {research_id}"+ except Exception as e:+ # ``password`` is in scope — same redaction rationale.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Failed to update thread ID for {research_id}: {safe_msg}")logger.info(@@ -493,15 +513,27 @@ class QueueProcessorV2:priority=0,)db_session.commit()- except Exception:- logger.exception(+ except Exception as e:+ # ``password`` is in scope — same redaction rationale.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(f"Cleanup after capacity reject failed for "f"{research_id}; the stale UserActiveResearch row is "- f"recovered by reclaim_stale_user_active_research"+ f"recovered by reclaim_stale_user_active_research: {safe_msg}")return- except Exception:- logger.exception(f"Failed to start research {research_id} directly")+ except Exception as e:+ # ``password`` is in lexical scope (function parameter,+ # passed through to ``start_research_process``). A+ # SQLAlchemy / requests exception from anywhere in+ # ``start_research_process`` could carry frame locals that+ # include the SQLCipher master password (which is+ # unrecoverable — see TRUST.md §5). Drop the traceback chain+ # and redact str(e) defensively.+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Failed to start research {research_id} directly: {safe_msg}"+ )# Clean up the active record AND mark the research terminal# FAILED so the user-visible state matches reality (no running# thread, not IN_PROGRESS). Same contract as the queue@@ -523,9 +555,11 @@ class QueueProcessorV2:if research_row:research_row.status = ResearchStatus.FAILEDdb_session.commit()- except Exception:- logger.exception(- f"Failed to clean up active research record for {research_id}"+ except Exception as e2:+ # ``password`` is in scope — same redaction rationale.+ safe_msg = redact_secrets(str(e2), password)+ logger.warning(+ f"Failed to clean up active research record for {research_id}: {safe_msg}")def notify_research_completed(@@ -562,9 +596,13 @@ class QueueProcessorV2:db_session=session,)- except Exception:- logger.exception(- f"Failed to update completion status for {username}"+ except Exception as e:+ # ``user_password`` is a parameter of this method — drop the+ # traceback chain and redact str(e) so the SQLCipher master+ # password can't leak via diagnose=True frame locals.+ safe_msg = redact_secrets(str(e), user_password)+ logger.warning(+ f"Failed to update completion status for {username}: {safe_msg}")# Auto-convert research to document in History collection.@@ -619,8 +657,13 @@ class QueueProcessorV2:db_session=session,)- except Exception:- logger.exception(f"Failed to update failure status for {username}")+ except Exception as e:+ # ``user_password`` is a parameter of this method — same+ # redaction rationale as notify_research_completed.+ safe_msg = redact_secrets(str(e), user_password)+ logger.warning(+ f"Failed to update failure status for {username}: {safe_msg}"+ )def _process_queue_loop(self):"""Main loop that processes the queue."""@@ -770,8 +813,13 @@ class QueueProcessorV2:}return bool(updated_status["queued_tasks"] == 0)- except Exception:- logger.exception(f"Error processing queue for user {username}")+ except Exception as e:+ # ``password`` (from the session store above) is in scope —+ # drop the traceback chain and redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error processing queue for user {username}: {safe_msg}"+ )return False # Keep checking - errors might be temporarydef _reclaim_stranded_queue_rows(self, db_session, username: str) -> int:@@ -958,18 +1006,24 @@ class QueueProcessorV2:)continue- except Exception:- logger.exception(- f"Error starting queued research {research_id}"+ except Exception as e:+ # ``password`` is a parameter of this method — drop the+ # traceback chain and redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error starting queued research {research_id}: {safe_msg}")# Session may be in PendingRollbackError state after a# failed commit inside _start_research.try:db_session.rollback()- except Exception:+ except Exception as rb_err:+ # No exc_info: ``password`` is in this frame and a+ # rendered traceback could expose it via+ # diagnose=True frame locals.logger.debug(- "Rollback after start failure",- exc_info=True,+ "Rollback after start failure: "+ f"{redact_secrets(str(rb_err), password)}")attempts = self._bump_spawn_retry_count(research_id)@@ -1001,10 +1055,8 @@ class QueueProcessorV2:# Exhausted retries — mark terminal FAILED, delete the# queue row to stop re-dispatch, and notify the user.- # Use logger.warning (not logger.exception) because the- # spawn traceback was already logged at the top of this- # except block; re-logging with exc_info emits a second- # full traceback.+ # The spawn failure was already logged (redacted) at the+ # top of this except block; no need to repeat it here.logger.warning(f"Spawn failed for research {research_id} "f"after {attempts} attempts; marking FAILED"@@ -1022,17 +1074,21 @@ class QueueProcessorV2:if fresh_queued:db_session.delete(fresh_queued)db_session.commit()- except Exception:- logger.exception(+ except Exception as e2:+ # ``password`` is in scope — same redaction rationale.+ safe_msg = redact_secrets(str(e2), password)+ logger.warning("Failed to persist terminal FAILED state for "- f"research {research_id}"+ f"research {research_id}: {safe_msg}")try:db_session.rollback()- except Exception:+ except Exception as rb_err:+ # No exc_info: same frame-locals rationale as the+ # rollback handler above.logger.debug(- "Rollback after terminal update failure",- exc_info=True,+ "Rollback after terminal update failure: "+ f"{redact_secrets(str(rb_err), password)}")# notify_research_failed opens its own session and@@ -1205,6 +1261,9 @@ class QueueProcessorV2:Returns:Number of researches started"""+ # Pre-declared so the except handler can pass it to redact_secrets+ # even if the exception is raised before the retrieve below.+ password = Nonetry:# Add user to check listself.notify_user_activity(username, session_id)@@ -1231,8 +1290,13 @@ class QueueProcessorV2:return 0- except Exception:- logger.exception(f"Error in process_user_request for {username}")+ except Exception as e:+ # ``password`` may be bound above — drop the traceback chain+ # and redact str(e).+ safe_msg = redact_secrets(str(e), password)+ logger.warning(+ f"Error in process_user_request for {username}: {safe_msg}"+ )return 0def queue_progress_update(diff --git a/tests/security/test_password_leakage.py b/tests/security/test_password_leakage.pynew file mode 100644index 0000000000..7925f486fb--- /dev/null+++ b/tests/security/test_password_leakage.py@@ -0,0 +1,751 @@+"""Tests that ``logger.exception`` sites in ``database/encrypted_db.py``,+``web/queue/processor_v2.py``, and ``scheduler/background.py`` never leak+the SQLCipher master password into log output.++Companion fix: issue #4182 (follow-up to #4131). API keys are rotatable,+but SQLCipher master passwords are not (TRUST.md §5) — a leaked password+in a CI log or shared error report forces the user to abandon their+encrypted DB.++The fix pattern, established in PRs #4168/#4175/#4181, is:++ except Exception as e:+ safe_msg = redact_secrets(str(e), password)+ logger.warning(f"... {safe_msg}")++These tests use ``loguru_caplog_full`` to capture the rendered exception+block (the existing ``loguru_caplog`` would false-pass on a leak that+lives only in the traceback frames). Each test is mutation-verified —+revert the production wrap and the test fails with the sentinel visible.+"""++import base64+from unittest.mock import MagicMock, patch+from urllib.parse import quote, quote_plus++import pytest+++_LEAKED_PASSWORD = "ldr-master-pw-SHOULD-NEVER-LEAK-987654321"+_LEAKED_OLD_PASSWORD = "ldr-old-master-pw-ALSO-NEVER-LEAK-123"+++def _all_encodings_of(secret: str) -> list:+ """Return every encoding the sentinel might appear under in a log.++ Mirrors the helper in ``test_api_key_leakage.py``. Extend when a+ fixed call site introduces a new transformation (e.g., bcrypt hash,+ JWT payload).+ """+ return [+ secret,+ quote(secret, safe=""),+ quote_plus(secret),+ repr(secret)[1:-1],+ base64.b64encode(secret.encode()).decode(),+ secret[:8],+ ]+++def _assert_no_leak(text: str, secret: str, where: str) -> None:+ """Helper: assert no encoding of *secret* appears in *text*."""+ for encoding in _all_encodings_of(secret):+ assert encoding not in text, (+ f"password leaked at {where} as encoding {encoding!r}. The "+ f"except handler must call redact_secrets(str(e), password) "+ f"and use logger.warning (not logger.exception)."+ )+++# ---------------------------------------------------------------------------+# database/encrypted_db.py+# ---------------------------------------------------------------------------+++class TestEncryptedDBPasswordLeakage:+ """``DatabaseManager`` lifecycle methods receive the user's SQLCipher+ master password and pass it through to ``set_sqlcipher_key`` /+ ``create_sqlcipher_connection`` / ``BackupService``. An exception in+ any of those paths can carry frame locals containing the plaintext+ password under loguru ``diagnose=True``. The except handlers must+ drop the traceback chain and redact str(e).+ """++ def test_change_password_does_not_leak_either_password(+ self, loguru_caplog_full, tmp_path+ ):+ """``change_password`` has BOTH ``old_password`` and+ ``new_password`` in lexical scope. A failure in+ ``open_user_database`` or ``set_sqlcipher_rekey`` whose exception+ message embeds either password must not surface to logs.+ """+ from local_deep_research.database.encrypted_db import DatabaseManager++ manager = DatabaseManager.__new__(DatabaseManager)+ manager.has_encryption = True+ manager.connections = {}+ manager._connections_lock = MagicMock()+ manager._connections_lock.__enter__ = lambda *a: None+ manager._connections_lock.__exit__ = lambda *a: None++ # Drive _get_user_db_path -> a real, existing path so the+ # ``if not db_path.exists()`` short-circuit doesn't fire.+ fake_db = tmp_path / "fake.db"+ fake_db.touch()+ manager._get_user_db_path = lambda u: fake_db+ manager.close_user_database = lambda u: None++ # Construct an exception whose str() embeds BOTH passwords —+ # simulates a SQLAlchemy OperationalError that carries frame+ # locals from set_sqlcipher_rekey's traceback.+ exc = RuntimeError(+ f"sqlcipher rekey failed: old={_LEAKED_OLD_PASSWORD} "+ f"new={_LEAKED_PASSWORD} target={fake_db}"+ )+ manager.open_user_database = MagicMock(side_effect=exc)++ with loguru_caplog_full.at_level("DEBUG"):+ result = manager.change_password(+ "alice", _LEAKED_OLD_PASSWORD, _LEAKED_PASSWORD+ )++ assert result is False+ _assert_no_leak(+ loguru_caplog_full.text, _LEAKED_PASSWORD, "change_password new_pw"+ )+ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_OLD_PASSWORD,+ "change_password old_pw",+ )+ assert "Failed to change password" in loguru_caplog_full.text, (+ "test did not exercise the except branch — check the fixtures"+ )++ def test_open_user_database_outer_catch_does_not_leak(+ self, loguru_caplog_full, tmp_path+ ):+ """The outer ``except Exception`` in ``open_user_database`` (was+ line 688 in the issue's listing) fires when the engine fails to+ open. ``password`` is in scope and was passed into the engine+ creator closure via ``set_sqlcipher_key`` — an OperationalError+ traceback could leak it.+ """+ from local_deep_research.database.encrypted_db import DatabaseManager++ manager = DatabaseManager.__new__(DatabaseManager)+ # has_encryption=True drives the SQLCipher-engine branch, which+ # uses a custom creator with ``sqlite://`` (no event.listen).+ manager.has_encryption = True+ manager.connections = {}+ manager._connections_lock = MagicMock()+ manager._connections_lock.__enter__ = lambda *a: None+ manager._connections_lock.__exit__ = lambda *a: None+ manager._pool_class = type("StubPool", (), {})+ manager._get_pool_kwargs = lambda: {}+ db_path = tmp_path / "alice.db"+ db_path.touch()+ manager._get_user_db_path = lambda u: db_path++ # Drive an exception from the SELECT 1 test connect that+ # follows engine creation.+ exc = RuntimeError(+ f"connect failed: dsn=sqlite:///x?key={_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.encrypted_db.create_engine"+ ) as mock_create:+ with patch(+ "local_deep_research.database.encrypted_db.has_per_database_salt",+ return_value=True,+ ):+ mock_engine = MagicMock()+ mock_engine.connect.side_effect = exc+ mock_create.return_value = mock_engine++ result = manager.open_user_database(+ "alice", _LEAKED_PASSWORD+ )++ assert result is None+ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "open_user_database outer catch",+ )+ assert "Failed to open database" in loguru_caplog_full.text+++# ---------------------------------------------------------------------------+# web/queue/processor_v2.py+# ---------------------------------------------------------------------------+++class TestProcessorV2PasswordLeakage:+ """``QueueProcessorV2._start_research_directly`` receives the user's+ password and forwards it to ``start_research_process``. A failure in+ the research startup path (or in the active-record DB write) whose+ exception embeds the password must not reach logs.+ """++ def test_start_research_directly_does_not_leak_on_research_failure(+ self, loguru_caplog_full+ ):+ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)++ exc = RuntimeError(+ f"could not start research thread, "+ f"connection url=sqlite:///x.db?pwd={_LEAKED_PASSWORD}"+ )++ # The initial active-record create + status update succeeds, and+ # then start_research_process raises with the password in the+ # exception message.+ fake_session = MagicMock()+ fake_session.__enter__ = lambda s: s+ fake_session.__exit__ = lambda s, *a: None+ fake_session.query.return_value.filter_by.return_value.first.return_value = None++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.web.queue.processor_v2.get_user_db_session",+ return_value=fake_session,+ ):+ with patch(+ "local_deep_research.web.queue.processor_v2.UserQueueService"+ ):+ with patch(+ "local_deep_research.web.queue.processor_v2.start_research_process",+ side_effect=exc,+ ):+ processor._start_research_directly(+ username="alice",+ research_id="r1",+ password=_LEAKED_PASSWORD,+ query="test",+ mode="quick",+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_start_research_directly",+ )+ # Sanity: the failure branch ran.+ assert (+ "Failed to start research" in loguru_caplog_full.text+ or "Failed to create active research record"+ in loguru_caplog_full.text+ )++ def test_notify_research_queued_does_not_leak(self, loguru_caplog_full):+ """``notify_research_queued`` retrieves the session password and+ opens the user DB in its direct-execution branch, then falls back+ to a queue-status update. Both except handlers have ``password``+ in scope; an exception from either path must not leak it.+ """+ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)++ exc = RuntimeError(f"open failed: key={_LEAKED_PASSWORD} db=alice.db")++ with loguru_caplog_full.at_level("DEBUG"):+ with (+ patch(+ "local_deep_research.web.queue.processor_v2.session_password_store"+ ) as mock_store,+ patch(+ "local_deep_research.web.queue.processor_v2.db_manager"+ ) as mock_db,+ patch(+ "local_deep_research.web.queue.processor_v2.get_user_db_session",+ side_effect=exc,+ ),+ ):+ mock_store.get_session_password.return_value = _LEAKED_PASSWORD+ mock_db.open_user_database.side_effect = exc++ processor.notify_research_queued(+ "alice", "r1", session_id="s1", query="q", mode="quick"+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "notify_research_queued",+ )+ # Sanity: both except branches ran (direct-execution error, then+ # the queue-status fallback error).+ assert "Error in direct execution" in loguru_caplog_full.text+ assert "Failed to update queue status" in loguru_caplog_full.text++ def test_process_user_queue_does_not_leak(self, loguru_caplog_full):+ """``_process_user_queue`` retrieves the session password before+ its try block and passes it into ``open_user_database`` /+ ``get_user_db_session``. The catch-all must redact it.+ """+ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)++ exc = RuntimeError(f"sqlcipher key rejected: {_LEAKED_PASSWORD}")++ with loguru_caplog_full.at_level("DEBUG"):+ with (+ patch(+ "local_deep_research.web.queue.processor_v2.session_password_store"+ ) as mock_store,+ patch(+ "local_deep_research.web.queue.processor_v2.db_manager"+ ) as mock_db,+ ):+ mock_store.get_session_password.return_value = _LEAKED_PASSWORD+ mock_db.open_user_database.side_effect = exc++ result = processor._process_user_queue("alice", "s1")++ assert result is False+ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_process_user_queue",+ )+ assert "Error processing queue for user" in loguru_caplog_full.text++ def test_notify_research_completed_does_not_leak(self, loguru_caplog_full):+ """``notify_research_completed`` has ``user_password`` as a+ parameter and passes it into ``get_user_db_session``.+ """+ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)++ exc = RuntimeError(+ f"session open failed: dsn=sqlite:///x?key={_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with (+ patch(+ "local_deep_research.web.queue.processor_v2.get_user_db_session",+ side_effect=exc,+ ),+ patch(+ "local_deep_research.research_library.search.services.research_history_indexer.auto_convert_research"+ ),+ ):+ processor.notify_research_completed(+ "alice", "r1", user_password=_LEAKED_PASSWORD+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "notify_research_completed",+ )+ assert "Failed to update completion status" in loguru_caplog_full.text++ def test_notify_research_failed_does_not_leak(self, loguru_caplog_full):+ """``notify_research_failed`` — same contract as the completed+ notification.+ """+ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)++ exc = RuntimeError(+ f"session open failed: dsn=sqlite:///x?key={_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.web.queue.processor_v2.get_user_db_session",+ side_effect=exc,+ ):+ processor.notify_research_failed(+ "alice",+ "r1",+ error_message="boom",+ user_password=_LEAKED_PASSWORD,+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "notify_research_failed",+ )+ assert "Failed to update failure status" in loguru_caplog_full.text++ def test_start_queued_researches_spawn_failure_does_not_leak(+ self, loguru_caplog_full+ ):+ """The spawn-failure handler in ``_start_queued_researches`` has+ ``password`` as a parameter. A spawn exception whose message+ embeds it must be redacted — and the rollback-failure debug log+ in the same handler must not re-render the traceback.+ """+ import threading++ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)+ processor._spawn_retry_counts = {}+ processor._spawn_retry_counts_lock = threading.Lock()+ processor._reclaim_stranded_queue_rows = lambda *a: None++ exc = RuntimeError(+ f"spawn failed: thread env carried pwd={_LEAKED_PASSWORD}"+ )+ processor._start_research = MagicMock(side_effect=exc)++ queued_item = MagicMock()+ queued_item.research_id = "r1"++ db_session = MagicMock()+ query_chain = db_session.query.return_value.filter_by.return_value+ query_chain.order_by.return_value.limit.return_value.all.return_value = [+ queued_item+ ]+ query_chain.update.return_value = 1 # claim succeeds+ # Rollback after the spawn failure raises too, with the password+ # embedded — exercises the redacted debug path.+ db_session.rollback.side_effect = RuntimeError(+ f"rollback failed: {_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ processor._start_queued_researches(+ db_session,+ MagicMock(), # queue_service+ "alice",+ _LEAKED_PASSWORD,+ available_slots=1,+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_start_queued_researches spawn failure",+ )+ assert "Error starting queued research" in loguru_caplog_full.text++ def test_process_user_request_does_not_leak(self, loguru_caplog_full):+ """``process_user_request`` retrieves the session password inside+ its try block (hence the ``password = None`` pre-declaration) and+ opens the user DB. The catch-all must redact it.+ """+ import threading++ from local_deep_research.web.queue.processor_v2 import (+ QueueProcessorV2,+ )++ processor = QueueProcessorV2.__new__(QueueProcessorV2)+ processor._users_to_check = set()+ processor._users_lock = threading.Lock()++ exc = RuntimeError(f"open failed, key in dsn: {_LEAKED_PASSWORD}")++ with loguru_caplog_full.at_level("DEBUG"):+ with (+ patch(+ "local_deep_research.web.queue.processor_v2.session_password_store"+ ) as mock_store,+ patch(+ "local_deep_research.web.queue.processor_v2.db_manager"+ ) as mock_db,+ ):+ mock_store.get_session_password.return_value = _LEAKED_PASSWORD+ mock_db.open_user_database.side_effect = exc++ result = processor.process_user_request("alice", "s1")++ assert result == 0+ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "process_user_request",+ )+ assert "Error in process_user_request" in loguru_caplog_full.text+++# ---------------------------------------------------------------------------+# scheduler/background.py+# ---------------------------------------------------------------------------+++class TestSchedulerBackgroundPasswordLeakage:+ """The scheduler retrieves user passwords from its credential store+ and passes them into encrypted-DB session contexts. Every+ ``except Exception`` site in those flows must scrub the password+ from str(e) before logging.+ """++ @pytest.fixture+ def scheduler(self):+ """Build a ``BackgroundJobScheduler`` with a populated credential+ store but no actual APScheduler running — enough for the except+ handlers to be exercised in isolation.+ """+ from local_deep_research.scheduler.background import (+ BackgroundJobScheduler,+ SchedulerCredentialStore,+ )++ sched = BackgroundJobScheduler.__new__(BackgroundJobScheduler)+ sched._credential_store = SchedulerCredentialStore(ttl_hours=1)+ sched._credential_store.store("alice", _LEAKED_PASSWORD)+ sched.user_sessions = {+ "alice": {"last_activity": None, "scheduled_jobs": set()}+ }+ sched.lock = MagicMock()+ sched.lock.__enter__ = lambda *a: None+ sched.lock.__exit__ = lambda *a: None+ sched.scheduler = MagicMock()+ sched.is_running = True+ sched.config = {}+ return sched++ def test_trigger_subscription_research_sync_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """``_trigger_subscription_research_sync`` retrieves the+ password from the credential store and passes it through to+ ``get_user_db_session`` and ``quick_summary``. A SQLAlchemy+ / requests exception from any of those paths must not surface+ the password.+ """+ exc = RuntimeError(f"db session open failed: pwd={_LEAKED_PASSWORD}")++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=exc,+ ):+ scheduler._trigger_subscription_research_sync(+ "alice",+ {"id": 42, "name": "test sub", "query": "test"},+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_trigger_subscription_research_sync",+ )+ assert "Error triggering research" in loguru_caplog_full.text++ def test_schedule_user_subscriptions_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """``_schedule_user_subscriptions`` retrieves the password and+ opens an encrypted DB session. Failure inside that block must+ not leak the password.+ """+ exc = RuntimeError(+ f"NewsSubscription query failed: dsn=sqlite:///x?pwd={_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=exc,+ ):+ # _schedule_user_subscriptions also calls+ # _schedule_document_processing at the end — stub it so+ # we isolate the failure to the subscription branch.+ scheduler._schedule_document_processing = lambda u: None+ scheduler._schedule_user_subscriptions("alice")++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_schedule_user_subscriptions",+ )+ assert "Error scheduling subscriptions" in loguru_caplog_full.text++ def test_check_user_overdue_subscriptions_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """``_check_user_overdue_subscriptions`` retrieves the password+ and opens an encrypted DB session. Same contract.+ """+ exc = RuntimeError(+ f"overdue query failed, pwd={_LEAKED_PASSWORD} in dsn"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=exc,+ ):+ scheduler._check_user_overdue_subscriptions("alice")++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_check_user_overdue_subscriptions",+ )+ assert "Error checking overdue subscriptions" in loguru_caplog_full.text++ def test_check_subscription_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """``_check_subscription`` retrieves the password from the+ credential store and opens an encrypted DB session. A failure+ from that path must not surface the password.+ """+ exc = RuntimeError(+ f"subscription refresh failed: dsn=sqlite:///x?pwd={_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=exc,+ ):+ scheduler._check_subscription("alice", 42)++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_check_subscription",+ )+ assert "Error checking subscription 42" in loguru_caplog_full.text++ def test_get_document_scheduler_settings_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """``_get_document_scheduler_settings`` retrieves the password+ before its try block and passes it into ``get_user_db_session``.+ On failure it must log redacted and fall back to defaults.+ """+ import threading++ scheduler._settings_cache = {}+ scheduler._settings_cache_lock = threading.Lock()++ exc = RuntimeError(+ f"settings fetch failed: key={_LEAKED_PASSWORD} db=alice"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=exc,+ ):+ settings = scheduler._get_document_scheduler_settings("alice")++ # Falls back to defaults rather than raising.+ assert settings is not None+ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_get_document_scheduler_settings",+ )+ assert "Error fetching settings" in loguru_caplog_full.text++ def test_store_research_result_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """``_store_research_result`` receives the password as a+ function parameter and passes it into ``get_user_db_session``.+ A failure anywhere inside the storage block must not surface+ the password.+ """+ exc = RuntimeError(+ f"history insert failed: dsn=sqlite:///x?pwd={_LEAKED_PASSWORD}"+ )++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=exc,+ ):+ scheduler._store_research_result(+ username="alice",+ password=_LEAKED_PASSWORD,+ research_id="r1",+ subscription_id=42,+ result={},+ subscription={},+ )++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_store_research_result",+ )+ assert "Error storing research result" in loguru_caplog_full.text++ def test_process_user_documents_snapshot_failure_does_not_leak(+ self, loguru_caplog_full, scheduler+ ):+ """The settings-snapshot handler inside ``_process_user_documents``+ runs nested inside ``get_user_db_session(username, password)`` —+ the password is live in the frame. A failure building the+ snapshot must not surface it.+ """+ exc = RuntimeError(+ f"snapshot build failed: dsn=sqlite:///x?pwd={_LEAKED_PASSWORD}"+ )++ # Document-scheduler settings: one processing flag enabled so+ # the function reaches the snapshot block instead of returning+ # early; no last_run so the date filter is skipped.+ doc_settings = MagicMock(+ download_pdfs=True,+ extract_text=False,+ generate_rag=False,+ last_run=None,+ )+ scheduler._get_document_scheduler_settings = lambda u: doc_settings+ scheduler._arm_egress_backstop = lambda sm, u: None++ # One fake completed research session so the snapshot block is+ # reached (empty result returns before it).+ fake_research = MagicMock(id="r1", title="t", completed_at=None)+ fake_db = MagicMock()+ fake_db.query.return_value.filter.return_value.order_by.return_value.limit.return_value.all.return_value = [+ fake_research+ ]+ fake_session = MagicMock()+ fake_session.__enter__ = lambda s: fake_db+ fake_session.__exit__ = lambda s, *a: None++ with loguru_caplog_full.at_level("DEBUG"):+ with patch(+ "local_deep_research.database.session_context.get_user_db_session",+ return_value=fake_session,+ ):+ with patch(+ "local_deep_research.settings.manager.SettingsManager"+ ) as mock_sm:+ mock_sm.return_value.get_settings_snapshot.side_effect = exc+ with patch(+ "local_deep_research.research_library.services.download_service.DownloadService"+ ):+ scheduler._process_user_documents("alice")++ _assert_no_leak(+ loguru_caplog_full.text,+ _LEAKED_PASSWORD,+ "_process_user_documents snapshot handler",+ )+ assert "Could not build settings snapshot" in loguru_caplog_full.text