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.

19.7 KB
Raw Blame
"""Regression coverage for BC-1a: hostile-input 500s in
``web/routers/rag.py``.

A round-4 sweep of malformed requests over all 318 routes found three
distinct shapes in this file where a client sending bad input got a 500
instead of a 4xx:

Shape A — a broad ``except Exception`` wrapped around ``await
request.json()`` shadowed the app's registered ``json.JSONDecodeError ->
400`` handler (``fastapi_app.py``), so malformed JSON *bytes* fell into a
route-specific error path that hardcoded a 500. On
``POST /library/api/rag/test-embedding`` this additionally leaked the raw
stdlib decoder message (e.g. "Expecting value: line 1 column 1 (char 0)")
into the response, because a bare ``json.JSONDecodeError`` falls through
``_format_test_embedding_error``'s internal/upstream module checks to the
generic branch that echoes ``str(exc)``.

Note: the ``isinstance(data, dict)`` guard for a *well-formed-but-wrong-
type* JSON body (``null``, ``[]``, a bare string/int) was already present
on all four Shape A routes below before this fix — that part of the
``@require_json_body`` gap (46 sites, commit ``d306f15ad``) was already
closed. What remained broken, and what this fix + these tests target, is
specifically the *malformed bytes* case, which raises before that
``isinstance`` check ever runs and was caught by the broad
``except Exception`` instead.

Shape B — ``POST /library/api/collections/{collection_id}/index/start``
did ``data = await request.json() or {}``, which only guards FALSY bodies.
``null``/``[]`` were silently normalized to ``{}`` (200, not an error);
a truthy non-dict (bare string, bare int, non-empty list) reached
``data.get(...)`` and raised ``AttributeError`` -> 500. Fixed by replacing
the idiom with the same ``isinstance(data, dict)`` guard used elsewhere in
this file — which also means ``null``/``[]`` now get a clean 400 instead of
being silently treated as an empty body, matching every other route in
this module.

Shape D — ``_create_collection_sync``/``_update_collection_sync``
(``POST /library/api/collections``, ``PUT
/library/api/collections/{collection_id}``) did
``data.get("name", "").strip()``: the ``""`` default only applies when the
key is ABSENT, so a present-but-wrong-typed value (``{"name": 123}`` or
``{"name": null}``) reached ``.strip()`` untouched and raised
``AttributeError`` -> 500. Fixed with an explicit ``isinstance(..., str)``
check before ``.strip()``.

Uses the ``auth_client`` harness pattern from
``tests/web/routers/test_full_surface_smoke.py``: a real, in-process
``fastapi.testclient.TestClient`` against the live app, with a freshly
registered+logged-in throwaway user and the CSRF header pre-attached. No
mocking, no network, no LLM for the HTTP-level tests.

One route (index/start) also gets a direct-call unit test (the idiom
established by ``tests/research_library/routes/test_rag_routes_cancel_and_worker_wiring.py``
/ ``test_rag_configure_atomicity.py``) to prove a well-formed body still
forwards ``force_reindex`` into ``run_db_sync`` unchanged, without
exercising the route's real background-thread side effect (the same
reason this route is excluded from ``test_full_surface_smoke.py``'s
mutating sweep).
"""

import asyncio
import os
import uuid
from unittest.mock import AsyncMock, Mock, patch

import pytest
from fastapi.testclient import TestClient

# The app's docs_url toggle reads PYTEST_CURRENT_TEST at app-import time;
# pytest only sets that per-test, not at collection. Force it now (mirrors
# test_full_surface_smoke.py / test_all_endpoints.py).
os.environ.setdefault("TESTING", "1")


# ----------------------------------------------------------------------------
# Malformed request bodies. Sent as raw ``content=`` bytes (not the
# ``json=`` kwarg) so we control exactly what hits the wire, independent of
# httpx's JSON-encoding behavior (e.g. ``json=None`` means "no body" to
# httpx, not a literal ``null`` body).
# ----------------------------------------------------------------------------
MALFORMED_BODIES = [
    ("malformed_json_bytes", b"{not valid json"),
    ("null", b"null"),
    ("empty_list", b"[]"),
    ("bare_string", b'"hello"'),
    ("bare_int", b"42"),
]
_MALFORMED_IDS = [label for label, _ in MALFORMED_BODIES]

# Same five bodies, minus "malformed_json_bytes". Kept as a separate set
# because the parsed-but-wrong-type cases exercise the routes' shape guard,
# while the malformed-bytes cases exercise their shared parser.
NON_DECODE_MALFORMED_BODIES = [
    (label, body)
    for label, body in MALFORMED_BODIES
    if label != "malformed_json_bytes"
]
_NON_DECODE_IDS = [label for label, _ in NON_DECODE_MALFORMED_BODIES]

