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.

16.3 KB
Raw Blame
"""Guards: Flask-port footguns that break silently under FastAPI.

Two response/request plumbing mistakes a literal Flask->FastAPI port
can make. Neither exists on the branch today (verified before this
guard was written -- the live scan found zero route-level tuple
returns and zero unawaited Request coroutines), so both are
preventive, in the same spirit as test_error_response_leakage.py:

1. ``return body, status_code`` (Flask's tuple idiom) in a router
   endpoint. Flask interpreted a ``(body, status)`` return as the
   response plus the HTTP status; FastAPI has no such convention --
   the tuple itself is the return value. A ``return {...}, 404``
   serialises as a two-element JSON *array* with HTTP 200 (a client
   checking ``response.ok`` sees success), and a
   ``return JSONResponse(...), 400`` fails to serialise the Response
   object at all. The fix is spelled-out status:
   ``JSONResponse(body, status_code=404)`` / ``Response(...,
   status_code=...)`` / ``raise HTTPException``.

   Scope discipline: only a return in the *endpoint's own body*
   counts. Nested helpers (``_load_sync``, ``_run_embedding_test``,
   ...) legitimately return tuples -- plain Python consumed by the
   enclosing function -- and are skipped, mirroring
   test_migration_antipattern_guards.py's dict-return scanner.

2. Unawaited Starlette ``Request`` coroutine methods --
   ``request.json()`` / ``request.form()`` / ``request.body()`` /
   ``request.stream()`` called without ``await``. These return
   coroutines; calling one without awaiting yields a coroutine object
   that is never scheduled (RuntimeWarning at GC, and the value is
   unusable -- comparisons/``.get()`` on it raise TypeError -> 500).
   The classic port slips a sync ``def`` handler that copies Flask's
   ``request.get_json()`` shape, or drops the ``await`` during
   translation. Only calls on a variable literally named ``request``
   are scanned (the codebase-wide convention for the Starlette
   Request), so ``response.json()`` on a ``requests``/``httpx``
   response -- which is sync and correct -- cannot false-positive.
   ``async with request.form() as form:`` is accepted: Starlette's
   ``form()`` returns a wrapper whose ``__aenter__`` awaits the parse
   and whose ``__aexit__`` closes the form, so the block consumes it.
   That exemption is ``form()``-only -- the other three have no such
   wrapper, and a sync ``with`` never awaits.

Both scanners are AST-based; comments/docstrings cannot trip them.
Scope: ``web/routers/*.py`` and the top-level ``web/*.py`` (endpoints
and the app live there; dependencies/services contain no endpoint
returns).
"""

import ast
from pathlib import Path

import local_deep_research.web as web_pkg
import local_deep_research.web.routers as routers_pkg

ROUTERS_DIR = Path(routers_pkg.__file__).resolve().parent
WEB_DIR = Path(web_pkg.__file__).resolve().parent

SCANNED_FILES = sorted({*ROUTERS_DIR.glob("*.py"), *WEB_DIR.glob("*.py")})

_HTTP_METHODS = {"get", "post", "put", "delete", "patch"}


def _rel(path: Path) -> str:
    return str(path.relative_to(ROUTERS_DIR.parents[3]))


def _parse(path: Path) -> ast.Module:
    return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))


def _annotate_parents(tree: ast.AST) -> ast.AST:
    for node in ast.walk(tree):
        for child in ast.iter_child_nodes(node):
            child.parent = node
    return tree


def _is_router_decorator(dec: ast.AST) -> bool:
    """``@router.get(...)`` / ``@app.post(...)`` etc. -- the endpoint
    convention across every router module and fastapi_app.py."""
    target = dec.func if isinstance(dec, ast.Call) else dec
    return (
        isinstance(target, ast.Attribute)
        and isinstance(target.value, ast.Name)
        and target.value.id in ("router", "app")
        and target.attr in _HTTP_METHODS
    )


def _own_scope_nodes(fn) -> "iter[ast.AST]":
    """Every node in *fn*'s own scope, skipping nested defs/lambdas/
    classes: a nested helper's ``return`` does not return the route."""
    stack = list(ast.iter_child_nodes(fn))
    while stack:
        node = stack.pop()
        if isinstance(
            node,
            (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda, ast.ClassDef),
        ):
            continue
        yield node
        stack.extend(ast.iter_child_nodes(node))


# ===========================================================================
# 1. Flask tuple-return idiom
# ===========================================================================


def find_tuple_returns_in_endpoints(tree: ast.AST):
    """-> [(lineno, func_name)] for every ``return <tuple>`` in a router
    endpoint's own scope. FastAPI does not interpret tuples: the tuple
    IS the response value (see module docstring)."""
    violations = []
    for fn in ast.walk(tree):
        if not isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        if not any(_is_router_decorator(d) for d in fn.decorator_list):
            continue
        for node in _own_scope_nodes(fn):
            if isinstance(node, ast.Return) and isinstance(
                node.value, ast.Tuple
            ):
                violations.append((node.lineno, fn.name))
    return violations


