~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
"""Port-fidelity checks for the JSON request-body contract of four routers
ported from Flask blueprints that read a body by hand:
* ``web/routers/api.py`` (main: ``web/routes/api_routes.py``)
* ``web/routers/api_v1.py`` (main: ``web/api.py``)
* ``web/routers/notes.py`` (main: ``web/routes/notes_routes.py``)
* ``web/routers/research.py`` (main: ``web/routes/research_routes.py``)
Flask centralised two guarantees in ``request.get_json()`` /
``@require_json_body`` that the hand-written FastAPI ports must preserve:
1. A body that will not parse and a body that parses to a non-object were
ONE failure with ONE response envelope.
2. ``get_json(silent=True)`` returned ``None`` for any request whose
Content-Type was not a JSON media type (``application/json`` or
``application/*+json``), so a mislabelled body never reached the
handler.
This file pins the first guarantee for resource creation and the second for
each route it names.
"""
from unittest.mock import patch
import pytest
# main's @require_json_body default message, kept as one constant by the
# port so the two halves of its contract cannot drift apart.
from local_deep_research.web.dependencies.json_body import DEFAULT_MESSAGE
# ---------------------------------------------------------------------------
# POST /research/api/resources/{research_id}
#
# main (web/routes/api_routes.py::api_add_resource) carried
# ``@require_json_body(error_format="status")``, which collapsed BOTH "body
# will not parse" and "body is not an object" into the same
# 400 {"status": "error", "message": "Request body must be valid JSON"}.
#
# The route uses read_json_dict so malformed and non-object bodies share
# that same envelope.
# ---------------------------------------------------------------------------
_ADD_RESOURCE = "/research/api/resources/some-research-id"
_MAIN_ENVELOPE = {"status": "error", "message": DEFAULT_MESSAGE}
@pytest.mark.parametrize(
"body", [b"null", b"[1, 2]", b'"a string"', b"12"], ids=repr
)
def test_add_resource_non_object_body_uses_main_status_envelope(
authenticated_client, body
):
"""A parseable non-object body keeps the legacy status envelope."""
resp = authenticated_client.post(
_ADD_RESOURCE,
content=body,
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400
assert resp.json() == _MAIN_ENVELOPE
@pytest.mark.parametrize(
"body", [b"{not json", b""], ids=["malformed", "empty"]
)
def test_add_resource_unparseable_body_uses_main_status_envelope(
authenticated_client, body
):
resp = authenticated_client.post(
_ADD_RESOURCE,
content=body,
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400
assert resp.json() == _MAIN_ENVELOPE
# ---------------------------------------------------------------------------
# Content-Type gate
#
# Every mutating notes route on main read its body with
# ``request.get_json(silent=True)``, which returns None when
# ``request.is_json`` is false — i.e. when the client sent neither an
# ``application/json`` nor an ``application/*+json`` Content-Type.
# create_note then answered
# 400 {"success": false, "error": "No data provided"}.
#
# The notes dependency still streams and bounds its body, but the media type
# must be checked before parsing. The direct JSON parsers need the same gate.
# ---------------------------------------------------------------------------
def test_notes_create_rejects_unparseable_body_as_no_data(
authenticated_client,
):
"""Control for the gate below: _notes_json_body's OTHER main-parity
branch still works — an unparseable body degrades to {} so the route's
own "No data provided" 400 fires, exactly as get_json(silent=True)
made it on main. The body is labelled ``application/json`` so it passes
the media-type gate and reaches the parser; a non-JSON label would stop
at the gate and never exercise this branch."""
resp = authenticated_client.post(
"/notes/api/notes",
content=b"{not json",
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400
assert resp.json() == {"success": False, "error": "No data provided"}
def test_notes_create_requires_json_content_type(authenticated_client):
resp = authenticated_client.post(
"/notes/api/notes",
content=b'{"title": "t", "content": "c"}',
headers={"Content-Type": "text/plain"},
)
assert resp.status_code == 400
assert resp.json() == {"success": False, "error": "No data provided"}
def test_api_v1_quick_summary_requires_json_content_type(
authenticated_client,
):
"""The body passes the route's own checks, so only the media-type gate
can produce this 400. The research function is patched so that a
regression fails here instead of running research."""
with patch(
"local_deep_research.api.research_functions.quick_summary",
return_value={},
) as quick_summary:
resp = authenticated_client.post(
"/api/v1/quick_summary",
content=b'{"query": "hello"}',
headers={"Content-Type": "text/plain"},
)
quick_summary.assert_not_called()
assert resp.status_code == 400
assert resp.json() == {"error": "Query parameter is required"}
# POST /api/start_research, and POST /research/api/start, which api.py hands
# to the same handler. main's @require_json_body(error_format="status")
# answered a non-JSON media type with _MAIN_ENVELOPE. The route keeps its own
# messages for a malformed or non-object JSON body, pinned in
# tests/security/test_hostile_input_matrix.py. _start_research_sync runs only
# after the handler has accepted the body.
_START_RESEARCH_SYNC = (
"local_deep_research.web.routers.research._start_research_sync"
)
_START_BODY = b'{"query": "hello"}'
# POST /api/save_raw_config: main used error_format="success". The config
# file write runs only after the handler has accepted the body.
_WRITE_CONFIG = (
"local_deep_research.security.file_write_verifier.write_file_verified"
)
_RAW_CONFIG_BODY = b'{"raw_config": "answer = 42"}'
_SUCCESS_ENVELOPE = {"success": False, "error": DEFAULT_MESSAGE}
_RESEARCH_ROUTES = [
pytest.param(
"/api/start_research",
_START_BODY,
_START_RESEARCH_SYNC,
_MAIN_ENVELOPE,
id="start_research",
),
pytest.param(
"/research/api/start",
_START_BODY,
_START_RESEARCH_SYNC,
_MAIN_ENVELOPE,
id="research_api_start",
),
pytest.param(
"/api/save_raw_config",
_RAW_CONFIG_BODY,
_WRITE_CONFIG,
_SUCCESS_ENVELOPE,
id="save_raw_config",
),
]
@pytest.mark.parametrize(
("path", "body", "downstream", "expected"),
[
*_RESEARCH_ROUTES,
pytest.param(
"/api/v1/generate_report",
b'{"query": "hello"}',
"local_deep_research.api.research_functions.generate_report",
{"error": "Query parameter is required"},
id="generate_report",
),
pytest.param(
"/api/v1/analyze_documents",
b'{"query": "hello", "collection_name": "c"}',
"local_deep_research.web.routers.api_v1.analyze_documents",
{"error": "Both query and collection_name parameters are required"},
id="analyze_documents",
),
pytest.param(
_ADD_RESOURCE,
b'{"title": "t", "url": "https://example.org"}',
"local_deep_research.security.ssrf_validator.validate_url",
_MAIN_ENVELOPE,
id="add_resource",
),
],
)
def test_other_json_body_routes_reject_text_plain_before_dispatch(
authenticated_client, path, body, downstream, expected
):
"""Each body passes its route's own field checks, so only the media-type
gate can produce this 400. ``downstream`` is a call the route makes only
after it has accepted the body: the research function for the API v1
routes, the URL validation for a resource, the research start for both
research-start paths and the file write for the raw config. It is
patched so that a regression fails here instead of running research,
resolving a hostname or writing the config file."""
with patch(downstream, return_value={}) as downstream_call:
resp = authenticated_client.post(
path,
content=body,
headers={"Content-Type": "text/plain"},
)
downstream_call.assert_not_called()
assert resp.status_code == 400
assert resp.json() == expected
@pytest.mark.parametrize(
("path", "body", "downstream", "expected"), _RESEARCH_ROUTES
)
def test_research_routes_dispatch_the_same_body_labelled_json(
authenticated_client, path, body, downstream, expected
):
"""Control for the research cases above: labelled ``application/json``,
the same body reaches the patched call. So the patch targets are live,
and the 400s above come from the media-type gate."""
with patch(downstream, return_value={}) as downstream_call:
resp = authenticated_client.post(
path,
content=body,
headers={"Content-Type": "application/json"},
)
downstream_call.assert_called_once()
assert resp.status_code == 200, resp.text[:300]
assert resp.json() != expected
@pytest.mark.parametrize(
"content_type",
[
"application/x-www-form-urlencoded",
"application/octet-stream",
"application/jsonx",
],
)
def test_start_research_rejects_json_lookalike_media_types(
authenticated_client, content_type
):
"""A JSON-looking body labelled with a near-miss media type stops at
the gate too. A check weakened to ``startswith("application/")`` would
accept the first two, and one weakened to ``"json" in`` the media type
would accept the third."""
with patch(_START_RESEARCH_SYNC, return_value={}) as start:
resp = authenticated_client.post(
"/api/start_research",
content=_START_BODY,
headers={"Content-Type": content_type},
)
start.assert_not_called()
assert resp.status_code == 400
assert resp.json() == _MAIN_ENVELOPE
def test_notes_create_accepts_vendor_json_media_type(authenticated_client):
resp = authenticated_client.post(
"/notes/api/notes",
content=b'{"title": "vendor JSON", "content": "c"}',
headers={"Content-Type": "application/vnd.example+json; charset=utf-8"},
)
assert resp.status_code == 201
@pytest.mark.parametrize(
"chunked", [False, True], ids=["content-length", "chunked"]
)
def test_notes_body_cap_runs_before_the_media_type_gate(
authenticated_client, monkeypatch, chunked
):
"""The notes body cap applies whatever the media type, because the
gate sits after the capped stream read. An over-cap ``text/plain`` body
is therefore a 413. Moving the gate above the Content-Length check
would turn both cases into ``{}``, which create_note answers with 400.
Moving it between the two cap checks would do that to the chunked
case, which has no Content-Length for the first check to see."""
import local_deep_research.web.routers.notes as notes_mod
monkeypatch.setattr(notes_mod, "_MAX_JSON_BODY_BYTES", 16)
body = b'{"title": "t", "content": "' + b"x" * 64 + b'"}'
def _chunks():
yield body[:20]
yield body[20:]
resp = authenticated_client.post(
"/notes/api/notes",
content=_chunks() if chunked else body,
headers={"Content-Type": "text/plain"},
)
assert resp.status_code == 413, resp.text[:300]
assert resp.json()["success"] is False