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): track match/case exception rebinds in sensitive-logging hook (#4999)

LearningCircuit6fa0331e2914
diff --git a/.pre-commit-hooks/check-env-vars.py b/.pre-commit-hooks/check-env-vars.py
index 3feda179e7..1bc67f3ff2 100755
--- a/.pre-commit-hooks/check-env-vars.py
+++ b/.pre-commit-hooks/check-env-vars.py
@@ -263,7 +263,9 @@ class EnvVarChecker(ast.NodeVisitor):
if ALLOWED_PATH_SEGMENTS.intersection(p.parts):
return True
for ending in ALLOWED_PATH_ENDINGS:
- if self.filename.endswith(ending):
+ # Normalize backslashes so the forward-slash-anchored endings
+ # still match on Windows checkouts.
+ if self.filename.replace("\\", "/").endswith(ending):
return True
return False
diff --git a/.pre-commit-hooks/check-sensitive-logging.py b/.pre-commit-hooks/check-sensitive-logging.py
index 7cbdbd7be6..5c12112ec2 100755
--- a/.pre-commit-hooks/check-sensitive-logging.py
+++ b/.pre-commit-hooks/check-sensitive-logging.py
@@ -134,6 +134,8 @@ SECURE_LOGGING_DIRS = (
# ``notsecurity.secure_logging`` cannot spoof it.
WRAPPER_MODULE_RELATIVE = "security.secure_logging"
WRAPPER_MODULE_ABSOLUTE = "local_deep_research.security.secure_logging"
+# Default hint; per-file depth is computed at __init__ time so the message
+# reflects the actual file under src/local_deep_research/.
WRAPPER_IMPORT_HINT = (
"use 'from ...security.secure_logging import logger' "
"(relative depth per file)"
@@ -201,6 +203,40 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
self.in_secure_dir = any(
d in filename.replace("\\", "/") for d in SECURE_LOGGING_DIRS
)
+ # Per-file wrapper-import hint. Depth is counted from the
+ # local_deep_research package root so the leading-dot count matches
+ # the file's actual position in the tree.
+ self.wrapper_import_hint = self._compute_wrapper_import_hint()
+
+ def _compute_wrapper_import_hint(self) -> str:
+ """Compute the per-file import hint with the correct dot count.
+
+ Depth = number of package levels from (and including)
+ ``local_deep_research`` down to the file's parent directory. Files
+ outside ``src/local_deep_research/...`` fall back to the generic
+ constant hint.
+ """
+ normalized = self.filename.replace("\\", "/")
+ marker = "/local_deep_research/"
+ idx = normalized.find(marker)
+ if idx == -1:
+ return WRAPPER_IMPORT_HINT
+ tail = normalized[idx + len(marker) :]
+ # tail is e.g. "llm/providers/implementations/x.py"; count slashes
+ # in the directory portion (= package depth below local_deep_research).
+ if "/" in tail:
+ dir_part = tail.rsplit("/", 1)[0]
+ else:
+ dir_part = "" # file sits directly under local_deep_research/
+ if not dir_part:
+ depth = 1 # local_deep_research package itself
+ else:
+ depth = dir_part.count("/") + 2
+ dots = "." * depth
+ return (
+ f"use 'from {dots}security.secure_logging import logger' "
+ f"(relative depth per file)"
+ )
def visit_ExceptHandler(self, node: ast.ExceptHandler) -> None:
"""Track exception variable names inside except blocks."""
@@ -213,11 +249,361 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
previous_shadowed_names = set(self._shadowed_name_scopes[-1])
if self.in_secure_dir and node.name:
self._mark_name_shadow(node.name)
+ previous_except_stack_len = len(self._except_var_stack)
self._except_var_stack.append(node.name)
self.generic_visit(node)
- self._except_var_stack.pop()
+ # A match statement may add aliases that remain bound for the rest of
+ # this handler. Discard the handler name and all such aliases together.
+ del self._except_var_stack[previous_except_stack_len:]
self._shadowed_name_scopes[-1] = previous_shadowed_names
+ @staticmethod
+ def _pattern_bound_names(pattern: ast.AST) -> List[str]:
+ """Collect every name bound anywhere in a match pattern."""
+ names: List[str] = []
+ if pattern is None:
+ return names
+ if isinstance(pattern, ast.MatchAs):
+ if pattern.name:
+ names.append(pattern.name)
+ names.extend(
+ SensitiveLoggingChecker._pattern_bound_names(pattern.pattern)
+ )
+ elif isinstance(pattern, ast.MatchSequence):
+ for child in pattern.patterns:
+ names.extend(
+ SensitiveLoggingChecker._pattern_bound_names(child)
+ )
+ elif isinstance(pattern, ast.MatchStar):
+ if pattern.name:
+ names.append(pattern.name)
+ elif isinstance(pattern, ast.MatchMapping):
+ for child in pattern.patterns:
+ names.extend(
+ SensitiveLoggingChecker._pattern_bound_names(child)
+ )
+ if pattern.rest:
+ names.append(pattern.rest)
+ elif isinstance(pattern, ast.MatchClass):
+ for child in pattern.patterns:
+ names.extend(
+ SensitiveLoggingChecker._pattern_bound_names(child)
+ )
+ for child in pattern.kwd_patterns:
+ names.extend(
+ SensitiveLoggingChecker._pattern_bound_names(child)
+ )
+ elif isinstance(pattern, ast.MatchOr):
+ for child in pattern.patterns:
+ names.extend(
+ SensitiveLoggingChecker._pattern_bound_names(child)
+ )
+ return names
+
+ @staticmethod
+ def _subject_is_exception(
+ expr: ast.AST, active: set, exception_expr_ids=None
+ ) -> bool:
+ """Whether an expression evaluates to an active exception itself."""
+ if exception_expr_ids is not None and id(expr) in exception_expr_ids:
+ return True
+ if isinstance(expr, ast.Name):
+ return expr.id in active
+ if isinstance(expr, ast.NamedExpr):
+ if isinstance(expr.target, ast.Name) and expr.target.id in active:
+ return True
+ return SensitiveLoggingChecker._subject_is_exception(
+ expr.value, active, exception_expr_ids
+ )
+ return False
+
+ def _apply_definite_subject_assignments(
+ self, expr: ast.AST, exception_expr_ids: set
+ ) -> None:
+ """Apply walrus transfers guaranteed while evaluating a subject.
+
+ Tuple/list/set elements and dict keys/values evaluate in order before
+ matching begins. Boolean and conditional expressions are deliberately
+ not descended because their branches may be skipped.
+ """
+ if isinstance(expr, ast.NamedExpr):
+ self._apply_definite_subject_assignments(
+ expr.value, exception_expr_ids
+ )
+ if isinstance(expr.target, ast.Name):
+ active = {
+ name for name in self._except_var_stack if name is not None
+ }
+ value_is_exception = self._subject_is_exception(
+ expr.value, active, exception_expr_ids
+ )
+ self._except_var_stack[:] = [
+ name
+ for name in self._except_var_stack
+ if name != expr.target.id
+ ]
+ if value_is_exception:
+ exception_expr_ids.add(id(expr))
+ self._except_var_stack.append(expr.target.id)
+ elif isinstance(expr, ast.Name):
+ if expr.id in self._except_var_stack:
+ exception_expr_ids.add(id(expr))
+ elif isinstance(expr, (ast.Tuple, ast.List, ast.Set)):
+ for element in expr.elts:
+ if isinstance(element, ast.Starred):
+ element = element.value
+ self._apply_definite_subject_assignments(
+ element, exception_expr_ids
+ )
+ elif isinstance(expr, ast.Dict):
+ for key, value in zip(expr.keys, expr.values):
+ if key is not None:
+ self._apply_definite_subject_assignments(
+ key, exception_expr_ids
+ )
+ self._apply_definite_subject_assignments(
+ value, exception_expr_ids
+ )
+
+ @classmethod
+ def _pattern_exception_aliases(
+ cls,
+ subject: ast.AST,
+ pattern: ast.AST,
+ active: set,
+ exception_expr_ids=None,
+ ) -> List[str]:
+ """Map provable exception identities from a subject into a pattern."""
+ aliases: List[str] = []
+ if pattern is None:
+ return aliases
+
+ subject_is_exception = cls._subject_is_exception(
+ subject, active, exception_expr_ids
+ )
+ if isinstance(subject, ast.NamedExpr):
+ if subject_is_exception and isinstance(subject.target, ast.Name):
+ aliases.append(subject.target.id)
+ subject = subject.value
+
+ if isinstance(pattern, ast.MatchAs):
+ if subject_is_exception and pattern.name:
+ aliases.append(pattern.name)
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject,
+ pattern.pattern,
+ active,
+ exception_expr_ids,
+ )
+ )
+ elif isinstance(pattern, ast.MatchOr):
+ for alternative in pattern.patterns:
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject,
+ alternative,
+ active,
+ exception_expr_ids,
+ )
+ )
+ elif isinstance(pattern, ast.MatchSequence) and isinstance(
+ subject, (ast.Tuple, ast.List)
+ ):
+ pattern_star = next(
+ (
+ index
+ for index, child in enumerate(pattern.patterns)
+ if isinstance(child, ast.MatchStar)
+ ),
+ None,
+ )
+ subject_star = next(
+ (
+ index
+ for index, child in enumerate(subject.elts)
+ if isinstance(child, ast.Starred)
+ ),
+ None,
+ )
+ if pattern_star is None and subject_star is None:
+ if len(subject.elts) != len(pattern.patterns):
+ return aliases
+ for element, child_pattern in zip(
+ subject.elts, pattern.patterns
+ ):
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ element,
+ child_pattern,
+ active,
+ exception_expr_ids,
+ )
+ )
+ elif pattern_star is not None:
+ # A MatchStar capture receives a new list, but fixed prefix
+ # and suffix positions still have provable correspondence.
+ prefix_count = pattern_star
+ for index in range(prefix_count):
+ if index >= len(subject.elts) or (
+ subject_star is not None and index >= subject_star
+ ):
+ break
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject.elts[index],
+ pattern.patterns[index],
+ active,
+ exception_expr_ids,
+ )
+ )
+
+ suffix_count = len(pattern.patterns) - pattern_star - 1
+ for offset in range(1, suffix_count + 1):
+ subject_index = len(subject.elts) - offset
+ if subject_index < 0 or (
+ subject_star is not None
+ and subject_index <= subject_star
+ ):
+ break
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject.elts[subject_index],
+ pattern.patterns[-offset],
+ active,
+ exception_expr_ids,
+ )
+ )
+ else:
+ # The subject contains a starred expansion but the pattern is
+ # fixed-length. Its explicit prefix and suffix still line up
+ # whenever the case matches.
+ for index in range(subject_star):
+ if index >= len(pattern.patterns):
+ break
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject.elts[index],
+ pattern.patterns[index],
+ active,
+ exception_expr_ids,
+ )
+ )
+ suffix_count = len(subject.elts) - subject_star - 1
+ for offset in range(1, suffix_count + 1):
+ if offset > len(pattern.patterns):
+ break
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject.elts[-offset],
+ pattern.patterns[-offset],
+ active,
+ exception_expr_ids,
+ )
+ )
+ elif isinstance(pattern, ast.MatchMapping) and isinstance(
+ subject, ast.Dict
+ ):
+ subject_values = {
+ key.value: value
+ for key, value in zip(subject.keys, subject.values)
+ if isinstance(key, ast.Constant)
+ }
+ for key, child_pattern in zip(pattern.keys, pattern.patterns):
+ if (
+ isinstance(key, ast.Constant)
+ and key.value in subject_values
+ ):
+ aliases.extend(
+ cls._pattern_exception_aliases(
+ subject_values[key.value],
+ child_pattern,
+ active,
+ exception_expr_ids,
+ )
+ )
+ return aliases
+
+ def visit_Match(self, node: ast.Match) -> None:
+ """Track match bindings separately from proven exception aliases."""
+ if not self.in_secure_dir:
+ self.generic_visit(node)
+ return
+
+ previous_subject_exception_vars = list(self._except_var_stack)
+ self.visit(node.subject)
+ self._except_var_stack[:] = previous_subject_exception_vars
+ subject_exception_expr_ids: set = set()
+ self._apply_definite_subject_assignments(
+ node.subject, subject_exception_expr_ids
+ )
+
+ aliases_after_match: List[str] = []
+ for case in node.cases:
+ active = {
+ name for name in self._except_var_stack if name is not None
+ }
+ all_bound = list(
+ dict.fromkeys(self._pattern_bound_names(case.pattern))
+ )
+ aliases = list(
+ dict.fromkeys(
+ self._pattern_exception_aliases(
+ node.subject,
+ case.pattern,
+ active,
+ subject_exception_expr_ids,
+ )
+ )
+ )
+ previous_except_vars = list(self._except_var_stack)
+ previous_sys_names = set(self._sys_name_scopes[-1])
+ previous_shadowed_names = set(self._shadowed_name_scopes[-1])
+
+ # Pattern bindings definitely shadow prior meanings in the guard
+ # and body of this case. Add back only proven exception aliases.
+ bound_set = set(all_bound)
+ self._except_var_stack[:] = [
+ name for name in self._except_var_stack if name not in bound_set
+ ]
+ for name in all_bound:
+ self._flag_logger_binding(
+ ast.Name(id=name, ctx=ast.Store()), case.pattern.lineno
+ )
+ self._mark_name_shadow(name)
+ self._except_var_stack.extend(aliases)
+ self.visit(case.pattern)
+
+ guard_state: List[Optional[str]] = []
+ if case.guard is not None:
+ self.visit(case.guard)
+ guard_state = list(self._except_var_stack)
+ for statement in case.body:
+ self.visit(statement)
+ body_state = list(self._except_var_stack)
+
+ self._except_var_stack[:] = previous_except_vars
+ self._sys_name_scopes[-1] = previous_sys_names
+ self._shadowed_name_scopes[-1] = previous_shadowed_names
+ if case.guard is not None:
+ aliases_after_match.extend(guard_state)
+ aliases_after_match.extend(body_state)
+
+ # A guard can fail after binding names, allowing evaluation to
+ # continue with the next case. Body effects cannot reach later
+ # cases because a selected body ends the match.
+ if case.guard is not None:
+ for name in dict.fromkeys(guard_state):
+ if name not in self._except_var_stack:
+ self._except_var_stack.append(name)
+
+ # Successful pattern bindings and nested matches remain visible after
+ # the match. Do not erase prior sys interpretations here: a case may
+ # not match, so both meanings must be considered possible.
+ for name in dict.fromkeys(aliases_after_match):
+ if name not in self._except_var_stack:
+ self._except_var_stack.append(name)
+
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))
@@ -310,21 +696,21 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
self.errors.append(
f"{self.filename}:{node.lineno}: "
f"raw loguru import is banned in secure-logging dirs — "
- f"{WRAPPER_IMPORT_HINT}"
+ f"{self.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}"
+ f"{self.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}"
+ f"{self.wrapper_import_hint}"
)
elif root == "local_deep_research" and alias.asname is None:
self.errors.append(
@@ -332,7 +718,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"{self.wrapper_import_hint}"
)
elif root == "traceback":
self.errors.append(
@@ -347,7 +733,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"{self.wrapper_import_hint}"
)
self.generic_visit(node)
@@ -370,7 +756,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
self.errors.append(
f"{self.filename}:{node.lineno}: "
f"raw loguru import is banned in secure-logging dirs — "
- f"{WRAPPER_IMPORT_HINT}"
+ f"{self.wrapper_import_hint}"
)
return
@@ -423,7 +809,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"logger; {self.wrapper_import_hint}"
)
return
@@ -433,7 +819,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"dirs — {self.wrapper_import_hint}"
)
elif alias.name == "secure_logging" and (
(node.level > 0 and module == "security")
@@ -445,7 +831,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
f"{self.filename}:{node.lineno}: "
f"importing the secure_logging module exposes raw "
f"handles (secure_logging._loguru_logger) — "
- f"{WRAPPER_IMPORT_HINT}"
+ f"{self.wrapper_import_hint}"
)
elif alias.name == "log_utils" and (
(node.level > 0 and module == "utilities")
@@ -457,14 +843,14 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"logger (log_utils.logger) — {self.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}"
+ f"{self.wrapper_import_hint}"
)
# ------------------------------------------------------------------
@@ -541,6 +927,13 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
self._flag_logger_binding(node.target, node.lineno)
self._check_logger_derivation(node.value, node.lineno)
self._mark_target_shadow(node.target)
+ if isinstance(node.target, ast.Name):
+ active = {
+ name for name in self._except_var_stack if name is not None
+ }
+ if self._subject_is_exception(node.value, active):
+ if node.target.id not in self._except_var_stack:
+ self._except_var_stack.append(node.target.id)
self.generic_visit(node)
def visit_For(self, node: ast.For) -> None:
@@ -651,7 +1044,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"{self.wrapper_import_hint}"
)
elif node.attr in ("_logger", "_loguru_logger", "__getattr__"):
self.errors.append(
@@ -737,7 +1130,7 @@ class SensitiveLoggingChecker(ast.NodeVisitor):
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}"
+ f"secure-logging dirs — {self.wrapper_import_hint}"
)
# logger.bind(...) chains are wrapper-preserving, but the message is
diff --git a/changelog.d/4999.bugfix.md b/changelog.d/4999.bugfix.md
new file mode 100644
index 0000000000..db5d1d21e9
--- /dev/null
+++ b/changelog.d/4999.bugfix.md
@@ -0,0 +1 @@
+The path-anchored exemptions in `check-env-vars.py` no longer fail on Windows checkouts where directory separators are backslashes — previously the hook would skip its own exemptions and flag legitimate constant definitions.
diff --git a/changelog.d/4999.security.md b/changelog.d/4999.security.md
new file mode 100644
index 0000000000..6664dd480c
--- /dev/null
+++ b/changelog.d/4999.security.md
@@ -0,0 +1 @@
+The `check-sensitive-logging` pre-commit hook now tracks exception-variable rebindings introduced by `match`/`case` arms (`except E as e: match e: case X() as v:`), so a renamed exception cannot slip past the scrubbing rule via structural pattern matching. The wrapper-import error message also now names the correct relative-dot depth for the file's actual position in the tree, instead of a fixed three dots.
diff --git a/tests/hooks/test_check_env_vars.py b/tests/hooks/test_check_env_vars.py
index fd77d9c30b..bf6ba63383 100644
--- a/tests/hooks/test_check_env_vars.py
+++ b/tests/hooks/test_check_env_vars.py
@@ -121,6 +121,34 @@ class TestBareRateLimiterStillFlagged:
assert result.returncode == 1
+class TestWindowsBackslashPathsExempt:
+ """Path-anchored exemptions must match backslash paths too, so the
+ forward-slash-anchored ALLOWED_PATH_ENDINGS survive a Windows
+ checkout (where Path.parts and str(Path(...)) use backslashes)."""
+
+ def _checker(self, filename: str):
+ import importlib.util
+
+ spec = importlib.util.spec_from_file_location(
+ "check_env_vars", HOOK_SCRIPT
+ )
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module.EnvVarChecker(filename)
+
+ def test_backslash_rate_limiter_path_exempt(self):
+ checker = self._checker(
+ "src\\local_deep_research\\security\\rate_limiter.py"
+ )
+ assert checker._is_file_allowed() is True
+
+ def test_backslash_secure_logging_path_exempt(self):
+ checker = self._checker(
+ "src\\local_deep_research\\security\\secure_logging.py"
+ )
+ assert checker._is_file_allowed() is True
+
+
class TestRealViolation:
"""Sanity check — normal production files are still flagged."""
diff --git a/tests/security/test_sensitive_logging_hook.py b/tests/security/test_sensitive_logging_hook.py
index af47dc4edd..2183eb3c70 100644
--- a/tests/security/test_sensitive_logging_hook.py
+++ b/tests/security/test_sensitive_logging_hook.py
@@ -748,6 +748,562 @@ class TestRecommendedPattern:
assert _check_code(code, IN_DIR) == []
+class TestWrapperImportHintPerFile:
+ """The wrapper-import hint must reflect the file's actual depth in the
+ tree so the suggested import resolves to the right module."""
+
+ @staticmethod
+ def _hint_for(filename: str) -> str:
+ checker = SensitiveLoggingChecker(filename)
+ return checker.wrapper_import_hint
+
+ def test_engines_dir_gets_three_dots(self):
+ hint = self._hint_for(IN_DIR)
+ # IN_DIR ends in web_search_engines/engines/x.py — three dots
+ # to reach local_deep_research from .engines.
+ assert "from ...security.secure_logging" in hint
+
+ def test_llm_implementations_gets_four_dots(self):
+ hint = self._hint_for(IN_DIR_LLM)
+ assert "from ....security.secure_logging" in hint
+
+ def test_embeddings_implementations_gets_four_dots(self):
+ hint = self._hint_for(IN_DIR_EMB)
+ assert "from ....security.secure_logging" in hint
+
+ def test_root_package_file_gets_one_dot(self):
+ # A hypothetical file directly under local_deep_research/ needs 1 dot.
+ hint = self._hint_for("src/local_deep_research/something.py")
+ assert "from .security.secure_logging" in hint
+
+ def test_error_message_uses_per_file_hint(self):
+ # Trigger an import ban and check the error carries the right dot count.
+ code = "import loguru\n"
+ errs = _check_code(code, IN_DIR_LLM)
+ assert len(errs) == 1
+ assert "from ....security.secure_logging" in errs[0]
+
+ def test_hint_rejects_lookalike_package_prefix(self):
+ """The marker ``/local_deep_research/`` is leading-slash-anchored
+ so a lookalike directory like ``src/notlocal_deep_research/foo.py``
+ must NOT trigger the per-file dot-count path — it falls back to
+ the generic constant hint. Without this anchoring, a hypothetical
+ sibling package could spoof the marker and produce a wrong dot
+ count. Mutates the marker to ``local_deep_research/`` (no leading
+ slash) and this test fails."""
+ # Lookalike path that contains the marker substring but is not
+ # actually under local_deep_research/. in_secure_dir is False so
+ # no error is generated, but we can still assert the hint shape.
+ checker = SensitiveLoggingChecker("src/notlocal_deep_research/foo.py")
+ # Falls back to the generic 3-dot hint (not a per-file dot count).
+ assert checker.wrapper_import_hint == hook_module.WRAPPER_IMPORT_HINT
+ assert "from ...security.secure_logging" in checker.wrapper_import_hint
+
+
+class TestMatchCaseExceptionCapture:
+ """``match e: case X() as v:`` rebinds the exception variable. The
+ rebind must be tracked so log calls inside the case body that
+ interpolate the new name are flagged like the original."""
+
+ def test_match_as_binds_exception_var_flagged(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError() as ve:\n"
+ " logger.error(f'err: {ve}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), code
+
+ def test_match_on_non_exception_subject_not_flagged(self):
+ """If the match subject is not the current exception variable, we
+ cannot prove the binding is an exception — do not flag."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " obj = get()\n"
+ " match obj:\n"
+ " case SomeClass() as x:\n"
+ " logger.error(f'val: {x}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_multiple_cases_each_flagged(self):
+ """Each ``case ... as v`` binds a distinct name; all must be
+ tracked separately (and the wildcard arm still sees the outer
+ exception var)."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError() as ve:\n"
+ " logger.error(f'v: {ve}')\n"
+ " case TypeError() as te:\n"
+ " logger.warning(f't: {te}')\n"
+ " case _:\n"
+ " logger.error(f'other: {e}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ flagged_lines = {
+ e.split(":")[1] for e in errors if "Exception variable" in e
+ }
+ assert len(flagged_lines) == 3, errors
+
+ def test_match_nested_sequence_components_not_treated_as_exception(self):
+ """Sequence captures are components, not aliases of the subject."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case [message, *rest]:\n"
+ " logger.error(f'message: {message}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_mapping_components_not_treated_as_exception(self):
+ """Mapping captures are values from the subject, not the subject."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case {'message': message}:\n"
+ " logger.error(f'message: {message}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_or_pattern_binds(self):
+ """``case (A() as x) | (B() as x):`` — both alternatives bind ``x``."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case (A() as x) | (B() as x):\n"
+ " logger.error(f'val: {x}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), code
+
+ def test_match_class_keyword_component_not_treated_as_exception(self):
+ """A class keyword capture is an attribute, not the whole subject."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case HttpError(code=code):\n"
+ " logger.error(f'code: {code}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_class_positional_component_not_treated_as_exception(self):
+ """A class positional capture is an attribute, not the subject."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case Wrapper(message, _):\n"
+ " logger.error(f'message: {message}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_expression_referencing_exception_does_not_alias_result(self):
+ """Referencing ``e`` does not make a transformed subject identical."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match str(e):\n"
+ " case message:\n"
+ " logger.error(f'message: {message}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_compound_subject_does_not_alias_unrelated_capture(self):
+ """A mixed subject must not taint every corresponding capture."""
+ code = (
+ "public_value = 'safe'\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match (e, public_value):\n"
+ " case (captured_e, captured_public):\n"
+ " logger.error(f'value: {captured_public}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_compound_subject_tracks_exception_position(self):
+ """Structural mapping tracks the exception without tainting siblings."""
+ code = (
+ "public_value = 'safe'\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match (e, public_value):\n"
+ " case (captured_e, captured_public):\n"
+ " logger.error(f'err: {captured_e}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_match_star_preserves_fixed_exception_positions(self):
+ cases = [
+ ("(e, 'safe')", "(captured_e, *rest)"),
+ ("('safe', e)", "(*rest, captured_e)"),
+ ("(e, *values)", "(captured_e, *rest)"),
+ ("(e, *values)", "(captured_e, other)"),
+ ("(*values, e)", "(other, captured_e)"),
+ ]
+ for subject, pattern in cases:
+ code = (
+ "values = ['safe']\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ f" match {subject}:\n"
+ f" case {pattern}:\n"
+ " logger.error(f'err: {captured_e}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), (
+ code,
+ errors,
+ )
+
+ def test_match_literal_mapping_tracks_exception_value_only(self):
+ code_flagged = (
+ "public_value = 'safe'\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match {'error': e, 'public': public_value}:\n"
+ " case {'error': captured_e, 'public': captured_public}:\n"
+ " logger.error(f'err: {captured_e}')\n"
+ )
+ errors = _check_code(code_flagged, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ code_safe = code_flagged.replace(
+ "logger.error(f'err: {captured_e}')",
+ "logger.error(f'public: {captured_public}')",
+ )
+ assert _check_code(code_safe, IN_DIR) == []
+
+ def test_match_subject_still_visited_for_existing_security_checks(self):
+ """Custom match traversal must not hide checks in the subject."""
+ code = (
+ "import sys\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match (e, sys.exc_info()):\n"
+ " case _:\n"
+ " pass\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("exc_info is banned" in error for error in errors), errors
+
+ def test_match_alias_remains_tracked_after_match(self):
+ """Successful pattern bindings remain in Python's enclosing scope."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ as rebound:\n"
+ " pass\n"
+ " logger.error(f'err: {rebound}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_nested_match_alias_remains_tracked_after_outer_match(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ as outer:\n"
+ " match outer:\n"
+ " case _ as inner:\n"
+ " pass\n"
+ " logger.error(f'err: {inner}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_conditional_capture_does_not_hide_possible_sys_alias(self):
+ code = (
+ "import sys as s\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError() as s:\n"
+ " pass\n"
+ " case _:\n"
+ " pass\n"
+ " s.exc_info()\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("exc_info is banned" in error for error in errors), errors
+
+ def test_unguarded_case_alias_does_not_flow_to_later_case(self):
+ code = (
+ "x = 'safe'\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError() as x:\n"
+ " pass\n"
+ " case TypeError():\n"
+ " logger.error(f'value: {x}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_component_capture_shadows_exception_name_inside_case(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError(args=e):\n"
+ " logger.error(f'args: {e}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_component_capture_shadows_sys_alias_inside_case(self):
+ code = (
+ "import sys as s\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError(args=s):\n"
+ " s.exc_info()\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_pattern_capture_named_logger_is_banned(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match {'sink': object()}:\n"
+ " case {'sink': logger}:\n"
+ " pass\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("rebinding the name 'logger'" in error for error in errors)
+
+ def test_walrus_subject_preserves_exception_identity(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match (subject := e):\n"
+ " case _ as rebound:\n"
+ " logger.error(f'err: {rebound}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_nested_walrus_in_compound_subject_persists_alias(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match ((alias := e), 'public'):\n"
+ " case _:\n"
+ " pass\n"
+ " logger.error(f'err: {alias}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_nested_walrus_in_compound_subject_kills_old_alias(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match ((e := 'safe'), 'public'):\n"
+ " case _:\n"
+ " logger.error(f'value: {e}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_ordered_subject_preserves_earlier_walrus_identity(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match ((alias := e), (e := 'safe')):\n"
+ " case (captured, _):\n"
+ " logger.error(f'err: {captured}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_ordered_subject_identity_survives_later_name_overwrites(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match ((alias := e), (alias := 'safe'), (e := 'safe')):\n"
+ " case (captured, _, _):\n"
+ " logger.error(f'err: {captured}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_match_guard_walrus_tracks_exception_alias(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ if (rebound := e):\n"
+ " logger.error(f'err: {rebound}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_failed_guard_walrus_alias_is_visible_to_later_case(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ if (rebound := e) and False:\n"
+ " pass\n"
+ " case _:\n"
+ " logger.error(f'err: {rebound}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_match_guard_walrus_safe_overwrite_remains_conservative(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ as rebound if (rebound := 'safe'):\n"
+ " logger.error(f'value: {rebound}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_short_circuit_guard_does_not_kill_possible_exception_alias(self):
+ code = (
+ "flag = True\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ as x if flag or (x := 'safe'):\n"
+ " logger.error(f'err: {x}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_short_circuit_guard_preserves_alias_for_later_case(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case _ as x if False and (x := 'safe'):\n"
+ " pass\n"
+ " case _:\n"
+ " logger.error(f'err: {x}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in error for error in errors), errors
+
+ def test_match_subject_walrus_kills_overwritten_exception_name(self):
+ code = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match (e := 'safe'):\n"
+ " case _:\n"
+ " logger.error(f'value: {e}')\n"
+ )
+ assert _check_code(code, IN_DIR) == []
+
+ def test_match_case_rebind_does_not_inherit_sys_alias(self):
+ """``import sys as v`` then ``case X() as v:`` rebinds ``v`` to
+ the exception. The sys-alias interpretation must NOT persist into
+ the case body — ``v.exc_info()`` should not be flagged as a
+ sys.exc_info dodge (the case-bound ``v`` is the exception, not
+ sys). The exception-var check still catches ``logger.error(f"{v}")``."""
+ code = (
+ "import sys as v\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError() as v:\n"
+ " logger.error(f'err: {v}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ # The exception-interpolation flag fires...
+ assert any("Exception variable" in e for e in errors), code
+ # ...and the sys.exc_info ban does NOT (v is the exception now,
+ # not sys).
+ assert not any("sys.exc_info" in e for e in errors), errors
+
+ def test_except_star_match_as_flagged(self):
+ """``except* Exception as e:`` (TryStar) followed by
+ ``case X() as v:`` must also flag the renamed binding."""
+ code = (
+ "try:\n"
+ " f()\n"
+ "except* Exception as e:\n"
+ " match e:\n"
+ " case ValueError() as ve:\n"
+ " logger.error(f'err: {ve}')\n"
+ )
+ errors = _check_code(code, IN_DIR)
+ assert any("Exception variable" in e for e in errors), code
+
+ def test_match_with_no_capture_not_flagged_for_other_vars(self):
+ """``case ValueError():`` (no ``as``) does not rebind — only the
+ outer ``e`` is on the stack. A log call inside this arm
+ referencing ``e`` is flagged; referencing an unrelated var is not.
+ """
+ code_flag = (
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError():\n"
+ " logger.error(f'v: {e}')\n"
+ )
+ errors_flag = _check_code(code_flag, IN_DIR)
+ assert any("Exception variable" in e for e in errors_flag), code_flag
+
+ code_safe = (
+ "x = 1\n"
+ "try:\n"
+ " f()\n"
+ "except Exception as e:\n"
+ " match e:\n"
+ " case ValueError():\n"
+ " logger.error(f'val: {x}')\n"
+ )
+ assert _check_code(code_safe, IN_DIR) == []
+
+
class TestSourceTreeInvariant:
"""The real secure dirs must satisfy every ban, even past --no-verify."""
@@ -992,3 +1548,14 @@ class TestSourceTreeInvariant:
"files using logger without the secure_logging wrapper import:\n"
+ "\n".join(failures)
)
+
+ def test_secure_logging_dirs_constant_in_sync_with_custom_checks(self):
+ """custom-checks.py duplicates SECURE_LOGGING_DIRS for its own
+ exception-message advice; the two must stay identical so the
+ wrapper-gated exception rule covers the same files in both hooks."""
+ custom_checks = import_module("custom-checks")
+ assert custom_checks.SECURE_LOGGING_DIRS == SECURE_LOGGING_DIRS, (
+ "SECURE_LOGGING_DIRS drifted between hooks:\n"
+ f" custom-checks.py: {custom_checks.SECURE_LOGGING_DIRS}\n"
+ f" check-sensitive-logging: {SECURE_LOGGING_DIRS}"
+ )