# file::func -> justification. (Currently empty: zero route-level tuple
# returns on the branch -- the only tuple returns near endpoints are in
# nested sync helpers consumed by the endpoint, which are plain Python.)
TUPLE_RETURN_ALLOWLIST: dict[str, str] = {}


def test_no_flask_tuple_returns_in_endpoints():
    violations = []
    for path in SCANNED_FILES:
        tree = _parse(path)
        for lineno, func_name in find_tuple_returns_in_endpoints(tree):
            key = f"{_rel(path)}::{func_name}"
            if key in TUPLE_RETURN_ALLOWLIST:
                continue
            violations.append(
                f"  {_rel(path)}:{lineno}: {func_name}() returns a tuple"
            )

    assert not violations, (
        "Router endpoint returns a tuple -- Flask's `return body, status` "
        "idiom. FastAPI has no tuple convention: the tuple itself is the "
        "response value, so `return {...}, 404` serialises as a 2-element "
        "JSON array with HTTP 200 (clients checking response.ok see "
        "success), and `return JSONResponse(...), 400` fails to serialise. "
        "Spell the status out: `JSONResponse(body, status_code=404)`, "
        "`Response(..., status_code=...)`, or `raise HTTPException(...)`. "
        "A nested helper returning a tuple is fine -- only the endpoint's "
        "own `return` counts.\n" + "\n".join(violations)
    )


class TestTupleReturnScannerSelfTest:
    def _scan(self, source):
        return find_tuple_returns_in_endpoints(ast.parse(source))

    def test_flags_dict_status_tuple(self):
        source = (
            "@router.get('/x')\n"
            "def h():\n"
            "    if bad:\n"
            "        return {'error': 'nope'}, 404\n"
            "    return {'ok': True}\n"
        )
        assert self._scan(source) == [(4, "h")]

    def test_flags_response_status_tuple(self):
        source = (
            "@router.post('/x')\n"
            "def h():\n"
            "    return JSONResponse({'a': 1}), 400\n"
        )
        assert self._scan(source) == [(3, "h")]

    def test_flags_app_decorator_endpoints_too(self):
        source = "@app.get('/x')\nasync def h():\n    return 'hello', 201\n"
        assert self._scan(source) == [(3, "h")]

    def test_ignores_nested_helper_tuple(self):
        source = (
            "@router.get('/x')\n"
            "def h():\n"
            "    def _load():\n"
            "        return 'strategy', 'parent'\n"
            "    a, b = _load()\n"
            "    return {'a': a, 'b': b}\n"
        )
        assert self._scan(source) == []

    def test_ignores_plain_dict_return(self):
        source = "@router.get('/x')\ndef h():\n    return {'status': 'ok'}\n"
        assert self._scan(source) == []

    def test_ignores_undecorated_function(self):
        source = "def h():\n    return 'a', 1\n"
        assert self._scan(source) == []


# ===========================================================================
# 2. Unawaited Starlette Request coroutine methods
# ===========================================================================


_COROUTINE_REQUEST_METHODS = frozenset({"json", "form", "body", "stream"})


def _is_async_with_form(node: ast.Call, parent: ast.AST | None) -> bool:
    """``async with request.form() as form:`` -- the one context-manager
    form Starlette supports. ``Request.form()`` returns a wrapper whose
    ``__aenter__`` awaits the parse and whose ``__aexit__`` closes the
    form (every spooled upload), so the block consumes the coroutine.
    ``json()``/``body()``/``stream()`` have no such wrapper, and a sync
    ``with`` never awaits, so neither is accepted here."""
    return (
        node.func.attr == "form"
        and isinstance(parent, ast.withitem)
        and parent.context_expr is node
        and isinstance(getattr(parent, "parent", None), ast.AsyncWith)
    )


def find_unawaited_request_coroutines(tree: ast.AST):
    """-> [(lineno, func_name, method)] for every
    ``request.json()/form()/body()/stream()`` call that is not consumed
    by ``await``, by ``async for`` (``stream()``) or by ``async with``
    (``form()`` only). These return coroutines; without ``await`` the
    call is never scheduled and the value is unusable."""
    violations = []
    for fn in ast.walk(tree):
        if not isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        for node in ast.walk(fn):
            if (
                isinstance(node, ast.Call)
                and isinstance(node.func, ast.Attribute)
                and node.func.attr in _COROUTINE_REQUEST_METHODS
                and isinstance(node.func.value, ast.Name)
                and node.func.value.id == "request"
            ):
                parent = getattr(node, "parent", None)
                # Safe forms: `await request.json()`, the async-iterator
                # idiom `async for chunk in request.stream():` (stream()
                # yields an async generator, not a coroutine) and the
                # form() context manager `async with request.form() as f:`
                # (see _is_async_with_form).
                consumed = (
                    isinstance(parent, ast.Await)
                    or (
                        isinstance(parent, ast.AsyncFor) and parent.iter is node
                    )
                    or _is_async_with_form(node, parent)
                )
                if not consumed:
                    violations.append((node.lineno, fn.name, node.func.attr))
    return violations


