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.

6.7 KB
Raw Blame
"""PR #6564 optional #1: single source of truth for safe download tokens."""

import errno
import os

import pytest

from local_deep_research.security.client_safe_errors import (
    CLIENT_SAFE_DOWNLOAD_MESSAGES,
    client_safe_download_message,
)


class TestSharedTable:
    def test_services_and_html_aliases_share_identity(self):
        from local_deep_research.research_library.services.download_service import (
            _CLIENT_SAFE_DOWNLOAD_MESSAGES as svc,
            _client_safe_download_message as svc_fn,
        )
        from local_deep_research.research_library.downloaders.html import (
            _CLIENT_SAFE_HTML_MESSAGES as html,
            _client_safe_html_message as html_fn,
        )

        assert svc is CLIENT_SAFE_DOWNLOAD_MESSAGES
        assert html is CLIENT_SAFE_DOWNLOAD_MESSAGES

        exc = ValueError("INSERT INTO document_collections ...")
        assert svc_fn(exc) == "download_error:ValueError"
        assert html_fn(exc) == "download_error:ValueError"
        assert client_safe_download_message(exc) == "download_error:ValueError"

    def test_previously_drifted_keys_present(self):
        # These existed only on the services side before the shared module.
        assert (
            CLIENT_SAFE_DOWNLOAD_MESSAGES["DataError"] == "database_data_error"
        )
        assert (
            CLIENT_SAFE_DOWNLOAD_MESSAGES["InterfaceError"]
            == "database_interface_error"
        )
        assert (
            CLIENT_SAFE_DOWNLOAD_MESSAGES["InvalidRequestError"]
            == "database_invalid_request"
        )

        class DataError(Exception):
            pass

        DataError.__name__ = "DataError"
        assert (
            client_safe_download_message(DataError()) == "database_data_error"
        )

    def test_oserror_canonical_branch(self):
        err_no = 2  # ENOENT
        canonical = os.strerror(err_no)
        exc = OSError(err_no, canonical)
        assert (
            client_safe_download_message(exc) == f"filesystem_error:{canonical}"
        )

        # Custom message must NOT leak through.
        leaked = OSError(28, "leak /etc/passwd")
        assert client_safe_download_message(leaked) == "download_error:OSError"

    @pytest.mark.parametrize(
        ("exc_class", "err_no", "expected"),
        [
            (ConnectionResetError, errno.ECONNRESET, "network_reset"),
            (ConnectionAbortedError, errno.ECONNABORTED, "network_reset"),
            (BrokenPipeError, errno.EPIPE, "network_reset"),
            (ConnectionRefusedError, errno.ECONNREFUSED, "network_unavailable"),
        ],
    )
    def test_socket_errors_with_errno_keep_network_token(
        self, exc_class, err_no, expected
    ):
        # Real socket errors carry an errno and the canonical strerror, the
        # same shape the filesystem branch matches on.
        exc = exc_class(err_no, os.strerror(err_no))
        assert client_safe_download_message(exc) == expected

    def test_errno_less_socket_error_keeps_network_token(self):
        assert (
            client_safe_download_message(ConnectionAbortedError())
            == "network_reset"
        )

    def test_plain_connection_error_with_errno_keeps_network_token(self):
        exc = ConnectionError(errno.ECONNRESET, os.strerror(errno.ECONNRESET))
        assert client_safe_download_message(exc) == "network_unavailable"

    def test_socket_error_with_custom_strerror_does_not_leak(self):
        exc = BrokenPipeError(errno.EPIPE, "leak secret")
        msg = client_safe_download_message(exc)
        assert msg == "network_reset"
        assert "secret" not in msg

    def test_socket_error_token_gets_network_cooldown(self):
        from datetime import timedelta

        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
        )

        token = client_safe_download_message(
            ConnectionResetError(
                errno.ECONNRESET, os.strerror(errno.ECONNRESET)
            )
        )
        failure = FailureClassifier().classify_failure(
            error_type=token, details=token
        )
        assert failure.error_type == "network_error"
        assert failure.retry_after == timedelta(minutes=5)

    def test_never_echoes_exception_text(self):
        exc = RuntimeError("INSERT INTO document_collections VALUES (1)")
        msg = client_safe_download_message(exc)
        assert msg == "download_error:RuntimeError"
        assert "INSERT INTO" not in msg

    def test_pdf_storage_refusal_uses_a_fixed_reason(self):
        from local_deep_research.research_library.services.download_service import (
            PDFStorageLimitError,
        )

        exc = PDFStorageLimitError("private URL https://example.com/token")
        assert client_safe_download_message(exc) == "pdf_storage_size_limit"

    def test_pdf_storage_refusal_is_shown_as_how_to_lift_it(self):
        """A person sees which setting to raise, never the raw token, and
        every token the user-message table names is a real token."""
        from local_deep_research.security.client_safe_errors import (
            CLIENT_SAFE_DOWNLOAD_USER_MESSAGES,
            download_reason_for_user,
        )

        message = download_reason_for_user("pdf_storage_size_limit")
        assert "Maximum PDF Size (MB)" in message
        assert "pdf_storage_size_limit" not in message
        assert set(CLIENT_SAFE_DOWNLOAD_USER_MESSAGES) <= set(
            CLIENT_SAFE_DOWNLOAD_MESSAGES.values()
        )
        # Any other reason is passed through unchanged.
        assert download_reason_for_user("network_timeout") == "network_timeout"
        assert download_reason_for_user(None) is None


class TestHtmlOperatorLogCarriesClass:
    """PR #6564 optional #3: tokenized HTML failure logs its class."""

    def test_download_with_result_logs_exc_class_not_text(self):
        from unittest.mock import patch

        from local_deep_research.research_library.downloaders.html import (
            HTMLDownloader,
        )
        import local_deep_research.research_library.downloaders.html as html_mod

        downloader = HTMLDownloader()
        secret = "supersecret1234567890"
        boom = ValueError(f"fetch failed api_key={secret}")
        with (
            patch.object(downloader, "_fetch_html", side_effect=boom),
            patch.object(html_mod, "logger") as mock_logger,
        ):
            result = downloader.download_with_result("https://example.com")
        assert result.skip_reason == "download_error:ValueError"
        error_call = mock_logger.opt.return_value.error.call_args
        kwargs = error_call.kwargs
        assert kwargs.get("exc_class") == "ValueError"
        # Neither the message template nor the kwargs carry the text/secret.
        assert secret not in str(error_call.args) + str(kwargs)