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.

10.4 KB
Raw Blame
"""Three library routes 500 on a truthy non-dict JSON body (BC-1c).

``POST /library/api/download-research/{research_id}`` and
``POST /library/api/research-history/convert-all`` guarded their body with
``data = await request.json() or {}``. ``or {}`` only substitutes a *falsy*
body (``None``, ``[]``, ``""``, ``0``, ``False``) — a truthy non-dict
(a bare string, a bare int, or a non-empty list) sails through unchanged and
then hits ``data.get(...)``, raising ``AttributeError``. download-research had
no try/except at all, so the error reached the app's catch-all handler as a
bare 500 ``{"error": "Server error"}``; convert-all's ``data.get`` line sat
outside its own local ``try:``, same outcome.

``POST /library/api/collections/{collection_id}/search`` already guarded the
top-level body shape (``isinstance(data, dict)``) before dispatching to
``_search_collection_sync``, but that function read
``data.get("query", "").strip()`` — the ``""`` default only applies when the
key is *absent*. A well-formed dict with a wrong-typed value for a present
key (``{"query": 123}``) sails past the top-level guard and still raises
``AttributeError`` on ``.strip()``, outside both of the function's own
``try:`` blocks.

Fix: each site now rejects a non-dict/non-string body shape up front via the
shared ``json_body_error()`` helper (``src/.../web/dependencies/json_body.py``),
matching the response shape ("simple" -> ``{"error": ...}``, "success" ->
``{"success": False, "error": ...}``) that route's *other* error returns
already use, so the front end's branching on that shape is unaffected.
"""

import json

import pytest

DOWNLOAD_RESEARCH_ROUTE = (
    "/library/api/download-research/nonexistent-research-id"
)
CONVERT_ALL_ROUTE = "/library/api/research-history/convert-all"
SEARCH_COLLECTION_ROUTE = (
    "/library/api/collections/nonexistent-collection-id/search"
)

# Route -> the json_body_error() format its OTHER error returns already use
# (verified by reading each route: library.py's isinstance guards all use
# "simple" -> {"error": ...}; library_search.py's routes all return
# {"success": False, "error": ...} on validation failure).
ROUTE_ERROR_SHAPE = {
    DOWNLOAD_RESEARCH_ROUTE: "simple",
    CONVERT_ALL_ROUTE: "success",
    SEARCH_COLLECTION_ROUTE: "success",
}

BAD_BODIES = [
    pytest.param([1, 2], id="json-array"),
    pytest.param("a string", id="json-string"),
    pytest.param(3, id="json-number"),
    pytest.param(True, id="json-bool"),
]


def _envelope_from_sut(fmt):
    """Ask the real helper what this format's envelope looks like.

    Deliberately derived from ``json_body_error`` rather than restated here.
    A test that hardcodes ``{"error": ...}`` still passes if the helper's
    contract drifts -- it would be asserting against a copy of the thing it is
    supposed to be checking. Reading the shape from the SUT means a change to
    the envelope surfaces here instead of reaching the front end, which
    branches on exactly these keys.
    """
    from local_deep_research.web.dependencies.json_body import json_body_error

    probe = json_body_error(fmt, "probe")
    return json.loads(bytes(probe.body))


def _assert_shape(route, body_json):
    """Assert the response matches the route's OWN pre-existing error shape,
    not just "some 4xx JSON"."""
    fmt = ROUTE_ERROR_SHAPE[route]
    expected = _envelope_from_sut(fmt)

    assert set(body_json) == set(expected), (
        f"{route}: expected the {fmt!r} envelope {sorted(expected)}, "
        f"got {sorted(body_json)} ({body_json})"
    )
    if "success" in expected:
        assert body_json["success"] is False, (
            f"{route}: 'success' must be False on an error, got {body_json}"
        )


@pytest.mark.parametrize(
    "route",
    [DOWNLOAD_RESEARCH_ROUTE, CONVERT_ALL_ROUTE, SEARCH_COLLECTION_ROUTE],
)
@pytest.mark.parametrize("body", BAD_BODIES)
def test_truthy_non_dict_body_is_client_error(
    authenticated_client, route, body
):
    """A truthy non-dict JSON body must be a clean 400, never a 500."""
    resp = authenticated_client.post(route, json=body)

    assert resp.status_code < 500, (
        f"{route} returned {resp.status_code} for body {body!r}: "
        f"{resp.text[:300]} -- the isinstance(data, dict) guard is missing "
        f"or a truthy non-dict body reached data.get() unguarded."
    )
    assert resp.status_code == 400, (
        f"{route} returned {resp.status_code} for body {body!r}, expected 400"
    )
    _assert_shape(route, resp.json())


@pytest.mark.parametrize(
    "route",
    [DOWNLOAD_RESEARCH_ROUTE, CONVERT_ALL_ROUTE, SEARCH_COLLECTION_ROUTE],
)
def test_malformed_json_bytes_is_400(authenticated_client, route):
    """Malformed JSON bytes (not even valid JSON) must still hit the app's
    registered json.JSONDecodeError -> 400 handler. None of these three
    routes wrap ``await request.json()`` in a local except, so this was
    never broken by the bug this file fixes -- pinned here anyway per the
    acceptance criteria (all 3xx/4xx inputs verified per route)."""
    resp = authenticated_client.post(
        route,
        content=b"{not valid json",
        headers={"Content-Type": "application/json"},
    )
    assert resp.status_code == 400, (
        f"{route} returned {resp.status_code} for malformed JSON bytes: "
        f"{resp.text[:300]}"
    )


