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.

9.5 KB
Raw Blame
"""Module-level helpers of ``web/routers/news_flask_api.py``.

Ported from four files the Flask->FastAPI migration deleted:
``tests/news/test_safe_error_message_behavior.py``,
``tests/news/test_flask_api_helpers.py``,
``tests/news/test_flask_api_extended.py`` and
``tests/news/test_flask_api_routes.py`` (all against
``news/flask_api.py``, whose successor is
``web/routers/news_flask_api.py``).

WHAT IS *NOT* RE-ASSERTED HERE
------------------------------
``tests/security/test_news_scheduler_isolation_fastapi.py::
TestSafeErrorMessageRedaction`` already pins, better than the deleted
files did, that the scrubber leaks nothing and that ValueError/KeyError/
TypeError/RuntimeError map to their four fixed strings.  What it does
NOT pin is the *default arm*: it only ever exercises ``RuntimeError`` and
``Exception`` as "generic".  Every other builtin the deleted files
enumerated -- ``OSError``, ``AttributeError``, ``IndexError``,
``ConnectionError``, ``UnicodeDecodeError`` -- would still take the
generic arm today, but a future ``if isinstance(e, OSError): return
str(e)`` ("just this one is safe, it's only a filename") passes every
test on the branch.  That is the mutation these tests exist to catch, and
it is the exact shape of the CWE-209 leak the scrubber was written for:
``OSError`` is the class that carries filesystem paths.

``get_user_id`` has no test at all on the branch.  Note honestly: it is
currently DEAD CODE -- ``grep -rn get_user_id src/`` finds only the
definition, because every route now takes ``username`` from
``Depends(require_auth)``.  The deleted tests are ported anyway rather
than dropped, because the helper is still exported by a module other
code imports from, and an untested public helper that returns a username
is exactly the thing a future route reaches for.  If it is deleted
instead, delete ``TestGetUserId`` with it.

The ``TestFlaskApiFieldMapping`` / ``TestSubscriptionIdValidation`` /
``TestVoteValidation`` classes of the deleted
``test_safe_error_message_behavior.py`` were tautologies -- they built a
local dict or list literal and asserted about *that*, never importing the
module.  Deleting the guard in the source could not have turned any of
them red.  Their subjects are ported as real assertions instead:
subscription-id and vote validation at the route in
``tests/web/routers/test_news_api_routes_ported.py``, and the
update_subscription field mapping is already pinned behaviourally by
``tests/news/test_news_router_contracts.py::
test_update_field_mapping_cannot_reassign_ownership``.
"""

import pytest

from local_deep_research.web.routers.news_flask_api import (
    get_user_id,
    safe_error_message,
)


class _CustomNewsError(Exception):
    """A caller-defined exception, as raised by news/api.py wrappers."""


# Exceptions that must fall through to the generic arm. Each carries a
# payload that would be a disclosure if the arm were ever "helpfully"
# widened to echo str(e).
GENERIC_ARM_EXCEPTIONS = [
    pytest.param(
        OSError("/home/victim/.config/ldr/encrypted_databases/victim.db"),
        "/home/victim",
        id="os_error",
    ),
    pytest.param(
        AttributeError("'NoneType' object has no attribute 'password_hash'"),
        "password_hash",
        id="attribute_error",
    ),
    pytest.param(
        IndexError("list index out of range in _rows[42]"),
        "_rows[42]",
        id="index_error",
    ),
    pytest.param(
        ConnectionError("Connection refused at 192.168.1.1:5432"),
        "192.168.1.1",
        id="connection_error",
    ),
    pytest.param(
        RuntimeError('Traceback: at line 42 in File "secret.py"'),
        "secret.py",
        id="runtime_error",
    ),
    pytest.param(
        _CustomNewsError("internal_column_name"),
        "internal_column_name",
        id="custom_exception",
    ),
]


