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.8 KB
Raw Blame
"""Every news handler must scrub a caught exception before returning it.

`safe_error_message()` is the sole error scrubber for the ~25 handlers in
`web/routers/news_flask_api.py` (CWE-209). Its *behaviour* is covered directly by
`tests/security/test_news_scheduler_isolation_fastapi.py`. This file covers the
other half — the **wiring** — which behavioural tests structurally cannot reach:
a perfectly good scrubber is worthless at a call site that does not use it, and
that is not a hypothetical failure mode in this repo. The original news SSRF
existed precisely because validated helpers sat behind a route that never called
them, and the coverage audit (ADR-0010) found the same shape again in
`library_delete.py:288`.

The check is static rather than behavioural on purpose. Driving all ~25 handlers
into every one of their exception branches over HTTP would need a fault-injection
harness per branch, and would still only prove the branches it managed to reach.
An AST walk proves the property for *every* branch, including the ones no test
exercises — which are exactly the ones that rot.

What is asserted: inside an ``except ... as e`` block, the caught exception may
not flow into a response body unless it passes through ``safe_error_message``.
Using ``str(e)`` to *inspect* the message and branch on it is fine and is what
the vote/feedback handlers legitimately do; what is forbidden is putting the
exception text where a client can read it.
"""

import ast
import inspect

import pytest

from local_deep_research.web.routers import news_flask_api

# The module under test is IMPORTED, not opened by path. Two reasons beyond
# satisfying the repo's shadow-test hook: the test follows the module if it is
# moved or renamed (a hardcoded path would silently start analysing nothing),
# and `SCRUBBER_FN` below is a real reference, so a rename of the scrubber is a
# collection error here rather than a quietly weakened check.
MODULE = news_flask_api
MODULE_PATH = inspect.getsourcefile(MODULE)
SCRUBBER_FN = MODULE.safe_error_message
SCRUBBER = SCRUBBER_FN.__name__

# Builders whose first positional argument becomes the response body.
# ``_subscription_validation_error`` wraps its message in a JSONResponse, so it
# counts too: the ``_subscription_validation_error(str(error))`` call sites that
# CodeQL flagged (alert #8185) slipped past this check while it was missing.
_RESPONSE_BUILDERS = {
    "JSONResponse",
    "HTMLResponse",
    "PlainTextResponse",
    MODULE._subscription_validation_error.__name__,
}

# A handler count far below the real one would mean the walk stopped finding
# things — see test_detector_is_not_scanning_an_empty_module.
_MIN_EXCEPT_HANDLERS = 20


def _source():
    return inspect.getsource(MODULE)


def _leaks_in(source: str):
    """Return [(lineno, snippet)] where a caught exception reaches a response.

    A reference to the exception variable counts as a leak when it appears
    inside a response-body expression and is not wrapped in a
    ``safe_error_message(...)`` call.
    """
    tree = ast.parse(source)
    leaks = []

    def _exception_refs_outside_scrubber(node, exc_name):
        """Names matching ``exc_name`` in ``node``, skipping scrubber calls."""
        found = []

        class _Walk(ast.NodeVisitor):
            def visit_Call(self, call):
                func = call.func
                is_scrubber = (
                    isinstance(func, ast.Name) and func.id == SCRUBBER
                ) or (isinstance(func, ast.Attribute) and func.attr == SCRUBBER)
                if is_scrubber:
                    # Anything inside the scrubber call is by definition safe.
                    return
                self.generic_visit(call)

            def visit_Name(self, name):
                if name.id == exc_name:
                    found.append(name)

        _Walk().visit(node)
        return found

    class _Handlers(ast.NodeVisitor):
        def visit_ExceptHandler(self, handler):
            exc_name = handler.name
            if exc_name:
                for node in ast.walk(handler):
                    body_exprs = []
                    if isinstance(node, ast.Call):
                        func = node.func
                        callee = getattr(func, "id", None) or getattr(
                            func, "attr", None
                        )
                        if callee in _RESPONSE_BUILDERS and node.args:
                            body_exprs.append(node.args[0])
                    # `return {"error": ...}` — a bare dict body
                    if isinstance(node, ast.Return) and isinstance(
                        node.value, ast.Dict
                    ):
                        body_exprs.append(node.value)

                    for expr in body_exprs:
                        for ref in _exception_refs_outside_scrubber(
                            expr, exc_name
                        ):
                            snippet = (
                                ast.get_source_segment(source, expr) or ""
                            ).replace("\n", " ")[:100]
                            leaks.append((ref.lineno, snippet))
            self.generic_visit(handler)

    _Handlers().visit(tree)
    return leaks


