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.

3.3 KB
Raw Blame
"""TokenCounter's no-username warning must not dump the raw research context.

``TokenCountingCallback`` carries the full ``research_context`` dict — in
production flows that dict is the search context, which includes
``user_password`` (``api/research_functions.py`` puts it there "for metrics
tracking") and a ``settings_snapshot`` that can carry engine credentials.
When a metrics save runs on a background thread and the context has no
``username`` key, ``token_counter._save_to_db`` logs the ENTIRE dict via a
f-string on a raw ``loguru`` logger (``from loguru import logger``). No
logger in this codebase redacts values inside a log message —
``security.secure_logging``'s wrapper only gates ``exception()``
tracebacks behind diagnose mode and forwards every other call (including
a ``.warning()`` like this one) straight to loguru unmodified — so nothing
would have scrubbed this line even had it gone through that wrapper:

    Cannot save token metrics - no username in research context.
    ... Research context: {self.research_context}

No current caller builds a password-bearing context without ``username``, so
this is a latent hazard rather than a demonstrated leak — but the sink is one
forgotten ``username`` key away from writing the plaintext password (and the
snapshot's secrets) into server logs. The pins here keep the diagnostic while
forcing value-free rendering of the context.
"""

import threading

from local_deep_research.metrics.token_counter import TokenCounter

_SECRET = "SuperSecretPassword-7f3a"


def _run_background_save(research_context):
    """Drive _save_to_db on a non-main thread; return when it finishes."""
    callback = TokenCounter().create_callback(
        "redaction-probe-1", research_context
    )

    def worker():
        callback._save_to_db(5, 7)

    thread = threading.Thread(target=worker, name="metrics-worker")
    thread.start()
    thread.join()


def test_no_username_warning_does_not_log_context_values(loguru_caplog):
    """A password-bearing, username-less context must never have its values
    written to the log — the warning may fire, but rendered without values.
    RED on main: the f-string dumps the whole dict verbatim."""
    context = {
        "user_password": _SECRET,
        "research_query": "benign query",
    }
    with loguru_caplog.at_level("DEBUG"):
        _run_background_save(context)

    assert _SECRET not in loguru_caplog.text, (
        "the no-username metrics warning logged the raw research context, "
        "including the plaintext user_password"
    )


def test_no_username_warning_still_fires_with_the_diagnostic(loguru_caplog):
    """The fix must not silence the diagnostic itself: the warning still
    fires and still says which context keys were present (so operators can
    see *why* the save failed) — just never the values."""
    context = {
        "user_password": _SECRET,
        "research_query": "benign query",
    }
    with loguru_caplog.at_level("DEBUG"):
        _run_background_save(context)

    warning_text = loguru_caplog.text
    assert "Cannot save token metrics" in warning_text, (
        "the missing-username diagnostic disappeared entirely"
    )
    assert "user_password" in warning_text, (
        "the diagnostic no longer names the context keys"
    )
    assert _SECRET not in warning_text