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.2 KB
Raw Blame
"""Regression coverage for BC-1b: the four bulk-delete/preview routes in
``web/routers/library_delete.py`` returned a hardcoded 500 for malformed
input instead of a clean 4xx.

Two distinct bugs, both in ``_validate_document_ids`` /
``_validate_document_ids_from_data`` (the helper shared by all four
routes):

1. Every route wraps its body in a broad ``except Exception ->
   handle_api_error(...)`` (a *hardcoded* 500, for genuine internal
   errors). ``await request.json()`` raises ``json.JSONDecodeError`` on
   malformed bytes; the app registers a ``json.JSONDecodeError -> 400``
   handler (``fastapi_app.py``), but the route's own broad except
   intercepts the error first, so it never reaches that handler.
2. ``_validate_document_ids_from_data`` guarded the body shape with
   ``if not data or "document_ids" not in data``, not
   ``isinstance(data, dict)``. A truthy non-dict body (a bare nonzero int,
   or a bare string that happens to equal ``"document_ids"``) sails past
   ``not data`` and then raises ``TypeError`` from ``"document_ids" not in
   <int>`` / ``"str"["document_ids"]`` -- caught by the same broad except
   into a 500. Separately, ``document_ids`` elements were never type
   checked: a huge int overflows the sqlite3 driver's 64-bit bind (crashes
   ``POST /documents/preview``, which has no per-item try/except) and an
   unhashable element (nested list/dict) blows up the per-document delete
   lock's dict key (crashes ``DELETE /documents/bulk`` specifically, since
   it is the one route that keys a lock before reaching the service
   layer's own defensive try/except).

Four routes affected, all going through the same two helpers:
    DELETE /library/api/documents/bulk
    DELETE /library/api/documents/blobs
    DELETE /library/api/collection/{collection_id}/documents/bulk
    POST   /library/api/documents/preview

The fix in ``library_delete.py``:
    - a new ``_parse_json_body(request)`` helper that catches
      ``json.JSONDecodeError`` and returns the shared
      ``json_body_error("success", ...)`` 400 (fixes bug 1). Both
      ``_validate_document_ids`` (used by the first three routes) and
      ``get_bulk_deletion_preview`` (the fourth) now go through it -- one
      helper covers all four routes.
    - ``_validate_document_ids_from_data`` now checks
      ``isinstance(data, dict)`` and requires every ``document_ids``
      element to be a ``str`` (document IDs are always UUID strings --
      see ``Document.id``, ``String(36)``) (fixes bug 2).

Harness pattern reused from ``test_full_surface_smoke.py``'s
``auth_client`` fixture: a real, in-process ``TestClient`` against the live
FastAPI app with a freshly registered and auto-authenticated throwaway user -- no
mocking, no network. Extended here to also isolate ``LDR_DATA_DIR`` to a
per-test temp directory (via the ``temp_data_dir`` fixture from
``tests/conftest.py``) so nothing touches a real data directory, and to
expose the username/password so the well-formed round-trip test can open a
direct DB session for the same user.
"""

import os
import uuid
from contextlib import suppress

import pytest
from fastapi.testclient import TestClient

os.environ.setdefault("TESTING", "1")


BULK_DELETE = ("DELETE", "/library/api/documents/bulk")
BULK_BLOBS = ("DELETE", "/library/api/documents/blobs")
COLLECTION_BULK = (
    "DELETE",
    "/library/api/collection/does-not-exist/documents/bulk",
)
BULK_PREVIEW = ("POST", "/library/api/documents/preview")

ALL_ROUTES = [BULK_DELETE, BULK_BLOBS, COLLECTION_BULK, BULK_PREVIEW]


def _cleanup_client(client, username):
    try:
        with suppress(Exception):
            client.post("/auth/logout", follow_redirects=False)
        with suppress(Exception):
            from local_deep_research.web.auth.session_manager import (
                session_manager,
            )

            session_manager.destroy_all_user_sessions(username)
        with suppress(Exception):
            from local_deep_research.database.session_passwords import (
                session_password_store,
            )

            session_password_store.clear_all_for_user(username)
        with suppress(Exception):
            from local_deep_research.database.thread_local_session import (
                clear_user_credentials,
            )

            clear_user_credentials(username)
    finally:
        client.close()


