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.9 KB
Raw Blame
# allow: no-sut-import — guardian; greps history_routes.py source to keep sensitive data out of logs
"""Tests for sensitive data logging changes in history_routes.

Verifies that PR #1896 changes are in place:
- logger.debug used instead of logger.info for research detail lookups
- Request headers are not logged
- Request URL is not logged
- Full research query results are not logged

The Flask `web/routes/history_routes.py` blueprint was deleted in
the FastAPI migration; the equivalent live module is now
`web/routers/history.py`. Update the path accordingly so this
defensive lint still applies to the new code.
"""

from pathlib import Path

import pytest

# Resolve the source file path relative to this test file. Try the new
# FastAPI router first, fall back to the legacy Flask path only for
# branches that haven't merged the migration yet.
_BASE = (
    Path(__file__).resolve().parents[2] / "src" / "local_deep_research" / "web"
)
_NEW = _BASE / "routers" / "history.py"
_LEGACY = _BASE / "routes" / "history_routes.py"

if _NEW.exists():
    _ROUTES_FILE = _NEW
elif _LEGACY.exists():
    _ROUTES_FILE = _LEGACY
else:
    pytest.skip(
        "history routes module not found at routers/history.py or "
        "routes/history_routes.py",
        allow_module_level=True,
    )


def _read_source() -> str:
    """Read the history_routes.py source file directly from disk."""
    return _ROUTES_FILE.read_text(encoding="utf-8")


class TestSensitiveDataLogging:
    """Verify that sensitive data is not exposed through logging."""

    def test_uses_debug_not_info_for_details_route(self):
        """logger.info should not be used for the details route access log."""
        source = _read_source()
        assert 'logger.info(f"Details route' not in source, (
            "Details route log should use logger.debug, not logger.info"
        )
        assert "logger.debug" in source, (
            "history_routes should contain at least one logger.debug call"
        )

    def test_no_request_headers_logging(self):
        """Request headers must not be logged as they may contain auth tokens."""
        source = _read_source()
        assert "request.headers" not in source, (
            "request.headers should not appear in history_routes.py"
        )

    def test_no_request_url_logging(self):
        """Request URL must not be logged as it may contain sensitive params."""
        source = _read_source()
        assert "request.url" not in source, (
            "request.url should not appear in history_routes.py"
        )

    def test_no_full_research_query_result_logging(self):
        """Full research query result objects should not be logged."""
        source = _read_source()
        assert 'logger.info(f"Research query result' not in source, (
            "Full research query result should not be logged via logger.info"
        )

    def test_no_user_research_list_logging(self):
        """Full list of user research entries should not be logged."""
        source = _read_source()
        assert 'logger.info(f"All research for user' not in source, (
            "Full user research list should not be logged via logger.info"
        )

    def test_research_count_uses_debug(self):
        """Research count log should use debug level."""
        source = _read_source()
        assert "All research count" in source, (
            "Should log research count (not full list)"
        )
        assert 'logger.debug(f"All research count' in source, (
            "Research count should be logged at debug level"
        )

    def test_research_found_uses_debug_with_id_only(self):
        """Research found log should use debug and only include the ID."""
        source = _read_source()
        assert 'logger.debug(f"Research found:' in source, (
            "Research found log should use logger.debug"
        )
        assert "research.id if research else None" in source, (
            "Should log only research.id, not the full object"
        )