@pytest.mark.parametrize(
    "route",
    [DOWNLOAD_RESEARCH_ROUTE, CONVERT_ALL_ROUTE, SEARCH_COLLECTION_ROUTE],
)
def test_explicit_json_null_body_is_400(authenticated_client, route):
    """A literal JSON ``null`` body (distinct from an EMPTY body -- httpx's
    ``json=None`` sends no body at all, which is the malformed-bytes case
    above, not this one) parses successfully to Python ``None``. ``None`` is
    falsy, so the OLD ``... or {}`` guard treated it as ``{}`` and let it
    through as 200 -- not a crash, but inconsistent with this file's other
    four ``isinstance(data, dict)`` guards, which already reject a non-dict
    ``None`` with 400. The fix aligns null with that existing local
    convention, so it now also 400s here."""
    resp = authenticated_client.post(
        route,
        content=b"null",
        headers={"Content-Type": "application/json"},
    )
    assert resp.status_code < 500, (
        f"{route} returned {resp.status_code} for a JSON null body: "
        f"{resp.text[:300]}"
    )
    assert resp.status_code == 400, (
        f"{route} returned {resp.status_code} for a JSON null body, "
        f"expected 400"
    )
    _assert_shape(route, resp.json())


def test_search_collection_wrong_typed_known_key_is_400(authenticated_client):
    """Shape D: {'query': 123} is a well-formed dict (passes the top-level
    isinstance guard) but 'query' is present with the wrong type, so the
    old ``data.get('query', '').strip()`` reached .strip() on an int."""
    resp = authenticated_client.post(
        SEARCH_COLLECTION_ROUTE, json={"query": 123}
    )
    assert resp.status_code < 500, (
        f"returned {resp.status_code}: {resp.text[:300]}"
    )
    assert resp.status_code == 400
    _assert_shape(SEARCH_COLLECTION_ROUTE, resp.json())


def test_search_collection_wrong_typed_limit_is_not_a_crash(
    authenticated_client,
):
    """Sibling field ``limit`` is read via ``int(data.get("limit", 10))``
    inside a try/except (TypeError, ValueError) -- already safe. Pinned so a
    future edit to that line is caught if it loses the guard."""
    resp = authenticated_client.post(
        SEARCH_COLLECTION_ROUTE, json={"query": "hi", "limit": "not-a-number"}
    )
    assert resp.status_code < 500


# ---------------------------------------------------------------------------
# Well-formed requests must behave exactly as before the fix.
# ---------------------------------------------------------------------------


def test_download_research_well_formed_body_unaffected(authenticated_client):
    """A proper dict body against a nonexistent research_id still queues 0
    items and returns 200 -- queue_research_downloads() filters resources by
    research_id and finds none, it does not raise "not found"."""
    resp = authenticated_client.post(DOWNLOAD_RESEARCH_ROUTE, json={})
    assert resp.status_code == 200, resp.text
    assert resp.json() == {"success": True, "queued": 0}


def test_convert_all_well_formed_body_unaffected(authenticated_client):
    """A proper dict body (including the empty-dict / default-force case)
    still runs the conversion and returns 200."""
    resp = authenticated_client.post(CONVERT_ALL_ROUTE, json={})
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body.get("success") is True


def test_search_collection_well_formed_body_unaffected(authenticated_client):
    """A proper string query against a nonexistent collection still reaches
    the collection-existence check (404), proving the new type guard does
    not reject valid string queries."""
    resp = authenticated_client.post(
        SEARCH_COLLECTION_ROUTE, json={"query": "hello world"}
    )
    assert resp.status_code == 404, resp.text
    assert resp.json() == {"success": False, "error": "Collection not found"}


# ---------------------------------------------------------------------------
# Regression proof without reverting src/ (forbidden by the review brief):
# reproduce the exact vulnerable expressions the old code ran, verbatim,
# against the exact hostile inputs used above.
# ---------------------------------------------------------------------------


def test_regression_reasoning_or_default_does_not_catch_truthy_non_dict():
    """library.py:806 and library_search.py:145 used to read
    ``data = await request.json() or {}`` then ``data.get(...)``. ``or {}``
    only substitutes a FALSY body; every truthy non-dict case in BAD_BODIES
    sails through unchanged and breaks on ``.get``."""
    for body in ["a string", 3, True, [1, 2]]:
        guarded = body or {}
        assert guarded is body, (
            f"{body!r} is truthy, so `or {{}}` must be a no-op here -- "
            "if this assertion fails the reasoning below no longer applies."
        )
        with pytest.raises(AttributeError):
            guarded.get("force", False)


def test_regression_reasoning_get_default_only_applies_when_key_absent():
    """library_search.py:300 used to read
    ``data.get("query", "").strip()``. The "" default only substitutes when
    the key is ABSENT, not when it is present with the wrong type."""
    data = {"query": 123}
    with pytest.raises(AttributeError):
        data.get("query", "").strip()