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.

5.4 KB
Raw Blame
"""User-database errors must not echo bound document URLs into logs (#6829)."""

import pytest
from loguru import logger
from sqlalchemy import text
from sqlalchemy.exc import IntegrityError, StatementError
from unittest.mock import MagicMock, patch

from local_deep_research.database.encrypted_db import DatabaseManager


@pytest.fixture(params=(True, False), ids=("encrypted", "plaintext"))
def manager(tmp_path, monkeypatch, request):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    if request.param:
        pytest.importorskip("sqlcipher3")
    else:
        monkeypatch.setattr(
            DatabaseManager, "_check_encryption_available", lambda self: False
        )
    db_manager = DatabaseManager()
    assert db_manager.has_encryption is request.param
    db_manager.data_dir = tmp_path
    yield db_manager
    db_manager.close_all_databases()


def _assert_duplicate_url_is_hidden(engine, url):
    # SQLAlchemy wraps the errors of plain SQLite and of the SQLCipher driver
    # alike as IntegrityError. The wrapper's message renders the statement's
    # bound values unless hide_parameters is set.
    with pytest.raises(
        IntegrityError, match="UNIQUE constraint failed"
    ) as failure:
        with engine.begin() as connection:
            connection.execute(
                text("INSERT INTO url_canary (url) VALUES (:url)"),
                {"url": url},
            )

    rendered = str(failure.value)
    assert "user:private-password" not in rendered
    assert "private-path" not in rendered
    assert "private-token" not in rendered


def test_user_db_create_and_reopen_hide_url_parameters(manager):
    """Both engine constructors hide bound values in real SQL errors."""
    username = "url_error_mask"
    password = "ParamMaskPw1!"  # noqa: S105
    url = (
        "https://user:[email protected]/private-path"
        "?token=private-token"
    )

    engine = manager.create_user_database(username, password)
    assert engine.hide_parameters is True
    with engine.begin() as connection:
        connection.execute(text("CREATE TABLE url_canary (url TEXT UNIQUE)"))
        connection.execute(
            text("INSERT INTO url_canary (url) VALUES (:url)"),
            {"url": url},
        )
    _assert_duplicate_url_is_hidden(engine, url)

    manager.close_user_database(username)
    reopened = manager.open_user_database(username, password)
    assert reopened is not None
    assert reopened.hide_parameters is True
    _assert_duplicate_url_is_hidden(reopened, url)


def test_failed_document_record_logs_only_exception_class_with_diagnose():
    """A debug sink must not render SQLAlchemy's bound URL from frame locals."""
    from local_deep_research.research_library.services import (
        download_service as ds,
    )

    url = (
        "https://user:[email protected]/private-path"
        "?token=private-token"
    )
    failure = StatementError(
        "insert failed",
        "INSERT INTO documents (original_url) VALUES (:original_url)",
        {"original_url": url},
        RuntimeError("duplicate document"),
    )
    service = ds.DownloadService.__new__(ds.DownloadService)
    service.username = "alice"
    service.password = "pw"  # noqa: S105
    resource = MagicMock(id=7, url=url, research_id="r1")
    session = MagicMock()
    session.add.side_effect = failure
    records = []
    sink = logger.add(
        lambda message: records.append(str(message)),
        format="{message}",
        level="ERROR",
        diagnose=True,
    )
    logger.enable("local_deep_research")
    try:
        with (
            patch.object(ds, "get_document_for_resource", return_value=None),
            patch.object(ds, "get_source_type_id", return_value="st"),
            patch.object(ds, "Document"),
        ):
            service._record_failed_text_extraction(
                session, resource, "extraction failed"
            )
    finally:
        logger.disable("local_deep_research")
        logger.remove(sink)

    rendered = "\n".join(records)
    assert "Error recording failed text extraction (StatementError)" in rendered
    assert "private-password" not in rendered
    assert "private-path" not in rendered
    assert "private-token" not in rendered


def test_cached_arxiv_save_failure_returns_fixed_error_token():
    """A SQL error from an offline arXiv save must not enter the SSE result."""
    from local_deep_research.research_library.services import (
        download_service as ds,
    )

    url = (
        "https://user:[email protected]/private-path"
        "?token=private-token"
    )
    failure = StatementError(
        "insert failed",
        "INSERT INTO documents (original_url) VALUES (:original_url)",
        {"original_url": url},
        RuntimeError("duplicate document"),
    )
    service = ds.DownloadService.__new__(ds.DownloadService)
    session = MagicMock()
    resource = MagicMock(url="https://arxiv.org/abs/2401.12345")
    with (
        patch.object(
            service, "_load_cached_arxiv_pdf", return_value=(b"%PDF", "doc1")
        ),
        patch.object(
            ds.ArxivDownloader,
            "extract_text_from_pdf",
            return_value="Paper text",
        ),
        patch.object(service, "_save_text_with_db", side_effect=failure),
    ):
        result = service._text_from_cached_pdf(session, resource)

    assert result == (False, "download_error:StatementError")