@pytest.fixture
def hostile_client(temp_data_dir, monkeypatch, request):
    """Authenticated TestClient for a throwaway user against a per-test
    ``LDR_DATA_DIR`` -- nothing real is ever touched, including on a
    "well-formed delete still works" pass. Returns (client, username,
    password) so tests can open a direct DB session for the same user.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(temp_data_dir))
    # Match conftest.py's `app` fixture: production PBKDF2 iteration count
    # makes register+login dominate wall-clock in a per-test fixture.
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

    from local_deep_research.web.fastapi_app import app

    client = TestClient(app, raise_server_exceptions=False)
    username = f"test_lib_del_{uuid.uuid4().hex[:8]}"
    password = "TestPassword123!"  # noqa: S105
    request.addfinalizer(lambda: _cleanup_client(client, username))

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

    reg = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    if reg.status_code != 302:
        pytest.fail(
            f"Auth bootstrap broken: registration returned "
            f"{reg.status_code} (expected 302): "
            f"{reg.text[:300]}"
        )

    who = client.get("/auth/check")
    if (
        who.status_code != 200
        or who.json().get("authenticated") is not True
        or who.json().get("username") != username
    ):
        pytest.fail(
            "Auth bootstrap broken: registration did not establish the "
            f"expected session: {who.status_code} {who.text[:300]}"
        )

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

    return client, username, password


def _assert_clean_400(resp, method, path, detail):
    assert resp.status_code == 400, (
        f"{method} {path} [{detail}]: expected 400, got "
        f"{resp.status_code}: {resp.text[:300]}"
    )
    body = resp.json()
    assert body.get("success") is False, (
        f"{method} {path} [{detail}]: expected success=False shape, got "
        f"{body!r}"
    )


# ---------------------------------------------------------------------------
# Malformed JSON bytes -- Shape A: except Exception shadowing the app's
# json.JSONDecodeError -> 400 handler.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("method,path", ALL_ROUTES)
def test_malformed_json_bytes_returns_400(hostile_client, method, path):
    client, _, _ = hostile_client
    resp = client.request(
        method,
        path,
        content=b"{not valid json",
        headers={"content-type": "application/json"},
    )
    _assert_clean_400(resp, method, path, "malformed JSON bytes")


# ---------------------------------------------------------------------------
# Top-level body is valid JSON but not a dict.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("method,path", ALL_ROUTES)
@pytest.mark.parametrize(
    "raw_body",
    [
        pytest.param(b"null", id="json-null"),
        pytest.param(b"[]", id="json-empty-array"),
        pytest.param(b'"hello"', id="json-bare-string"),
        # Adversarial: a bare string equal to the key the validator looks
        # for. Pre-fix, "document_ids" not in "document_ids" is False, so
        # code falls through to data["document_ids"] -- a TypeError on a
        # str (string indices must be integers).
        pytest.param(b'"document_ids"', id="json-bare-string-matches-key"),
        pytest.param(b"42", id="json-bare-nonzero-int"),
    ],
)
def test_non_dict_json_body_returns_400(hostile_client, method, path, raw_body):
    client, _, _ = hostile_client
    resp = client.request(
        method,
        path,
        content=raw_body,
        headers={"content-type": "application/json"},
    )
    _assert_clean_400(resp, method, path, raw_body.decode())


# ---------------------------------------------------------------------------
# Well-formed dict body, but document_ids itself is missing / wrong shape /
# contains a hostile element type.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("method,path", ALL_ROUTES)
@pytest.mark.parametrize(
    "payload",
    [
        pytest.param({"foo": "bar"}, id="document_ids-missing"),
        pytest.param({"document_ids": "abc"}, id="document_ids-not-a-list"),
        pytest.param({"document_ids": []}, id="document_ids-empty-list"),
        pytest.param(
            {"document_ids": [-1]}, id="document_ids-negative-int-element"
        ),
        pytest.param(
            {"document_ids": [10**30]}, id="document_ids-huge-int-element"
        ),
        pytest.param({"document_ids": [1.5]}, id="document_ids-float-element"),
        pytest.param({"document_ids": [None]}, id="document_ids-null-element"),
        pytest.param({"document_ids": [True]}, id="document_ids-bool-element"),
        pytest.param(
            {"document_ids": [["nested"]]},
            id="document_ids-nested-list-element",
        ),
        pytest.param(
            {"document_ids": [{"a": 1}]},
            id="document_ids-nested-dict-element",
        ),
    ],
)
def test_document_ids_edge_cases_return_400(
    hostile_client, method, path, payload
):
    client, _, _ = hostile_client
    resp = client.request(method, path, json=payload)
    _assert_clean_400(resp, method, path, repr(payload))


@pytest.mark.parametrize("method,path", ALL_ROUTES)
@pytest.mark.parametrize(
    "document_ids",
    [
        pytest.param(["id"] * 1001, id="too-many-ids"),
        pytest.param(["x" * 2049], id="overlong-id"),
    ],
)
def test_document_ids_bounds_reject_before_deletion(
    hostile_client, method, path, document_ids
):
    client, _, _ = hostile_client
    resp = client.request(method, path, json={"document_ids": document_ids})
    _assert_clean_400(resp, method, path, "oversized document_ids")


# ---------------------------------------------------------------------------
# Well-formed request must still work exactly as before: nonexistent-but-
# valid string IDs 200 with a per-item "not found", and a real document ID
# actually gets deleted.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("method,path", ALL_ROUTES)
def test_well_formed_nonexistent_id_returns_200(hostile_client, method, path):
    client, _, _ = hostile_client
    resp = client.request(
        method, path, json={"document_ids": ["nonexistent-uuid-value"]}
    )
    assert resp.status_code == 200, (
        f"{method} {path}: well-formed request regressed -- "
        f"{resp.status_code}: {resp.text[:300]}"
    )
    body = resp.json()
    assert body.get("success") is True


def _seed_document(username, password):
    """Insert one real Document row for *username*, tied to the
    "user_upload" SourceType that ``initialize_library_for_user`` seeds
    automatically on register/login. Returns the new document's id.
    """
    from local_deep_research.database.models.library import (
        Document,
        SourceType,
    )
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    doc_id = str(uuid.uuid4())
    with get_user_db_session(username, password) as session:
        source_type = (
            session.query(SourceType).filter_by(name="user_upload").first()
        )
        assert source_type is not None, (
            "user_upload SourceType should have been seeded by "
            "initialize_library_for_user on register/login"
        )
        doc = Document(
            id=doc_id,
            source_type_id=source_type.id,
            document_hash=uuid.uuid4().hex,
            filename="hostile-input-test.txt",
            file_size=11,
            file_type="txt",
            text_content="hello world",
            title="Hostile input test doc",
        )
        session.add(doc)
        session.commit()
    return doc_id


def test_well_formed_bulk_delete_actually_deletes(hostile_client):
    """A real document ID, sent through the exact same validation path as
    the hostile inputs above, must still be deleted -- proving the fix
    changed nothing about the happy path.
    """
    from local_deep_research.database.models.library import Document
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    client, username, password = hostile_client
    doc_id = _seed_document(username, password)

    resp = client.request(
        "DELETE",
        "/library/api/documents/bulk",
        json={"document_ids": [doc_id]},
    )
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["success"] is True
    assert body["deleted"] == 1
    assert body["failed"] == 0

    with get_user_db_session(username, password) as session:
        assert session.get(Document, doc_id) is None, (
            "document should actually be gone from the DB, not just "
            "reported deleted"
        )


# ---------------------------------------------------------------------------
# Regression proof: monkeypatch the exact pre-fix implementations back in
# (no git-revert, per the review brief) and show the same requests that
# pass above go back to a 500. This pins that the assertions above are
# actually exercising the fix in library_delete.py, not something else.
# ---------------------------------------------------------------------------


class TestFailsWithoutFix:
    def test_malformed_json_bytes_500_without_json_decode_guard(
        self, hostile_client, monkeypatch
    ):
        """Pre-fix ``_validate_document_ids``: a bare ``await
        request.json()`` with no ``json.JSONDecodeError`` guard, so the
        route's own broad ``except Exception`` (a hardcoded 500)
        intercepts the decode error before the app's registered handler
        can run.
        """
        from local_deep_research.web.routers import library_delete

        async def _pre_fix_validate_document_ids(request):
            data = await request.json()
            return library_delete._validate_document_ids_from_data(data)

        monkeypatch.setattr(
            library_delete,
            "_validate_document_ids",
            _pre_fix_validate_document_ids,
        )

        client, _, _ = hostile_client
        resp = client.request(
            "DELETE",
            "/library/api/documents/bulk",
            content=b"{not valid json",
            headers={"content-type": "application/json"},
        )
        assert resp.status_code == 500, (
            "sanity check failed: the pre-fix code path should still 500 "
            f"on malformed JSON, got {resp.status_code} -- this test no "
            "longer proves the fix matters"
        )

    def test_bare_int_body_500_without_isinstance_dict_guard(
        self, hostile_client, monkeypatch
    ):
        """Pre-fix guard: ``if not data or "document_ids" not in data``.
        A truthy non-dict body (bare nonzero int) passes ``not data`` and
        then ``"document_ids" not in 42`` raises ``TypeError`` -- caught
        by the route's broad except into a 500.
        """
        from local_deep_research.web.routers import library_delete
        from fastapi.responses import JSONResponse

        def _pre_fix_validate(data):
            if not data or "document_ids" not in data:
                return None, JSONResponse(
                    {
                        "success": False,
                        "error": "document_ids required in request body",
                    },
                    status_code=400,
                )
            document_ids = data["document_ids"]
            if not isinstance(document_ids, list) or not document_ids:
                return None, JSONResponse(
                    {
                        "success": False,
                        "error": "document_ids must be a non-empty list",
                    },
                    status_code=400,
                )
            return document_ids, None

        monkeypatch.setattr(
            library_delete,
            "_validate_document_ids_from_data",
            _pre_fix_validate,
        )

        client, _, _ = hostile_client
        resp = client.request(
            "DELETE",
            "/library/api/documents/bulk",
            content=b"42",
            headers={"content-type": "application/json"},
        )
        assert resp.status_code == 500, (
            "sanity check failed: the pre-fix guard should still 500 on a "
            f"bare int body, got {resp.status_code} -- this test no "
            "longer proves the fix matters"
        )

    def test_nested_list_element_500_without_element_type_check(
        self, hostile_client, monkeypatch
    ):
        """Pre-fix ``document_ids`` validation only checked "is a
        non-empty list", never element types. A nested-list element is
        unhashable; ``DELETE /documents/bulk``'s per-document delete lock
        keys a dict on ``(username, document_id)`` before the service
        layer's own defensive try/except can catch it, so the TypeError
        surfaces as a 500.
        """
        from local_deep_research.web.routers import library_delete
        from fastapi.responses import JSONResponse

        def _pre_fix_validate(data):
            if not isinstance(data, dict) or "document_ids" not in data:
                return None, JSONResponse(
                    {
                        "success": False,
                        "error": "document_ids required in request body",
                    },
                    status_code=400,
                )
            document_ids = data["document_ids"]
            if not isinstance(document_ids, list) or not document_ids:
                return None, JSONResponse(
                    {
                        "success": False,
                        "error": "document_ids must be a non-empty list",
                    },
                    status_code=400,
                )
            return document_ids, None

        monkeypatch.setattr(
            library_delete,
            "_validate_document_ids_from_data",
            _pre_fix_validate,
        )

        client, _, _ = hostile_client
        resp = client.request(
            "DELETE",
            "/library/api/documents/bulk",
            json={"document_ids": [["nested"]]},
        )
        assert resp.status_code == 500, (
            "sanity check failed: the pre-fix validator should still 500 "
            f"on a nested-list element, got {resp.status_code} -- this "
            "test no longer proves the fix matters"
        )