Skip to content

~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.

feat(security): enforce diagnose-gated logging wrapper in provider/engine paths (#4183) (#4976)

Dongping Guo (Donny)e01f8c952d00
diff --git a/.pre-commit-hooks/check-env-vars.py b/.pre-commit-hooks/check-env-vars.py
index 2fa2383222..3feda179e7 100755
--- a/.pre-commit-hooks/check-env-vars.py
+++ b/.pre-commit-hooks/check-env-vars.py
@@ -46,9 +46,11 @@ ALLOWED_NAMES = {
# Path-anchored entries where the basename alone is too generic to match
# reliably.
+# Leading "/" anchors the path segment so endswith() cannot match lookalike
+# paths (e.g. "notsecurity/secure_logging.py").
ALLOWED_PATH_ENDINGS = (
- "security/rate_limiter.py", # Module-level RATE_LIMIT_FAIL_CLOSED at decorator time
- "security/secure_logging.py", # Diagnose-mode gate must work before/without SettingsManager (same rationale as log_utils.py)
+ "/security/rate_limiter.py", # Module-level RATE_LIMIT_FAIL_CLOSED at decorator time
+ "/security/secure_logging.py", # Diagnose-mode gate must work before/without SettingsManager (same rationale as log_utils.py)
)
# System environment variables that are always allowed
diff --git a/.pre-commit-hooks/check-loguru-formatting.py b/.pre-commit-hooks/check-loguru-formatting.py
index afd9edf92b..6f1a8da43f 100755
--- a/.pre-commit-hooks/check-loguru-formatting.py
+++ b/.pre-commit-hooks/check-loguru-formatting.py
@@ -4,6 +4,11 @@ Pre-commit hook to prevent stdlib printf-style formatting in direct loguru calls
loguru uses brace formatting (`{}`), not stdlib logging placeholders like
`%s` or `%d`. Mixing the two leaves placeholders unrendered in runtime logs.
+
+Applies to files importing either raw loguru or the project's diagnose-gated
+``security.secure_logging`` wrapper (which delegates formatting to loguru).
+The pytest guardian ``tests/utilities/test_loguru_placeholder_formatting.py``
+imports this module so hook and guardian cannot diverge.
"""
import ast
@@ -27,39 +32,56 @@ LOGURU_METHODS = {
"log",
}
+# The secure_logging wrapper import, matched exactly (module + level) like
+# check-sensitive-logging.py does — never by suffix.
+WRAPPER_MODULE_RELATIVE = "security.secure_logging"
+WRAPPER_MODULE_ABSOLUTE = "local_deep_research.security.secure_logging"
-def check_file(file_path: str) -> list[tuple[int, str]]:
- path = Path(file_path)
- if path.suffix != ".py":
- return []
+# SecureLogger.bind()/.patch() re-wrap and keep loguru brace formatting, so
+# chained calls need the same placeholder check as direct ones.
+WRAPPER_CHAIN_METHODS = {"bind", "patch"}
- try:
- source = path.read_text(encoding="utf-8")
- except Exception as exc:
- return [(0, f"failed to read file: {exc}")]
- try:
- tree = ast.parse(source, filename=file_path)
- except SyntaxError:
- return []
+def imports_project_logger(tree: ast.AST) -> bool:
+ """True if the module imports ``logger`` from loguru or the wrapper.
- imports_loguru_logger = any(
- isinstance(node, ast.ImportFrom)
- and node.module == "loguru"
- and any(alias.name == "logger" for alias in node.names)
- for node in tree.body
- )
- if not imports_loguru_logger:
- return []
+ Walks the whole tree so function-local imports are detected too.
+ """
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.ImportFrom):
+ continue
+ if not any(alias.name == "logger" for alias in node.names):
+ continue
+ if node.level == 0 and node.module in (
+ "loguru",
+ WRAPPER_MODULE_ABSOLUTE,
+ ):
+ return True
+ if node.level > 0 and node.module == WRAPPER_MODULE_RELATIVE:
+ return True
+ return False
+
+
+def _is_logger_receiver(expr: ast.AST) -> bool:
+ """True for ``logger`` or a bind()/patch() chain rooted at ``logger``."""
+ while (
+ isinstance(expr, ast.Call)
+ and isinstance(expr.func, ast.Attribute)
+ and expr.func.attr in WRAPPER_CHAIN_METHODS
+ ):
+ expr = expr.func.value
+ return isinstance(expr, ast.Name) and expr.id == "logger"
+
+def find_printf_violations(tree: ast.AST) -> list[tuple[int, str]]:
+ """Return (lineno, message) for logger calls using printf placeholders."""
violations = []
for node in ast.walk(tree):
if not (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
- and isinstance(node.func.value, ast.Name)
- and node.func.value.id == "logger"
and node.func.attr in LOGURU_METHODS
+ and _is_logger_receiver(node.func.value)
):
continue
@@ -87,6 +109,27 @@ def check_file(file_path: str) -> list[tuple[int, str]]:
return violations
+def check_file(file_path: str) -> list[tuple[int, str]]:
+ path = Path(file_path)
+ if path.suffix != ".py":
+ return []
+
+ try:
+ source = path.read_text(encoding="utf-8")
+ except Exception as exc:
+ return [(0, f"failed to read file: {exc}")]
+
+ try:
+ tree = ast.parse(source, filename=file_path)
+ except SyntaxError:
+ return []
+
+ if not imports_project_logger(tree):
+ return []
+
+ return find_printf_violations(tree)
+
+
def main() -> int:
exit_code = 0
diff --git a/.pre-commit-hooks/check-sensitive-logging.py b/.pre-commit-hooks/check-sensitive-logging.py
index e1335545a6..4aae304078 100755
--- a/.pre-commit-hooks/check-sensitive-logging.py
+++ b/.pre-commit-hooks/check-sensitive-logging.py
@@ -129,6 +129,58 @@ SECURE_LOGGING_DIRS = (
"src/local_deep_research/web_search_engines/",
)
+# The only sanctioned logger import inside SECURE_LOGGING_DIRS. Matched
+# exactly (module + level), never by suffix, so lookalike modules such as
+# ``notsecurity.secure_logging`` cannot spoof it.
+WRAPPER_MODULE_RELATIVE = "security.secure_logging"
+WRAPPER_MODULE_ABSOLUTE = "local_deep_research.security.secure_logging"
+WRAPPER_IMPORT_HINT = (
+ "use 'from ...security.secure_logging import logger' "
+ "(relative depth per file)"
+)
+
+# Attribute names reserved inside SECURE_LOGGING_DIRS: routes from the
+# SecureLogger wrapper (or any object) back to raw loguru handles.
+# .opt / .catch — wrapper delegates these to raw loguru ungated
+# .patch — patchers can mutate record["exception"] and attach
+# an ungated traceback
+# .logger — raw singleton re-exports (log_utils.logger,
+# local_deep_research.logger) and future self.logger
+# ._logger — SecureLogger's wrapped handle (__slots__)
+# ._loguru_logger — raw singleton inside secure_logging
+# .__getattr__ — logger.__getattr__("opt") delegates to raw loguru
+BANNED_LOGGER_ATTRS = {
+ "opt",
+ "catch",
+ "patch",
+ "logger",
+ "_logger",
+ "_loguru_logger",
+ "__getattr__",
+}
+
+# Traceback-formatting attribute names: even when the ``traceback`` module
+# import ban is dodged (dynamic import, re-export), these calls produce raw
+# traceback text that must never reach production logs.
+BANNED_TRACEBACK_ATTRS = {
+ "format_exc",
+ "format_exception",
+ "format_exception_only",
+ "format_tb",
+ "print_exc",
+ "print_exception",
+ "print_tb",
+}
+
+# Module names whose dynamic import (import_module / __import__) is banned in
+# SECURE_LOGGING_DIRS — same modules the static import bans cover.
+BANNED_DYNAMIC_IMPORTS = {"loguru", "traceback"}
+
+# Wrapper-preserving chain methods safe for secure dirs: SecureLogger.bind()
+# re-wraps without letting callers mutate the raw loguru record. Messages are
+# still production-visible and must pass the exception-variable check.
+WRAPPER_CHAIN_METHODS = {"bind"}
+
class SensitiveLoggingChecker(ast.NodeVisitor):
"""AST visitor to detect sensitive data in logging statements."""
@@ -138,12 +190,577 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
self.errors: List[str] = []
# Track exception variable names from enclosing except handlers
self._except_var_stack: List[Optional[str]] = []
+ # Scope-local names that refer to the sys module. Used to catch
+ # ``import sys as s; s.exc_info()`` without leaking local aliases into
+ # unrelated functions.
+ self._sys_name_scopes = [{"sys"}]
+ self._shadowed_name_scopes = [set()]
+ self._name_scope_kinds = ["module"]
+ # Single normalized dir check reused by every dir-scoped rule so
+ # Windows paths cannot diverge between checks.
+ self.in_secure_dir = any(
+ d in filename.replace("\\", "/") for d in SECURE_LOGGING_DIRS
+ )
def visit_ExceptHandler(self, node: ast.ExceptHandler) -> None:
"""Track exception variable names inside except blocks."""
+ if self.in_secure_dir and node.name == "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"'except ... as logger' shadows the secure logger — "
+ f"rename the exception variable"
+ )
+ previous_shadowed_names = set(self._shadowed_name_scopes[-1])
+ if self.in_secure_dir and node.name:
+ self._mark_name_shadow(node.name)
self._except_var_stack.append(node.name)
self.generic_visit(node)
self._except_var_stack.pop()
+ self._shadowed_name_scopes[-1] = previous_shadowed_names
+
+ def _push_name_scope(self, shadowed_names=(), kind: str = "block") -> None:
+ self._sys_name_scopes.append(set())
+ self._shadowed_name_scopes.append(set(shadowed_names))
+ self._name_scope_kinds.append(kind)
+
+ def _pop_name_scope(self) -> None:
+ self._sys_name_scopes.pop()
+ self._shadowed_name_scopes.pop()
+ self._name_scope_kinds.pop()
+
+ def _name_refers_to_sys(self, name: str) -> bool:
+ nearest_function_scope = max(
+ (
+ idx
+ for idx, kind in enumerate(self._name_scope_kinds)
+ if kind == "function"
+ ),
+ default=-1,
+ )
+ for idx in range(len(self._sys_name_scopes) - 1, -1, -1):
+ if (
+ self._name_scope_kinds[idx] == "class"
+ and nearest_function_scope > idx
+ ):
+ continue
+ sys_names = self._sys_name_scopes[idx]
+ shadowed_names = self._shadowed_name_scopes[idx]
+ if name in shadowed_names:
+ return False
+ if name in sys_names:
+ return True
+ return False
+
+ def _mark_name_shadow(self, name: str) -> None:
+ self._sys_name_scopes[-1].discard(name)
+ self._shadowed_name_scopes[-1].add(name)
+
+ def _mark_target_shadow(self, target: ast.AST) -> None:
+ if isinstance(target, ast.Name):
+ self._mark_name_shadow(target.id)
+ elif isinstance(target, (ast.Tuple, ast.List)):
+ for elt in target.elts:
+ self._mark_target_shadow(elt)
+ elif isinstance(target, ast.Starred):
+ self._mark_target_shadow(target.value)
+
+ @staticmethod
+ def _is_literal_dynamic_import(node: ast.AST, module: str) -> bool:
+ if not isinstance(node, ast.Call) or not node.args:
+ return False
+ func = node.func
+ is_dynamic_import = (
+ (isinstance(func, ast.Attribute) and func.attr == "import_module")
+ or (isinstance(func, ast.Name) and func.id == "import_module")
+ or (isinstance(func, ast.Name) and func.id == "__import__")
+ )
+ return (
+ is_dynamic_import
+ and isinstance(node.args[0], ast.Constant)
+ and node.args[0].value == module
+ )
+
+ def _expr_is_sys_module(self, node: ast.AST) -> bool:
+ return (
+ isinstance(node, ast.Name) and self._name_refers_to_sys(node.id)
+ ) or self._is_literal_dynamic_import(node, "sys")
+
+ # ------------------------------------------------------------------
+ # Wrapper-import enforcement (SECURE_LOGGING_DIRS only, #4183 step 2)
+ # ------------------------------------------------------------------
+
+ def visit_Import(self, node: ast.Import) -> None:
+ """Ban raw-logger import routes in secure-logging dirs."""
+ if self.in_secure_dir:
+ for alias in node.names:
+ root = alias.name.split(".")[0]
+ leaf = alias.name.split(".")[-1]
+ bound = alias.asname or root
+ if alias.name == "sys" or (
+ root == "sys" and alias.asname is None
+ ):
+ # `import sys`, `import sys as s`, and submodule forms
+ # like `import sys.monitoring` (which bind the root
+ # name `sys`) all make the bound name refer to sys.
+ self._sys_name_scopes[-1].add(bound)
+ self._shadowed_name_scopes[-1].discard(bound)
+ else:
+ self._mark_name_shadow(bound)
+ if root == "loguru":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"raw loguru import is banned in secure-logging dirs — "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ elif leaf == "secure_logging":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing the secure_logging module exposes raw "
+ f"handles (secure_logging._loguru_logger) — "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ elif leaf == "log_utils":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing utilities.log_utils exposes the raw "
+ f"loguru logger (log_utils.logger) — "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ elif root == "local_deep_research" and alias.asname is None:
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"absolute 'import local_deep_research...' binds the "
+ f"root package, whose .logger attribute is the raw "
+ f"loguru logger — use relative imports; "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ elif root == "traceback":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"'import traceback' is banned in secure-logging dirs — "
+ f"tracebacks must never be interpolated into log "
+ f"messages; use logger.exception() (diagnose-gated) "
+ f"with a scrubbed safe_msg"
+ )
+ if bound == "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"import binds the local name 'logger' — only the "
+ f"secure_logging wrapper may bind 'logger' here; "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ self.generic_visit(node)
+
+ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
+ """Allow only the exact secure_logging wrapper import of 'logger'."""
+ if self.in_secure_dir:
+ self._check_import_from_secure_dir(node)
+ self.generic_visit(node)
+
+ def _check_import_from_secure_dir(self, node: ast.ImportFrom) -> None:
+ module = node.module or ""
+ is_wrapper_module = (
+ node.level > 0 and module == WRAPPER_MODULE_RELATIVE
+ ) or (node.level == 0 and module == WRAPPER_MODULE_ABSOLUTE)
+ for alias in node.names:
+ if alias.name != "*":
+ self._mark_name_shadow(alias.asname or alias.name)
+
+ if module.split(".")[0] == "loguru":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"raw loguru import is banned in secure-logging dirs — "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ return
+
+ if node.level == 0 and module == "traceback":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"'from traceback import ...' is banned in secure-logging "
+ f"dirs — tracebacks must never be interpolated into log "
+ f"messages; use logger.exception() (diagnose-gated) with a "
+ f"scrubbed safe_msg"
+ )
+ return
+
+ if node.level == 0 and module == "sys":
+ for alias in node.names:
+ if alias.name in ("*", "exc_info"):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"'from sys import exc_info' is banned in "
+ f"secure-logging dirs — tracebacks must never be "
+ f"interpolated into log messages; use "
+ f"logger.exception() (diagnose-gated) with a "
+ f"scrubbed safe_msg"
+ )
+
+ if is_wrapper_module:
+ for alias in node.names:
+ if alias.name != "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing '{alias.name}' from secure_logging is "
+ f"banned — only the unaliased 'logger' proxy may be "
+ f"imported in secure-logging dirs"
+ )
+ elif alias.asname not in (None, "logger"):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"aliasing the secure logger to "
+ f"'{alias.asname}' is banned — the local name must "
+ f"remain 'logger'"
+ )
+ return
+
+ # Module-import routes to raw handles
+ if (node.level > 0 and module == "utilities.log_utils") or (
+ node.level == 0
+ and module == "local_deep_research.utilities.log_utils"
+ ):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing from utilities.log_utils is banned in "
+ f"secure-logging dirs — it re-exports the raw loguru "
+ f"logger; {WRAPPER_IMPORT_HINT}"
+ )
+ return
+
+ for alias in node.names:
+ if alias.name == "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing 'logger' from a module other than "
+ f"security.secure_logging is banned in secure-logging "
+ f"dirs — {WRAPPER_IMPORT_HINT}"
+ )
+ elif alias.name == "secure_logging" and (
+ (node.level > 0 and module == "security")
+ or (
+ node.level == 0 and module == "local_deep_research.security"
+ )
+ ):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing the secure_logging module exposes raw "
+ f"handles (secure_logging._loguru_logger) — "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ elif alias.name == "log_utils" and (
+ (node.level > 0 and module == "utilities")
+ or (
+ node.level == 0
+ and module == "local_deep_research.utilities"
+ )
+ ):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"importing utilities.log_utils exposes the raw loguru "
+ f"logger (log_utils.logger) — {WRAPPER_IMPORT_HINT}"
+ )
+ elif alias.asname == "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"import binds the local name 'logger' — only the "
+ f"secure_logging wrapper may bind 'logger' here; "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+
+ # ------------------------------------------------------------------
+ # Logger rebinding / shadowing bans (SECURE_LOGGING_DIRS only)
+ # ------------------------------------------------------------------
+
+ def _flag_logger_binding(self, target: ast.AST, lineno: int) -> None:
+ """Flag any assignment target that binds the name 'logger'."""
+ if isinstance(target, ast.Name) and target.id == "logger":
+ self.errors.append(
+ f"{self.filename}:{lineno}: "
+ f"rebinding the name 'logger' is banned in secure-logging "
+ f"dirs — only the secure_logging wrapper import may bind "
+ f"'logger'"
+ )
+ elif isinstance(target, (ast.Tuple, ast.List)):
+ for elt in target.elts:
+ self._flag_logger_binding(elt, lineno)
+ elif isinstance(target, ast.Starred):
+ self._flag_logger_binding(target.value, lineno)
+
+ def _check_logger_derivation(self, value: ast.AST, lineno: int) -> None:
+ """Flag assignments that derive a new local name from a logger.
+
+ Covers ``log = logger``, ``log = logger.bind(...)``,
+ ``x = log_utils.logger``, ``log = pkg.logger.bind(...)``.
+ """
+ expr = value
+ while (
+ isinstance(expr, ast.Call)
+ and isinstance(expr.func, ast.Attribute)
+ and expr.func.attr in WRAPPER_CHAIN_METHODS
+ ):
+ expr = expr.func.value
+ if (isinstance(expr, ast.Name) and expr.id == "logger") or (
+ isinstance(expr, ast.Attribute) and expr.attr == "logger"
+ ):
+ if isinstance(expr, ast.Attribute):
+ # ``x = pkg.logger`` is already reported by visit_Attribute's
+ # .logger ban; avoid a second diagnostic for the same line.
+ return
+ self.errors.append(
+ f"{self.filename}:{lineno}: "
+ f"assigning a logger to another name is banned in "
+ f"secure-logging dirs — aliased loggers evade the "
+ f"exception-variable checks; call 'logger' directly"
+ )
+
+ def visit_Assign(self, node: ast.Assign) -> None:
+ if self.in_secure_dir:
+ for target in node.targets:
+ self._flag_logger_binding(target, node.lineno)
+ self._check_logger_derivation(node.value, node.lineno)
+ for target in node.targets:
+ self._mark_target_shadow(target)
+ self.generic_visit(node)
+
+ def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
+ if self.in_secure_dir:
+ self._flag_logger_binding(node.target, node.lineno)
+ if node.value is not None:
+ self._check_logger_derivation(node.value, node.lineno)
+ self._mark_target_shadow(node.target)
+ self.generic_visit(node)
+
+ def visit_AugAssign(self, node: ast.AugAssign) -> None:
+ if self.in_secure_dir:
+ self._flag_logger_binding(node.target, node.lineno)
+ self._mark_target_shadow(node.target)
+ self.generic_visit(node)
+
+ def visit_NamedExpr(self, node: ast.NamedExpr) -> None:
+ if self.in_secure_dir:
+ self._flag_logger_binding(node.target, node.lineno)
+ self._check_logger_derivation(node.value, node.lineno)
+ self._mark_target_shadow(node.target)
+ self.generic_visit(node)
+
+ def visit_For(self, node: ast.For) -> None:
+ if self.in_secure_dir:
+ self._flag_logger_binding(node.target, node.lineno)
+ self._mark_target_shadow(node.target)
+ self.generic_visit(node)
+
+ def visit_AsyncFor(self, node: ast.AsyncFor) -> None:
+ if self.in_secure_dir:
+ self._flag_logger_binding(node.target, node.lineno)
+ self._mark_target_shadow(node.target)
+ self.generic_visit(node)
+
+ def visit_With(self, node: ast.With) -> None:
+ if self.in_secure_dir:
+ for item in node.items:
+ if item.optional_vars is not None:
+ self._flag_logger_binding(item.optional_vars, node.lineno)
+ self._mark_target_shadow(item.optional_vars)
+ self.generic_visit(node)
+
+ def visit_AsyncWith(self, node: ast.AsyncWith) -> None:
+ if self.in_secure_dir:
+ for item in node.items:
+ if item.optional_vars is not None:
+ self._flag_logger_binding(item.optional_vars, node.lineno)
+ self._mark_target_shadow(item.optional_vars)
+ self.generic_visit(node)
+
+ @staticmethod
+ def _func_param_names(args: ast.arguments) -> List[str]:
+ all_args = (
+ list(args.posonlyargs) + list(args.args) + list(args.kwonlyargs)
+ )
+ if args.vararg:
+ all_args.append(args.vararg)
+ if args.kwarg:
+ all_args.append(args.kwarg)
+ return [arg.arg for arg in all_args]
+
+ def _check_func_params(self, node) -> None:
+ args = node.args
+ for arg_name in self._func_param_names(args):
+ if arg_name == "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"parameter named 'logger' shadows the secure logger in "
+ f"secure-logging dirs — rename the parameter"
+ )
+
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
+ if self.in_secure_dir:
+ self._check_func_params(node)
+ self._push_name_scope(
+ self._func_param_names(node.args), kind="function"
+ )
+ self.generic_visit(node)
+ self._pop_name_scope()
+
+ def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
+ if self.in_secure_dir:
+ self._check_func_params(node)
+ self._push_name_scope(
+ self._func_param_names(node.args), kind="function"
+ )
+ self.generic_visit(node)
+ self._pop_name_scope()
+
+ def visit_Lambda(self, node: ast.Lambda) -> None:
+ if self.in_secure_dir:
+ self._check_func_params(node)
+ self._push_name_scope(
+ self._func_param_names(node.args), kind="function"
+ )
+ self.generic_visit(node)
+ self._pop_name_scope()
+
+ def visit_ClassDef(self, node: ast.ClassDef) -> None:
+ self._push_name_scope(kind="class")
+ self.generic_visit(node)
+ self._pop_name_scope()
+
+ # ------------------------------------------------------------------
+ # Bypass-route detection (SECURE_LOGGING_DIRS only)
+ # ------------------------------------------------------------------
+
+ def visit_Attribute(self, node: ast.Attribute) -> None:
+ if self.in_secure_dir:
+ if node.attr in ("opt", "catch"):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f".{node.attr} is banned in secure-logging dirs — the "
+ f"secure_logging wrapper delegates it to raw loguru, "
+ f"reattaching tracebacks ungated; use logger.exception() "
+ f"with a scrubbed safe_msg"
+ )
+ elif node.attr == "patch":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f".patch is banned in secure-logging dirs — patchers can "
+ f'mutate record["exception"] and reattach tracebacks '
+ f"ungated; use logger.bind() for metadata"
+ )
+ elif node.attr == "logger":
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f".logger attribute access is banned in secure-logging "
+ f"dirs — it reaches raw loguru re-exports "
+ f"(log_utils.logger, local_deep_research.logger); "
+ f"{WRAPPER_IMPORT_HINT}"
+ )
+ elif node.attr in ("_logger", "_loguru_logger", "__getattr__"):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f".{node.attr} accesses a raw loguru handle — banned in "
+ f"secure-logging dirs"
+ )
+ elif node.attr in BANNED_TRACEBACK_ATTRS:
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f".{node.attr} produces raw traceback text — banned in "
+ f"secure-logging dirs; use logger.exception() "
+ f"(diagnose-gated) with a scrubbed safe_msg"
+ )
+ elif node.attr == "exc_info" and self._expr_is_sys_module(
+ node.value
+ ):
+ receiver = (
+ node.value.id if isinstance(node.value, ast.Name) else "sys"
+ )
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f"{receiver}.exc_info is banned in secure-logging dirs — "
+ f"tracebacks must never be interpolated into log "
+ f"messages; use logger.exception() (diagnose-gated) "
+ f"with a scrubbed safe_msg"
+ )
+ self.generic_visit(node)
+
+ def _check_secure_dir_call(self, node: ast.Call) -> None:
+ """Literal getattr/import_module dodges + chained message checks."""
+ func = node.func
+ # getattr(x, "opt"/"catch"/"logger"/private handles)
+ if (
+ isinstance(func, ast.Name)
+ and func.id == "getattr"
+ and len(node.args) >= 2
+ and isinstance(node.args[1], ast.Constant)
+ and isinstance(node.args[1].value, str)
+ ):
+ attr = node.args[1].value
+ if attr in BANNED_LOGGER_ATTRS:
+ if attr == "patch":
+ reason = (
+ 'patchers can mutate record["exception"] and '
+ "reattach tracebacks ungated"
+ )
+ else:
+ reason = "it reaches raw loguru handles"
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f'getattr(..., "{attr}") is banned in secure-logging '
+ f"dirs — {reason}"
+ )
+ elif attr in BANNED_TRACEBACK_ATTRS:
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f'getattr(..., "{attr}") produces raw traceback text — '
+ f"banned in secure-logging dirs"
+ )
+ elif attr == "exc_info" and self._expr_is_sys_module(node.args[0]):
+ receiver = (
+ node.args[0].id
+ if isinstance(node.args[0], ast.Name)
+ else "sys"
+ )
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f'getattr({receiver}, "exc_info") is banned in secure-logging '
+ f"dirs — tracebacks must never be interpolated into "
+ f"log messages"
+ )
+
+ # importlib.import_module("loguru"/"traceback"), __import__(...)
+ dynamic_import_module = (
+ node.args[0].value
+ if node.args and isinstance(node.args[0], ast.Constant)
+ else None
+ )
+ if dynamic_import_module in BANNED_DYNAMIC_IMPORTS and (
+ self._is_literal_dynamic_import(node, dynamic_import_module)
+ ):
+ self.errors.append(
+ f"{self.filename}:{node.lineno}: "
+ f'dynamic import of "{dynamic_import_module}" is banned in '
+ f"secure-logging dirs — {WRAPPER_IMPORT_HINT}"
+ )
+
+ # logger.bind(...) chains are wrapper-preserving, but the message is
+ # production-visible — run the exception-variable check.
+ if (
+ isinstance(func, ast.Attribute)
+ and func.attr
+ in {"info", "warning", "error", "critical", "exception", "log"}
+ and self._is_wrapper_chain(func.value)
+ ):
+ self._check_exception_var_in_log(node)
+
+ def _is_wrapper_chain(self, expr: ast.AST) -> bool:
+ """True for bind() call chains rooted at the name 'logger'."""
+ if not isinstance(expr, ast.Call):
+ return False
+ while (
+ isinstance(expr, ast.Call)
+ and isinstance(expr.func, ast.Attribute)
+ and expr.func.attr in WRAPPER_CHAIN_METHODS
+ ):
+ expr = expr.func.value
+ return isinstance(expr, ast.Name) and expr.id == "logger"
def visit_Call(self, node: ast.Call) -> None:
"""Check function calls for logging sensitive data."""
@@ -153,6 +770,9 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
self._check_exc_info_on_warning(node)
self._check_exception_var_in_log(node)
+ if self.in_secure_dir:
+ self._check_secure_dir_call(node)
+
self.generic_visit(node)
def _is_logger_call(self, node: ast.Call) -> bool:
@@ -228,10 +848,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
# Plain loguru logger.exception() output is dev-only — but in the
# secure_logging dirs the wrapper makes the message production-visible
# at ERROR, so the exception variable must not be interpolated there.
- in_secure_dir = any(
- d in self.filename.replace("\\", "/") for d in SECURE_LOGGING_DIRS
- )
- if level == "exception" and not in_secure_dir:
+ if level == "exception" and not self.in_secure_dir:
return
# Collect current except variable names
@@ -288,10 +905,34 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
for v in expr.values
)
if isinstance(expr, ast.Call):
- # str(e), repr(e), type(e), etc.
- return any(
- self._expr_references_vars(a, var_names) for a in expr.args
+ # str(e), repr(e), "...".format(x=e), str(e).upper(), etc. —
+ # the exception can hide in the receiver (func) and in keywords,
+ # not just positional args.
+ return (
+ self._expr_references_vars(expr.func, var_names)
+ or any(
+ self._expr_references_vars(a, var_names) for a in expr.args
+ )
+ or any(
+ self._expr_references_vars(kw.value, var_names)
+ for kw in expr.keywords
+ )
)
+ if isinstance(expr, ast.Attribute):
+ # e.args, e.strerror, e.response.text, e.__cause__ carry the
+ # same error detail as str(e). Allowed: the exception *class
+ # name* — type(e).__name__ / e.__class__.__name__ — which is
+ # the sanctioned pattern for scrubbed log lines.
+ if expr.attr == "__name__" and self._is_exc_class_ref(
+ expr.value, var_names
+ ):
+ return False
+ return self._expr_references_vars(expr.value, var_names)
+ if isinstance(expr, ast.Subscript):
+ # e.args[0], e["detail"]
+ return self._expr_references_vars(
+ expr.value, var_names
+ ) or self._expr_references_vars(expr.slice, var_names)
if isinstance(expr, ast.BinOp):
# "message: %s" % e
return self._expr_references_vars(
@@ -304,6 +945,20 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
)
return False
+ def _is_exc_class_ref(self, expr: ast.AST, var_names: set) -> bool:
+ """True for type(e) or e.__class__ over an exception variable."""
+ if (
+ isinstance(expr, ast.Call)
+ and isinstance(expr.func, ast.Name)
+ and expr.func.id == "type"
+ and len(expr.args) == 1
+ and not expr.keywords
+ ):
+ return self._expr_references_vars(expr.args[0], var_names)
+ if isinstance(expr, ast.Attribute) and expr.attr == "__class__":
+ return self._expr_references_vars(expr.value, var_names)
+ return False
+
def _is_allowed_logging(self, variable_name: str) -> bool:
"""Check if this specific logging is allowed."""
for allowed in ALLOWED_LOGGING:
diff --git a/.pre-commit-hooks/custom-checks.py b/.pre-commit-hooks/custom-checks.py
index 82dda8234c..3b3f56dceb 100755
--- a/.pre-commit-hooks/custom-checks.py
+++ b/.pre-commit-hooks/custom-checks.py
@@ -19,6 +19,16 @@ from typing import List, Tuple
# Set environment variable for pre-commit hooks to allow unencrypted databases
os.environ["LDR_ALLOW_UNENCRYPTED"] = "true"
+# Dirs where logger.exception() routes through the diagnose-gated
+# security.secure_logging wrapper (#4183): the message is production-visible
+# at ERROR there, so the "automatically includes exception details" advice
+# is wrong — keep in sync with check-sensitive-logging.py.
+SECURE_LOGGING_DIRS = (
+ "src/local_deep_research/llm/providers/",
+ "src/local_deep_research/embeddings/providers/",
+ "src/local_deep_research/web_search_engines/",
+)
+
class CustomCodeChecker(ast.NodeVisitor):
EXCEPTION_VAR_NAMES = {"e", "ex", "exc", "exception", "err", "error"}
@@ -119,6 +129,30 @@ class CustomCodeChecker(ast.NodeVisitor):
isinstance(node, ast.Name) and node.id in self.EXCEPTION_VAR_NAMES
)
+ def _in_secure_logging_dir(self):
+ """True in dirs where logger.exception() is the secure_logging wrapper."""
+ normalized = self.filename.replace("\\", "/")
+ return any(d in normalized for d in SECURE_LOGGING_DIRS)
+
+ def _redundant_exception_msg(self, detail):
+ """Rule-5 message; path-aware since #4183 step 2.
+
+ In SECURE_LOGGING_DIRS the wrapper gates the traceback behind
+ diagnose mode, so "automatically includes exception details" would
+ be wrong there — the message itself is production-visible at ERROR.
+ """
+ if self._in_secure_logging_dir():
+ return (
+ "logger.exception() message is production-visible at ERROR "
+ "here (security.secure_logging wrapper; only the traceback "
+ "is diagnose-gated) — log a scrubbed safe_msg "
+ "(sanitize_error_message/redact_secrets) and " + detail
+ )
+ return (
+ "logger.exception() automatically includes exception details, "
+ + detail
+ )
+
def _format_string_references_exception(self, node):
"""Check if a format string (f-string) contains references to exception variables.
@@ -223,8 +257,9 @@ class CustomCodeChecker(ast.NodeVisitor):
self.errors.append(
(
node.lineno,
- "logger.exception() automatically includes exception details, "
- "remove redundant exception variable from message",
+ self._redundant_exception_msg(
+ "remove redundant exception variable from message"
+ ),
)
)
# Check %-style: logger.exception("..%s..", e)
@@ -234,8 +269,9 @@ class CustomCodeChecker(ast.NodeVisitor):
self.errors.append(
(
node.lineno,
- "logger.exception() automatically includes exception details, "
- "remove redundant exception variable from arguments",
+ self._redundant_exception_msg(
+ "remove redundant exception variable from arguments"
+ ),
)
)
# Check str(e) or repr(e) as argument
@@ -250,8 +286,9 @@ class CustomCodeChecker(ast.NodeVisitor):
self.errors.append(
(
node.lineno,
- "logger.exception() automatically includes exception details, "
- "remove redundant str(e)/repr(e) from arguments",
+ self._redundant_exception_msg(
+ "remove redundant str(e)/repr(e) from arguments"
+ ),
)
)
# Check string concatenation: logger.exception("Error: " + str(e))
@@ -259,8 +296,9 @@ class CustomCodeChecker(ast.NodeVisitor):
self.errors.append(
(
node.lineno,
- "logger.exception() automatically includes exception details, "
- "remove redundant exception variable from concatenation",
+ self._redundant_exception_msg(
+ "remove redundant exception variable from concatenation"
+ ),
)
)
@@ -547,6 +585,11 @@ def main():
print("\n❌ Custom checks failed. Please fix the issues above.")
print("\nGuidelines:")
print("1. Use 'from loguru import logger' instead of standard logging")
+ print(" - Exception: in llm/providers/, embeddings/providers/, and")
+ print(
+ " web_search_engines/, use "
+ "'from ...security.secure_logging import logger' instead"
+ )
print(
"2. Use 'logger.exception()' instead of 'logger.error()' in exception handlers"
)
diff --git a/changelog.d/4976.security.md b/changelog.d/4976.security.md
new file mode 100644
index 0000000000..59d6700a25
--- /dev/null
+++ b/changelog.d/4976.security.md
@@ -0,0 +1 @@
+All LLM provider, embedding provider, and web search engine modules now log through the diagnose-gated `security.secure_logging` wrapper, so exception tracebacks in these paths only appear in logs when diagnose mode is explicitly enabled (`LDR_APP_DEBUG` + `LDR_LOGURU_DIAGNOSE`) — production logs no longer receive full tracebacks that could echo API keys or request internals from provider/engine failures. The `check-sensitive-logging` pre-commit hook now enforces this by construction: raw `loguru` imports and every known bypass route (`logger.opt()`, `@logger.catch`, private logger handles, `traceback`/`sys.exc_info()` interpolation, dynamic loguru/traceback imports, logger rebinding, and `getattr` dodges) are rejected in these directories, `bind()`/`patch()` chain messages get the same exception-scrubbing checks as direct logger calls, and exception detail reached through attribute or subscript access (`e.args`, `e.response.text`, `e.strerror`) is flagged like `str(e)` itself.
diff --git a/docs/developing/EXTENDING.md b/docs/developing/EXTENDING.md
index ad3ab743fb..0ec40cf875 100644
--- a/docs/developing/EXTENDING.md
+++ b/docs/developing/EXTENDING.md
@@ -25,7 +25,8 @@ Create a new file in `src/local_deep_research/web_search_engines/engines/`:
from typing import Any, Dict, List, Optional
from langchain_core.language_models import BaseLLM
-from loguru import logger
+
+from ...security.secure_logging import logger
from ..search_engine_base import BaseSearchEngine
@@ -205,12 +206,24 @@ def create_search_engine(engine_name: str, ...) -> BaseSearchEngine:
3. **Handle errors gracefully** - return empty list on failure, don't crash
-4. **Use logging** for debugging:
+4. **Use logging** for debugging — engine/provider dirs must import the
+ diagnose-gated `security.secure_logging` wrapper, never raw loguru
+ (a pre-commit hook enforces this), and error messages must be scrubbed
+ before logging because they are production-visible:
```python
- from loguru import logger
+ from ...security.secure_logging import logger
+
logger.info(f"Searching for: {query}")
- logger.error(f"API error: {e}")
+
+ # In except blocks: never interpolate the raw exception — scrub it first.
+ # (Engines inherit self._scrub_error() from BaseSearchEngine.)
+ except Exception as e:
+ safe_msg = self._scrub_error(e)
+ logger.exception(f"API error: {safe_msg}")
```
+ The hook is syntactic, not data-flow analysis: reviewers should still
+ reject laundering raw exception text through temporary variables such as
+ `msg = str(e); logger.error(msg)` unless the variable came from a scrubber.
5. **Support snippet-only mode** by checking the config:
```python
diff --git a/src/local_deep_research/embeddings/providers/implementations/openai.py b/src/local_deep_research/embeddings/providers/implementations/openai.py
index fa47c97f35..165ca798e5 100644
--- a/src/local_deep_research/embeddings/providers/implementations/openai.py
+++ b/src/local_deep_research/embeddings/providers/implementations/openai.py
@@ -4,7 +4,7 @@ from typing import Any, Dict, List, Optional
from urllib.parse import urlparse
from langchain_core.embeddings import Embeddings
-from loguru import logger
+from ....security.secure_logging import logger
from ....config.thread_settings import get_setting_from_snapshot
from ....security.log_sanitizer import redact_secrets
diff --git a/src/local_deep_research/embeddings/providers/implementations/sentence_transformers.py b/src/local_deep_research/embeddings/providers/implementations/sentence_transformers.py
index afe653a742..2e27ed267e 100644
--- a/src/local_deep_research/embeddings/providers/implementations/sentence_transformers.py
+++ b/src/local_deep_research/embeddings/providers/implementations/sentence_transformers.py
@@ -4,7 +4,7 @@ from pathlib import Path
from typing import Any, Dict, List, Optional
from langchain_core.embeddings import Embeddings
-from loguru import logger
+from ....security.secure_logging import logger
from ....config.thread_settings import get_setting_from_snapshot
from ..base import BaseEmbeddingProvider, Exposure
diff --git a/src/local_deep_research/llm/providers/implementations/__init__.py b/src/local_deep_research/llm/providers/implementations/__init__.py
new file mode 100644
index 0000000000..ef50677285
--- /dev/null
+++ b/src/local_deep_research/llm/providers/implementations/__init__.py
@@ -0,0 +1,6 @@
+"""Concrete LLM provider implementations (auto-discovered).
+
+Modules here are loaded file-by-file by ``auto_discovery.py``, which skips
+``_``-prefixed files including this one. Do not add eager imports of
+provider classes here — several pull heavy optional dependencies.
+"""
diff --git a/src/local_deep_research/llm/providers/implementations/anthropic.py b/src/local_deep_research/llm/providers/implementations/anthropic.py
index 3a42020318..c20b1f6057 100644
--- a/src/local_deep_research/llm/providers/implementations/anthropic.py
+++ b/src/local_deep_research/llm/providers/implementations/anthropic.py
@@ -1,7 +1,7 @@
"""Anthropic LLM provider for Local Deep Research."""
from langchain_anthropic import ChatAnthropic
-from loguru import logger
+from ....security.secure_logging import logger
# get_setting_from_snapshot and NoSettingsContextError are imported inside
# the methods that use them so test patches at the source module
diff --git a/src/local_deep_research/llm/providers/implementations/custom_anthropic_endpoint.py b/src/local_deep_research/llm/providers/implementations/custom_anthropic_endpoint.py
index c0d1a896f4..83446af9e9 100644
--- a/src/local_deep_research/llm/providers/implementations/custom_anthropic_endpoint.py
+++ b/src/local_deep_research/llm/providers/implementations/custom_anthropic_endpoint.py
@@ -6,7 +6,7 @@ Connects to a self-hosted LLM service that speaks the Anthropic Messages API
parent ``AnthropicProvider``) pointed at an operator-configured ``base_url``.
"""
-from loguru import logger
+from ....security.secure_logging import logger
from ....config.thread_settings import get_setting_from_snapshot
from ....security.log_sanitizer import redact_secrets
diff --git a/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py b/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py
index 2a5d3a0c34..500229fff7 100644
--- a/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py
+++ b/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py
@@ -1,6 +1,6 @@
"""Custom OpenAI-compatible endpoint provider for Local Deep Research."""
-from loguru import logger
+from ....security.secure_logging import logger
from ....config.thread_settings import get_setting_from_snapshot
from ....security.log_sanitizer import redact_secrets
diff --git a/src/local_deep_research/llm/providers/implementations/google.py b/src/local_deep_research/llm/providers/implementations/google.py
index 44eb290ce1..4e466edc10 100644
--- a/src/local_deep_research/llm/providers/implementations/google.py
+++ b/src/local_deep_research/llm/providers/implementations/google.py
@@ -1,6 +1,6 @@
"""Google/Gemini LLM provider for Local Deep Research."""
-from loguru import logger
+from ....security.secure_logging import logger
from ....security.log_sanitizer import redact_secrets
from ..base import Exposure
diff --git a/src/local_deep_research/llm/providers/implementations/ollama.py b/src/local_deep_research/llm/providers/implementations/ollama.py
index 64a45df458..2f20ec3a9e 100644
--- a/src/local_deep_research/llm/providers/implementations/ollama.py
+++ b/src/local_deep_research/llm/providers/implementations/ollama.py
@@ -2,12 +2,12 @@
import requests
from langchain_ollama import ChatOllama
-from loguru import logger
from ....config.thread_settings import get_setting_from_snapshot
-from ....utilities.url_utils import normalize_url
from ....security import safe_get
+from ....security.secure_logging import logger
from ....security.ssrf_validator import assert_base_url_safe
+from ....utilities.url_utils import normalize_url
from ..base import BaseLLMProvider, Exposure
diff --git a/src/local_deep_research/llm/providers/implementations/openai.py b/src/local_deep_research/llm/providers/implementations/openai.py
index b126ada401..f6c4b0f4c3 100644
--- a/src/local_deep_research/llm/providers/implementations/openai.py
+++ b/src/local_deep_research/llm/providers/implementations/openai.py
@@ -1,7 +1,7 @@
"""OpenAI LLM provider for Local Deep Research."""
from langchain_openai import ChatOpenAI
-from loguru import logger
+from ....security.secure_logging import logger
# get_setting_from_snapshot and NoSettingsContextError are imported inside
# create_llm() so test patches at the source module
diff --git a/src/local_deep_research/llm/providers/openai_base.py b/src/local_deep_research/llm/providers/openai_base.py
index 022170bccf..7422b4839a 100644
--- a/src/local_deep_research/llm/providers/openai_base.py
+++ b/src/local_deep_research/llm/providers/openai_base.py
@@ -1,7 +1,7 @@
"""Base OpenAI-compatible endpoint provider for Local Deep Research."""
from langchain_openai import ChatOpenAI
-from loguru import logger
+from ...security.secure_logging import logger
# get_setting_from_snapshot and NoSettingsContextError are imported inside
# the methods that use them so test patches at the source module
diff --git a/src/local_deep_research/news/recommender/topic_based.py b/src/local_deep_research/news/recommender/topic_based.py
index 80b2dc4a8b..25a7f8e02c 100644
--- a/src/local_deep_research/news/recommender/topic_based.py
+++ b/src/local_deep_research/news/recommender/topic_based.py
@@ -246,10 +246,12 @@ class TopicBasedRecommender(BaseRecommender):
# The egress policy refused the LLM (no snapshot in this
# background path + non-local provider). Recurring
# scheduler — log concisely, do not retry.
+ # Decision.reason is a short machine code, safe to log.
+ denial_reason = exc.decision.reason
logger.warning(
f"Skipping news recommendation for topic "
f"'{sanitize_for_log(topic)}': "
- f"LLM blocked by egress policy ({exc.decision.reason}). "
+ f"LLM blocked by egress policy ({denial_reason}). "
"Use a local provider (ollama/lmstudio/llamacpp) or "
"thread settings_snapshot through this caller."
)
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_brave.py b/src/local_deep_research/web_search_engines/engines/search_engine_brave.py
index c1477f4fe4..77c6c62a16 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_brave.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_brave.py
@@ -2,7 +2,8 @@ from typing import Any, Dict, List, Optional
from langchain_community.tools import BraveSearch
from langchain_core.language_models import BaseLLM
-from loguru import logger
+
+from ...security.secure_logging import logger
from ..rate_limiting import RateLimitError
from ..search_engine_base import BaseSearchEngine, Exposure, Sensitivity
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_elasticsearch.py b/src/local_deep_research/web_search_engines/engines/search_engine_elasticsearch.py
index 72ef711a94..f39b863ac2 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_elasticsearch.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_elasticsearch.py
@@ -1,4 +1,4 @@
-from loguru import logger
+from ...security.secure_logging import logger
from typing import Any, Dict, List, Optional
from elasticsearch import Elasticsearch
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_exa.py b/src/local_deep_research/web_search_engines/engines/search_engine_exa.py
index f371c130fb..61b6fc779c 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_exa.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_exa.py
@@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional
import requests
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from ...security.safe_requests import safe_post
from ..rate_limiting import RateLimitError
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_github.py b/src/local_deep_research/web_search_engines/engines/search_engine_github.py
index 74e5f7341c..cd5807458c 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_github.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_github.py
@@ -4,7 +4,7 @@ import time
from typing import Any, Dict, List, Optional
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from ...config import llm_config
from ...constants import USER_AGENT
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_google_pse.py b/src/local_deep_research/web_search_engines/engines/search_engine_google_pse.py
index 4698b99f7c..7ab4cc9d75 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_google_pse.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_google_pse.py
@@ -1,4 +1,4 @@
-from loguru import logger
+from ...security.secure_logging import logger
import random
import time
from typing import Any, Dict, List, Optional
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_guardian.py b/src/local_deep_research/web_search_engines/engines/search_engine_guardian.py
index 90f2fca4df..18d4a03657 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_guardian.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_guardian.py
@@ -2,7 +2,7 @@ from datetime import datetime, timedelta, UTC
from typing import Any, Dict, List, Optional, Tuple
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from ...utilities.search_utilities import remove_think_tags
from ...security.safe_requests import safe_get
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_nasa_ads.py b/src/local_deep_research/web_search_engines/engines/search_engine_nasa_ads.py
index 8742b2a3b4..9cd8ee0cd7 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_nasa_ads.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_nasa_ads.py
@@ -3,7 +3,7 @@
from typing import Any, Dict, List, Optional
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from ...constants import SNIPPET_LENGTH_LONG, USER_AGENT
from ...security.safe_requests import safe_get
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_paperless.py b/src/local_deep_research/web_search_engines/engines/search_engine_paperless.py
index 92379a63c7..2a3664dc8e 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_paperless.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_paperless.py
@@ -12,7 +12,7 @@ import requests
from urllib.parse import urljoin
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from ..search_engine_base import BaseSearchEngine, Exposure, Sensitivity
from ...security import redact_url_for_log, safe_get
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_scaleserp.py b/src/local_deep_research/web_search_engines/engines/search_engine_scaleserp.py
index 7fb08bc9db..f71b0efa95 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_scaleserp.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_scaleserp.py
@@ -1,4 +1,4 @@
-from loguru import logger
+from ...security.secure_logging import logger
from typing import Any, Dict, List, Optional
import requests
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py b/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py
index 0b6e2b602d..b4affdded5 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_semantic_scholar.py
@@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional, Tuple
import requests
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_serpapi.py b/src/local_deep_research/web_search_engines/engines/search_engine_serpapi.py
index 44206b2ad2..1d1206d826 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_serpapi.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_serpapi.py
@@ -1,4 +1,4 @@
-from loguru import logger
+from ...security.secure_logging import logger
from typing import Any, Dict, List, Optional
from langchain_community.utilities import SerpAPIWrapper
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_serper.py b/src/local_deep_research/web_search_engines/engines/search_engine_serper.py
index dfe6b90559..1b2b83bfb1 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_serper.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_serper.py
@@ -1,4 +1,4 @@
-from loguru import logger
+from ...security.secure_logging import logger
from typing import Any, Dict, List, Optional
import requests
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_tavily.py b/src/local_deep_research/web_search_engines/engines/search_engine_tavily.py
index 0d70dd9c73..2c54a77cb4 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_tavily.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_tavily.py
@@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional
import requests
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ...security.secure_logging import logger
from ...security.safe_requests import safe_post
from ..rate_limiting import RateLimitError
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_wikipedia.py b/src/local_deep_research/web_search_engines/engines/search_engine_wikipedia.py
index b45aaa61b5..652c656c46 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_wikipedia.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_wikipedia.py
@@ -6,6 +6,7 @@ import wikipedia
from langchain_core.language_models import BaseLLM
from ..search_engine_base import BaseSearchEngine, Exposure, Sensitivity
+from ...security import sanitize_for_log
from ...security.secure_logging import logger
@@ -88,9 +89,13 @@ class WikipediaSearchEngine(BaseSearchEngine):
# Get search results (just titles)
search_results = wikipedia.search(query, results=self.max_results)
+ safe_search_results = [
+ sanitize_for_log(title) for title in search_results
+ ]
logger.info(
- f"Found {len(search_results)} Wikipedia results: {search_results}"
+ f"Found {len(search_results)} Wikipedia results: "
+ f"{safe_search_results}"
)
if not search_results:
@@ -109,6 +114,7 @@ class WikipediaSearchEngine(BaseSearchEngine):
# completion order would cause the LLM to select wrong articles.
previews = []
for title in search_results:
+ safe_title = sanitize_for_log(title)
try:
# Get just the summary, with auto_suggest=False to be more precise
summary = None
@@ -124,16 +130,21 @@ class WikipediaSearchEngine(BaseSearchEngine):
except wikipedia.exceptions.DisambiguationError as e:
# If disambiguation error, try the first option
if e.options and len(e.options) > 0:
+ # Page titles are external content; sanitize
+ # before interpolation to prevent log injection.
+ first_option = e.options[0]
+ safe_first_option = sanitize_for_log(first_option)
logger.info(
- f"Disambiguation for '{title}', trying first option: {e.options[0]}"
+ f"Disambiguation for '{safe_title}', trying "
+ f"first option: {safe_first_option}"
)
try:
summary = wikipedia.summary(
- e.options[0],
+ first_option,
sentences=self.sentences,
auto_suggest=False,
)
- title = e.options[0] # Use the new title
+ title = first_option # Use the new title
except Exception as inner_e:
safe_msg = self._scrub_error(inner_e)
logger.exception(
@@ -142,7 +153,7 @@ class WikipediaSearchEngine(BaseSearchEngine):
continue
else:
logger.warning(
- f"Disambiguation with no options for '{title}'"
+ f"Disambiguation with no options for '{safe_title}'"
)
continue
@@ -162,7 +173,7 @@ class WikipediaSearchEngine(BaseSearchEngine):
wikipedia.exceptions.WikipediaException,
):
# Skip pages with errors
- logger.warning(f"Error getting summary for '{title}'")
+ logger.warning(f"Error getting summary for '{safe_title}'")
continue
except _TRANSIENT_DECODE_ERRORS:
# MediaWiki almost certainly returned a 429 (or other
@@ -172,14 +183,15 @@ class WikipediaSearchEngine(BaseSearchEngine):
logger.warning(
"Wikipedia rate-limited (non-JSON response while "
"fetching '{}'); returning {} previews collected so far",
- title,
+ safe_title,
len(previews),
)
break
except Exception as e:
safe_msg = self._scrub_error(e)
logger.exception(
- f"Unexpected error for '{title}' ({type(e).__name__}): {safe_msg}"
+ f"Unexpected error for '{safe_title}' "
+ f"({type(e).__name__}): {safe_msg}"
)
continue
@@ -228,6 +240,7 @@ class WikipediaSearchEngine(BaseSearchEngine):
results.append(item)
continue
+ safe_title = sanitize_for_log(str(title))
try:
# Apply rate limiting before page request
self._last_wait_time = self.rate_tracker.apply_rate_limit(
@@ -262,12 +275,13 @@ class WikipediaSearchEngine(BaseSearchEngine):
wikipedia.exceptions.WikipediaException,
):
# If error, use the preview
- logger.warning(f"Error getting full content for '{title}'")
+ logger.warning(f"Error getting full content for '{safe_title}'")
results.append(item)
except Exception as e:
safe_msg = self._scrub_error(e)
logger.exception(
- f"Unexpected error getting full content for '{title}' ({type(e).__name__}): {safe_msg}"
+ f"Unexpected error getting full content for "
+ f"'{safe_title}' ({type(e).__name__}): {safe_msg}"
)
results.append(item)
diff --git a/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py b/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py
index 7ee251128a..7dcceeb71a 100644
--- a/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py
+++ b/src/local_deep_research/web_search_engines/rate_limiting/llm/detection.py
@@ -2,7 +2,7 @@
LLM-specific rate limit error detection.
"""
-from loguru import logger
+from ....security.secure_logging import logger
def is_llm_rate_limit_error(error: BaseException) -> bool:
diff --git a/src/local_deep_research/web_search_engines/rate_limiting/llm/wrapper.py b/src/local_deep_research/web_search_engines/rate_limiting/llm/wrapper.py
index ae40af16eb..35595d4f1c 100644
--- a/src/local_deep_research/web_search_engines/rate_limiting/llm/wrapper.py
+++ b/src/local_deep_research/web_search_engines/rate_limiting/llm/wrapper.py
@@ -5,7 +5,7 @@ Rate-limited wrapper for LLM calls.
from typing import Optional
from urllib.parse import urlparse
-from loguru import logger
+from ....security.secure_logging import logger
from tenacity import (
retry,
stop_after_attempt,
diff --git a/src/local_deep_research/web_search_engines/rate_limiting/tracker.py b/src/local_deep_research/web_search_engines/rate_limiting/tracker.py
index 7c8333a894..0390da3f7e 100644
--- a/src/local_deep_research/web_search_engines/rate_limiting/tracker.py
+++ b/src/local_deep_research/web_search_engines/rate_limiting/tracker.py
@@ -12,7 +12,7 @@ from cachetools import LRUCache
if TYPE_CHECKING:
from sqlalchemy.orm import Session
-from loguru import logger
+from ...security.secure_logging import logger
from ...config.thread_settings import (
NoSettingsContextError,
diff --git a/src/local_deep_research/web_search_engines/retriever_registry.py b/src/local_deep_research/web_search_engines/retriever_registry.py
index 19f178e06f..bc443cb679 100644
--- a/src/local_deep_research/web_search_engines/retriever_registry.py
+++ b/src/local_deep_research/web_search_engines/retriever_registry.py
@@ -6,7 +6,7 @@ from threading import Lock
from typing import Dict, Optional
from langchain_core.retrievers import BaseRetriever
-from loguru import logger
+from ..security.secure_logging import logger
class RetrieverRegistry:
diff --git a/src/local_deep_research/web_search_engines/search_engine_base.py b/src/local_deep_research/web_search_engines/search_engine_base.py
index 06415a1a4a..998444297b 100644
--- a/src/local_deep_research/web_search_engines/search_engine_base.py
+++ b/src/local_deep_research/web_search_engines/search_engine_base.py
@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Union
from urllib.parse import urlparse
from langchain_core.language_models import BaseLLM
-from loguru import logger
+from ..security.secure_logging import logger
from tenacity import (
RetryError,
retry,
@@ -1154,10 +1154,11 @@ class BaseSearchEngine(ABC):
self.settings_snapshot, primary_raw or DEFAULT_SEARCH_TOOL
)
except (PolicyDeniedError, ValueError) as exc:
+ safe_msg = self._scrub_error(exc)
logger.bind(policy_audit=True).warning(
"Disabling include_full_content: egress policy could "
"not be evaluated for this engine",
- reason=str(exc),
+ reason=safe_msg,
)
self.include_full_content = False
return None
diff --git a/src/local_deep_research/web_search_engines/search_engines_config.py b/src/local_deep_research/web_search_engines/search_engines_config.py
index 4068121270..0aa9ea5118 100644
--- a/src/local_deep_research/web_search_engines/search_engines_config.py
+++ b/src/local_deep_research/web_search_engines/search_engines_config.py
@@ -6,7 +6,7 @@ Loads search engine definitions from the user's configuration.
from typing import Any, Dict, Optional
from sqlalchemy.orm import Session
-from loguru import logger
+from ..security.secure_logging import logger
from ..config.thread_settings import get_setting_from_snapshot
from ..utilities.db_utils import get_settings_manager
diff --git a/tests/search_engines/test_search_engine_wikipedia.py b/tests/search_engines/test_search_engine_wikipedia.py
index 2a45252ee0..6125b1c8a1 100644
--- a/tests/search_engines/test_search_engine_wikipedia.py
+++ b/tests/search_engines/test_search_engine_wikipedia.py
@@ -121,6 +121,42 @@ class TestWikipediaSearchEngine:
or "planet" in previews[0]["title"].lower()
)
+ @patch("wikipedia.search")
+ @patch("wikipedia.summary")
+ def test_get_previews_disambiguation_log_sanitizes_titles(
+ self, mock_summary, mock_search, loguru_caplog
+ ):
+ """Disambiguation logs must not interpolate raw external titles."""
+ import wikipedia
+
+ from local_deep_research.web_search_engines.engines.search_engine_wikipedia import (
+ WikipediaSearchEngine,
+ )
+
+ raw_title = "Mercury\nInjected"
+ raw_option = "Mercury (planet)\nInjected"
+ mock_search.return_value = [raw_title]
+ disambiguation_error = wikipedia.exceptions.DisambiguationError(
+ raw_title, [raw_option]
+ )
+ mock_summary.side_effect = [
+ disambiguation_error,
+ "Mercury is the closest planet to the Sun.",
+ ]
+
+ with patch("wikipedia.set_lang"):
+ engine = WikipediaSearchEngine()
+ with loguru_caplog.at_level("INFO"):
+ previews = engine._get_previews("Mercury")
+
+ assert len(previews) == 1
+ assert raw_title not in loguru_caplog.text
+ assert raw_option not in loguru_caplog.text
+ assert "Mercury\\nInjected" not in loguru_caplog.text
+ assert "Mercury (planet)\\nInjected" not in loguru_caplog.text
+ assert "MercuryInjected" in loguru_caplog.text
+ assert "Mercury (planet)Injected" in loguru_caplog.text
+
@patch("wikipedia.page")
def test_get_full_content(self, mock_page):
"""Test full content retrieval."""
diff --git a/tests/security/test_sensitive_logging_hook.py b/tests/security/test_sensitive_logging_hook.py
index ca5c77ef1a..af47dc4edd 100644
--- a/tests/security/test_sensitive_logging_hook.py
+++ b/tests/security/test_sensitive_logging_hook.py
@@ -181,3 +181,814 @@ class TestAllowsSafePatterns:
# Tests may have relaxed rules for specific vars
# (depends on ALLOWED_LOGGING config)
_check_code(code, filename="tests/test_auth.py")
+
+
+# ---------------------------------------------------------------------------
+# #4183 step 2: wrapper-import enforcement + bypass-route detection in the
+# secure-logging dirs (llm/providers/, embeddings/providers/,
+# web_search_engines/).
+# ---------------------------------------------------------------------------
+
+SECURE_LOGGING_DIRS = hook_module.SECURE_LOGGING_DIRS
+
+# Representative in-dir filenames (one per protected dir) and an unprotected
+# control path.
+IN_DIR = "src/local_deep_research/web_search_engines/engines/search_engine_x.py"
+IN_DIR_LLM = "src/local_deep_research/llm/providers/implementations/x.py"
+IN_DIR_EMB = "src/local_deep_research/embeddings/providers/implementations/x.py"
+OUT_DIR = "src/local_deep_research/web/routes/x.py"
+
+WRAPPER_IMPORT = "from ...security.secure_logging import logger\n"
+
+
+class TestWrapperImportEnforcement:
+ """Only the secure_logging wrapper may provide 'logger' in secure dirs."""
+
+ def test_raw_loguru_flagged_in_each_secure_dir(self):
+ for filename in (IN_DIR, IN_DIR_LLM, IN_DIR_EMB):
+ errors = _check_code("from loguru import logger\n", filename)
+ assert any("raw loguru" in e for e in errors), filename
+
+ def test_plain_and_aliased_loguru_import_flagged(self):
+ assert _check_code("import loguru\n", IN_DIR)
+ assert _check_code("import loguru as lg\n", IN_DIR)
+ assert _check_code("from loguru import logger as log\n", IN_DIR)
+
+ def test_raw_loguru_not_flagged_outside_secure_dirs(self):
+ assert _check_code("from loguru import logger\n", OUT_DIR) == []
+ assert _check_code("import loguru\n", OUT_DIR) == []
+
+ def test_wrapper_import_allowed(self):
+ assert _check_code(WRAPPER_IMPORT, IN_DIR) == []
+ assert (
+ _check_code(
+ "from local_deep_research.security.secure_logging import logger\n",
+ IN_DIR,
+ )
+ == []
+ )
+
+ def test_wrapper_import_aliased_away_from_logger_flagged(self):
+ errors = _check_code(
+ "from ...security.secure_logging import logger as log\n", IN_DIR
+ )
+ assert any("aliasing" in e for e in errors)
+
+ def test_non_wrapper_logger_imports_flagged(self):
+ for code in (
+ "from local_deep_research import logger\n",
+ "from ...utilities.log_utils import logger\n",
+ "from helper import logger\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_suffix_lookalike_module_is_not_the_wrapper(self):
+ errors = _check_code(
+ "from ...notsecurity.secure_logging import logger\n", IN_DIR
+ )
+ assert any("other than" in e for e in errors)
+
+ def test_secure_logging_internals_flagged(self):
+ for code in (
+ "from ...security.secure_logging import _loguru_logger\n",
+ "from ...security.secure_logging import SecureLogger\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_secure_logging_module_import_flagged(self):
+ """v4 headline bypass: module import reaches _loguru_logger."""
+ for code in (
+ "from ...security import secure_logging\n",
+ "from local_deep_research.security import secure_logging\n",
+ "import local_deep_research.security.secure_logging\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any("secure_logging module" in e for e in errors), code
+
+ def test_log_utils_imports_flagged_in_all_forms(self):
+ """v5 headline bypass: log_utils re-exports the raw loguru logger."""
+ for code in (
+ "from ...utilities.log_utils import logger\n",
+ "from ...utilities.log_utils import config_logger\n",
+ "from ...utilities import log_utils\n",
+ "from local_deep_research.utilities.log_utils import config_logger\n",
+ "from local_deep_research.utilities import log_utils\n",
+ "import local_deep_research.utilities.log_utils as lu\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+ assert (
+ _check_code("from ...utilities import log_utils\n", OUT_DIR) == []
+ )
+
+ def test_absolute_self_package_import_flagged(self):
+ for code in (
+ "import local_deep_research\n",
+ "import local_deep_research.web\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any("root package" in e for e in errors), code
+ assert _check_code("import local_deep_research\n", OUT_DIR) == []
+
+ def test_traceback_imports_flagged(self):
+ for code in (
+ "import traceback\n",
+ "from traceback import format_exc\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any("traceback" in e for e in errors), code
+ assert _check_code("import traceback\n", OUT_DIR) == []
+
+ def test_function_local_import_flagged(self):
+ code = "def f():\n from loguru import logger\n"
+ assert _check_code(code, IN_DIR)
+
+ def test_dynamic_loguru_import_flagged(self):
+ code = "import importlib\nx = importlib.import_module('loguru')\n"
+ errors = _check_code(code, IN_DIR)
+ assert any("dynamic import" in e for e in errors)
+
+ def test_dynamic_import_of_other_modules_allowed(self):
+ # The real auto_discovery.py pattern: f-string first arg, not "loguru"
+ code = (
+ "import importlib\n"
+ "m = importlib.import_module(\n"
+ " f'.implementations.{module_name}', package='pkg'\n"
+ ")\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+
+class TestLoggerRebindingBans:
+ """The local name 'logger' is reserved for the wrapper import."""
+
+ def test_assignment_rebinding_flagged(self):
+ for code in (
+ "logger = get_raw()\n",
+ "logger, x = a(), None\n",
+ "[logger, x] = pair\n",
+ "logger: object = get_raw()\n",
+ "(logger := get_raw())\n",
+ "logger += 1\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_control_flow_shadowing_flagged(self):
+ for code in (
+ "for logger in xs:\n pass\n",
+ "with open('f') as logger:\n pass\n",
+ "try:\n f()\nexcept Exception as logger:\n pass\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_parameters_named_logger_flagged(self):
+ for code in (
+ "def f(logger):\n pass\n",
+ "async def f(logger):\n pass\n",
+ "g = lambda logger: 1\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_import_binding_logger_flagged(self):
+ for code in (
+ "import foo as logger\n",
+ "from foo import bar as logger\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_deriving_new_name_from_logger_flagged(self):
+ for code in (
+ "log = logger\n",
+ "log = logger.bind(x=1)\n",
+ "log = pkg.logger.bind(x=1)\n",
+ "log = pkg.logger.patch(f).bind(x=1)\n",
+ "x = log_utils.logger\n",
+ "x = ldr.logger\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_ordinary_assignments_allowed(self):
+ for code in (
+ "results = engine.run(q)\n",
+ "log = get_audit_trail()\n",
+ "safe_msg = scrub(e)\n",
+ ):
+ assert _check_code(code, IN_DIR) == [], code
+
+
+class TestBypassRouteDetection:
+ """.opt/.catch/.patch/.logger/private handles/sys.exc_info are banned."""
+
+ def test_opt_flagged_in_all_forms(self):
+ for code in (
+ "logger.opt(exception=True).error('x')\n",
+ "logger.opt(**kwargs).error('x')\n",
+ "raw = logger.opt(lazy=True)\n",
+ "logger.bind(x=1).opt(depth=1)\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any(".opt" in e for e in errors), code
+
+ def test_opt_not_flagged_outside_secure_dirs(self):
+ assert (
+ _check_code("logger.opt(exception=True).error('x')\n", OUT_DIR)
+ == []
+ )
+
+ def test_catch_flagged_in_all_forms(self):
+ for code in (
+ "@logger.catch\ndef f():\n pass\n",
+ "with logger.catch():\n pass\n",
+ "h = logger.catch\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any(".catch" in e for e in errors), code
+
+ def test_catch_decorator_with_args_reports_exactly_once(self):
+ errors = _check_code(
+ "@logger.catch(reraise=True)\ndef f():\n pass\n", IN_DIR
+ )
+ assert len(errors) == 1
+
+ def test_patch_flagged_in_all_forms(self):
+ for code in (
+ "logger.patch(lambda record: None).error('x')\n",
+ "logger.bind(x=1).patch(lambda record: None).error('x')\n",
+ "raw = logger.patch(lambda record: None)\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any(".patch" in e for e in errors), code
+
+ def test_getattr_dodges_flagged(self):
+ for attr in (
+ "opt",
+ "catch",
+ "patch",
+ "logger",
+ "_logger",
+ "_loguru_logger",
+ "__getattr__",
+ ):
+ code = f"getattr(obj, '{attr}')\n"
+ errors = _check_code(code, IN_DIR)
+ assert any("getattr" in e for e in errors), attr
+
+ def test_logger_attribute_access_flagged(self):
+ for code in (
+ "something.logger.exception('x')\n",
+ "x = pkg.logger\n",
+ "f(log_utils.logger)\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any(".logger" in e for e in errors), code
+
+ def test_logger_attribute_assignment_reports_once(self):
+ errors = _check_code("x = pkg.logger\n", IN_DIR)
+ assert len(errors) == 1
+ assert ".logger" in errors[0]
+
+ def test_private_handle_access_flagged(self):
+ for code in (
+ "logger._logger\n",
+ "secure_logging._loguru_logger\n",
+ "logger.__getattr__('exception')\n",
+ ):
+ assert _check_code(code, IN_DIR), code
+
+ def test_sys_exc_info_flagged(self):
+ for code in (
+ "import sys\nx = sys.exc_info()\n",
+ "import sys as s\nx = s.exc_info()\n",
+ "import sys.monitoring\nx = sys.exc_info()\n",
+ "import sys\nx = getattr(sys, 'exc_info')\n",
+ "import sys as s\nx = getattr(s, 'exc_info')\n",
+ "from sys import exc_info\nx = exc_info()\n",
+ "from sys import exc_info as get_exc_info\nx = get_exc_info()\n",
+ "from sys import *\nx = exc_info()\n", # noqa: F403
+ "x = __import__('sys').exc_info()\n",
+ "x = getattr(__import__('sys'), 'exc_info')()\n",
+ "import importlib\nx = importlib.import_module('sys').exc_info()\n",
+ "from importlib import import_module\nx = import_module('sys').exc_info()\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any("exc_info" in e for e in errors), code
+
+ def test_sys_alias_does_not_leak_between_function_scopes(self):
+ code = (
+ "def f():\n"
+ " import sys as s\n"
+ " return s.exc_info()\n"
+ "\n"
+ "def g(s):\n"
+ " return s.exc_info()\n"
+ )
+ errors = [e for e in _check_code(code, IN_DIR) if "exc_info" in e]
+ assert len(errors) == 1
+
+ def test_sys_alias_shadowing_shapes_not_flagged(self):
+ cases = [
+ (
+ "import sys as s\n"
+ "def g():\n"
+ " s = obj\n"
+ " return s.exc_info()\n"
+ ),
+ (
+ "import sys as s\n"
+ "def g():\n"
+ " import something as s\n"
+ " return s.exc_info()\n"
+ ),
+ (
+ "import sys as s\n"
+ "def g():\n"
+ " try:\n"
+ " raise RuntimeError()\n"
+ " except Exception as s:\n"
+ " return s.exc_info()\n"
+ ),
+ (
+ "class C:\n"
+ " import sys as s\n"
+ " def m(self):\n"
+ " return s.exc_info()\n"
+ ),
+ ]
+ for code in cases:
+ errors = [e for e in _check_code(code, IN_DIR) if "exc_info" in e]
+ assert errors == [], code
+
+ def test_non_logger_exception_call_allowed(self):
+ # tenacity pattern from rate_limiting/llm/wrapper.py:34
+ code = "exc = retry_state.outcome.exception()\n"
+ assert _check_code(code, IN_DIR) == []
+
+ def test_dunder_getattr_method_definition_allowed(self):
+ # shape of rate_limiting/llm/wrapper.py's attribute pass-through:
+ # bans target Attribute access / literal getattr, not FunctionDef
+ code = (
+ "class W:\n"
+ " def __getattr__(self, name):\n"
+ " return object.__getattribute__(self, name)\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+
+class TestWrapperChainMessageChecks:
+ """bind() chains keep the wrapper but messages stay checked."""
+
+ def test_bind_chain_with_safe_message_allowed(self):
+ assert _check_code("logger.bind(x=1).exception('safe')\n", IN_DIR) == []
+
+ def test_bind_chain_with_exception_var_flagged(self):
+ for method, args in (
+ ("exception", "f'failed: {e}'"),
+ ("warning", "f'failed: {e}'"),
+ ("error", "f'failed: {e}'"),
+ ("critical", "f'failed: {e}'"),
+ ("info", "f'failed: {e}'"),
+ ("log", "'ERROR', f'failed: {e}'"),
+ ):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ f" logger.bind(x=1).{method}({args})\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), method
+
+ def test_bind_chain_debug_with_exception_var_allowed(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " logger.bind(x=1).debug(f'failed: {e}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_bind_chain_exc_info_true_not_flagged(self):
+ # loguru stores exc_info=True in extra, not record["exception"] —
+ # matches the pre-existing search_engine_elasticsearch.py site.
+ code = "logger.bind(x=1).warning('failed', exc_info=True)\n"
+ assert _check_code(code, IN_DIR) == []
+
+
+class TestExceptionAttributeInterpolation:
+ """Access *through* the exception variable leaks like str(e).
+
+ Adversarial-review finding C1: e.args, e.response.text, e.strerror,
+ e.__cause__, e.args[0] and receiver/keyword shapes evaded
+ _expr_references_vars, which only recursed Name/JoinedStr/Call-args/
+ BinOp/Tuple.
+ """
+
+ def test_attribute_access_flagged(self):
+ for expr in (
+ "f'failed: {e.args}'",
+ "f'disk: {e.strerror}'",
+ "f'API {e.response.status_code}: {e.response.text}'",
+ "f'cause: {e.__cause__}'",
+ ):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ f" logger.error({expr})\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), expr
+
+ def test_subscript_access_flagged(self):
+ for expr in ("f'{e.args[0]}'", "f'{e[\"detail\"]}'"):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ f" logger.error({expr})\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), expr
+
+ def test_receiver_and_keyword_shapes_flagged(self):
+ for expr in (
+ "f'{str(e).upper()}'", # exception hidden in call receiver
+ "'{x}'.format(x=e)", # exception hidden in call keyword
+ "'%s' % e.args", # %-format with attribute
+ "str(e.args)",
+ ):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ f" logger.error({expr})\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), expr
+
+ def test_percent_style_attribute_arg_flagged(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " logger.error('failed %s', e.args)\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors)
+
+ def test_exception_level_attribute_flagged_in_secure_dir(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " logger.exception(f'boom: {e.args}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("production-visible" in e for e in errors)
+
+ def test_exception_class_name_allowed(self):
+ # The sanctioned step-1 pattern: the class name carries no error
+ # detail. Both spellings, incl. combined with a scrubbed message —
+ # the exact shape used by search_engine_wayback.py et al.
+ for expr in (
+ "f'{type(e).__name__}'",
+ "f'{e.__class__.__name__}'",
+ "f'failed ({type(e).__name__}): {safe_msg}'",
+ ):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " safe_msg = sanitize_error_message(str(e))\n"
+ f" logger.error({expr})\n"
+ )
+ assert _check_code(code, IN_DIR) == [], expr
+
+ def test_bare_type_call_still_flagged(self):
+ # type(e) without .__name__ stringifies via repr — keep flagging it
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " logger.error(f'{type(e)}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors)
+
+ def test_debug_level_attribute_allowed(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " logger.debug(f'{e.args}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+
+class TestTracebackDynamicRoutes:
+ """Adversarial-review finding M1: dynamic imports of traceback and
+ traceback-formatting attributes must be banned like the static forms."""
+
+ def test_dynamic_import_of_traceback_flagged(self):
+ for code in (
+ "tb = importlib.import_module('traceback')\n",
+ "tb = import_module('traceback')\n",
+ "tb = __import__('traceback')\n",
+ "raw = __import__('loguru')\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any("dynamic import" in e for e in errors), code
+
+ def test_dynamic_import_of_other_modules_allowed(self):
+ # auto_discovery.py's f-string import_module and constant imports of
+ # ordinary modules must stay clean
+ for code in (
+ "m = importlib.import_module(f'.implementations.{name}', pkg)\n",
+ "m = importlib.import_module('json')\n",
+ "m = __import__('os')\n",
+ ):
+ assert _check_code(code, IN_DIR) == [], code
+
+ def test_format_exc_attribute_flagged(self):
+ for code in (
+ "logger.error('boom: ' + tb.format_exc())\n",
+ "x = tb.format_exception(e)\n",
+ "x = tb.format_exception_only(e)\n",
+ "x = tb.format_tb(e.__traceback__)\n",
+ "tb.print_exc()\n",
+ ):
+ errors = _check_code(code, IN_DIR)
+ assert any("traceback text" in e for e in errors), code
+
+ def test_format_exc_getattr_flagged(self):
+ errors = _check_code("f = getattr(tb, 'format_exc')\n", IN_DIR)
+ assert any("traceback text" in e for e in errors)
+
+ def test_traceback_routes_not_flagged_outside_secure_dirs(self):
+ for code in (
+ "tb = importlib.import_module('traceback')\n",
+ "x = tb.format_exc()\n",
+ ):
+ assert _check_code(code, OUT_DIR) == [], code
+
+
+class TestRecommendedPattern:
+ def test_wrapper_import_plus_scrubbed_safe_msg_clean(self):
+ code = (
+ "from ...security.secure_logging import logger\n"
+ "\n"
+ "\n"
+ "def search(self, q):\n"
+ " try:\n"
+ " return self._run(q)\n"
+ " except Exception as e:\n"
+ " safe_msg = self._scrub_error(e)\n"
+ " logger.exception(f'Search failed: {safe_msg}')\n"
+ " return []\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+
+class TestSourceTreeInvariant:
+ """The real secure dirs must satisfy every ban, even past --no-verify."""
+
+ @staticmethod
+ def _src_root() -> Path:
+ return Path(__file__).parent.parent.parent / "src"
+
+ @classmethod
+ def _secure_dir_files(cls):
+ repo_root = cls._src_root().parent
+ for d in SECURE_LOGGING_DIRS:
+ for path in sorted((repo_root / d).rglob("*.py")):
+ yield path
+
+ def test_checker_clean_on_real_secure_dirs(self):
+ """Run the full checker over every file in the three dirs."""
+ failures = []
+ for path in self._secure_dir_files():
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ checker = SensitiveLoggingChecker(str(path))
+ checker.visit(tree)
+ failures.extend(checker.errors)
+ assert not failures, "\n".join(failures)
+
+ def test_no_raw_or_reexported_logger_imports_in_tree(self):
+ """Independent import scan: headline v4/v5 bypasses asserted
+ explicitly, without relying on the checker's own rules."""
+ failures = []
+ for path in self._secure_dir_files():
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Import):
+ for alias in node.names:
+ root = alias.name.split(".")[0]
+ leaf = alias.name.split(".")[-1]
+ if (
+ root in ("loguru", "traceback")
+ or leaf in ("secure_logging", "log_utils")
+ or root == "local_deep_research"
+ ):
+ failures.append(
+ f"{path}:{node.lineno}: import {alias.name}"
+ )
+ elif isinstance(node, ast.ImportFrom):
+ module = node.module or ""
+ is_wrapper = (
+ node.level > 0 and module == "security.secure_logging"
+ ) or (
+ node.level == 0
+ and module
+ == "local_deep_research.security.secure_logging"
+ )
+ if module.split(".")[0] in ("loguru", "traceback"):
+ failures.append(
+ f"{path}:{node.lineno}: from {module} import ..."
+ )
+ continue
+ if module.split(".")[-1] == "log_utils":
+ failures.append(
+ f"{path}:{node.lineno}: from {module} import ..."
+ )
+ continue
+ for alias in node.names:
+ bound = alias.asname or alias.name
+ if bound == "logger" and not (
+ is_wrapper and alias.name == "logger"
+ ):
+ failures.append(
+ f"{path}:{node.lineno}: non-wrapper logger "
+ f"import from {module!r}"
+ )
+ if alias.name in ("secure_logging", "log_utils") and (
+ not is_wrapper
+ ):
+ failures.append(
+ f"{path}:{node.lineno}: module import of "
+ f"{alias.name} from {module!r}"
+ )
+ if is_wrapper and alias.name != "logger":
+ failures.append(
+ f"{path}:{node.lineno}: secure_logging "
+ f"internal {alias.name!r} imported"
+ )
+ assert not failures, "\n".join(failures)
+
+ def test_no_banned_attribute_or_getattr_access_in_tree(self):
+ """Attribute/literal-getattr shapes only — a FunctionDef named
+ __getattr__ (rate_limiting/llm/wrapper.py) must not trip this."""
+ banned_attrs = {
+ "opt",
+ "catch",
+ "patch",
+ "logger",
+ "_logger",
+ "_loguru_logger",
+ "__getattr__",
+ }
+
+ def is_literal_dynamic_sys_import(expr):
+ if not isinstance(expr, ast.Call) or not expr.args:
+ return False
+ func = expr.func
+ is_dynamic_import = (
+ (
+ isinstance(func, ast.Attribute)
+ and func.attr == "import_module"
+ )
+ or (isinstance(func, ast.Name) and func.id == "import_module")
+ or (isinstance(func, ast.Name) and func.id == "__import__")
+ )
+ return (
+ is_dynamic_import
+ and isinstance(expr.args[0], ast.Constant)
+ and expr.args[0].value == "sys"
+ )
+
+ def is_sys_module_expr(expr, sys_names):
+ return (
+ isinstance(expr, ast.Name) and expr.id in sys_names
+ ) or is_literal_dynamic_sys_import(expr)
+
+ failures = []
+ saw_dunder_getattr_def = False
+ for path in self._secure_dir_files():
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ sys_names = {"sys"}
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Import):
+ for alias in node.names:
+ if alias.name == "sys" or (
+ alias.name.split(".")[0] == "sys"
+ and alias.asname is None
+ ):
+ sys_names.add(alias.asname or "sys")
+ for node in ast.walk(tree):
+ if isinstance(node, ast.FunctionDef) and (
+ node.name == "__getattr__"
+ ):
+ saw_dunder_getattr_def = True
+ if isinstance(node, ast.Attribute):
+ if node.attr in banned_attrs:
+ failures.append(
+ f"{path}:{node.lineno}: .{node.attr} access"
+ )
+ elif node.attr == "exc_info" and is_sys_module_expr(
+ node.value, sys_names
+ ):
+ receiver = (
+ node.value.id
+ if isinstance(node.value, ast.Name)
+ else "sys"
+ )
+ failures.append(
+ f"{path}:{node.lineno}: {receiver}.exc_info access"
+ )
+ elif (
+ isinstance(node, ast.ImportFrom)
+ and node.level == 0
+ and node.module == "sys"
+ and any(
+ alias.name in ("*", "exc_info") for alias in node.names
+ )
+ ):
+ failures.append(
+ f"{path}:{node.lineno}: from sys import exc_info"
+ )
+ elif (
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Name)
+ and node.func.id == "getattr"
+ and len(node.args) >= 2
+ and isinstance(node.args[1], ast.Constant)
+ and isinstance(node.args[1].value, str)
+ and (
+ node.args[1].value in banned_attrs
+ or (
+ node.args[1].value == "exc_info"
+ and is_sys_module_expr(node.args[0], sys_names)
+ )
+ )
+ ):
+ failures.append(
+ f"{path}:{node.lineno}: literal getattr dodge"
+ )
+ assert not failures, "\n".join(failures)
+ # In-tree negative fixture: the attribute pass-through method exists
+ # and did not trip the shape-based scan above.
+ assert saw_dunder_getattr_def, (
+ "expected def __getattr__ in rate_limiting/llm/wrapper.py "
+ "as the negative fixture for the shape-based scan"
+ )
+
+ def test_no_logger_rebinding_or_derivation_in_tree(self):
+ failures = []
+ for path in self._secure_dir_files():
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ checker = SensitiveLoggingChecker(str(path))
+ checker.visit(tree)
+ failures.extend(
+ e
+ for e in checker.errors
+ if "rebinding" in e
+ or "shadows" in e
+ or "assigning a logger" in e
+ )
+ assert not failures, "\n".join(failures)
+
+ def test_every_secure_dir_file_using_logger_imports_wrapper(self):
+ """Files calling logger.* must import it from the wrapper."""
+ failures = []
+ for path in self._secure_dir_files():
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ uses_logger = any(
+ isinstance(node, ast.Attribute)
+ and isinstance(node.value, ast.Name)
+ and node.value.id == "logger"
+ for node in ast.walk(tree)
+ )
+ if not uses_logger:
+ continue
+ has_wrapper_import = any(
+ isinstance(node, ast.ImportFrom)
+ and any(
+ a.name == "logger" and a.asname is None for a in node.names
+ )
+ and (
+ (
+ node.level > 0
+ and node.module == "security.secure_logging"
+ )
+ or (
+ node.level == 0
+ and node.module
+ == "local_deep_research.security.secure_logging"
+ )
+ )
+ for node in ast.walk(tree)
+ )
+ if not has_wrapper_import:
+ failures.append(str(path))
+ assert not failures, (
+ "files using logger without the secure_logging wrapper import:\n"
+ + "\n".join(failures)
+ )
diff --git a/tests/utilities/test_loguru_placeholder_formatting.py b/tests/utilities/test_loguru_placeholder_formatting.py
index 099e045806..e0b8ebd123 100644
--- a/tests/utilities/test_loguru_placeholder_formatting.py
+++ b/tests/utilities/test_loguru_placeholder_formatting.py
@@ -1,27 +1,31 @@
# allow: no-sut-import — guardian; sweeps src/ to forbid printf-style placeholders in loguru calls
import ast
+import importlib.util
from pathlib import Path
-import re
-PRINTF_PLACEHOLDER_RE = re.compile(
- r"%(?:\([^)]+\))?[#0 +\-]*\d*(?:\.\d+)?[sdfr]"
+HOOKS_DIR = Path(__file__).resolve().parents[2] / ".pre-commit-hooks"
+HOOK_PATH = HOOKS_DIR / "check-loguru-formatting.py"
+
+# Reuse the pre-commit hook's logic directly so the guardian and the hook
+# cannot diverge (they previously drifted: both gated on a top-level raw
+# loguru import and were blind to the security.secure_logging wrapper).
+spec = importlib.util.spec_from_file_location(
+ "check_loguru_formatting", HOOK_PATH
)
-LOGURU_METHODS = {
- "trace",
- "debug",
- "info",
- "success",
- "warning",
- "error",
- "critical",
- "exception",
- "log",
-}
+hook = importlib.util.module_from_spec(spec)
+assert spec.loader is not None
+spec.loader.exec_module(hook)
def test_loguru_calls_do_not_use_printf_style_placeholders():
- """Guard direct `loguru` logger calls against stdlib printf formatting."""
+ """Guard project logger calls against stdlib printf formatting.
+
+ Covers both raw ``from loguru import logger`` files and files using the
+ diagnose-gated ``security.secure_logging`` wrapper, including
+ ``logger.bind(...)``/``logger.patch(...)`` chains and function-local
+ imports.
+ """
src_root = Path(__file__).resolve().parents[2] / "src"
failures = []
@@ -29,45 +33,57 @@ def test_loguru_calls_do_not_use_printf_style_placeholders():
source = path.read_text()
tree = ast.parse(source)
- imports_loguru_logger = any(
- isinstance(node, ast.ImportFrom)
- and node.module == "loguru"
- and any(alias.name == "logger" for alias in node.names)
- for node in tree.body
- )
- if not imports_loguru_logger:
+ if not hook.imports_project_logger(tree):
continue
- for node in ast.walk(tree):
- if not (
- isinstance(node, ast.Call)
- and isinstance(node.func, ast.Attribute)
- and isinstance(node.func.value, ast.Name)
- and node.func.value.id == "logger"
- and node.func.attr in LOGURU_METHODS
- ):
- continue
-
- method_name = node.func.attr
- message_index = 1 if method_name == "log" else 0
- min_args = 3 if method_name == "log" else 2
- if len(node.args) < min_args:
- continue
-
- message_arg = node.args[message_index]
- if not (
- isinstance(message_arg, ast.Constant)
- and isinstance(message_arg.value, str)
- and PRINTF_PLACEHOLDER_RE.search(message_arg.value)
- ):
- continue
-
+ for lineno, message in hook.find_printf_violations(tree):
failures.append(
- f"{path.relative_to(src_root.parent)}:{node.lineno} -> "
- f"{message_arg.value!r}"
+ f"{path.relative_to(src_root.parent)}:{lineno} -> {message!r}"
)
assert not failures, (
"found loguru calls using printf-style placeholders:\n"
+ "\n".join(sorted(failures))
)
+
+
+def test_hook_gate_sees_wrapper_and_local_imports():
+ """Regression: the import gate must accept wrapper + local imports."""
+ gated = [
+ "from loguru import logger\n",
+ "from ...security.secure_logging import logger\n",
+ "from local_deep_research.security.secure_logging import logger\n",
+ "def f():\n from ..security.secure_logging import logger\n",
+ ]
+ for code in gated:
+ assert hook.imports_project_logger(ast.parse(code)), code
+
+ skipped = [
+ "import os\n",
+ # suffix lookalike must not count as the wrapper
+ "from notsecurity.secure_logging import logger\n",
+ ]
+ for code in skipped:
+ assert not hook.imports_project_logger(ast.parse(code)), code
+
+
+def test_hook_flags_printf_in_direct_and_chained_calls():
+ flagged = [
+ 'logger.info("value: %s", x)\n',
+ 'logger.bind(a=1).warning("value: %s", x)\n',
+ 'logger.patch(p).bind(a=1).error("count: %d", n)\n',
+ 'logger.log("INFO", "value: %s", x)\n',
+ ]
+ for code in flagged:
+ assert hook.find_printf_violations(ast.parse(code)), code
+
+ clean = [
+ 'logger.info("value: {}", x)\n',
+ 'logger.bind(a=1).warning("value: {}", x)\n',
+ # message without extra args is loguru-legal even with a % in it
+ 'logger.info("100%s")\n',
+ # non-logger receivers are out of scope
+ 'other.info("value: %s", x)\n',
+ ]
+ for code in clean:
+ assert not hook.find_printf_violations(ast.parse(code)), code