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.

12.4 KB
Raw Blame
"""Unit tests for BodySizeLimitMiddleware (Flask MAX_CONTENT_LENGTH port).

Main capped every request body globally via Werkzeug's MAX_CONTENT_LENGTH
(413 on overflow); the FastAPI port dropped the cap, leaving unbounded
body buffering (the CSRF middleware reads whole form bodies, route
handlers read JSON/multipart). The middleware re-adds the cap with both
the declared-Content-Length fast path and chunk counting for streamed
bodies.
"""

import asyncio
import json

import pytest

from local_deep_research.web.fastapi_app import (
    BodySizeLimitMiddleware,
    _RequestBodyTooLarge,
)


def _scope(path="/api/v1/research", headers=None):
    return {
        "type": "http",
        "method": "POST",
        "path": path,
        "headers": headers or [],
    }


class _App:
    """Records whether the wrapped app ran; drains the body like a route."""

    def __init__(self):
        self.ran = False

    async def __call__(self, scope, receive, send):
        self.ran = True
        more = True
        while more:
            message = await receive()
            more = message.get("more_body", False)
        await send(
            {
                "type": "http.response.start",
                "status": 200,
                "headers": [(b"content-type", b"application/json")],
            }
        )
        await send({"type": "http.response.body", "body": b"{}"})


def _run(middleware, scope, body_chunks):
    """Drive the ASGI app; returns (status, body)."""
    sent = []
    chunks = list(body_chunks)

    async def receive():
        more = len(chunks) > 1
        return {
            "type": "http.request",
            "body": chunks.pop(0) if chunks else b"",
            "more_body": more,
        }

    async def send(message):
        sent.append(message)

    asyncio.run(middleware(scope, receive, send))

    status = next(
        m["status"] for m in sent if m["type"] == "http.response.start"
    )
    body = b"".join(
        m.get("body", b"") for m in sent if m["type"] == "http.response.body"
    )
    return status, body


class TestDeclaredContentLength:
    def test_over_cap_is_rejected_before_app_runs(self):
        app = _App()
        mw = BodySizeLimitMiddleware(app, max_body_size=100)
        scope = _scope(headers=[(b"content-length", b"101")])

        status, body = _run(mw, scope, [b""])

        assert status == 413
        assert json.loads(body) == {"error": "Request too large"}
        assert app.ran is False

    def test_under_cap_passes_through(self):
        app = _App()
        mw = BodySizeLimitMiddleware(app, max_body_size=100)
        scope = _scope(headers=[(b"content-length", b"100")])

        status, _ = _run(mw, scope, [b"x" * 100])

        assert status == 200
        assert app.ran is True

    def test_non_api_path_gets_branded_html_413(self):
        """Mirrors main's 413 errorhandler content negotiation.

        PR #5424 upgraded the non-API path's body from the plain
        "Request too large" string to the branded pages/error.html page
        (see fastapi_app.py::_render_branded_error_page); the plain text
        is now only the fallback if rendering that page fails -- see
        ``test_render_failure_falls_back_to_plain_text_413`` below. This
        test's own name used to say "plain_text", which is what this
        exact body is NOT: it asserts on ``data-error-page`` HTML markers.
        """
        mw = BodySizeLimitMiddleware(_App(), max_body_size=100)
        scope = _scope(
            path="/settings/save_settings",
            headers=[(b"content-length", b"101")],
        )

        status, body = _run(mw, scope, [b""])

        assert status == 413
        text = body.decode("utf-8")
        assert "data-error-page" in text
        assert 'data-status-code="413"' in text

    def test_render_failure_falls_back_to_plain_text_413(self, monkeypatch):
        """When rendering the branded page raises, the 413 must still be
        a well-formed plain-text response, not an unhandled 500.

        ``_render_branded_error_page``'s own ``except Exception``
        (fastapi_app.py) is the only thing standing between a broken
        template/Jinja global and an unhandled exception on every
        oversized non-API request; nothing pinned that swallow before
        this test. Not a regression guard (this fallback already worked
        on head, unrelated to PR #5424's tidy-round session-handling
        fix) -- coverage against it quietly being dropped later.
        """
        from local_deep_research.web import template_config

        def _boom(*args, **kwargs):
            raise RuntimeError("template render exploded")

        monkeypatch.setattr(
            template_config.templates, "TemplateResponse", _boom
        )

        mw = BodySizeLimitMiddleware(_App(), max_body_size=100)
        scope = _scope(
            path="/settings/save_settings",
            headers=[(b"content-length", b"101")],
        )

        status, body = _run(mw, scope, [b""])

        assert status == 413
        assert body == b"Request too large"


