~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: prevent logging of sensitive data like passwords and API keys
LearningCircuit7d1a25df290c
diff --git a/.gitignore b/.gitignoreindex 6fcbffc655..70c3c34259 100644--- a/.gitignore+++ b/.gitignore@@ -49,6 +49,7 @@# Allow pre-commit hooks!.pre-commit-hooks/!.pre-commit-hooks/*.py+!.pre-commit-hooks/*.sh# Block all other dot files/foldersdiff --git a/.pre-commit-config.yaml b/.pre-commit-config.yamlindex c0e47fa762..c10590368d 100644--- a/.pre-commit-config.yaml+++ b/.pre-commit-config.yaml@@ -87,3 +87,9 @@ repos:language: scriptfiles: \.(html|js|css|py)$description: "Ensure no external CDN URLs or external resources are referenced in the codebase"+ - id: check-sensitive-logging+ name: Check for sensitive data in logging+ entry: .pre-commit-hooks/check-sensitive-logging.py+ language: script+ files: \.py$+ description: "Prevent logging of passwords, API keys, and other sensitive information"diff --git a/.pre-commit-hooks/check-sensitive-logging.py b/.pre-commit-hooks/check-sensitive-logging.pynew file mode 100755index 0000000000..7c4b5725ed--- /dev/null+++ b/.pre-commit-hooks/check-sensitive-logging.py@@ -0,0 +1,347 @@+#!/usr/bin/env python3+"""+Pre-commit hook to detect potential logging of sensitive data.+Prevents passwords, tokens, and other sensitive information from being logged.+"""++import ast+import sys+from pathlib import Path+from typing import List+++SENSITIVE_VARS = {+ "password",+ "user_password",+ "pwd",+ "secret",+ "api_key",+ "private_key",+ "auth",+ "credential",+ "csrf_token", # CSRF tokens should not be logged+ "bearer_token",+ "access_token",+ "refresh_token",+ "auth_token",+ "session_id", # Can be used to retrieve passwords+ "encryption_key",+ "secret_key",+ "auth_header",+}++SENSITIVE_DICT_KEYS = {+ "shared_research_context", # Contains user_password for SQLCipher+ "settings_snapshot", # Contains API keys and other sensitive data+ "kwargs", # Often contains user_password and gets passed through many functions+ "credentials", # Obviously contains authentication data+ "auth_data", # Authentication related data+ "auth_context", # Authentication context+ "user_data", # May contain user passwords+ "login_data", # Login form data including passwords+ "form_data", # Form submissions may contain passwords+ "post_data", # POST request data+ "payload", # API request payloads+ "body", # Request body data+ "session", # Flask session data (when used as dict)+ "flask_session", # Explicit Flask session+ "g.user_password", # Flask g object with password+ "connection_string", # Database connection strings with passwords+ "conn_str",+ "db_url",+ "database_url",+ "conn_params",+ "db_config",+ "environ", # os.environ contains sensitive vars+}++# These are false positives - specific variable names that are safe to log+FALSE_POSITIVE_VARS = {+ # Token counting related - these are LLM tokens, not auth tokens+ "prompt_tokens",+ "completion_tokens",+ "token_research_count",+ "tokens_truncated",+ "estimated_prompt_tokens",+ "estimated_completion_tokens",+ "max_tokens",+ "new_max_tokens",+ # Configuration paths and names (not the actual config contents)+ "config_path",+ "config_name",+ "config_hash",+ "context_window_size",+ # Database paths (not credentials)+ "auth_db_path",+ # Safe/redacted versions+ "safe_settings", # This is already redacted+}++# Specific file + variable combinations that are allowed+# Each entry must have an explanation of why it's safe+ALLOWED_LOGGING = [+ {+ "file_pattern": "settings/logger.py",+ "variable": "safe_settings",+ "reason": "This variable contains already redacted settings with sensitive values replaced by ***REDACTED***",+ },+ {+ "file_pattern": "settings/logger.py",+ "variable": "settings",+ "reason": "This module is responsible for safe logging and applies redaction before logging when log_level is 'debug_unsafe'",+ },+ {+ "file_pattern": "tests/",+ "variable": "csrf_token",+ "reason": "Test files need to log CSRF tokens for debugging authentication issues during test development",+ },+ {+ "file_pattern": "web/services/socket_service.py",+ "variable": "kwargs",+ "reason": "These are logger wrapper methods where kwargs are logger configuration options (like exc_info, extra), not actual data being logged",+ },+]+++class SensitiveLoggingChecker(ast.NodeVisitor):+ """AST visitor to detect sensitive data in logging statements."""++ def __init__(self, filename: str):+ self.filename = filename+ self.errors: List[str] = []++ def visit_Call(self, node: ast.Call) -> None:+ """Check function calls for logging sensitive data."""+ # Check if it's a logger call+ if self._is_logger_call(node):+ self._check_sensitive_logging(node)++ self.generic_visit(node)++ def _is_logger_call(self, node: ast.Call) -> bool:+ """Check if the call is to a logger method."""+ if isinstance(node.func, ast.Attribute):+ # Check for logger.info, logger.debug, etc.+ attr_name = node.func.attr+ if attr_name in {+ "info",+ "debug",+ "warning",+ "error",+ "critical",+ "log",+ }:+ if isinstance(node.func.value, ast.Name):+ return node.func.value.id == "logger"+ elif isinstance(node.func.value, ast.Attribute):+ # Handle self.logger, cls.logger, etc.+ return node.func.value.attr == "logger"+ return False++ def _check_sensitive_logging(self, node: ast.Call) -> None:+ """Check if sensitive data is being logged."""+ for arg in node.args:+ self._check_expression_for_sensitive_data(arg, node.lineno)++ for keyword in node.keywords:+ self._check_expression_for_sensitive_data(+ keyword.value, node.lineno+ )++ def _is_allowed_logging(self, variable_name: str) -> bool:+ """Check if this specific logging is allowed."""+ for allowed in ALLOWED_LOGGING:+ if (+ allowed["file_pattern"] in self.filename+ and allowed["variable"] == variable_name+ ):+ return True+ return False++ def _check_expression_for_sensitive_data(+ self, expr: ast.AST, lineno: int+ ) -> None:+ """Recursively check an expression for sensitive data."""+ if isinstance(expr, ast.Name):+ # Skip if it's a known false positive+ if expr.id in FALSE_POSITIVE_VARS:+ return++ # Skip if it's specifically allowed for this file+ if self._is_allowed_logging(expr.id):+ return++ # Direct variable logging+ if any(+ sensitive in expr.id.lower() for sensitive in SENSITIVE_VARS+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive variable '{expr.id}'"+ )+ # Check for sensitive dictionaries being logged directly+ if any(+ sensitive in expr.id.lower()+ for sensitive in SENSITIVE_DICT_KEYS+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive dictionary '{expr.id}'"+ )++ elif isinstance(expr, ast.Attribute):+ # Check for Flask request object attributes+ if isinstance(expr.value, ast.Name) and expr.value.id == "request":+ if expr.attr in {+ "form",+ "json",+ "data",+ "headers",+ "authorization",+ "cookies",+ }:+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive Flask request.{expr.attr} which may contain passwords or tokens"+ )+ return++ # Check for Flask g object attributes+ if isinstance(expr.value, ast.Name) and expr.value.id == "g":+ if (+ "password" in expr.attr.lower()+ or "token" in expr.attr.lower()+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive Flask g.{expr.attr}"+ )+ return++ # Check for os.environ+ if (+ isinstance(expr.value, ast.Name)+ and expr.value.id == "os"+ and expr.attr == "environ"+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of os.environ which contains environment variables"+ )+ return++ # Check for dict.keys(), dict.values(), dict.items()+ if expr.attr in {"keys", "values", "items"}:+ if isinstance(expr.value, ast.Name):+ if any(+ sensitive in expr.value.id.lower()+ for sensitive in SENSITIVE_DICT_KEYS+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive dictionary keys/values from '{expr.value.id}.{expr.attr}()'"+ )++ # Check for sensitive attributes like obj.password, obj.api_key, etc.+ if any(+ sensitive in expr.attr.lower() for sensitive in SENSITIVE_VARS+ ):+ # Skip false positives+ if expr.attr in FALSE_POSITIVE_VARS:+ return+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive attribute '{expr.attr}'"+ )++ elif isinstance(expr, ast.Call):+ # Check for list(), str(), repr() of sensitive dicts+ if isinstance(expr.func, ast.Name):+ if expr.func.id in {"list", "str", "repr"}:+ for arg in expr.args:+ # Check for list(dict.keys()), list(dict.values()), etc.+ if isinstance(arg, ast.Call) and isinstance(+ arg.func, ast.Attribute+ ):+ if arg.func.attr in {"keys", "values", "items"}:+ if isinstance(arg.func.value, ast.Name):+ if any(+ sensitive in arg.func.value.id.lower()+ for sensitive in SENSITIVE_DICT_KEYS+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive dictionary keys/values via '{expr.func.id}({arg.func.value.id}.{arg.func.attr}())'"+ )+ # Also check direct conversion of sensitive dicts+ elif isinstance(arg, ast.Name):+ if any(+ sensitive in arg.id.lower()+ for sensitive in SENSITIVE_DICT_KEYS+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of sensitive dictionary via '{expr.func.id}({arg.id})'"+ )++ elif isinstance(expr, ast.JoinedStr):+ # Check f-strings+ for value in expr.values:+ if isinstance(value, ast.FormattedValue):+ self._check_expression_for_sensitive_data(+ value.value, lineno+ )++ elif isinstance(expr, ast.Dict):+ # Check dictionary literals for sensitive keys+ for key in expr.keys:+ if isinstance(key, ast.Constant) and isinstance(key.value, str):+ if any(+ sensitive in key.value.lower()+ for sensitive in SENSITIVE_VARS+ ):+ self.errors.append(+ f"{self.filename}:{lineno}: Potential logging of dictionary with sensitive key '{key.value}'"+ )++ elif isinstance(expr, ast.BinOp):+ # Check string formatting+ self._check_expression_for_sensitive_data(expr.left, lineno)+ self._check_expression_for_sensitive_data(expr.right, lineno)+++def check_file(filepath: Path) -> List[str]:+ """Check a single Python file for sensitive logging."""+ try:+ with open(filepath, "r", encoding="utf-8") as f:+ content = f.read()++ tree = ast.parse(content, filename=str(filepath))+ checker = SensitiveLoggingChecker(str(filepath))+ checker.visit(tree)+ return checker.errors++ except SyntaxError as e:+ return [f"{filepath}:{e.lineno}: Syntax error - {e.msg}"]+ except Exception as e:+ return [f"{filepath}: Error checking file - {e}"]+++def main():+ """Main entry point for the pre-commit hook."""+ files = sys.argv[1:]+ all_errors = []++ for filename in files:+ filepath = Path(filename)+ if filepath.suffix == ".py":+ errors = check_file(filepath)+ all_errors.extend(errors)++ if all_errors:+ print("Sensitive data logging detected:")+ for error in all_errors:+ print(f" {error}")+ print("\nPlease ensure sensitive data is not logged directly.")+ print("Consider:")+ print(" - Removing sensitive data from log messages")+ print(" - Using sanitized versions of data structures")+ print(" - Logging only necessary non-sensitive information")+ return 1++ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/src/local_deep_research/advanced_search_system/tools/base_tool.py b/src/local_deep_research/advanced_search_system/tools/base_tool.pyindex b5e782841e..2f71d1a5d4 100644--- a/src/local_deep_research/advanced_search_system/tools/base_tool.py+++ b/src/local_deep_research/advanced_search_system/tools/base_tool.py@@ -89,7 +89,8 @@ class BaseTool(ABC):Args:**kwargs: Parameters used in execution"""- logger.info(f"Executing tool {self.name} with parameters: {kwargs}")+ # Don't log kwargs directly as it may contain sensitive data+ logger.info(f"Executing tool {self.name} with {len(kwargs)} parameters")def _log_result(self, result: Any) -> None:"""diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.pyindex 49e9617293..c3c280b2ac 100644--- a/src/local_deep_research/config/llm_config.py+++ b/src/local_deep_research/config/llm_config.py@@ -924,9 +924,7 @@ def wrap_llm_without_think_tags(logger.debug(f"LLM Request - Model: {getattr(self.base_llm, 'model', 'unknown')}")- logger.debug(- f"LLM Request - Args count: {len(args)}, Kwargs: {list(kwargs.keys())}"- )+ logger.debug(f"LLM Request - Args count: {len(args)}")# Log the prompt if it's in argsif args and len(args) > 0:diff --git a/src/local_deep_research/metrics/token_counter.py b/src/local_deep_research/metrics/token_counter.pyindex 394b20894a..3434294840 100644--- a/src/local_deep_research/metrics/token_counter.py+++ b/src/local_deep_research/metrics/token_counter.py@@ -146,7 +146,10 @@ class TokenCountingCallback(BaseCallbackHandler):# Debug logginglogger.debug(f"on_llm_start serialized: {serialized}")- logger.debug(f"on_llm_start kwargs: {kwargs}")+ # Don't log kwargs directly as it may contain sensitive data+ logger.debug(+ f"on_llm_start kwargs keys: {list(kwargs.keys()) if kwargs else []}"+ )# First, use preset values if availableif self.preset_model:diff --git a/src/local_deep_research/search_system_factory.py b/src/local_deep_research/search_system_factory.pyindex cf6a6e29af..80056c3a13 100644--- a/src/local_deep_research/search_system_factory.py+++ b/src/local_deep_research/search_system_factory.py@@ -180,7 +180,11 @@ def create_strategy(NewsAggregationStrategy,)- return NewsAggregationStrategy()+ return NewsAggregationStrategy(+ model=model,+ search=search,+ all_links_of_system=all_links_of_system,+ )# IterDRAG strategyelif strategy_name_lower == "iterdrag":diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.pyindex 26ec19afd6..d57623288b 100644--- a/src/local_deep_research/web/services/research_service.py+++ b/src/local_deep_research/web/services/research_service.py@@ -274,9 +274,7 @@ def run_research_process(# Extract username - required for database accessusername = kwargs.get("username")- logger.info(- f"Research thread started with username: {username}, kwargs keys: {list(kwargs.keys())}"- )+ logger.info(f"Research thread started with username: {username}")if not username:logger.error("No username provided to research thread")raise ValueError("Username is required for research process")@@ -395,9 +393,8 @@ def run_research_process()shared_research_context.update(kwargs["research_context"])- logger.info(- f"Created shared_research_context with username: {username}, all keys: {list(shared_research_context.keys())}"- )+ # Do not log context keys as they may contain sensitive information+ logger.info(f"Created shared_research_context for user: {username}")# Set search context for search trackingset_search_context(shared_research_context)