~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Character-encoding behaviour of the FastAPI port.
Flask/Werkzeug and Starlette do not agree about bytes. Werkzeug decoded a
request body inside ``Request.get_json()`` and turned *every* ``ValueError``
raised there into a 400; Starlette's ``Request.json()`` is three lines that
hand the raw body straight to ``json.loads`` and let whatever it raises
escape. This app carries research queries, document titles and uploaded
filenames in arbitrary languages, so the difference is not academic.
What this file covers, and what it deliberately leaves to its neighbours:
* **Invalid UTF-8 in a JSON body** -- the headline defect (#5761), and the
full sweep of how far it reaches. Not covered anywhere else.
* **``Content-Disposition``** -- specifically the plain
``filename=`` fallback. ``tests/web/routers/test_export_filename_
encoding.py`` already pins that the RFC 5987 ``filename*`` form is
produced and is injection-safe; it never asks whether a client that does
not implement RFC 5987 gets a usable name. These tests cover both forms.
* **A multipart part whose only name is ``filename*=UTF-8''...``** --
Werkzeug parsed it as a file, Starlette does not parse it as a file at
all.
* **Emoji / astral-plane / RTL / mixed-script text** as positive controls
on each of those paths, so a failure above cannot be blamed on
"non-ASCII is broken generally".
Already covered elsewhere -- NOT repeated here:
* Query-string decoding (percent-encoded vs raw bytes, Werkzeug oracle):
``tests/web/test_query_param_parsing_parity.py``.
* CSRF token bytes (non-ASCII header token, ``%FF`` in a form field,
``errors="replace"`` decoding): ``tests/web/test_csrf_middleware_edges.py``.
* Non-Latin *uploaded* filenames collapsing in ``sanitize_filename``:
``tests/web/test_multipart_upload_boundary.py::
test_non_latin_name_should_not_be_rejected_outright``.
* The ``filename*`` encoding itself: ``tests/web/routers/
test_export_filename_encoding.py``.
Verification status of every claim below is recorded per test. The
Werkzeug side is not a *model* of main -- it is ``werkzeug~=3.1.6``, still
a declared runtime dependency (pyproject.toml keeps it for the upload
sanitisation helpers), driven through a real WSGI environ. So "what main
did" is executed, not remembered.
"""
import ast
import asyncio
import io
import json
import re
import sys
import uuid
from pathlib import Path
from urllib.parse import quote
import pytest
from tests.test_utils import nested_json_past_the_scanner_limit
SRC_WEB = (
Path(__file__).resolve().parents[2] / "src" / "local_deep_research" / "web"
)
#: A body that is a syntactically perfect JSON object -- balanced braces,
#: quoted key, quoted value -- carrying one byte (0xFF) that is not legal
#: UTF-8 anywhere. ``json.loads`` never reaches its parser: it decodes
#: first, so this raises ``UnicodeDecodeError`` rather than
#: ``JSONDecodeError``. That single fact is the whole defect.
INVALID_UTF8_JSON_BODY = b'{"query": "\xff", "collection_id": 1}'
#: The same shape with legal UTF-8 covering the four hard classes: BMP CJK,
#: an astral-plane emoji (U+1F680, surrogate pair in UTF-16), an astral
#: non-emoji (U+1D11E MUSICAL SYMBOL G CLEF), and RTL Arabic + Hebrew.
NON_ASCII_TEXT = "研究 \U0001f680 \U0001d11e مرحبا שלום"
VALID_UTF8_JSON_BODY = json.dumps(
{"query": NON_ASCII_TEXT}, ensure_ascii=False
).encode("utf-8")
# ---------------------------------------------------------------------------
# Harnesses
# ---------------------------------------------------------------------------
def _starlette_request(body: bytes, content_type: bytes):
"""A real ``starlette.requests.Request`` over ``body``.
No app, no TestClient: ``Request`` only needs a scope and a ``receive``
callable, and ``Request.json()`` / ``Request.form()`` are exactly the
code every ported route runs.
"""
from starlette.requests import Request
scope = {
"type": "http",
"http_version": "1.1",
"method": "POST",
"path": "/x",
"raw_path": b"/x",
"root_path": "",
"scheme": "http",
"query_string": b"",
"server": ("testserver", 80),
"client": ("1.2.3.4", 1234),
"headers": [(b"content-type", content_type)],
}
state = {"sent": False}
async def receive():
if not state["sent"]:
state["sent"] = True
return {
"type": "http.request",
"body": body,
"more_body": False,
}
return {"type": "http.disconnect"}
return Request(scope, receive)
def _werkzeug_request(body: bytes, content_type: str):
"""A real ``werkzeug.wrappers.Request`` over the same bytes.
This is main's request object, not a description of it.
"""
from werkzeug.test import EnvironBuilder
from werkzeug.wrappers import Request as WerkzeugRequest
environ = EnvironBuilder(method="POST", path="/x").get_environ()
environ["CONTENT_TYPE"] = content_type
environ["CONTENT_LENGTH"] = str(len(body))
environ["wsgi.input"] = io.BytesIO(body)
return WerkzeugRequest(environ)
# ===========================================================================
# 1. Invalid UTF-8 in a JSON body: the mechanism
# ===========================================================================
class TestInvalidUtf8BodyMechanism:
"""Why an unparseable *byte* is a different failure to unparseable
*syntax*, and why only one of the two has a handler."""
def test_starlette_raises_unicodedecodeerror_not_jsondecodeerror(self):
"""EXECUTION-VERIFIED (starlette 1.3.1).
``Request.json()`` is ``json.loads(await self.body())``.
``json.loads`` on ``bytes`` decodes before parsing, so a bad byte
never becomes a syntax error. The registered handler in
``fastapi_app.py`` is keyed on ``json.JSONDecodeError``; this
exception is not one.
"""
request = _starlette_request(
INVALID_UTF8_JSON_BODY, b"application/json"
)
with pytest.raises(UnicodeDecodeError) as excinfo:
asyncio.run(request.json())
exc = excinfo.value
assert isinstance(exc, ValueError), (
"the app-level handler is registered for JSONDecodeError, a "
"ValueError subclass; this must be a sibling ValueError, not "
"a JSONDecodeError, for the defect to exist at all"
)
assert not isinstance(exc, json.JSONDecodeError), (
"if this ever becomes a JSONDecodeError the registered 400 "
"handler catches it and #5761 is fixed by construction"
)
def test_the_same_bytes_parse_fine_once_they_are_valid_utf8(self):
"""Positive control for the test above.
Same shape, same parser, same harness -- only the bytes differ. So
the failure above is the encoding, not "Starlette dislikes this
body". Covers BMP CJK, an astral emoji, an astral non-emoji and
RTL Arabic + Hebrew in one value.
"""
request = _starlette_request(VALID_UTF8_JSON_BODY, b"application/json")
parsed = asyncio.run(request.json())
assert parsed == {"query": NON_ASCII_TEXT}
# Astral characters must survive as single code points, not as a
# UTF-16 surrogate pair leaked through the decoder.
assert "\U0001f680" in parsed["query"]
assert "\ud83d" not in parsed["query"]
def test_werkzeug_answered_400_for_the_identical_bytes(self):
"""EXECUTION-VERIFIED against werkzeug 3.1.8, the version this
project still pins.
This is what main did. ``web/routes/*`` and
``research_library/routes/library_routes.py`` read bodies as
``request.json`` / ``request.get_json()``; Werkzeug's ``get_json``
catches ``ValueError`` -- the common base of ``JSONDecodeError``
AND ``UnicodeDecodeError`` -- and routes it through
``on_json_loading_failed``, which raises ``BadRequest`` (400).
``silent=True`` (what ``@require_json_body`` used) returned
``None``, which the decorator turned into its own 400.
So the pre-port answer to these bytes was 400 on every route that
read a body. Any 500 below is a regression, not a pre-existing
wart.
"""
from werkzeug.exceptions import BadRequest
request = _werkzeug_request(INVALID_UTF8_JSON_BODY, "application/json")
with pytest.raises(BadRequest) as excinfo:
request.get_json()
assert excinfo.value.code == 400
silent = _werkzeug_request(
INVALID_UTF8_JSON_BODY, "application/json"
).get_json(silent=True)
assert silent is None, (
"@require_json_body used get_json(silent=True) and 400'd on a "
"None result; if this stopped being None the decorator's 400 "
"would have been an unhandled crash on main too"
)
class TestAppLevelHandlerResolution:
"""Which registered handler Starlette picks for each exception.
``starlette._exception_handler._lookup_exception_handler`` is the real
resolver: it walks ``type(exc).__mro__`` and returns the first
registered class. Calling it directly answers the routing question
exactly, without booting a server or a database.
"""
@staticmethod
def _resolve(exc):
from starlette._exception_handler import _lookup_exception_handler
from local_deep_research.web.fastapi_app import app
return app.exception_handlers, _lookup_exception_handler(
app.exception_handlers, exc
)
def test_a_json_syntax_error_reaches_the_registered_400_handler(self):
"""Control: the sibling failure IS handled, and answers 400.
If this ever fails, the comparison in the next test is meaningless
-- it would mean nothing routes, rather than that this particular
exception does not.
"""
handlers, resolved = self._resolve(
json.JSONDecodeError("Expecting value", "", 0)
)
assert resolved is handlers[json.JSONDecodeError]
assert resolved is not handlers[Exception]
assert resolved.__name__ == "handle_json_decode_error"
def test_invalid_utf8_does_not_fall_through_to_the_500_catch_all(self):
exc = None
try:
json.loads(INVALID_UTF8_JSON_BODY)
except UnicodeDecodeError as raised:
exc = raised
assert exc is not None, "harness precondition"
handlers, resolved = self._resolve(exc)
assert resolved is handlers[UnicodeDecodeError]
assert resolved is not handlers[Exception]
assert resolved.__name__ == "handle_json_decode_error"
def test_a_client_disconnect_does_not_fall_through_to_the_500_catch_all(
self,
):
"""``read_json_dict`` lets ``ClientDisconnect`` propagate; this is
its receiver, so a hang-up mid-body is not an error-level 500."""
from starlette.requests import ClientDisconnect
handlers, resolved = self._resolve(ClientDisconnect())
assert resolved is handlers[ClientDisconnect]
assert resolved is not handlers[Exception]
assert resolved.__name__ == "handle_client_disconnect"
def test_the_body_size_exception_is_left_for_its_own_middleware(self):
"""The other exception ``read_json_dict`` lets through already has a
receiver: ``BodySizeLimitMiddleware`` turns it into a logged 413. No
handler may claim it first, so it resolves to nothing narrower than
the catch-all (which Starlette hands to ``ServerErrorMiddleware``,
outside that middleware, not to the router's own handler stack)."""
from local_deep_research.web.fastapi_app import _RequestBodyTooLarge
handlers, resolved = self._resolve(_RequestBodyTooLarge())
assert resolved is handlers[Exception]
# ===========================================================================
# 2. Invalid UTF-8 in a JSON body: how far it reaches
# ===========================================================================
#
# Booting every authenticated route to ask each one the same
# question is not affordable, and an HTTP sweep only samples whatever it can
# reach past rate limits and path parameters. The AST answer is exact and
# total: for every ``request.json()`` call in the web layer, which enclosing
# ``except`` clauses (if any) would catch a UnicodeDecodeError?
#
# This reads the real shipped source. It is not a model of the routers.
_JSON_BODY_FILES = sorted(
[
*(SRC_WEB / "routers").glob("*.py"),
*(SRC_WEB / "dependencies").glob("*.py"),
SRC_WEB / "fastapi_app.py",
]
)
#: Exception names that WOULD catch a UnicodeDecodeError. ``""`` is a bare
#: ``except:``. UnicodeDecodeError's MRO decides membership; nothing here
#: is a judgement call.
_CATCHES_UNICODE_DECODE_ERROR = frozenset(
{
"",
"BaseException",
"Exception",
"ValueError",
"UnicodeError",
"UnicodeDecodeError",
}
)
#: Exception names that would NOT. Listed explicitly rather than inferred
#: so a newly-introduced ``except`` clause fails
#: ``test_every_guard_clause_is_classified`` instead of being silently
#: assumed harmless.
_DOES_NOT_CATCH_UNICODE_DECODE_ERROR = frozenset(
{
"json.JSONDecodeError",
"JSONDecodeError",
"NewsAPIException",
"TypeError",
"AttributeError",
"KeyError",
"SQLAlchemyError",
"RuntimeError",
"RecursionError",
"OSError",
# database/rekey_lock.py: ``class RekeyOperationalError(Exception)``,
# a direct Exception subclass, so not in UnicodeDecodeError's MRO.
# metrics.py's api_journal_data_download re-raises it from its outer
# ``try`` for the app's retryable 503; the body parse inside keeps
# its own ``except Exception`` and still answers 400.
"RekeyOperationalError",
}
)
def _handler_names(handler: ast.ExceptHandler) -> list[str]:
if handler.type is None:
return [""]
if isinstance(handler.type, ast.Tuple):
return [ast.unparse(elt) for elt in handler.type.elts]
return [ast.unparse(handler.type)]
def _scan_json_body_sites(files=None):
"""Every ``request.json()`` call in the web layer, with the exception
names of the ``try`` blocks that actually enclose it.
Returns ``[(module_basename, function_name, frozenset(caught_names))]``.
``files`` replaces the shipped source with other modules, for the
controls that show the scan telling the cases apart.
"""
sites = []
for path in _JSON_BODY_FILES if files is None else files:
tree = ast.parse(path.read_text(encoding="utf-8"))
parents = {}
for node in ast.walk(tree):
for child in ast.iter_child_nodes(node):
parents[child] = node
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
func = node.func
if not (
isinstance(func, ast.Attribute)
and func.attr == "json"
and isinstance(func.value, ast.Name)
and func.value.id == "request"
):
continue
caught: list[str] = []
function_name = None
current = node
while current in parents:
child, current = current, parents[current]
if isinstance(current, ast.Try):
# Only a call in the ``try`` BODY is guarded; one in an
# ``except``/``finally`` block is not.
on_try_body = any(
child is stmt or child in set(ast.walk(stmt))
for stmt in current.body
)
if on_try_body:
for handler in current.handlers:
caught.extend(_handler_names(handler))
if function_name is None and isinstance(
current, (ast.FunctionDef, ast.AsyncFunctionDef)
):
function_name = current.name
sites.append((path.name, function_name, frozenset(caught)))
return sites
def _unguarded_sites():
return {
(module, function)
for module, function, caught in _scan_json_body_sites()
if not (caught & _CATCHES_UNICODE_DECODE_ERROR)
}
#: Frozen inventory of the four remaining unguarded ``request.json()`` calls.
#: The other routes now use ``read_json_dict`` or a route-local parse guard.
#:
#: What the application-level handlers do for these four is narrower than
#: "convert the failure to 400". They answer 400 for two of the four ways
#: the parser refuses a body: undecodable bytes (``UnicodeDecodeError``) and
#: bad syntax (``JSONDecodeError``). Nesting past the scanner limit
#: (``RecursionError``) and an integer literal past
#: ``sys.get_int_max_str_digits()`` (a plain ``ValueError``) are not
#: registered app-wide, so on a bare route they reach the catch-all and answer
#: 500. ``test_only_two_of_the_four_parser_refusals_have_an_app_level_400``
#: pins exactly that. The inventory detects new unguarded sites; it does not
#: close the gap.
UNGUARDED_JSON_BODY_SITES = frozenset(
{
("benchmark.py", "validate_config"),
("library_delete.py", "_parse_json_body"),
("news_flask_api.py", "add_search_history"),
("settings.py", "_notification_test_body"),
}
)
#: Exception names that swallow EVERYTHING a body read can raise, not only a
#: parse failure. ``request.json()`` reads the request stream, so it is also
#: where the request-size limit's exception (the receiver is
#: ``BodySizeLimitMiddleware``, which logs the rejection and answers 413) and
#: a client disconnect surface. ``""`` is a bare ``except:``.
_SWALLOWS_EVERYTHING = frozenset({"", "BaseException", "Exception"})
def _broadly_wrapped_sites(files=None):
return {
(module, function)
for module, function, caught in _scan_json_body_sites(files)
if caught & _SWALLOWS_EVERYTHING
}
#: Frozen inventory of the bare ``request.json()`` calls that sit inside a
#: broad ``except``, the third parse idiom. These are invisible to
#: ``UNGUARDED_JSON_BODY_SITES``, because a broad ``except`` does catch the
#: encoding error, and to the guard over ``read_json_dict`` callers in
#: ``tests/web/routers/test_json_body_error_envelopes.py``, which only
#: polices that helper. At each site an over-cap chunked body is answered by
#: the route's own error handling (a 400, or a logged 500) instead of the
#: middleware's logged 413. Not converted here: the set only ever shrinks,
#: by moving a site onto ``read_json_dict``. ``followup.start_followup`` is
#: deliberately absent: its ``except ValueError`` lets both exceptions
#: through, and it answers 413.
BROADLY_WRAPPED_JSON_BODY_SITES = frozenset(
{
("api_v1.py", "api_analyze_documents"),
("api_v1.py", "api_generate_report"),
("api_v1.py", "api_quick_summary"),
("chat.py", "_json_object_body"),
("followup.py", "prepare_followup"),
("metrics.py", "api_journal_data_download"),
("news_flask_api.py", "research_news_item"),
("rag.py", "configure_rag"),
("rag.py", "index_document"),
("rag.py", "remove_document"),
("rag.py", "test_embedding"),
("research.py", "start_research"),
}
)
def _integer_past_the_digit_limit() -> bytes:
limit = sys.get_int_max_str_digits()
if limit == 0:
pytest.skip("this interpreter does not limit integer literals")
return b"1" * (limit + 1)
class TestInvalidUtf8BodyReach:
def test_every_guard_clause_is_classified(self):
"""No ``except`` name may be silently assumed harmless.
Without this, someone adding ``except SomeNewError`` around a body
parse would be classified "unguarded" by default and the
inventory below would drift for the wrong reason.
"""
seen = set()
for _module, _function, caught in _scan_json_body_sites():
seen |= set(caught)
unclassified = seen - _CATCHES_UNICODE_DECODE_ERROR
unclassified -= _DOES_NOT_CATCH_UNICODE_DECODE_ERROR
assert not unclassified, (
"new exception name(s) guarding a request.json() call -- add "
"each to _CATCHES_UNICODE_DECODE_ERROR or to "
f"_DOES_NOT_CATCH_UNICODE_DECODE_ERROR: {sorted(unclassified)}"
)
def test_the_scanner_finds_the_body_parses_it_is_supposed_to(self):
"""Guard against the scan silently matching nothing.
Every assertion in this section is about a *set*; an empty set
would satisfy a subset check without proving anything. Two known
anchors: a route that is guarded and one that is not.
"""
found = {
(module, function)
for module, function, _caught in _scan_json_body_sites()
}
assert ("benchmark.py", "validate_config") in found
assert ("json_body.py", "read_json_dict") in found
assert len(found) >= 19, (
f"only {len(found)} request.json() call sites found; the "
"scanner has stopped matching the source"
)
def test_unguarded_body_parse_inventory_is_unchanged(self):
"""Pin exactly which handlers let the bad byte escape."""
assert _unguarded_sites() == UNGUARDED_JSON_BODY_SITES
def test_the_gap_is_not_confined_to_the_library_router(self):
"""#5761 was filed for six library endpoints. It is app-wide.
Route-local guards now handle most body parses, but a library-only
patch would still leave three unguarded handlers in other modules.
At all four remaining sites the application-level handlers answer 400
for the encoding and syntax failures; they do not for the other two
parser refusals (see ``UNGUARDED_JSON_BODY_SITES``).
"""
outside_library = {
(module, function)
for module, function in _unguarded_sites()
if module not in {"library.py", "library_delete.py"}
}
modules = {module for module, _function in outside_library}
assert len(outside_library) >= 3, sorted(outside_library)
assert modules >= {
"benchmark.py",
"news_flask_api.py",
"settings.py",
}, sorted(modules)
@pytest.mark.parametrize(
("build_body", "refusal", "has_an_app_level_400"),
[
pytest.param(
lambda: b"{not json",
json.JSONDecodeError,
True,
id="bad-syntax",
),
pytest.param(
lambda: INVALID_UTF8_JSON_BODY,
UnicodeDecodeError,
True,
id="undecodable-bytes",
),
pytest.param(
_integer_past_the_digit_limit,
ValueError,
False,
id="integer-past-the-digit-limit",
),
pytest.param(
nested_json_past_the_scanner_limit,
RecursionError,
False,
id="nesting-too-deep",
),
],
)
def test_only_two_of_the_four_parser_refusals_have_an_app_level_400(
self, build_body, refusal, has_an_app_level_400
):
"""The most the unguarded inventory can claim, stated exactly.
``json.loads`` refuses a body in four ways. A route with no guard of
its own gets an application-level 400 for the first two only; the
other two reach the catch-all, which answers 500. Resolving the real
exception against the real app is what the claim is made of.
"""
with pytest.raises(refusal) as refused:
json.loads(build_body())
# Premise: the body fails in exactly the way it is named for.
assert type(refused.value) is refusal
handlers, resolved = TestAppLevelHandlerResolution._resolve(
refused.value
)
if has_an_app_level_400:
assert resolved.__name__ == "handle_json_decode_error"
else:
assert resolved is handlers[Exception], (
f"{refusal.__name__} now has its own app-level handler "
"(resolved to "
f"{resolved.__name__}); update the claims on "
"UNGUARDED_JSON_BODY_SITES"
)
def test_the_shared_body_helper_is_the_one_that_gets_it_right(self):
"""``read_json_dict`` exists precisely to close this gap.
It catches ``ValueError`` (the base of both ``UnicodeDecodeError``
and ``json.JSONDecodeError``) and ``RecursionError``, so it handles
the encoding failure and the syntax failure identically -- which is
what main's ``@require_json_body`` did. The four sites above are the
call sites that did not adopt it. Exercised, not merely read: the
helper is driven with the real bad bytes through a real Request.
"""
from local_deep_research.web.dependencies.json_body import (
read_json_dict,
)
request = _starlette_request(
INVALID_UTF8_JSON_BODY, b"application/json"
)
data, error = asyncio.run(read_json_dict(request, "status"))
assert data is None
assert error is not None
assert error.status_code == 400
assert json.loads(error.body) == {
"status": "error",
"message": "Request body must be valid JSON",
}
class TestBroadlyWrappedBodyParses:
"""A bare ``request.json()`` inside ``except Exception``: the third idiom.
``read_json_dict`` answers a body the CLIENT got wrong and lets the rest
propagate to its receiver: the request-size limit's exception to
``BodySizeLimitMiddleware`` (a logged 413) and a client disconnect to the
app-level handler. A route that wraps its own bare parse in a broad
``except`` answers both itself, and the middleware's 413 and its log line
never happen.
"""
def test_broadly_wrapped_body_parse_inventory_is_unchanged(self):
"""Pin exactly which bare parses sit inside a broad ``except``."""
found = _broadly_wrapped_sites()
added = sorted(found - BROADLY_WRAPPED_JSON_BODY_SITES)
assert not added, (
"request.json() is now called inside a broad `except` at "
f"{added}. That swallows what read_json_dict lets through on "
"purpose -- the request-size limit's exception (a logged 413) "
"and a client disconnect -- and answers with the route's own "
"error instead. Read the body with read_json_dict "
"(web/dependencies/json_body.py), outside any broad `except`; "
"do not add the site to BROADLY_WRAPPED_JSON_BODY_SITES, which "
"only shrinks."
)
removed = sorted(BROADLY_WRAPPED_JSON_BODY_SITES - found)
assert not removed, (
f"{removed} no longer wrap request.json() in a broad `except` "
"(moved onto read_json_dict?). Delete them from "
"BROADLY_WRAPPED_JSON_BODY_SITES."
)
def test_the_scan_tells_a_broad_except_from_a_narrow_or_hoisted_one(
self, tmp_path
):
"""Discrimination control: only the swallowing shapes are counted."""
module = tmp_path / "routes.py"
module.write_text(
"async def swallowed(request):\n"
" try:\n"
" return await request.json()\n"
" except Exception:\n"
" return None\n"
"async def bare(request):\n"
" try:\n"
" return await request.json()\n"
" except:\n"
" return None\n"
"async def nested(request):\n"
" try:\n"
" try:\n"
" return await request.json()\n"
" except ValueError:\n"
" return None\n"
" except Exception:\n"
" return None\n"
"async def narrow(request):\n"
" try:\n"
" return await request.json()\n"
" except ValueError:\n"
" return None\n"
"async def hoisted(request):\n"
" data = await request.json()\n"
" try:\n"
" return data\n"
" except Exception:\n"
" return None\n",
encoding="utf-8",
)
assert _broadly_wrapped_sites([module]) == {
("routes.py", "swallowed"),
("routes.py", "bare"),
("routes.py", "nested"),
}
class TestNonAsciiSurvivesTheResponseSide:
def test_json_error_bodies_carry_raw_utf8_for_astral_and_rtl(self):
"""The port's own error envelope, driven with hard text.
``JSONResponse`` renders with ``ensure_ascii=False``, so the body
is real UTF-8 rather than ``\\uXXXX`` escapes. An astral character
must be one 4-byte sequence, not a surrogate pair -- Python's json
module emits surrogates for astral code points when
``ensure_ascii`` is on, and ``str.encode('utf-8')`` would then
raise on them.
"""
from local_deep_research.web.dependencies.json_body import (
json_body_error,
)
response = json_body_error("status", NON_ASCII_TEXT)
assert response.status_code == 400
assert "\U0001f680".encode("utf-8") in response.body, response.body
assert b"\\u" not in response.body, (
"body was escaped to ASCII; astral code points would have to "
"round-trip through surrogate pairs"
)
assert json.loads(response.body)["message"] == NON_ASCII_TEXT
# ===========================================================================
# 3. Content-Disposition: the plain filename= fallback
# ===========================================================================
#: A legacy client scans the header for a ``filename`` parameter. RFC 5987
#: adds ``filename*``; a client that does not implement it must still find
#: ``filename=``. This matches the plain one and never the extended one
#: (``filename*=`` has ``*`` where this needs ``=``).
_PLAIN_FILENAME_PARAM = re.compile(r"(?:^|;)\s*filename\s*=")
class TestContentDispositionFilenameFallback:
def test_werkzeug_always_emitted_a_plain_filename_parameter(self):
"""EXECUTION-VERIFIED baseline (werkzeug 3.1.8).
main served both of these routes with ``send_file(...,
download_name=...)``. Werkzeug's ``send_file`` emits ``filename=``
alone for an ASCII name, and for a non-ASCII name emits BOTH: an
NFKD-folded ASCII ``filename=`` and the RFC 5987 ``filename*=``.
It never emits ``filename*`` on its own.
"""
from werkzeug.test import EnvironBuilder
from werkzeug.utils import send_file
environ = EnvironBuilder(method="GET", path="/x").get_environ()
for name in ("report.tex", "研究报告_2026.tex"):
response = send_file(
io.BytesIO(b"x"),
environ,
mimetype="application/octet-stream",
as_attachment=True,
download_name=name,
)
disposition = response.headers["Content-Disposition"]
assert _PLAIN_FILENAME_PARAM.search(disposition), (
f"werkzeug baseline changed for {name!r}: {disposition}"
)
def test_port_still_emits_the_rfc5987_form(self):
"""Control for the fallback tests below.
They assert an ADDITION. If ``filename*`` had been dropped instead
of the fallback being missing, the diagnosis would be different --
so pin that the extended form is present and correctly encoded.
"""
disposition = _export_content_disposition("研究报告_2026.tex")
assert "filename*=UTF-8''" in disposition
assert "%E7%A0%94" in disposition, disposition
@pytest.mark.parametrize(
"filename",
[
"report.tex",
"研究报告_2026.tex",
"исследование.tex",
],
ids=["ascii", "cjk", "cyrillic"],
)
def test_report_export_offers_an_ascii_filename_fallback(self, filename):
disposition = _export_content_disposition(filename)
assert _PLAIN_FILENAME_PARAM.search(disposition), disposition
@pytest.mark.parametrize("route_prefix", ["/library", "/library/api"])
@pytest.mark.parametrize(
"filename", ["report.pdf", "研究报告_2026.pdf", "исследование.pdf"]
)
def test_library_pdf_view_offers_an_ascii_filename_fallback(
self, authenticated_client, route_prefix, filename
):
"""Read the real response for an authenticated, stored PDF."""
from local_deep_research.database.models.library import (
Document,
DocumentBlob,
SourceType,
)
from local_deep_research.database.session_context import (
get_user_db_session,
)
username = authenticated_client.get("/auth/check").json()["username"]
document_id = str(uuid.uuid4())
pdf_bytes = b"%PDF-1.4\nfixture PDF\n%%EOF\n"
with get_user_db_session(username) as session:
source_type = session.query(SourceType).first()
assert source_type is not None
session.add(
Document(
id=document_id,
source_type_id=source_type.id,
document_hash=uuid.uuid4().hex,
file_size=len(pdf_bytes),
file_type="pdf",
filename=filename,
title="Download header fixture",
status="completed",
)
)
session.flush()
session.add(
DocumentBlob(document_id=document_id, pdf_binary=pdf_bytes)
)
session.commit()
response = authenticated_client.get(
f"{route_prefix}/document/{document_id}/pdf"
)
assert response.status_code == 200, response.text[:300]
assert response.content == pdf_bytes
assert response.headers["content-type"] == "application/pdf"
disposition = response.headers["content-disposition"]
assert disposition.startswith('inline; filename="')
assert _PLAIN_FILENAME_PARAM.search(disposition), disposition
assert f"filename*=UTF-8''{quote(filename, safe='')}" in disposition
def _export_content_disposition(filename: str) -> str:
"""Drive the real export route far enough to read its header.
Mirrors the mocking recipe already used by
``tests/web/routers/test_export_filename_encoding.py`` -- the DB
session, the report assembly and the exporter are stubbed, so what
remains executing is the route's own header construction.
"""
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
module = "local_deep_research.web.routers.research"
from local_deep_research.web.routers.research import (
export_research_report,
)
row = Mock()
row.title = "t"
row.query = "t"
query = MagicMock()
query.filter_by.return_value.first.return_value = row
session = MagicMock()
session.query.return_value = query
@contextmanager
def _fake_db_session(*args, **kwargs):
yield session
with (
patch(f"{module}.get_user_db_session", side_effect=_fake_db_session),
patch(
"local_deep_research.web.services.report_assembly_service"
".assemble_full_report",
return_value="# report",
),
patch(
f"{module}.export_report_to_memory",
return_value=(b"content", filename, "application/x-latex"),
),
):
response = export_research_report(
Mock(), "rid-1", "latex", username="alice"
)
assert response.status_code == 200, response.body
return response.headers["content-disposition"]
# ===========================================================================
# 4. Multipart: a part named only by RFC 2231 filename*
# ===========================================================================
_BOUNDARY = b"LDRencodingBoundary"
#: One part whose Content-Disposition carries ONLY the RFC 2231/5987
#: extended parameter. This is what a client sends when the filename has
#: no representable ASCII form; it is legal, and Werkzeug accepted it.
_FILENAME_STAR_PART = (
b"--" + _BOUNDARY + b"\r\n"
b'Content-Disposition: form-data; name="files"; '
b"filename*=UTF-8''%E7%A0%94%E7%A9%B6.pdf\r\n"
b"Content-Type: application/pdf\r\n\r\n"
b"%PDF-1.4 body\r\n"
b"--" + _BOUNDARY + b"--\r\n"
)
_MULTIPART_CT = b"multipart/form-data; boundary=" + _BOUNDARY
class TestMultipartFilenameEncoding:
@pytest.mark.asyncio
async def test_raw_utf8_filename_and_field_decode_identically_both_sides(
self,
):
"""Positive control: the ordinary case is fine and at parity.
A ``filename="..."`` carrying raw UTF-8 bytes -- which is what
every browser sends -- decodes to the same string under Werkzeug
and Starlette, astral emoji included, and so do RTL field values.
Without this, the divergence below could be read as "Starlette
cannot do non-ASCII filenames".
"""
from starlette.datastructures import UploadFile
name = "研究\U0001f680.pdf"
body = (
b"--" + _BOUNDARY + b"\r\n"
b'Content-Disposition: form-data; name="files"; filename="'
+ name.encode("utf-8")
+ b'"\r\n'
b"Content-Type: application/pdf\r\n\r\nX\r\n"
b"--" + _BOUNDARY + b"\r\n"
b'Content-Disposition: form-data; name="title"\r\n\r\n'
+ NON_ASCII_TEXT.encode("utf-8")
+ b"\r\n--"
+ _BOUNDARY
+ b"--\r\n"
)
request = _starlette_request(body, _MULTIPART_CT)
form = await request.form()
werkzeug_files = _werkzeug_request(
body, _MULTIPART_CT.decode("ascii")
).files
assert isinstance(form["files"], UploadFile)
assert form["files"].filename == name
assert werkzeug_files["files"].filename == name
assert form["title"] == NON_ASCII_TEXT
def test_werkzeug_parsed_a_filename_star_part_as_a_file(self):
"""EXECUTION-VERIFIED baseline (werkzeug 3.1.8).
main's parser understood RFC 2231 continuations and produced a
FileStorage with the decoded name.
"""
files = _werkzeug_request(
_FILENAME_STAR_PART, _MULTIPART_CT.decode("ascii")
).files
assert "files" in files
assert files["files"].filename == "研究.pdf"
@pytest.mark.asyncio
async def test_starlette_turns_that_part_into_a_plain_form_string(self):
"""The mechanism behind the xfail below, pinned on its own.
Starlette's multipart parser looks only for a ``filename``
parameter. With just ``filename*`` it decides the part is not a
file, so the part becomes an ordinary form field whose *value is
the file's bytes*. Two consequences worth pinning separately from
the "should work" assertion: the upload is lost, and the raw file
content is now sitting in a string form field.
"""
from starlette.datastructures import UploadFile
request = _starlette_request(_FILENAME_STAR_PART, _MULTIPART_CT)
form = await request.form()
value = form["files"]
assert not isinstance(value, UploadFile)
assert isinstance(value, str)
assert value == "%PDF-1.4 body"
@pytest.mark.xfail(
strict=True,
reason=(
"DEFECT: Starlette's multipart parser recognises only the "
"plain `filename` parameter, so a part named solely by RFC "
"2231 `filename*=UTF-8''...` is not an UploadFile. Both "
"upload handlers -- routers/rag.py::upload_to_collection and "
"routers/research.py's PDF upload -- select parts with "
"`isinstance(f, UploadFile)` over form.getlist('files'), so "
"the part is filtered out and the request is answered "
"400 'No files provided' with no indication that a file was "
"received and dropped. Werkzeug parsed the same bytes as a "
"file (see the test above), so this is a port regression, not "
"a client error. Fix belongs in the handlers: fall back to "
"the part's filename* when Starlette leaves it unnamed, or "
"pin/patch the parser."
),
)
@pytest.mark.asyncio
async def test_a_filename_star_part_should_arrive_as_a_named_upload(self):
from starlette.datastructures import UploadFile
request = _starlette_request(_FILENAME_STAR_PART, _MULTIPART_CT)
form = await request.form()
uploads = [
part
for part in form.getlist("files")
if isinstance(part, UploadFile)
]
assert uploads, "the part was not parsed as a file at all"
assert uploads[0].filename == "研究.pdf"
class TestUrlencodedFormBodyEncoding:
@pytest.mark.asyncio
async def test_invalid_utf8_in_a_form_field_diverges_from_werkzeug(self):
"""Pinned divergence, both sides executed.
Werkzeug leaves an undecodable percent-escape as the literal text
``%FF``; Starlette unquotes it and substitutes U+FFFD. Neither
raises, so no route 500s over it -- but a value that reached a
handler as ``"%FF"`` on main now reaches it as ``"\\ufffd"``, and
anything comparing or storing that value sees different bytes.
Framework-inherent (``QueryParams``/``FormData`` hardcode the
substitution), so this is pinned rather than filed.
"""
body = b"password=%FF&username=bob"
content_type = "application/x-www-form-urlencoded"
starlette_form = await _starlette_request(
body, content_type.encode("ascii")
).form()
werkzeug_form = _werkzeug_request(body, content_type).form
assert werkzeug_form["password"] == "%FF"
assert starlette_form["password"] == "�"
assert starlette_form["username"] == werkzeug_form["username"]