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.

28.8 KB
Raw Blame
"""Boundary behaviour of the multipart upload layer after the FastAPI port.

Werkzeug's multipart parser was replaced by Starlette's
``python-multipart``. The two parsers differ in what they *tolerate*, in
what Python type they hand back for a part, and in which layer raises on
a malformed body — so the interesting failures live between the parser
and the handler, not inside either one.

Scope of this file, and what it deliberately does not repeat:

* ``sanitize_filename`` is unit-tested in
  ``tests/security/test_filename_sanitizer.py``. Here the question is
  whether a hostile ``filename`` actually *survives the wire* into the
  handler and gets sanitized there — a unit test of the sanitizer says
  nothing about whether the route calls it.
* ``BodySizeLimitMiddleware``'s path-vs-Content-Type gate is unit-tested
  with an injected fake app and injected caps in
  ``tests/web/test_body_size_limit.py``. Here the cap is exercised
  through the *assembled* app with the *real* default cap, which is what
  a deployment actually runs.
* Collection-upload dedup semantics are covered in
  ``tests/web/routers/test_collection_upload_dedup.py`` and are not
  touched here.

Every over-cap case below declares an oversized ``Content-Length``
instead of transmitting the bytes: the middleware's fast path decides
before the body is read, so a header is sufficient and a 600 GB payload
is not.
"""

import asyncio
import uuid

import pytest
from fastapi.testclient import TestClient

BOUNDARY = "----LDRmultipartBoundaryTest"
MULTIPART_CT = f"multipart/form-data; boundary={BOUNDARY}"

#: Has the PDF magic bytes but no object graph, so it clears
#: ``validate_mime_type`` and fails ``validate_pdf_structure``. That is
#: deliberate: these tests are about the *filename* and the *parser*, and
#: a body that dies at structure validation still proves the file reached
#: the handler under the name we want to inspect.
PDF_MAGIC_ONLY = b"%PDF-1.4\ntruncated object graph\n%%EOF\n"

#: The handler's blanket filename rejection. Several tests turn on
#: whether this specific string is or is not what came back.
FILENAME_REJECTED = "Rejected file: invalid or disallowed filename"


def _part(field, filename, data, ctype="application/pdf"):
    """One multipart part. ``filename=None`` omits the parameter entirely.

    Omitting it is the whole point of several tests below: a part with no
    ``filename`` is a *form field*, and python-multipart hands it back as
    ``str``, not ``UploadFile``.
    """
    if filename is None:
        disposition = f'form-data; name="{field}"'
    else:
        disposition = f'form-data; name="{field}"; filename="{filename}"'
    head = (
        f"--{BOUNDARY}\r\n"
        f"Content-Disposition: {disposition}\r\n"
        f"Content-Type: {ctype}\r\n\r\n"
    ).encode()
    return head + data + b"\r\n"


def _body(*parts):
    return b"".join(parts) + f"--{BOUNDARY}--\r\n".encode()


def _post_multipart(client, raw, path="/api/upload/pdf", ct=MULTIPART_CT):
    """POST a hand-built body, refusing to let a 429 masquerade as a result.

    The route carries ``@upload_rate_limit_user`` (10/min). If the
    limiter ever starts firing in-process, a rate-limited response would
    otherwise be silently asserted against as if it were the parser's
    verdict.
    """
    resp = client.post(path, content=raw, headers={"Content-Type": ct})
    assert resp.status_code != 429, (
        "upload rate limiter fired during the test run; the assertions "
        "below would be testing the limiter, not the multipart layer"
    )
    return resp


def _errors(resp):
    """The per-file ``errors`` list, insisting the handler really ran."""
    body = resp.json()
    assert "errors" in body, (
        f"expected the per-file error path, got {resp.status_code}: "
        f"{resp.text[:300]}"
    )
    return body["errors"]


def _reported_name(error_entry):
    """``upload_pdf`` formats per-file errors as ``f"{filename}: {msg}"``."""
    return error_entry.split(": ", 1)[0]


