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.

4.7 KB
Raw Blame
"""No traceback may be rendered from an auth-router frame holding a password.

``login``, ``register``, ``change_password`` and the post-login worker
(``_perform_post_login_tasks*``) hold the plaintext password in their
locals; ``_rollback_partial_session`` runs inside ``login`` and
``register``, and loguru's ``backtrace`` extends a traceback rendered
from it up into those caller frames.
A traceback rendered with loguru ``diagnose`` on prints every frame local,
and whether any sink has ``diagnose`` on is not knowable at the call site
(``config_logger`` enables it on the stderr sink when debug is on, from
the ``app.debug`` setting or the environment, and ``LDR_LOGURU_DIAGNOSE``
is set; loguru's default handler has it on). So these functions log
exception TYPES only. This source sweep pins that:
no ``logger.exception``, no ``exc_info=``, no ``logger.opt(exception=...)``
other than a literal False, and no ``logger.catch`` inside them.
"""

import ast
from pathlib import Path

import local_deep_research.web.routers.auth as auth_module

CREDENTIAL_FRAMES = {
    "login",
    "register",
    "change_password",
    "_perform_post_login_tasks",
    "_perform_post_login_tasks_body",
    "_rollback_partial_session",
}


def _traceback_emitters(func: ast.AST) -> list[str]:
    found = []
    for node in ast.walk(func):
        if not isinstance(node, ast.Call):
            continue
        for keyword in node.keywords:
            if keyword.arg == "exc_info":
                found.append(f"line {node.lineno}: exc_info=")
        target = node.func
        if not isinstance(target, ast.Attribute):
            continue
        if target.attr in {"exception", "catch"}:
            found.append(f"line {node.lineno}: .{target.attr}()")
        if target.attr == "opt":
            for keyword in node.keywords:
                if keyword.arg == "exception" and not (
                    isinstance(keyword.value, ast.Constant)
                    and keyword.value.value is False
                ):
                    found.append(f"line {node.lineno}: opt(exception=...)")
    return found


def test_credential_frames_render_no_tracebacks() -> None:
    source = Path(auth_module.__file__).read_text(encoding="utf-8")
    tree = ast.parse(source)
    functions = {
        node.name: node
        for node in ast.walk(tree)
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
        and node.name in CREDENTIAL_FRAMES
    }

    # Non-vacuous: every listed frame is still there to be checked.
    assert set(functions) == CREDENTIAL_FRAMES

    offenders = {
        name: hits
        for name, func in functions.items()
        if (hits := _traceback_emitters(func))
    }
    assert offenders == {}, offenders


def test_post_login_failure_with_diagnostics_does_not_render_credentials(
    monkeypatch, real_post_login_tasks
):
    """Exercise real Loguru rendering, including a sink with diagnose enabled."""
    import importlib
    import inspect
    from loguru._logger import Core, Logger

    session_module = importlib.import_module(
        "local_deep_research.web.auth.session_manager"
    )
    monkeypatch.setattr(
        session_module,
        "read_user_authorization_epoch",
        lambda username: "test-epoch",
    )
    manager = session_module.SessionManager()
    session_id = manager.create_session("alice")
    monkeypatch.setattr(auth_module, "session_manager", manager)

    isolated_logger = Logger(
        core=Core(),
        exception=None,
        depth=0,
        record=False,
        lazy=False,
        colors=False,
        raw=False,
        capture=True,
        patchers=[],
        extra={},
    )
    rendered = []
    records = []

    def capture(message):
        rendered.append(str(message))
        records.append(message.record)

    sink = isolated_logger.add(
        capture, format="{message}", diagnose=True, backtrace=True
    )
    password = "auth-password-opaque-canary"
    upstream_body = "opaque-upstream-credential-body"

    body_calls = []

    def fail_body(username, password, session_id):
        body_calls.append((username, password, session_id))
        raise RuntimeError(upstream_body)

    monkeypatch.setattr(auth_module, "logger", isolated_logger)
    monkeypatch.setattr(
        auth_module, "_perform_post_login_tasks_body", fail_body
    )
    try:
        worker = inspect.unwrap(real_post_login_tasks)
        assert worker("alice", password, session_id) is None
    finally:
        isolated_logger.remove(sink)

    assert body_calls == [("alice", password, session_id)]
    text = "".join(rendered)
    assert "alice" in text and "RuntimeError" in text
    assert password not in text
    assert upstream_body not in text
    assert records and all(record["exception"] is None for record in records)