class TestStreamedBody:
    def test_chunked_body_over_cap_is_rejected(self):
        """No Content-Length (chunked transfer) must not bypass the cap."""
        app = _App()
        mw = BodySizeLimitMiddleware(app, max_body_size=100)

        status, body = _run(mw, _scope(), [b"x" * 60, b"x" * 60])

        assert status == 413
        assert json.loads(body) == {"error": "Request too large"}

    def test_chunked_body_under_cap_passes(self):
        app = _App()
        mw = BodySizeLimitMiddleware(app, max_body_size=100)

        status, _ = _run(mw, _scope(), [b"x" * 40, b"x" * 40])

        assert status == 200
        assert app.ran is True

    def test_overflow_after_response_start_does_not_emit_a_second_response(
        self,
    ):
        """Once an inner app starts a response it is too late to send 413.

        An ASGI app can start a streaming response before consuming the whole
        request body.  If a later chunk crosses the cap, the middleware must
        propagate the overflow so the server can tear down the connection;
        emitting a second ``http.response.start`` would violate the ASGI
        protocol and hide the incomplete original response behind a bogus
        413.
        """
        sent = []

        async def starts_then_reads(scope, receive, send):
            await send(
                {
                    "type": "http.response.start",
                    "status": 200,
                    "headers": [],
                }
            )
            await receive()

        async def receive():
            return {
                "type": "http.request",
                "body": b"x" * 101,
                "more_body": False,
            }

        async def send(message):
            sent.append(message)

        middleware = BodySizeLimitMiddleware(
            starts_then_reads, max_body_size=100
        )

        with pytest.raises(_RequestBodyTooLarge):
            asyncio.run(middleware(_scope(), receive, send))

        starts = [
            message
            for message in sent
            if message["type"] == "http.response.start"
        ]
        assert starts == [
            {"type": "http.response.start", "status": 200, "headers": []}
        ]
        assert all(message.get("status") != 413 for message in sent)


def test_default_cap_matches_flask_max_content_length():
    """Parity: main computed MAX_CONTENT_LENGTH from the upload validator
    constants."""
    from local_deep_research.security.file_upload_validator import (
        FileUploadValidator,
    )

    mw = BodySizeLimitMiddleware(_App())
    assert mw.max_body_size == (
        FileUploadValidator.MAX_FILES_PER_REQUEST
        * FileUploadValidator.MAX_FILE_SIZE
    )


def test_middleware_is_registered_on_the_app():
    """Fence: the cap must stay in the production middleware stack."""
    from local_deep_research.web.fastapi_app import app

    assert any(m.cls is BodySizeLimitMiddleware for m in app.user_middleware)


@pytest.mark.parametrize("bad", [b"not-a-number", b""])
def test_malformed_content_length_falls_back_to_counting(bad):
    """A garbage Content-Length is not trusted — the streamed counter
    still enforces the cap."""
    app = _App()
    mw = BodySizeLimitMiddleware(app, max_body_size=100)
    scope = _scope(headers=[(b"content-length", bad)])

    status, _ = _run(mw, scope, [b"x" * 101])

    assert status == 413