@pytest.fixture(scope="module")
def upload_client():
    """Authenticated client (same bootstrap as test_collection_upload_http)."""
    from local_deep_research.web.fastapi_app import app

    client = TestClient(app, raise_server_exceptions=False)
    user = f"test_multipart_{uuid.uuid4().hex[:8]}"
    password = "TestPassword123!"  # noqa: S105

    def _csrf():
        client.get("/auth/login")
        resp = client.get("/auth/csrf-token")
        if resp.status_code != 200:
            return ""
        return resp.json().get("csrf_token", "")

    client.post(
        "/auth/register",
        data={
            "username": user,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    login = client.post(
        "/auth/login",
        data={
            "username": user,
            "password": password,
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    if login.status_code != 302:
        pytest.fail(
            f"auth bootstrap broken: login returned {login.status_code} "
            f"(expected 302): {login.text[:300]}"
        )

    token_resp = client.get("/auth/csrf-token")
    token = ""
    if token_resp.status_code == 200:
        token = token_resp.json().get("csrf_token", "")
    client.headers.update({"X-CSRFToken": token})

    yield client

    client.post("/auth/logout", follow_redirects=False)


# ---------------------------------------------------------------------------
# Filename sanitisation, exercised through the real parser
# ---------------------------------------------------------------------------


@pytest.mark.timeout(120)
class TestFilenameSanitisationOverTheWire:
    """A hostile ``filename`` must not survive the parser->handler hop.

    python-multipart performs no sanitisation of its own: whatever bytes
    sit between the quotes in ``Content-Disposition`` become
    ``UploadFile.filename`` verbatim. The only defence is the handler's
    ``sanitize_filename`` call, so these assert on what comes back out.
    """

    def test_traversal_is_reduced_to_a_basename(self, upload_client):
        resp = _post_multipart(
            upload_client,
            _body(_part("files", "../../../etc/passwd.pdf", PDF_MAGIC_ONLY)),
        )

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert len(errors) == 1, errors
        assert _reported_name(errors[0]) == "etc_passwd.pdf", errors
        # The separators must be gone, not merely escaped somewhere.
        assert "../" not in resp.text, resp.text
        assert "/etc/" not in resp.text, resp.text

    def test_null_byte_is_stripped_before_the_name_is_used(self, upload_client):
        resp = _post_multipart(
            upload_client,
            _body(_part("files", "ev\x00il.pdf", PDF_MAGIC_ONLY)),
        )

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert _reported_name(errors[0]) == "evil.pdf", errors
        # A NUL that reaches a filesystem call truncates the path there.
        assert "\x00" not in resp.text
        assert "\\u0000" not in resp.text

    def test_overlong_name_is_truncated_but_keeps_its_extension(
        self, upload_client
    ):
        resp = _post_multipart(
            upload_client,
            _body(_part("files", "a" * 3000 + ".pdf", PDF_MAGIC_ONLY)),
        )

        assert resp.status_code == 400, resp.text
        name = _reported_name(_errors(resp)[0])
        # MAX_FILENAME_LENGTH is 255 and truncation preserves the suffix,
        # so 251 name characters plus ".pdf".
        assert len(name) == 255, len(name)
        assert name.endswith(".pdf"), name

    def test_transliterable_non_ascii_name_is_accepted(self, upload_client):
        """``café.pdf`` must not be treated as a hostile name."""
        resp = _post_multipart(
            upload_client,
            _body(_part("files", "café.pdf", PDF_MAGIC_ONLY)),
        )

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert FILENAME_REJECTED not in errors, (
            "an accented but otherwise ordinary filename was rejected "
            "outright instead of being transliterated"
        )
        assert _reported_name(errors[0]) == "cafe.pdf", errors

    def test_non_latin_name_should_not_be_rejected_outright(
        self, upload_client
    ):
        resp = _post_multipart(
            upload_client,
            _body(_part("files", "研究報告.pdf", PDF_MAGIC_ONLY)),
        )

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert FILENAME_REJECTED not in errors
        assert len(errors) == 1, errors
        reported_name = _reported_name(errors[0])
        assert reported_name.startswith("upload-"), errors
        assert reported_name.endswith(".pdf"), errors

    def test_duplicate_filenames_are_processed_independently(
        self, upload_client
    ):
        """Two parts, one name: neither may be collapsed away."""
        resp = _post_multipart(
            upload_client,
            _body(
                _part("files", "dup.pdf", PDF_MAGIC_ONLY),
                _part("files", "dup.pdf", PDF_MAGIC_ONLY + b"second\n"),
            ),
        )

        assert resp.status_code == 400, resp.text
        # When nothing validates, the handler reports one entry per part
        # it attempted -- so two entries means two parts survived the
        # parser under the same name.
        errors = _errors(resp)
        assert len(errors) == 2, (
            "duplicate filenames in one request were collapsed; each part "
            f"must be handled on its own: {errors}"
        )
        assert [_reported_name(e) for e in errors] == [
            "dup.pdf",
            "dup.pdf",
        ], errors

    def test_empty_filename_is_reported_as_nothing_selected(
        self, upload_client
    ):
        """``filename=""`` is a browser's empty file input, not an error.

        It must produce the distinct "No files selected" answer, not the
        "No files provided" one, which means the part never arrived.
        """
        resp = _post_multipart(
            upload_client, _body(_part("files", "", PDF_MAGIC_ONLY))
        )

        assert resp.status_code == 400, resp.text
        assert resp.json().get("error") == "No files selected", resp.text


# ---------------------------------------------------------------------------
# Declared Content-Type vs. actual bytes
# ---------------------------------------------------------------------------


@pytest.mark.timeout(120)
class TestDeclaredContentTypeIsNotTrusted:
    """The per-part ``Content-Type`` is client input and costs nothing to
    forge. Acceptance must turn on the extension allowlist and the magic
    bytes, never on the label."""

    def test_pdf_label_does_not_excuse_missing_magic_bytes(self, upload_client):
        resp = _post_multipart(
            upload_client,
            _body(
                _part(
                    "files",
                    "invoice.pdf",
                    b"MZ\x90\x00 this is a PE binary, not a PDF",
                    ctype="application/pdf",
                )
            ),
        )

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert "File signature mismatch" in errors[0], (
            "a non-PDF payload was accepted on the strength of its "
            f"declared Content-Type alone: {errors}"
        )

    def test_pdf_magic_bytes_do_not_excuse_a_disallowed_extension(
        self, upload_client
    ):
        """The other direction: real PDF bytes under a ``.txt`` name."""
        resp = _post_multipart(
            upload_client,
            _body(
                _part(
                    "files",
                    "payload.txt",
                    PDF_MAGIC_ONLY,
                    ctype="application/pdf",
                )
            ),
        )

        assert resp.status_code == 400, resp.text
        assert FILENAME_REJECTED in _errors(resp), resp.text


# ---------------------------------------------------------------------------
# The handler must be the first reader of each upload
# ---------------------------------------------------------------------------


@pytest.mark.timeout(120)
class TestUploadBytesReachTheHandler:
    """``upload_pdf`` reads every part itself. If anything upstream has
    already read a part's stream to its end, the handler gets empty bytes
    and rejects every real PDF as a signature mismatch. FastAPI's own
    form extraction did exactly that while the handler still carried a
    vestigial ``files`` body parameter (#5989)."""

    def test_pdf_magic_bytes_survive_to_the_handler(self, upload_client):
        resp = _post_multipart(
            upload_client, _body(_part("files", "real.pdf", PDF_MAGIC_ONLY))
        )

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert len(errors) == 1, errors
        # PDF_MAGIC_ONLY clears the signature check and dies at structure
        # validation; a signature mismatch means the bytes never arrived.
        assert "File signature mismatch" not in errors[0], (
            f"the handler read an already-consumed upload stream: {errors}"
        )


# ---------------------------------------------------------------------------
# A part with no filename at all
# ---------------------------------------------------------------------------


@pytest.mark.timeout(120)
class TestPartWithoutAFilename:
    """A part named ``files`` but carrying no ``filename`` parameter.

    python-multipart yields ``str`` for such a part, not ``UploadFile``.
    Both upload routes must filter it out rather than crash. ``upload_pdf``
    once 500'd here (#5989): a vestigial ``files: list = None`` signature
    parameter made FastAPI extract the field itself and call ``.read()``
    on the ``str`` before the handler was entered.
    """

    NO_FILENAME_BODY = _body(_part("files", None, PDF_MAGIC_ONLY))

    def test_filenameless_part_should_be_a_400_not_a_crash(self, upload_client):
        resp = _post_multipart(upload_client, self.NO_FILENAME_BODY)

        assert resp.status_code == 400, resp.text
        assert resp.json().get("error") == "No files provided", resp.text

    def test_valid_file_beside_a_filenameless_part_still_succeeds(
        self, upload_client
    ):
        resp = _post_multipart(
            upload_client,
            _body(
                _part("files", "real.pdf", PDF_MAGIC_ONLY),
                _part("files", None, b"stray form field"),
            ),
        )

        # PDF_MAGIC_ONLY dies at structure validation, so the proof that
        # real.pdf was processed is its per-file entry, not a 200. A
        # handler that dropped every file would answer 400 "No files
        # provided" with no errors list at all.
        assert resp.status_code == 400, resp.text
        assert [_reported_name(e) for e in _errors(resp)] == ["real.pdf"], (
            f"a valid file was lost to a stray form field: {resp.text[:200]}"
        )

    def test_sibling_collection_route_handles_it_cleanly(self, upload_client):
        """Negative control for the two tests above: the same body, the
        other upload route, no crash. A regression there would be
        route-local, not a parser or framework limitation."""
        create = upload_client.post(
            "/library/api/collections",
            json={"name": f"multipart-{uuid.uuid4().hex[:6]}"},
        )
        assert create.status_code == 200, create.text
        collection_id = create.json()["collection"]["id"]

        resp = _post_multipart(
            upload_client,
            self.NO_FILENAME_BODY,
            path=f"/library/api/collections/{collection_id}/upload",
        )

        assert resp.status_code == 400, resp.text
        assert resp.json().get("error") == "No files provided", resp.text


# ---------------------------------------------------------------------------
# Malformed bodies
# ---------------------------------------------------------------------------


@pytest.mark.timeout(120)
class TestMalformedBodies:
    """Starlette turns ``MultiPartException`` into ``HTTPException(400)``
    only while ``"app" in scope``. These pin that the 400 actually
    reaches the client rather than being re-raised as a 500."""

    def test_garbage_body_is_a_400_not_a_500(self, upload_client):
        resp = _post_multipart(
            upload_client, b"not a multipart body at all, no boundary here"
        )

        assert resp.status_code == 400, (
            "a malformed multipart body must be the client's fault (400), "
            f"not the server's (got {resp.status_code}): {resp.text[:200]}"
        )

    def test_missing_boundary_parameter_is_a_400(self, upload_client):
        resp = _post_multipart(
            upload_client,
            _body(_part("files", "a.pdf", PDF_MAGIC_ONLY)),
            ct="multipart/form-data",
        )

        assert resp.status_code == 400, resp.text
        assert "boundary" in resp.text.lower(), resp.text

    def test_malformed_part_header_is_a_400(self, upload_client):
        raw = (
            f"--{BOUNDARY}\r\n"
            "Content-Disposition\r\n"  # no colon, no value
            "\r\nzz\r\n"
            f"--{BOUNDARY}--\r\n"
        ).encode()

        resp = _post_multipart(upload_client, raw)

        assert resp.status_code == 400, resp.text

    def test_overlong_boundary_is_a_400(self, upload_client):
        """python-multipart refuses a boundary longer than its
        ``MAX_BOUNDARY_LENGTH`` (256 in 0.0.32) before reading a byte of
        the body, and does so with its base ``FormParserError`` rather
        than the ``MultipartParseError`` a garbage body raises. Both are
        the client's doing and must answer the same 400."""
        boundary = "B" * 300
        raw = (
            (
                f"--{boundary}\r\n"
                'Content-Disposition: form-data; name="files"; '
                'filename="a.pdf"\r\n'
                "Content-Type: application/pdf\r\n\r\n"
            ).encode()
            + PDF_MAGIC_ONLY
            + f"\r\n--{boundary}--\r\n".encode()
        )

        resp = _post_multipart(
            upload_client, raw, ct=f"multipart/form-data; boundary={boundary}"
        )

        assert resp.status_code == 400, resp.text
        assert resp.json() == {"detail": "There was an error parsing the body"}

    def test_oversized_non_file_field_is_a_400(self, upload_client):
        """Starlette caps *non-file* parts at ``max_part_size`` (1 MB).

        File parts stream to a SpooledTemporaryFile and are exempt, which
        is what lets the 3 GB per-file cap mean anything; a plain field is
        buffered in memory and must be bounded.
        """
        oversized_field = (
            (
                f"--{BOUNDARY}\r\n"
                'Content-Disposition: form-data; name="note"\r\n\r\n'
            ).encode()
            + b"x" * (1024 * 1024 + 16)
            + b"\r\n"
        )

        resp = _post_multipart(
            upload_client,
            _body(_part("files", "a.pdf", PDF_MAGIC_ONLY), oversized_field),
        )

        assert resp.status_code == 400, resp.text
        assert "maximum size" in resp.text.lower(), resp.text

    def test_truncated_body_yields_only_the_complete_parts(self, upload_client):
        """Documented (not desired) behaviour, pinned so a parser bump
        cannot change it silently.

        A body cut mid-part is *not* an error: python-multipart hands back
        the parts it completed and drops the partial one without a word.
        A caller therefore cannot distinguish "user sent one file" from
        "user sent two and the connection died". Werkzeug behaved the same
        way, so this is not a port regression — but it is the reason a
        truncated upload can look like a successful partial upload.
        """
        complete = _part("files", "first.pdf", PDF_MAGIC_ONLY)
        cut_short = _part("files", "second.pdf", PDF_MAGIC_ONLY)[:-8]

        resp = _post_multipart(upload_client, complete + cut_short)

        assert resp.status_code == 400, resp.text
        errors = _errors(resp)
        assert len(errors) == 1, (
            f"expected the truncated part to be dropped silently: {errors}"
        )
        assert _reported_name(errors[0]) == "first.pdf", errors
        assert "second.pdf" not in resp.text, (
            "the truncated part was surfaced after all; update this test "
            "and the callers that assume silent truncation"
        )


# ---------------------------------------------------------------------------
# Failures inside the form read keep their blame
# ---------------------------------------------------------------------------


@pytest.mark.timeout(120)
class TestFormReadFailuresKeepTheirBlame:
    """``upload_pdf`` reads the form itself, so it also decides whose
    fault a failed read is. python-multipart's own errors are the
    client's and keep the 400 FastAPI used to answer; anything else
    raised while reading the form (an ``OSError`` spooling an upload to
    disk, say) is the server's and must reach the handler's generic
    path, which logs it and answers 500, rather than be relabelled as a
    malformed request nobody hears about.
    """

    @staticmethod
    def _post_with_form_read_raising(client, monkeypatch, exc):
        """POST a well-formed body through a ``Request.form()`` that
        raises ``exc``: the handler's own read is the only place this
        route touches the form."""
        from starlette.requests import Request

        def _failing_form(self, *args, **kwargs):
            raise exc

        monkeypatch.setattr(Request, "form", _failing_form)
        return _post_multipart(
            client, _body(_part("files", "real.pdf", PDF_MAGIC_ONLY))
        )

    def test_parser_error_keeps_the_historical_400(
        self, upload_client, monkeypatch
    ):
        from python_multipart.exceptions import MultipartParseError

        resp = self._post_with_form_read_raising(
            upload_client, monkeypatch, MultipartParseError("no boundary")
        )

        assert resp.status_code == 400, resp.text
        assert resp.json() == {"detail": "There was an error parsing the body"}

    def test_any_other_failure_is_a_logged_500(
        self, upload_client, monkeypatch, loguru_caplog_full
    ):
        resp = self._post_with_form_read_raising(
            upload_client, monkeypatch, OSError("no space left to spool")
        )

        assert resp.status_code == 500, resp.text
        assert resp.json() == {"error": "Failed to process PDF files"}
        assert "Error processing PDF upload" in loguru_caplog_full.text
        assert "no space left to spool" in loguru_caplog_full.text, (
            "the generic handler must log the failure it hides from the client"
        )


# ---------------------------------------------------------------------------
# ASGI-level cases: the size cap and a mid-upload disconnect
# ---------------------------------------------------------------------------


def _drive_asgi(path, headers, messages, timeout=60):
    """Call the assembled app directly and collect what it sends.

    TestClient cannot declare a Content-Length that disagrees with the
    body it transmits, and cannot hang up mid-request — both of which are
    exactly what needs testing here. Driving the ASGI callable keeps the
    entire real middleware stack in the picture.
    """
    from local_deep_research.web.fastapi_app import app

    async def _run():
        scope = {
            "type": "http",
            "asgi": {"version": "3.0", "spec_version": "2.3"},
            "http_version": "1.1",
            "method": "POST",
            "scheme": "http",
            "path": path,
            "raw_path": path.encode(),
            "root_path": "",
            "query_string": b"",
            "client": ("127.0.0.1", 12345),
            "server": ("testserver", 80),
            "headers": [(b"host", b"testserver")] + headers,
        }
        pending = list(messages)

        async def receive():
            if pending:
                return pending.pop(0)
            return {"type": "http.disconnect"}

        sent = []

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

        await asyncio.wait_for(app(scope, receive, send), timeout=timeout)
        return sent

    sent = asyncio.run(_run())
    starts = [m 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 (starts[0]["status"] if starts else None), body


def _cl_headers(declared, content_type=MULTIPART_CT):
    return [
        (b"content-type", content_type.encode()),
        (b"content-length", str(declared).encode()),
    ]


#: A closing delimiter and nothing else. The over-cap tests never
#: transmit the size they declare — the middleware decides on the header
#: before reading, so allocating 600 GB would only test the test runner.
EMPTY_MULTIPART = f"--{BOUNDARY}--\r\n".encode()
ONE_CHUNK = [{"type": "http.request", "body": EMPTY_MULTIPART}]

MB = 1024 * 1024


@pytest.mark.timeout(120)
class TestSizeCapThroughTheRealStack:
    """The unit tests in test_body_size_limit.py inject both the app and
    the caps. These use the real assembled app and the real default cap,
    which is what catches a middleware that was registered wrongly or a
    default that drifted."""

    @staticmethod
    def _real_cap():
        from local_deep_research.security.file_upload_validator import (
            FileUploadValidator,
        )

        return (
            FileUploadValidator.MAX_FILES_PER_REQUEST
            * FileUploadValidator.MAX_FILE_SIZE
        )

    def test_over_cap_upload_is_rejected_with_413(self):
        status, body = _drive_asgi(
            "/api/upload/pdf",
            _cl_headers(self._real_cap() + 1),
            ONE_CHUNK,
        )

        assert status == 413, (status, body[:200])
        assert b"Request too large" in body, body[:200]

    def test_a_large_but_legal_upload_is_not_capped(self):
        """Negative control for the test above.

        200 MB is far over the 16 MB JSON cap and far under the upload
        cap, so it must reach the app. 403 (CSRF) proves it did: this
        client deliberately sends no token, and the CSRF middleware sits
        behind the size limiter.
        """
        status, body = _drive_asgi(
            "/api/upload/pdf", _cl_headers(200 * MB), ONE_CHUNK
        )

        assert status != 413, (
            "a 200 MB upload was rejected by the body-size cap; the "
            "upload routes are supposed to keep the large cap"
        )
        assert status == 403, (status, body[:200])

    def test_collection_upload_path_regex_also_keeps_the_large_cap(self):
        status, _ = _drive_asgi(
            "/library/api/collections/abc-123/upload",
            _cl_headers(200 * MB),
            ONE_CHUNK,
        )

        assert status != 413, "the regex-matched upload path lost its cap"

    def test_multipart_label_on_a_non_upload_path_is_still_capped(self):
        """The forged-label bypass, through the real stack this time."""
        status, body = _drive_asgi(
            "/api/v1/research", _cl_headers(200 * MB), ONE_CHUNK
        )

        assert status == 413, (
            "a multipart-labelled 200 MB body on a JSON route was let "
            f"through: {(status, body[:200])}"
        )

    def test_upload_path_lookalike_is_still_capped(self):
        """``/api/upload/pdf/`` is not ``/api/upload/pdf``."""
        status, _ = _drive_asgi(
            "/api/upload/pdf/", _cl_headers(200 * MB), ONE_CHUNK
        )

        assert status == 413, "the path gate is not anchored"


@pytest.mark.timeout(120)
def test_client_disconnect_mid_upload_terminates_cleanly():
    """Hanging up mid-body must not hang the worker or look like success.

    uvicorn delivers ``http.disconnect`` where werkzeug simply raised on
    a short read, so this path is genuinely new after the port. The
    contract worth pinning is narrow and stable: the request completes,
    it completes with a client-error status, and it is never mistaken for
    an accepted upload.
    """
    full = _body(
        _part("files", "first.pdf", PDF_MAGIC_ONLY),
        _part("files", "second.pdf", PDF_MAGIC_ONLY),
    )
    half = full[: len(full) // 2]

    status, body = _drive_asgi(
        "/api/upload/pdf",
        _cl_headers(len(full)),
        [
            {"type": "http.request", "body": half, "more_body": True},
            {"type": "http.disconnect"},
        ],
    )

    # asyncio.wait_for inside _drive_asgi turns a hang into a TimeoutError
    # rather than a stuck test session.
    assert status is not None, "no response was started before teardown"
    assert 400 <= status < 500, (
        f"a mid-upload disconnect produced {status}; it is the client's "
        f"doing, not a server fault: {body[:200]}"
    )
    assert b"success" not in body.lower(), (
        f"a half-received upload was reported as successful: {body[:200]}"
    )