# file::func -> justification. (Currently empty.)
UNAWAITED_REQUEST_ALLOWLIST: dict[str, str] = {}


def test_no_unawaited_request_coroutines():
    violations = []
    for path in SCANNED_FILES:
        tree = _annotate_parents(_parse(path))
        for lineno, func_name, method in find_unawaited_request_coroutines(
            tree
        ):
            key = f"{_rel(path)}::{func_name}"
            if key in UNAWAITED_REQUEST_ALLOWLIST:
                continue
            violations.append(
                f"  {_rel(path)}:{lineno}: {func_name}() calls "
                f"request.{method}() without await"
            )

    assert not violations, (
        "request.json()/form()/body()/stream() called without `await`. "
        "These Starlette Request methods return coroutines; an unawaited "
        "call is never scheduled (RuntimeWarning at GC) and the result is "
        "a coroutine object, not the data -- every use of it raises "
        "TypeError, which FastAPI turns into an unhandled 500. Use "
        "`await request.json()` (and an `async def` handler), or declare "
        "the body as a Pydantic model parameter and let FastAPI parse it. "
        "Only calls on a variable named `request` are scanned, so sync "
        "`.json()` on a requests/httpx response cannot false-positive.\n"
        + "\n".join(violations)
    )


class TestUnawaitedRequestScannerSelfTest:
    def _scan(self, source):
        return find_unawaited_request_coroutines(
            _annotate_parents(ast.parse(source))
        )

    def test_flags_bare_call(self):
        source = (
            "@router.post('/x')\n"
            "def h(request):\n"
            "    data = request.json()\n"
            "    return data\n"
        )
        assert self._scan(source) == [(3, "h", "json")]

    def test_flags_sync_handler_form_call(self):
        source = (
            "@router.post('/x')\n"
            "def h(request):\n"
            "    return dict(request.form())\n"
        )
        assert self._scan(source) == [(3, "h", "form")]

    def test_ignores_awaited_call(self):
        source = (
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    return await request.json()\n"
        )
        assert self._scan(source) == []

    def test_ignores_other_base_names(self):
        """``response.json()`` on a requests/httpx response is sync."""
        source = (
            "@router.get('/x')\n"
            "def h():\n"
            "    r = session.get(url)\n"
            "    return r.json()\n"
        )
        assert self._scan(source) == []

    def test_ignores_sync_request_attributes(self):
        """query_params/headers are plain attributes -- not coroutines."""
        source = (
            "@router.get('/x')\n"
            "def h(request):\n"
            "    return dict(request.query_params)\n"
        )
        assert self._scan(source) == []

    def test_ignores_async_for_stream_idiom(self):
        """`async for chunk in request.stream():` is the correct
        async-iterator consumption (notes.py's body-cap dependency)."""
        source = (
            "async def cap(request):\n"
            "    body = bytearray()\n"
            "    async for chunk in request.stream():\n"
            "        body.extend(chunk)\n"
            "    return body\n"
        )
        assert self._scan(source) == []

    def test_ignores_async_with_form_context_manager(self):
        """`async with request.form() as form:` is the context-manager
        form Starlette supports: `__aenter__` awaits the parse and
        `__aexit__` closes the form (research.py's upload_pdf)."""
        source = (
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    async with request.form() as form:\n"
            "        return dict(form)\n"
        )
        assert self._scan(source) == []

    def test_flags_bare_form_statement(self):
        """A `request.form()` that nothing awaits, iterates or enters is
        still a dropped coroutine, `async def` handler or not."""
        source = (
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    request.form()\n"
            "    return {'ok': True}\n"
        )
        assert self._scan(source) == [(3, "h", "form")]

    def test_flags_async_with_on_other_request_methods(self):
        """Only form() has the context-manager wrapper: `async with
        request.json()` / `request.body()` is a coroutine misused as a
        context manager, and being a `with` item does not excuse it."""
        source = (
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    async with request.json() as data, request.body() as raw:\n"
            "        return raw\n"
        )
        assert self._scan(source) == [(3, "h", "json"), (3, "h", "body")]

    def test_flags_sync_with_form(self):
        """A plain `with` never awaits the wrapper's `__aenter__`."""
        source = (
            "@router.post('/x')\n"
            "async def h(request):\n"
            "    with request.form() as form:\n"
            "        return dict(form)\n"
        )
        assert self._scan(source) == [(3, "h", "form")]


def test_scan_covers_the_endpoint_surface():
    """Same floor discipline as the sibling guards: a layout move must
    not silently shrink SCANNED_FILES to near-nothing."""
    names = {p.name for p in SCANNED_FILES}
    assert len(SCANNED_FILES) >= 15, (
        f"Only {len(SCANNED_FILES)} files scanned ({sorted(names)}) -- "
        "the web/routers layout moved; update this test's scope."
    )
    for expected in ("fastapi_app.py", "research.py", "settings.py"):
        assert expected in names, (
            f"{expected} is no longer in the scanned scope -- update "
            "SCANNED_FILES."
        )