class TestGenericArm:
    """Everything that is not ValueError/KeyError/TypeError is generic.

    The three special-cased classes are covered by the security file; this
    class covers the default arm, which is where a leak would be added.
    """

    @pytest.mark.parametrize("exc,payload", GENERIC_ARM_EXCEPTIONS)
    def test_takes_the_generic_arm(self, exc, payload):
        assert (
            safe_error_message(exc, "loading data")
            == "An error occurred while loading data"
        )

    @pytest.mark.parametrize("exc,payload", GENERIC_ARM_EXCEPTIONS)
    def test_payload_never_reaches_the_message(self, exc, payload):
        assert payload not in safe_error_message(exc, "loading data")

    @pytest.mark.parametrize(
        "exc,expected",
        [
            (
                UnicodeDecodeError(
                    "utf-8", b"\xff\xfe", 0, 1, "invalid start byte"
                ),
                "Invalid input provided",
            ),
            (
                ValueError("Invalid value: \u4e2d\u6587"),
                "Invalid input provided",
            ),
        ],
        ids=["unicode_decode_error", "unicode_in_message"],
    )
    def test_unicode_payloads_do_not_crash_or_leak(self, exc, expected):
        """``UnicodeDecodeError`` subclasses ``ValueError``, so it takes the
        typed arm -- pinned so a reader does not "fix" it into the generic
        arm above. Either way the payload must not survive."""
        result = safe_error_message(exc, "parsing")
        assert result == expected
        assert "invalid start byte" not in result
        assert "\u4e2d\u6587" not in result

    def test_an_exception_with_no_message_is_still_a_useful_string(self):
        """``str(Exception())`` is "" -- the result must not degrade to it."""
        result = safe_error_message(Exception(), "loading data")
        assert result == "An error occurred while loading data"


class TestContextInterpolation:
    """The one variable channel in the output.

    ``test_only_the_developer_supplied_context_is_interpolated`` in the
    security file proves every call site passes a literal; these pin what
    the function does with the literal once it has it.
    """

    def test_context_is_appended_after_while(self):
        assert (
            safe_error_message(Exception("err"), "getting news feed")
            == "An error occurred while getting news feed"
        )

    def test_empty_context_omits_the_while_clause(self):
        result = safe_error_message(Exception("err"), "")
        assert result == "An error occurred"
        assert "while" not in result

    def test_omitted_context_omits_the_while_clause(self):
        assert safe_error_message(Exception("err")) == "An error occurred"

    def test_none_context_omits_the_while_clause(self):
        """``None`` is falsy, so it must take the no-context branch rather
        than rendering the literal string "while None"."""
        result = safe_error_message(Exception("err"), None)
        assert result == "An error occurred"
        assert "None" not in result

    @pytest.mark.parametrize(
        "context",
        [
            "getting news feed!@#$%^&*()",
            "processing 42 items",
            "  padded  ",
            "chargement des données 新闻",
            "x" * 500,
        ],
        ids=["special", "numeric", "whitespace", "unicode", "long"],
    )
    def test_context_is_interpolated_verbatim(self, context):
        assert (
            safe_error_message(Exception("err"), context)
            == f"An error occurred while {context}"
        )

    @pytest.mark.parametrize(
        "exc,expected",
        [
            (ValueError("err"), "Invalid input provided"),
            (KeyError("err"), "Required data missing"),
            (TypeError("err"), "Invalid data format"),
        ],
        ids=["value", "key", "type"],
    )
    def test_context_is_not_appended_to_the_typed_arms(self, exc, expected):
        """The three typed messages are fixed strings; appending the
        context to them would put a developer-supplied string into a
        response body that today never carries one."""
        assert safe_error_message(exc, "loading data") == expected


class _Req:
    """Minimal stand-in for a Starlette Request's ``.session`` mapping."""

    def __init__(self, session):
        self.session = session


class TestGetUserId:
    """``get_user_id`` -- the port of main's ``current_user()`` reader.

    No test on the branch. Its contract is "an authenticated username or
    None"; the failure mode it guards against is returning a falsy-but-
    not-None value (``""``), which every caller would then treat as a
    real user id and use to select a per-user database.
    """

    def test_no_request_returns_none(self):
        """Legacy callers that pass nothing get None, never a stray user."""
        assert get_user_id() is None
        assert get_user_id(None) is None

    def test_returns_the_session_username(self):
        assert get_user_id(_Req({"username": "alice"})) == "alice"

    def test_empty_session_returns_none(self):
        assert get_user_id(_Req({})) is None

    def test_empty_username_returns_none(self):
        """An empty string must normalise to None, not pass through."""
        assert get_user_id(_Req({"username": ""})) is None

    def test_request_without_a_session_returns_none(self):
        """SessionMiddleware missing -> no ``.session`` attribute at all."""

        class _NoSession:
            pass

        assert get_user_id(_NoSession()) is None

    @pytest.mark.parametrize(
        "username",
        ["[email protected]", "user-name_123", "用户", "user.with.dots"],
    )
    def test_special_characters_survive_verbatim(self, username):
        assert get_user_id(_Req({"username": username})) == username