class TestMultipartCapIsGatedOnPathNotOnContentType:
    """A forged ``multipart/`` label must not buy the upload-sized cap.

    The cap used to be chosen from the client's declared Content-Type
    alone::

        is_multipart = content_type.lower().startswith(b"multipart/")

    Starlette's ``Request.json()`` is ``json.loads(await self.body())`` --
    it never looks at Content-Type -- so labelling a JSON body
    ``multipart/form-data`` handed that route ``max_body_size``
    (MAX_FILES_PER_REQUEST 200 x MAX_FILE_SIZE 3 GB = ~600 GB, i.e. no
    cap in practice) while the handler still parsed it as JSON. Every
    ``await request.json()`` route in the app was reachable this way.

    The two paths below are the only routes that genuinely consume a
    multipart body (``research.py::upload_pdf`` and
    ``rag.py::upload_to_collection``); the cap follows the path now, so a
    mislabelled body falls back to the ordinary JSON cap.
    """

    #: Bodies here declare a Content-Length so the middleware's fast path
    #: decides BEFORE the app is entered. Without it the body is counted
    #: mid-stream, the app has already run by the time the cap trips, and
    #: `app.ran` is True even on a correct rejection -- which is exactly why
    #: the sibling TestStreamedBody cases assert only on status.
    BODY = b"x" * 500
    MULTIPART = [
        (b"content-type", b"multipart/form-data; boundary=zz"),
        (b"content-length", b"500"),
    ]
    JSON_CT = [
        (b"content-type", b"application/json"),
        (b"content-length", b"500"),
    ]

    def test_forged_multipart_on_a_json_route_gets_the_json_cap(self):
        """The bug: 413 expected, and it did NOT fire before the fix."""
        app = _App()
        mw = BodySizeLimitMiddleware(
            app, max_body_size=10_000, max_json_body_size=100
        )
        status, _ = _run(
            mw,
            _scope(
                path="/research/api/save-raw-config", headers=self.MULTIPART
            ),
            [self.BODY],
        )
        assert status == 413, (
            "a multipart-labelled body on a JSON route was granted the "
            "upload cap; the cap must follow the path, not the header"
        )
        assert not app.ran

    @pytest.mark.parametrize(
        "path",
        [
            "/api/upload/pdf",
            "/library/api/collections/42/upload",
            "/library/api/collections/a-uuid-like-name/upload",
        ],
    )
    def test_real_upload_paths_still_get_the_large_cap(self, path):
        """Negative control: the fix must not break actual uploads."""
        app = _App()
        mw = BodySizeLimitMiddleware(
            app, max_body_size=10_000, max_json_body_size=100
        )
        status, _ = _run(
            mw, _scope(path=path, headers=self.MULTIPART), [self.BODY]
        )
        assert status == 200, f"{path} is a real upload route; 500B < 10kB cap"
        assert app.ran

    @pytest.mark.parametrize(
        "path",
        [
            "/library/api/collections/42/upload/../../evil",
            "/library/api/collections/42/uploadx",
            "/library/api/collections/a/b/upload",
            "/api/upload/pdf/extra",
        ],
    )
    def test_lookalike_paths_do_not_get_the_large_cap(self, path):
        """The matcher is anchored, so near-misses stay on the JSON cap."""
        app = _App()
        mw = BodySizeLimitMiddleware(
            app, max_body_size=10_000, max_json_body_size=100
        )
        status, _ = _run(
            mw, _scope(path=path, headers=self.MULTIPART), [self.BODY]
        )
        assert status == 413, f"{path} is not an upload route"

    def test_honest_json_on_an_upload_path_still_gets_the_json_cap(self):
        """Both halves are required: the label matters too, not just path."""
        app = _App()
        mw = BodySizeLimitMiddleware(
            app, max_body_size=10_000, max_json_body_size=100
        )
        status, _ = _run(
            mw,
            _scope(path="/api/upload/pdf", headers=self.JSON_CT),
            [self.BODY],
        )
        assert status == 413