def _count_except_handlers(source: str) -> int:
    return sum(
        1
        for n in ast.walk(ast.parse(source))
        if isinstance(n, ast.ExceptHandler)
    )


class TestDetectorIsNotVacuous:
    """The checks below are only worth anything if the walk actually walks.

    A refactor that renames the module, or an AST change that stops matching
    the response builders, would otherwise turn every assertion in this file
    into "found no leaks in nothing".
    """

    def test_module_is_readable_and_parses(self):
        assert "def " in _source()

    def test_the_scrubber_is_the_real_function(self):
        """A real reference to production code, so a rename cannot leave this
        file quietly analysing a scrubber that no longer exists."""
        assert callable(SCRUBBER_FN)
        assert SCRUBBER_FN.__module__ == MODULE.__name__

    def test_detector_is_not_scanning_an_empty_module(self):
        handlers = _count_except_handlers(_source())
        assert handlers >= _MIN_EXCEPT_HANDLERS, (
            f"only {handlers} except-handlers found in {MODULE_PATH}; the "
            "detector is probably no longer looking at the right module"
        )

    def test_the_scrubber_is_actually_used_here(self):
        assert _source().count(f"{SCRUBBER}(") >= 20, (
            "news handlers barely reference the scrubber — either it was "
            "renamed or the error paths stopped using it"
        )

    @pytest.mark.parametrize(
        "planted",
        [
            'return JSONResponse({"error": str(e)}, status_code=500)',
            'return JSONResponse({"error": f"failed: {e}"}, status_code=500)',
            'return {"error": str(e)}',
            "return _subscription_validation_error(str(e))",
        ],
    )
    def test_detector_catches_a_planted_leak(self, planted):
        """Self-test: the detector must fail on code that does leak.

        Without this, a detector that silently matched nothing would report a
        clean bill of health for a module full of leaks.
        """
        source = "\n".join(
            [
                "from fastapi.responses import JSONResponse",
                "async def handler():",
                "    try:",
                "        pass",
                "    except Exception as e:",
                f"        {planted}",
            ]
        )
        assert _leaks_in(source), f"detector missed a planted leak: {planted}"

    def test_detector_accepts_a_scrubbed_response(self):
        """...and must NOT fire on the correct pattern, or it would be
        impossible to satisfy and equally useless."""
        source = "\n".join(
            [
                "from fastapi.responses import JSONResponse",
                "async def handler():",
                "    try:",
                "        pass",
                "    except Exception as e:",
                '        return JSONResponse({"error": safe_error_message(e, "x")},',
                "                            status_code=500)",
            ]
        )
        assert _leaks_in(source) == []

    def test_detector_allows_inspecting_the_message_to_branch(self):
        """`str(e)` used to CHOOSE a fixed response is not a leak.

        The vote and feedback handlers do exactly this — they check for
        "not found" and return a canned 404. Flagging that would push authors
        toward deleting a useful distinction to satisfy a test.
        """
        source = "\n".join(
            [
                "from fastapi.responses import JSONResponse",
                "async def handler():",
                "    try:",
                "        pass",
                "    except ValueError as e:",
                "        msg = str(e)",
                '        if "not found" in msg.lower():',
                '            return JSONResponse({"error": "Resource not found"},',
                "                                status_code=404)",
                '        return JSONResponse({"error": safe_error_message(e, "x")},',
                "                            status_code=400)",
            ]
        )
        assert _leaks_in(source) == []


class TestNoNewsHandlerLeaksACaughtException:
    def test_every_except_block_scrubs_before_responding(self):
        leaks = _leaks_in(_source())

        assert not leaks, (
            "a caught exception reaches a client-visible response body without "
            f"passing through {SCRUBBER}() in {MODULE_PATH}:\n"
            + "\n".join(f"  line {ln}: {snippet}" for ln, snippet in leaks)
            + "\n\nException text can carry filesystem paths, connection "
            "strings and SQL fragments (CWE-209). Wrap it: "
            f'JSONResponse({{"error": {SCRUBBER}(e, "what failed")}}, ...)'
        )