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.

test: strengthen metrics scrub_error assertions and edge cases (#5355)

Ziyang Guo6dfd7f149814
diff --git a/tests/metrics/test_search_tracker_coverage.py b/tests/metrics/test_search_tracker_coverage.py
index 71615fb589..10ded40d1b 100644
--- a/tests/metrics/test_search_tracker_coverage.py
+++ b/tests/metrics/test_search_tracker_coverage.py
@@ -237,7 +237,7 @@ class TestRecordSearchOuterException:
query="q",
)
- def test_search_metrics_write_failure_logs_via_logger_warning_with_redaction(
+ def test_search_metrics_write_failure_logs_via_logger_warning_with_scrub_error(
self,
):
"""The search-metrics write failure path runs while the user's
@@ -283,10 +283,12 @@ class TestRecordSearchOuterException:
assert "simulated DB failure" in str(scrub_args[0])
assert scrub_args[1] == ctx["user_password"]
mock_logger.warning.assert_called_once()
- assert (
- "Failed to write search metrics"
- in mock_logger.warning.call_args[0][0]
- )
+ warning_msg = mock_logger.warning.call_args[0][0]
+ assert "Failed to write search metrics" in warning_msg
+ # Defense-in-depth: the in-scope encryption password must never
+ # reach ``logger.warning`` verbatim, regardless of what
+ # ``scrub_error`` returns.
+ assert ctx["user_password"] not in warning_msg
mock_logger.exception.assert_not_called()
def test_search_tracker_outer_exception_logs_via_logger_warning_with_scrub_error(
diff --git a/tests/metrics/test_token_counter_coverage.py b/tests/metrics/test_token_counter_coverage.py
index c916fc8f24..9815ab312a 100644
--- a/tests/metrics/test_token_counter_coverage.py
+++ b/tests/metrics/test_token_counter_coverage.py
@@ -202,7 +202,7 @@ class TestSaveToDbBackgroundThreadCoverage:
):
cb._save_to_db(100, 50)
- def test_metrics_writer_exception_logs_via_logger_warning_with_redaction(
+ def test_metrics_writer_exception_logs_via_logger_warning_with_scrub_error(
self,
):
"""The metrics-write failure path runs inside a worker thread
@@ -239,10 +239,13 @@ class TestSaveToDbBackgroundThreadCoverage:
assert "fail" in str(scrub_args[0])
assert scrub_args[1] == "secret"
mock_logger.warning.assert_called_once()
- assert (
- "Failed to write metrics from thread"
- in mock_logger.warning.call_args[0][0]
- )
+ warning_msg = mock_logger.warning.call_args[0][0]
+ assert "Failed to write metrics from thread" in warning_msg
+ # Defense-in-depth: the in-scope encryption password must never
+ # reach ``logger.warning`` verbatim, regardless of what
+ # ``scrub_error`` returns. Guards against a future regression that
+ # interpolates the raw password into the identifying message.
+ assert "secret" not in warning_msg
mock_logger.exception.assert_not_called()
def test_main_thread_save_to_db_exception_logs_via_logger_warning_with_scrub_error(
diff --git a/tests/security/test_log_sanitizer.py b/tests/security/test_log_sanitizer.py
index 9950d6f870..b066dbe18e 100644
--- a/tests/security/test_log_sanitizer.py
+++ b/tests/security/test_log_sanitizer.py
@@ -457,6 +457,53 @@ class TestScrubError:
result = scrub_error(Unprintable())
assert result == "<unprintable Unprintable>"
+ def test_combined_pathology_never_raises(self):
+ """Lock the documented "must never raise" contract against the two
+ theoretical edges called out in the review of #5261: the
+ ``<unprintable {type(error).__name__}>`` fallback
+ (``log_sanitizer.py:328``) and the unguarded per-secret coercion
+ feeding ``redact_secrets`` (``:340``).
+
+ Exercises both at once: an exception built from a custom metaclass
+ whose ``__str__`` raises (forcing the type-name fallback) together
+ with a secret whose ``__bool__`` is non-deterministic/raising. The
+ handler must swallow both, still redact the well-behaved secret, and
+ render the type-name fallback — never propagate out of the ``except``
+ block it is meant to protect.
+ """
+
+ class _RaisingMeta(type):
+ def __getattribute__(cls, name):
+ # Even resolving __name__ via the metaclass is hostile;
+ # scrub_error must still degrade gracefully.
+ if name == "__name__":
+ return type.__getattribute__(cls, "__name__")
+ return type.__getattribute__(cls, name)
+
+ class PathologicalError(Exception, metaclass=_RaisingMeta):
+ def __str__(self):
+ raise RuntimeError("cannot stringify")
+
+ class NonDeterministicBool:
+ def __bool__(self):
+ raise ValueError("no truth value")
+
+ def __str__(self):
+ raise ValueError("no string value")
+
+ # Must not raise even with a hostile exception AND a hostile secret.
+ result = scrub_error(
+ PathologicalError(),
+ NonDeterministicBool(),
+ "goodsecret123456",
+ )
+
+ # Type-name fallback is used because __str__ raised.
+ assert result == "<unprintable PathologicalError>"
+ # The well-behaved literal secret is unrelated to this message, but
+ # the point is the call completed without propagating an exception.
+ assert "goodsecret123456" not in result
+
def test_matches_engine_scrub_error(self):
# BaseSearchEngine._scrub_error delegates here; the two must
# produce identical output for the same inputs.
diff --git a/tests/utilities/test_log_utils.py b/tests/utilities/test_log_utils.py
index ee54d7f3b3..b553fd5082 100644
--- a/tests/utilities/test_log_utils.py
+++ b/tests/utilities/test_log_utils.py
@@ -792,6 +792,32 @@ class TestTruncateForDatabase:
assert result.startswith("[RuntimeError: AAAAAA")
assert result.endswith("…] ")
+ def test_exception_context_scrubs_token_inside_retained_region(self):
+ """Companion to the pre-cap test: place a Bearer token early in the
+ message (well inside both the 4096 pre-cap and the 240 render cap)
+ so it survives truncation and must be removed by
+ ``sanitize_error_message`` rather than merely dropped by the cap.
+
+ The pre-cap test alone can pass even if scrubbing regressed, because
+ it hides the token past both caps. This variant exercises the
+ sanitization step itself on the retained prefix.
+ """
+ from loguru._logger import RecordException
+
+ from local_deep_research.utilities.log_utils import _exception_context
+
+ # ~pos 40 → inside the 237-char rendered prefix and the 4096 pre-cap.
+ err_msg = "auth failed: " + "x" * 30 + " Bearer secret-token-12345"
+ record = {
+ "exception": RecordException(
+ RuntimeError, RuntimeError(err_msg), None
+ )
+ }
+
+ result = _exception_context(record)
+ assert "secret-token-12345" not in result
+ assert "Bearer [REDACTED]" in result
+
class TestFrontendProgressSink:
"""Tests for frontend_progress_sink function."""