~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
"""Malformed and non-object JSON must share each route's validation envelope.
The envelope answers a body the CLIENT got wrong. A body that is too large
for the JSON cap is not the route's to answer: the second half of this file
pins that it still reaches ``BodySizeLimitMiddleware`` (413) instead of being
turned into the envelope. A client that disconnects mid-body propagates the
same way, to the app-level handler: the third section pins that it is logged
at info level and answered 499, not reported as a 500. That the helper lets
both through is pinned at the helper, in
``tests/web/dependencies/test_json_body_contract.py``.
"""
import ast
import asyncio
import json
from pathlib import Path
import pytest
from local_deep_research import web
from local_deep_research.web.dependencies.json_body import DEFAULT_MESSAGE
#: The routes this change moved onto ``read_json_dict``, each with the 400
#: envelope it answers for a body that is not a JSON object. Callers that
#: already used the helper are listed below only where they also have a broad
#: ``try``; four (``api.py``'s add-resource route and three news folder and
#: subscription routes) have no entry in this file.
ENVELOPES = [
(
"post",
"/settings/save_all_settings",
{"status": "error", "message": "No settings data provided"},
),
(
"put",
"/settings/api/search-favorites",
{"error": "No data provided"},
),
(
"post",
"/settings/api/search-favorites/toggle",
{"error": "No data provided"},
),
("put", "/settings/api/example.key", {"error": "No data provided"}),
(
"post",
"/library/api/download-research/research-id",
{"error": DEFAULT_MESSAGE},
),
("post", "/library/api/download-bulk", {"error": DEFAULT_MESSAGE}),
("post", "/library/api/mark-redownload", {"error": DEFAULT_MESSAGE}),
("post", "/library/api/check-downloads", {"error": DEFAULT_MESSAGE}),
("post", "/library/api/download-source", {"error": DEFAULT_MESSAGE}),
(
"post",
"/metrics/api/ratings/research-id",
{"status": "error", "message": DEFAULT_MESSAGE},
),
(
"post",
"/metrics/api/cost-calculation",
{"error": "No data provided"},
),
(
"post",
"/metrics/api/domain-classifications/classify",
{"status": "error", "message": DEFAULT_MESSAGE},
),
(
"post",
"/library/api/research-history/convert-all",
{"success": False, "error": DEFAULT_MESSAGE},
),
(
"post",
"/library/api/research/research-id/add-to-collection",
{"success": False, "error": DEFAULT_MESSAGE},
),
(
"post",
"/library/api/collections/collection-id/search",
{"success": False, "error": DEFAULT_MESSAGE},
),
(
"post",
"/library/api/collections",
{"success": False, "error": DEFAULT_MESSAGE},
),
(
"put",
"/library/api/collections/collection-id",
{"success": False, "error": DEFAULT_MESSAGE},
),
(
"post",
"/library/api/collections/collection-id/index/start",
{"success": False, "error": DEFAULT_MESSAGE},
),
("post", "/benchmark/api/start", {"error": "No data provided"}),
("post", "/benchmark/api/start-simple", {"error": DEFAULT_MESSAGE}),
(
"post",
"/api/save_raw_config",
{"success": False, "error": DEFAULT_MESSAGE},
),
]
@pytest.mark.parametrize(("method", "path", "envelope"), ENVELOPES)
@pytest.mark.parametrize(
"body", [b"{bad json", b"[1]"], ids=["malformed", "non-object"]
)
def test_json_body_error_envelope(
authenticated_client, method, path, envelope, body
):
response = getattr(authenticated_client, method)(
path,
content=body,
headers={"Content-Type": "application/json"},
)
assert response.status_code == 400
assert response.json() == envelope
# ---------------------------------------------------------------------------
# A body past the JSON cap is the middleware's to reject, not the route's
# ---------------------------------------------------------------------------
#
# BodySizeLimitMiddleware rejects a declared over-cap Content-Length before
# any route runs. A body streamed WITHOUT one (chunked transfer) can only be
# counted as it is read, so the middleware raises from its receive wrapper;
# the exception surfaces inside ``await request.json()`` and the middleware
# turns it into the 413 and logs the rejection once it reaches it. A broad
# ``except`` between the two answers 400 with the route's envelope instead.
#: Routes that call ``read_json_dict`` from a handler that also has a broad
#: ``try``/``except`` for the work after the parse (news_flask_api.py). The
#: routes above have none, so these are the ones where a wrapper around the
#: helper, rather than the helper itself, could still swallow the exception.
NEWS_ROUTES_WITH_A_BROAD_TRY = [
("post", "/news/api/subscribe"),
("post", "/news/api/vote"),
("post", "/news/api/feedback/batch"),
("post", "/news/api/feedback/card-id"),
("put", "/news/api/subscriptions/subscription-id"),
("post", "/news/api/preferences"),
]
OVER_CAP_ROUTES = [
(method, path) for method, path, _envelope in ENVELOPES
] + NEWS_ROUTES_WITH_A_BROAD_TRY
def _chunks_past_the_json_cap():
"""A JSON object a little past the 16 MB JSON cap, yielded in pieces.
A generator body is sent as ``Transfer-Encoding: chunked`` with no
Content-Length header, so the middleware's declared-length check has
nothing to reject and only the streaming count can.
"""
from local_deep_research.web.fastapi_app import (
_DEFAULT_MAX_SMALL_JSON_BODY_SIZE,
)
yield b'{"padding": "'
yield b"x" * _DEFAULT_MAX_SMALL_JSON_BODY_SIZE
yield b'"}'
@pytest.mark.parametrize(("method", "path"), OVER_CAP_ROUTES)
def test_over_cap_chunked_body_is_a_413_from_the_middleware(
authenticated_client, loguru_caplog, method, path
):
with loguru_caplog.at_level("WARNING"):
response = getattr(authenticated_client, method)(
path,
content=_chunks_past_the_json_cap(),
headers={"Content-Type": "application/json"},
)
assert response.status_code == 413, (
f"{method.upper()} {path} answered {response.status_code} "
f"{response.text[:200]!r} for a body streamed past the JSON cap; "
"the middleware's 413 was swallowed on the way out"
)
# Same negotiation as BodySizeLimitMiddleware._send_413: a JSON body
# for API paths, the branded page elsewhere.
if "/api/" in path:
assert response.json() == {"error": "Request too large"}
# The 413 came from the middleware's own handler, which is also what
# puts the rejection in the log for monitoring.
assert "Rejected over-limit request body" in loguru_caplog.text
# ---------------------------------------------------------------------------
# A client that hangs up mid-body is not a server fault
# ---------------------------------------------------------------------------
#
# ``read_json_dict`` lets ``ClientDisconnect`` propagate, as it does the
# over-cap exception. The over-cap exception has BodySizeLimitMiddleware to
# receive it; ``ClientDisconnect`` has the app-level handler in
# fastapi_app.py. Without that handler the catch-all reports the hang-up as a
# backend outage: an error-level log with a traceback, and a 500.
def _hang_up_mid_body(client, method, path):
"""Start a JSON body at ``path`` through the real stack, then disconnect.
Returns ``(status, body, raised)``. TestClient cannot hang up
mid-request, so the assembled app is called as an ASGI callable. The
request is built by the authenticated client, so it carries the session
cookie and CSRF header and reaches the route, and it promises more bytes
than it sends, as a connection dropped in transit does.
"""
start = b'{"padding": "'
request = client.build_request(
method.upper(),
path,
headers={"Content-Type": "application/json", "Content-Length": "4096"},
content=start,
)
scope = {
"type": "http",
"asgi": {"version": "3.0", "spec_version": "2.3"},
"http_version": "1.1",
"method": request.method,
"scheme": "http",
"path": path,
"raw_path": path.encode(),
"root_path": "",
"query_string": b"",
"client": ("testclient", 50000),
"server": ("testserver", 80),
"headers": [(k.lower(), v) for k, v in request.headers.raw],
}
pending = [
{"type": "http.request", "body": start, "more_body": True},
{"type": "http.disconnect"},
]
sent = []
async def receive():
return pending.pop(0) if pending else {"type": "http.disconnect"}
async def send(message):
sent.append(message)
async def call():
try:
await asyncio.wait_for(client.app(scope, receive, send), 30)
except Exception as exc: # the catch-all re-raises after its 500
return exc
return None
raised = asyncio.run(call())
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, raised
def _json_or_text(body):
try:
return json.loads(body)
except ValueError:
return body.decode("utf-8", "replace")[:200]
def test_a_client_hanging_up_mid_body_is_not_a_server_error(
authenticated_client, loguru_caplog
):
"""On every route that reads its body through ``read_json_dict``, the
hang-up is an info line with no traceback and a 499, not an error-level
log, a 500 and an exception escaping the app."""
expected = {
"status": 499,
"body": {"error": "Client disconnected"},
"raised": None,
"error_logs": [],
# (level, carries a traceback, names the route)
"hang_up_logs": [("INFO", False, True)],
}
wrong = {}
for method, path in OVER_CAP_ROUTES:
loguru_caplog.clear()
status, body, raised = _hang_up_mid_body(
authenticated_client, method, path
)
records = loguru_caplog.records
observed = {
"status": status,
"body": _json_or_text(body),
"raised": type(raised).__name__ if raised else None,
"error_logs": [
r.getMessage()
for r in records
if r.levelname in ("ERROR", "CRITICAL")
],
"hang_up_logs": [
(r.levelname, bool(r.exc_info), path in r.getMessage())
for r in records
if "Client disconnected" in r.getMessage()
],
}
if observed != expected:
wrong[f"{method.upper()} {path}"] = observed
assert not wrong, (
f"{len(wrong)} of {len(OVER_CAP_ROUTES)} routes did not treat a "
f"mid-body disconnect as the client's doing; expected {expected}, "
f"first: {next(iter(wrong.items()))}"
)
# --- static half: no route may swallow what the helper now lets through ---
WEB_DIR = Path(web.__file__).resolve().parent
_BROAD_EXCEPTIONS = frozenset({"Exception", "BaseException"})
def _handler_is_broad(handler):
"""A bare ``except``, or one naming ``Exception``/``BaseException``."""
if handler.type is None:
return True
clauses = (
handler.type.elts
if isinstance(handler.type, ast.Tuple)
else [handler.type]
)
return any(
isinstance(clause, ast.Name) and clause.id in _BROAD_EXCEPTIONS
for clause in clauses
)
def _read_json_dict_calls(tree):
"""``[(lineno, in_a_broad_try)]`` for each ``read_json_dict`` call.
``in_a_broad_try`` means the call sits in the BODY of a ``try`` whose
handlers include a broad one, within the same function: that handler
would catch anything the helper lets propagate.
"""
parents = {
child: parent
for parent in ast.walk(tree)
for child in ast.iter_child_nodes(parent)
}
calls = []
for call in ast.walk(tree):
if not isinstance(call, ast.Call):
continue
name = getattr(call.func, "id", None) or getattr(
call.func, "attr", None
)
if name != "read_json_dict":
continue
wrapped = False
child, node = call, parents.get(call)
while node is not None and not isinstance(
node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)
):
if (
isinstance(node, ast.Try)
and child in node.body
and any(_handler_is_broad(h) for h in node.handlers)
):
wrapped = True
break
child, node = node, parents.get(node)
calls.append((call.lineno, wrapped))
return calls
_WRAPPED_CALL = """
async def handler(request):
try:
data, err = await read_json_dict(request)
except Exception:
return None
"""
_HOISTED_CALL = """
async def handler(request):
data, err = await read_json_dict(request)
try:
use(data)
except Exception:
return None
"""
def test_the_scan_tells_a_wrapped_call_from_a_hoisted_one():
wrapped = _read_json_dict_calls(ast.parse(_WRAPPED_CALL))
hoisted = _read_json_dict_calls(ast.parse(_HOISTED_CALL))
assert [in_try for _line, in_try in wrapped] == [True]
assert [in_try for _line, in_try in hoisted] == [False]
def test_no_read_json_dict_caller_wraps_it_in_a_broad_except():
"""A handler's own ``except Exception`` would undo the helper's contract.
``read_json_dict`` lets the size-limit exception and a client disconnect
propagate; a ``try``/``except Exception`` around the call in the route
catches them one frame up and answers 400 or 500 instead of the
middleware's 413. Parse the body before the ``try``.
"""
calls = 0
offenders = []
for path in sorted(WEB_DIR.rglob("*.py")):
tree = ast.parse(path.read_text(encoding="utf-8"))
for lineno, wrapped in _read_json_dict_calls(tree):
calls += 1
if wrapped:
offenders.append(f"{path.relative_to(WEB_DIR)}:{lineno}")
assert calls >= 30, (
f"only {calls} read_json_dict call sites found; the scan has "
"stopped matching the source"
)
assert offenders == [], (
"read_json_dict is called inside a broad except, which swallows the "
f"request-size limit's exception: {offenders}"
)