~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
"""``read_json_dict`` must reject every non-object body, in all three shapes.
Ported from ``tests/security/test_decorators.py`` on ``origin/main``, which
tested ``security/decorators.py::require_json_body`` — a Flask decorator the
migration deleted. Its FastAPI successor is
``web/dependencies/json_body.py``. ``read_json_dict`` and
``json_body_error`` reproduce the decorator's parse-and-shape check and its
400, so that part is the same contract on a new spelling. The decorator's
media-type check is ``is_json_request``, which ``read_json_dict`` does not
call; the last section of this file pins it.
What was actually unpinned before this file
-------------------------------------------
The helper has *incidental* coverage on the branch: the malformed-body and
invalid-UTF-8 paths in ``tests/web/test_request_response_boundary_contracts.py``
and ``tests/web/test_encoding_and_non_ascii.py``, and the ``success``
envelope at one route in
``tests/web/routers/test_json_body_contract_port_fidelity.py``. None of it
drives a body that *parses cleanly and is not an object* — JSON ``null``,
an array, a bare string, a number, a boolean. That ``isinstance(data, dict)``
check is the entire reason ``require_json_body`` existed: without it a
handler's ``data.get(...)`` raises ``AttributeError`` and a malformed client
request becomes a 500. Deleting the isinstance line from ``read_json_dict``
today turns no test in the tree red.
``tests/web/routers/test_library_hostile_input.py`` deliberately derives the
envelope from ``json_body_error`` itself rather than restating it, which is
right for its purpose but means the literal key sets are not pinned there
either. They are pinned here, as main pinned them: the front end branches on
exactly these keys.
Harness: main built a Flask app with six routes, one per (format, message)
combination, and drove real requests through them — the point being to test
the guard as mounted, not the helper in isolation. Same shape here with a
FastAPI app, since ``read_json_dict`` is called from inside handlers rather
than applied as a decorator.
Dropped from the port: ``TestDecoratorMetadata::test_wraps_preserves_name``
pinned that ``@require_json_body`` used ``functools.wraps`` so Flask's
endpoint name survived. There is no decorator any more — the guard is a
plain call inside the handler — so there is no wrapper to preserve anything.
"""
import asyncio
import json
import sys
import pytest
from fastapi import FastAPI, Request
from starlette.requests import ClientDisconnect
from starlette.testclient import TestClient
from local_deep_research.web.dependencies.json_body import (
is_json_request,
read_json_dict,
)
from tests.test_utils import nested_json_past_the_scanner_limit
DEFAULT = "Request body must be valid JSON"
@pytest.fixture(scope="module")
def app():
"""One route per (error_format, error_message) main exercised."""
app = FastAPI()
def _route(error_format, error_message=None):
async def handler(request: Request):
kwargs = (
{}
if error_message is None
else {"error_message": error_message}
)
data, err = await read_json_dict(request, error_format, **kwargs)
if err is not None:
return err
return {"ok": True}
return handler
app.post("/simple")(_route("simple"))
app.post("/simple-custom")(_route("simple", "Query parameter is required"))
app.post("/status")(_route("status"))
app.post("/status-custom")(_route("status", "No settings data provided"))
app.post("/success")(_route("success"))
app.post("/success-custom")(_route("success", "Missing required fields"))
return app
@pytest.fixture(scope="module")
def client(app):
return TestClient(app, raise_server_exceptions=False)
def _post(client, path, body=None, content_type="application/json"):
"""POST a raw body, or no body at all when ``body`` is None."""
headers = {"Content-Type": content_type}
if body is None:
return client.post(path, headers=headers, follow_redirects=False)
return client.post(
path, content=body, headers=headers, follow_redirects=False
)
# ---------------------------------------------------------------------------
# Valid dict payloads — should always pass through
# ---------------------------------------------------------------------------
class TestValidPayloads:
def test_valid_dict_passes_simple(self, client):
resp = _post(client, "/simple", json.dumps({"key": "value"}))
assert resp.status_code == 200
assert resp.json()["ok"] is True
def test_valid_dict_passes_status(self, client):
resp = _post(client, "/status", json.dumps({"key": "value"}))
assert resp.status_code == 200
def test_valid_dict_passes_success(self, client):
resp = _post(client, "/success", json.dumps({"key": "value"}))
assert resp.status_code == 200
def test_empty_dict_passes(self, client):
"""An empty dict {} is a valid JSON body."""
resp = _post(client, "/simple", json.dumps({}))
assert resp.status_code == 200
def test_nested_dict_passes(self, client):
resp = _post(
client, "/simple", json.dumps({"nested": {"a": 1}, "list": [1, 2]})
)
assert resp.status_code == 200
# ---------------------------------------------------------------------------
# Empty / missing body — should reject
# ---------------------------------------------------------------------------
class TestEmptyBody:
def test_no_body_simple(self, client):
resp = _post(client, "/simple")
assert resp.status_code == 400
assert resp.json()["error"] == DEFAULT
def test_no_body_status(self, client):
resp = _post(client, "/status")
assert resp.status_code == 400
assert resp.json()["status"] == "error"
assert resp.json()["message"] == DEFAULT
def test_no_body_success(self, client):
resp = _post(client, "/success")
assert resp.status_code == 400
assert resp.json()["success"] is False
assert resp.json()["error"] == DEFAULT
def test_empty_string_body(self, client):
resp = _post(client, "/simple", "")
assert resp.status_code == 400
# ---------------------------------------------------------------------------
# Malformed JSON — should reject
# ---------------------------------------------------------------------------
class TestMalformedJSON:
def test_invalid_json_string(self, client):
resp = _post(client, "/simple", "{not valid json")
assert resp.status_code == 400
assert "error" in resp.json()
def test_plain_text_body(self, client):
resp = _post(client, "/simple", "hello world", "text/plain")
assert resp.status_code == 400
# ---------------------------------------------------------------------------
# Non-dict JSON values — should reject
#
# The heart of the guard, and the part with no coverage on this branch
# before now: these bodies PARSE, so nothing upstream rejects them; only
# the isinstance check stands between them and `data.get(...)`.
# ---------------------------------------------------------------------------
class TestNonDictJSON:
@pytest.mark.parametrize(
("label", "value"),
[
("null", None),
("list", [1, 2, 3]),
("string", "just a string"),
("number", 42),
("boolean", True),
],
)
def test_non_object_bodies_are_rejected(self, client, label, value):
resp = _post(client, "/simple", json.dumps(value))
assert resp.status_code == 400, (
f"a JSON {label} body was accepted as an object; the handler's "
f"data.get(...) would raise and surface as a 500"
)
assert resp.json() == {"error": DEFAULT}
# ---------------------------------------------------------------------------
# Every way the parser can refuse a body is the client's 400
#
# ``request.json()`` is ``json.loads(await self.body())``. Besides
# ``json.JSONDecodeError`` it raises ``UnicodeDecodeError`` for undecodable
# bytes, a plain ``ValueError`` (not a JSONDecodeError) for an integer literal
# past ``sys.get_int_max_str_digits()``, and ``RecursionError`` (not a
# ValueError) for nesting deeper than the C scanner can follow. All four are
# a malformed body. A guard narrowed to the first two would answer the other
# two with a 500.
# ---------------------------------------------------------------------------
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 TestEveryParserRefusalIsTheClientsFourHundred:
@pytest.mark.parametrize(
("build_body", "refusal"),
[
pytest.param(
lambda: b"{not json", json.JSONDecodeError, id="bad-syntax"
),
pytest.param(
lambda: b'{"name": "\xff\xfe"}',
UnicodeDecodeError,
id="undecodable-bytes",
),
pytest.param(
_integer_past_the_digit_limit,
ValueError,
id="integer-past-the-digit-limit",
),
pytest.param(
nested_json_past_the_scanner_limit,
RecursionError,
id="nesting-too-deep",
),
],
)
def test_a_body_the_parser_refuses_gets_the_envelope(
self, client, build_body, refusal
):
body = build_body()
# Premise: the body fails in exactly the way it is named for, so the
# 400 below cannot be a pass for some other reason.
with pytest.raises(refusal) as caught:
json.loads(body)
assert type(caught.value) is refusal
resp = _post(client, "/simple", body)
assert resp.status_code == 400, (
f"{refusal.__name__} from the parser was not answered as the "
f"client's malformed body: {resp.status_code} {resp.text[:200]}"
)
assert resp.json() == {"error": DEFAULT}
# ---------------------------------------------------------------------------
# Nothing else is the helper's to answer
#
# ``request.json()`` reads the request stream, so failures of the stream
# surface from it as well: the over-cap exception BodySizeLimitMiddleware
# raises from its receive wrapper, and a client that disconnects mid-body.
# Neither is a malformed body, and the helper must not make it one.
# ---------------------------------------------------------------------------
def _request_with_receive(receive):
return Request(
{"type": "http", "method": "POST", "path": "/", "headers": []},
receive,
)
class TestOnlyAMalformedBodyIsAnsweredHere:
def test_a_body_delivered_in_pieces_parses(self):
"""Positive control: the scripted channel below is a working one."""
pieces = iter(
[
{"type": "http.request", "body": b'{"a": ', "more_body": True},
{"type": "http.request", "body": b"1}", "more_body": False},
]
)
async def receive():
return next(pieces)
data, err = asyncio.run(read_json_dict(_request_with_receive(receive)))
assert (data, err) == ({"a": 1}, None)
def test_a_client_disconnect_propagates(self):
async def receive():
return {"type": "http.disconnect"}
with pytest.raises(ClientDisconnect):
asyncio.run(read_json_dict(_request_with_receive(receive)))
def test_an_error_raised_by_the_receive_channel_propagates(self):
class _OverTheCap(Exception):
"""Stands in for BodySizeLimitMiddleware's over-cap exception."""
reads = []
async def receive():
reads.append(1)
if len(reads) == 1:
return {
"type": "http.request",
"body": b'{"padding": "xxxx',
"more_body": True,
}
raise _OverTheCap
with pytest.raises(_OverTheCap):
asyncio.run(read_json_dict(_request_with_receive(receive)))
assert len(reads) == 2, "the error must come from mid-body, not setup"
# ---------------------------------------------------------------------------
# Custom error messages
# ---------------------------------------------------------------------------
class TestCustomMessages:
def test_simple_custom_message(self, client):
resp = _post(client, "/simple-custom")
assert resp.status_code == 400
assert resp.json()["error"] == "Query parameter is required"
def test_status_custom_message(self, client):
resp = _post(client, "/status-custom")
assert resp.status_code == 400
assert resp.json()["message"] == "No settings data provided"
def test_success_custom_message(self, client):
resp = _post(client, "/success-custom")
assert resp.status_code == 400
assert resp.json()["error"] == "Missing required fields"
# ---------------------------------------------------------------------------
# Error format response structure
#
# The front end branches on these key sets: `success` drives the chat, RAG
# and library-search views; `status` is what the settings and ratings pages
# expect. A renamed key turns a handled validation error into an unhandled
# one in the browser.
# ---------------------------------------------------------------------------
class TestErrorFormatStructure:
def test_simple_format_keys(self, client):
resp = _post(client, "/simple")
assert resp.status_code == 400
assert set(resp.json()) == {"error"}
def test_status_format_keys(self, client):
resp = _post(client, "/status")
assert resp.status_code == 400
assert set(resp.json()) == {"status", "message"}
def test_success_format_keys(self, client):
resp = _post(client, "/success")
assert resp.status_code == 400
assert set(resp.json()) == {"success", "error"}
# ---------------------------------------------------------------------------
# Media type: is_json_request
#
# main's get_json(silent=True) returned None unless Werkzeug's
# Request.is_json held: the media type (the part before any ";", stripped
# and lowercased) is application/json or application/*+json. Routes that
# restore that check call is_json_request before parsing. The rejected list
# holds the near misses a weakened check would let through:
# startswith("application/") accepts the form and octet-stream types, a
# "json" substring test accepts jsonx, x-json, json-seq and text/json, an
# endswith("json") test accepts x-json and text/json, and a +json suffix
# test without the application/ prefix accepts text/x+json.
# ---------------------------------------------------------------------------
def _request_with_content_type(content_type):
"""A real request whose only header is the given Content-Type."""
headers = (
[]
if content_type is None
else [(b"content-type", content_type.encode("latin-1"))]
)
return Request(
{"type": "http", "method": "POST", "path": "/", "headers": headers}
)
class TestIsJsonRequest:
@pytest.mark.parametrize(
"content_type",
[
"application/json",
"application/json; charset=utf-8",
"Application/JSON",
" application/json ; charset=utf-8",
"application/vnd.api+json",
"application/problem+json; charset=utf-8",
"APPLICATION/VND.EXAMPLE+JSON",
],
)
def test_json_media_types_are_accepted(self, content_type):
request = _request_with_content_type(content_type)
assert is_json_request(request) is True
@pytest.mark.parametrize(
"content_type",
[
None,
"",
"text/plain",
"text/plain; charset=utf-8",
"application/x-www-form-urlencoded",
"multipart/form-data; boundary=x",
"application/octet-stream",
"application/jsonx",
"application/x-json",
"application/json-seq",
"text/json",
"text/x+json",
],
)
def test_other_media_types_are_rejected(self, content_type):
request = _request_with_content_type(content_type)
assert is_json_request(request) is False