_JSON_HEADERS = {
    "Content-Type": "application/json",
    "Accept": "application/json",
}

EXPECTED_JSON_ERROR_BODY = {
    "success": False,
    "error": "Request body must be valid JSON",
}


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated TestClient for a freshly-created throwaway user, with
    the CSRF header pre-attached. Copied from
    ``tests/web/routers/test_full_surface_smoke.py``'s ``auth_client``
    fixture (that file documents it as itself reusing
    ``test_all_endpoints.py``'s pattern) rather than imported, so this file
    has no import-time coupling to another slice's test module.
    """
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

    user = f"test_rag_hostile_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    def _csrf():
        c.get("/auth/login")
        r = c.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Login bootstrap failed: expected 302, got {resp.status_code}: "
            f"{resp.text[:500]}"
        )

    csrf_resp = c.get("/auth/csrf-token")
    if csrf_resp.status_code == 200:
        token = csrf_resp.json().get("csrf_token")
        if token:
            c.headers.update({"X-CSRFToken": token})

    yield c

    c.post("/auth/logout", follow_redirects=False)


def _unique_name(prefix: str = "collection") -> str:
    return f"{prefix}-{uuid.uuid4().hex[:8]}"


# ----------------------------------------------------------------------------
# Shape A: POST /library/api/rag/{index-document,remove-document,configure,
# test-embedding} — malformed JSON bytes must not fall through the broad
# except Exception into a hardcoded 500.
# ----------------------------------------------------------------------------
SHAPE_A_ROUTES = [
    "/library/api/rag/index-document",
    "/library/api/rag/remove-document",
    "/library/api/rag/configure",
    "/library/api/rag/test-embedding",
]


@pytest.mark.parametrize("path", SHAPE_A_ROUTES)
@pytest.mark.parametrize("label,body", MALFORMED_BODIES, ids=_MALFORMED_IDS)
def test_shape_a_routes_reject_hostile_bodies_with_400(
    auth_client, path, label, body
):
    resp = auth_client.post(path, content=body, headers=_JSON_HEADERS)

    assert resp.status_code == 400, (
        f"POST {path} [{label}] returned {resp.status_code} (expected 400): "
        f"{resp.text[:300]}"
    )
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


def test_test_embedding_does_not_leak_decoder_message(auth_client):
    """The one route the brief called out by name for a CWE-209-flavored
    leak: a malformed body used to reach ``_format_test_embedding_error``,
    which echoes ``str(exc)`` for a bare stdlib exception -- for
    ``json.JSONDecodeError`` that is a message like "Expecting value: line
    1 column 1 (char 0)". Confirms the response is the route's own clean
    "success"-shaped 400, not that leaked text, and not a 500."""
    resp = auth_client.post(
        "/library/api/rag/test-embedding",
        content=b"{not valid json",
        headers=_JSON_HEADERS,
    )

    assert resp.status_code == 400, resp.text
    body = resp.json()
    assert body == EXPECTED_JSON_ERROR_BODY, body
    assert "Expecting value" not in resp.text
    assert "line 1 column" not in resp.text
    assert "char 0" not in resp.text


WELL_FORMED_MISSING_FIELD_CASES = [
    ("/library/api/rag/index-document", {}, "text_doc_id is required"),
    ("/library/api/rag/remove-document", {}, "text_doc_id is required"),
    (
        "/library/api/rag/configure",
        {},
        "All configuration parameters are required (embedding_model, "
        "embedding_provider, chunk_size, chunk_overlap",
    ),
    (
        "/library/api/rag/test-embedding",
        {},
        "Provider and model are required",
    ),
]


@pytest.mark.parametrize(
    "path,payload,expected_error",
    WELL_FORMED_MISSING_FIELD_CASES,
    ids=[p for p, _, _ in WELL_FORMED_MISSING_FIELD_CASES],
)
def test_shape_a_well_formed_dict_body_unaffected(
    auth_client, path, payload, expected_error
):
    """A well-formed (but incomplete) JSON dict body must still reach the
    route's normal field-validation logic untouched by the new
    ``except json.JSONDecodeError`` clause -- proving that clause only
    intercepts an actual decode failure, not ordinary dict bodies."""
    resp = auth_client.post(
        path, json=payload, headers={"Accept": "application/json"}
    )

    assert resp.status_code == 400, resp.text
    body = resp.json()
    assert body["success"] is False
    assert body["error"] == expected_error


# ----------------------------------------------------------------------------
# Shape B: POST /library/api/collections/{collection_id}/index/start
# ----------------------------------------------------------------------------
_INDEX_START_PATH = (
    "/library/api/collections/00000000-0000-4000-8000-000000000000/index/start"
)


def test_index_start_rejects_malformed_json_bytes_with_400(auth_client):
    """Malformed bytes use the route's success envelope like non-object JSON."""
    resp = auth_client.post(
        _INDEX_START_PATH, content=b"{not valid json", headers=_JSON_HEADERS
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


@pytest.mark.parametrize(
    "label,body", NON_DECODE_MALFORMED_BODIES, ids=_NON_DECODE_IDS
)
def test_index_start_rejects_hostile_bodies_with_400(auth_client, label, body):
    """The actual Shape B fix: ``null``/``[]``/a bare string/int must 400
    *before* the handler ever reaches ``_start_background_index_sync``
    (which spawns a real ``threading.Thread`` unconditionally -- the
    reason this route is excluded from test_full_surface_smoke.py's
    mutating sweep). A 400 here is also proof no background thread was
    started for the hostile request. Before the fix, ``null``/``[]`` were
    silently normalized to ``{}`` (200, no error at all) and a bare
    string/int raised AttributeError -> 500; now all four get a clean,
    route-shaped 400."""
    resp = auth_client.post(
        _INDEX_START_PATH, content=body, headers=_JSON_HEADERS
    )

    assert resp.status_code == 400, (
        f"POST index/start [{label}] returned {resp.status_code}: "
        f"{resp.text[:300]}"
    )
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


def test_index_start_well_formed_body_still_forwards_force_reindex():
    """Direct-call unit test (bypassing HTTP + Depends(require_auth), the
    idiom established by test_rag_routes_cancel_and_worker_wiring.py /
    test_rag_configure_atomicity.py) proving the new
    ``isinstance(data, dict)`` guard does not disturb a genuinely
    well-formed body: ``force_reindex`` still reaches ``run_db_sync``
    unchanged. ``run_db_sync`` itself is mocked out so this test never
    touches the DB or spawns the route's real background thread."""
    from local_deep_research.web.routers import rag as rag_module

    request = Mock()
    request.session = {}
    request.json = AsyncMock(return_value={"force_reindex": True})

    fake_result = {"success": True, "task_id": "fake-task-id"}
    with patch.object(
        rag_module, "run_db_sync", new=AsyncMock(return_value=fake_result)
    ) as mock_run_db_sync:
        result = asyncio.run(
            rag_module.start_background_index(
                request, "collection-1", username="testuser"
            )
        )

    assert result == fake_result
    mock_run_db_sync.assert_awaited_once()
    call_args = mock_run_db_sync.call_args.args
    assert call_args[0] is rag_module._start_background_index_sync
    assert call_args[1] == "collection-1"
    assert call_args[2] == "testuser"
    assert call_args[4] is True  # force_reindex forwarded unchanged


def test_index_start_malformed_body_never_calls_run_db_sync():
    """Companion to the HTTP-level malformed-body test above: confirms at
    the unit level that a hostile body short-circuits before
    ``run_db_sync`` (and therefore the real background thread) is ever
    invoked."""
    from local_deep_research.web.routers import rag as rag_module

    request = Mock()
    request.session = {}
    request.json = AsyncMock(return_value="hostile-bare-string")

    with patch.object(
        rag_module, "run_db_sync", new=AsyncMock()
    ) as mock_run_db_sync:
        result = asyncio.run(
            rag_module.start_background_index(
                request, "collection-1", username="testuser"
            )
        )

    mock_run_db_sync.assert_not_awaited()
    assert result.status_code == 400
    import json as _json

    assert _json.loads(result.body) == EXPECTED_JSON_ERROR_BODY


# ----------------------------------------------------------------------------
# Shape D: POST /library/api/collections, PUT
# /library/api/collections/{collection_id}
# ----------------------------------------------------------------------------
_COLLECTIONS_PATH = "/library/api/collections"


def test_create_collection_rejects_malformed_json_bytes_with_400(auth_client):
    """Malformed bytes use the collection route's success envelope."""
    resp = auth_client.post(
        _COLLECTIONS_PATH, content=b"{not valid json", headers=_JSON_HEADERS
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


@pytest.mark.parametrize(
    "label,body", NON_DECODE_MALFORMED_BODIES, ids=_NON_DECODE_IDS
)
def test_create_collection_rejects_hostile_top_level_bodies(
    auth_client, label, body
):
    """``null``/``[]``/a bare string/int at the top level: pre-existing
    ``isinstance(data, dict)`` guard (not part of this fix), confirmed
    still 400 in the route's "success" shape."""
    resp = auth_client.post(
        _COLLECTIONS_PATH, content=body, headers=_JSON_HEADERS
    )

    assert resp.status_code == 400, (
        f"POST {_COLLECTIONS_PATH} [{label}] returned {resp.status_code}: "
        f"{resp.text[:300]}"
    )
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


def test_update_collection_rejects_malformed_json_bytes_with_400(auth_client):
    path = "/library/api/collections/00000000-0000-4000-8000-000000000000"
    resp = auth_client.put(
        path, content=b"{not valid json", headers=_JSON_HEADERS
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


@pytest.mark.parametrize(
    "label,body", NON_DECODE_MALFORMED_BODIES, ids=_NON_DECODE_IDS
)
def test_update_collection_rejects_hostile_top_level_bodies(
    auth_client, label, body
):
    path = "/library/api/collections/00000000-0000-4000-8000-000000000000"
    resp = auth_client.put(path, content=body, headers=_JSON_HEADERS)

    assert resp.status_code == 400, (
        f"PUT {path} [{label}] returned {resp.status_code}: {resp.text[:300]}"
    )
    assert resp.json() == EXPECTED_JSON_ERROR_BODY, resp.json()


@pytest.mark.parametrize(
    "field,value",
    [("name", 123), ("name", None), ("name", ["a", "b"])],
    ids=["name=int", "name=null", "name=list"],
)
def test_create_collection_rejects_wrong_typed_name(auth_client, field, value):
    """The Shape D bug itself: ``data.get("name", "").strip()`` only
    defaults when the key is ABSENT. A present ``name`` of the wrong type
    used to reach ``.strip()`` and raise AttributeError -> 500."""
    resp = auth_client.post(
        _COLLECTIONS_PATH,
        json={field: value},
        headers={"Accept": "application/json"},
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == {"success": False, "error": "Name must be a string"}


def test_create_collection_rejects_wrong_typed_description(auth_client):
    resp = auth_client.post(
        _COLLECTIONS_PATH,
        json={"name": _unique_name(), "description": 456},
        headers={"Accept": "application/json"},
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == {
        "success": False,
        "error": "Description must be a string",
    }


def test_create_collection_well_formed_still_succeeds(auth_client):
    """Proves the added isinstance(str) guards don't disturb a genuinely
    well-formed request: real 200, real collection, real name/description
    round-trip."""
    name = _unique_name()
    resp = auth_client.post(
        _COLLECTIONS_PATH,
        json={"name": name, "description": "hostile-input regression check"},
        headers={"Accept": "application/json"},
    )

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["success"] is True
    assert body["collection"]["name"] == name
    assert body["collection"]["description"] == "hostile-input regression check"


def test_update_collection_rejects_wrong_typed_name(auth_client):
    name = _unique_name()
    create_resp = auth_client.post(
        _COLLECTIONS_PATH,
        json={"name": name},
        headers={"Accept": "application/json"},
    )
    assert create_resp.status_code == 200, create_resp.text
    collection_id = create_resp.json()["collection"]["id"]

    resp = auth_client.put(
        f"{_COLLECTIONS_PATH}/{collection_id}",
        json={"name": 123},
        headers={"Accept": "application/json"},
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == {"success": False, "error": "Name must be a string"}


def test_update_collection_rejects_wrong_typed_description(auth_client):
    name = _unique_name()
    create_resp = auth_client.post(
        _COLLECTIONS_PATH,
        json={"name": name},
        headers={"Accept": "application/json"},
    )
    assert create_resp.status_code == 200, create_resp.text
    collection_id = create_resp.json()["collection"]["id"]

    resp = auth_client.put(
        f"{_COLLECTIONS_PATH}/{collection_id}",
        json={"description": {"nested": "object"}},
        headers={"Accept": "application/json"},
    )

    assert resp.status_code == 400, resp.text
    assert resp.json() == {
        "success": False,
        "error": "Description must be a string",
    }


def test_update_collection_well_formed_still_succeeds(auth_client):
    name = _unique_name()
    create_resp = auth_client.post(
        _COLLECTIONS_PATH,
        json={"name": name},
        headers={"Accept": "application/json"},
    )
    assert create_resp.status_code == 200, create_resp.text
    collection_id = create_resp.json()["collection"]["id"]

    new_name = _unique_name("renamed")
    resp = auth_client.put(
        f"{_COLLECTIONS_PATH}/{collection_id}",
        json={"name": new_name, "description": "renamed description"},
        headers={"Accept": "application/json"},
    )

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["success"] is True
    assert body["collection"]["name"] == new_name
    assert body["collection"]["description"] == "renamed description"