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.

5.4 KB
Raw Blame
"""Exactly one route may own ``DELETE /library/api/document/{id}``.

Ported from
``tests/research_library/deletion/routes/test_delete_document_route_resolution.py``,
deleted in the Flask->FastAPI migration.

The bug it locks: under Flask, ``library_bp`` (prefix ``/library``) and
``delete_bp`` (prefix ``/library/api``) both declared a rule compiling to the
same ``DELETE /library/api/document/<id>``. ``library_bp`` was registered
first, so Werkzeug resolved to its UNGUARDED
``LibraryService.delete_document`` handler -- the note-protection guard in
``delete_bp`` became dead code and a note Document could be hard-deleted
through the document API. The duplicate rule was removed and this invariant
put in its place.

FastAPI has the same failure mode for the same reason: Starlette matches in
REGISTRATION order and stops at the first route whose path and method match,
so a second route declared for this path anywhere in ``fastapi_app.py``'s
include order silently takes over. The routers even keep the old registration
order (``library`` before ``library_delete``), so a re-added duplicate would
land on exactly the unguarded side again.

``tests/web/routers/test_route_ordering.py`` walks the same table but looks
for a different shape -- a STATIC path swallowed by an earlier PARAMETERIZED
one. Two identical parameterized paths do not trip it: neither is static, so
both are skipped. It stays green with the duplicate restored.

The guarded handler is ``library_delete.delete_document``; the assertions
below name the function object, so a rename that keeps the URL still has to
come through here.
"""

import pytest
from fastapi.routing import APIRoute

from local_deep_research.web.fastapi_app import app
from local_deep_research.web.routers import library_delete

DELETE_DOCUMENT_PATH = "/library/api/document/{document_id}"


def _routes_for(path, method):
    return [
        route
        for route in app.routes
        if isinstance(route, APIRoute)
        and route.path == path
        and method in (route.methods or set())
    ]


def test_exactly_one_route_owns_the_document_delete_path():
    """A duplicate reintroduces the guard bypass: whichever router is
    included first wins, and the loser's note protection becomes dead code.
    """
    matches = _routes_for(DELETE_DOCUMENT_PATH, "DELETE")

    assert len(matches) == 1, (
        "exactly one route may own DELETE "
        f"{DELETE_DOCUMENT_PATH}; a duplicate reintroduces the guard "
        f"bypass. Found: {[(r.path, r.name, r.endpoint) for r in matches]}"
    )


def test_the_document_delete_resolves_to_the_guarded_handler():
    """It must be ``library_delete.delete_document`` -- the handler that
    consults ``DocumentDeletionService`` and maps its ``is_note`` refusal to
    403. Any other endpoint on this path is by definition the unguarded one.
    """
    matches = _routes_for(DELETE_DOCUMENT_PATH, "DELETE")
    assert matches, f"DELETE {DELETE_DOCUMENT_PATH} is not mounted at all"

    assert matches[0].endpoint is library_delete.delete_document, (
        "DELETE on the document API must resolve to the guarded "
        "library_delete.delete_document handler, never a sibling that "
        f"deletes without the note check; got {matches[0].endpoint!r}"
    )


@pytest.mark.parametrize(
    "path,method,expected",
    [
        (
            "/library/api/document/{document_id}/blob",
            "DELETE",
            library_delete.delete_document_blob,
        ),
        (
            "/library/api/collection/{collection_id}/document/{document_id}",
            "DELETE",
            library_delete.remove_document_from_collection,
        ),
        (
            "/library/api/collections/{collection_id}",
            "DELETE",
            library_delete.delete_collection,
        ),
        (
            "/library/api/documents/bulk",
            "DELETE",
            library_delete.delete_documents_bulk,
        ),
    ],
    ids=["blob", "collection-document", "collection", "bulk"],
)
def test_each_guarded_delete_path_is_owned_by_exactly_one_handler(
    path, method, expected
):
    """The same invariant for the sibling delete routes. Each of these also
    carries a refusal (note / protected-home / element-type validation) that
    a duplicate registered earlier would bypass wholesale.
    """
    matches = _routes_for(path, method)

    assert len(matches) == 1, (
        f"{method} {path} must be owned by exactly one route; found "
        f"{[(r.name, r.endpoint) for r in matches]}"
    )
    assert matches[0].endpoint is expected, (
        f"{method} {path} resolves to {matches[0].endpoint!r}, not the "
        f"guarded {expected!r}"
    )


def test_the_library_router_declares_no_document_delete_of_its_own():
    """The Flask bug in its FastAPI form: the duplicate lived on the library
    router. Checking the module directly catches a re-added route even if the
    include order in ``fastapi_app.py`` happens to hide it today.
    """
    from local_deep_research.web.routers import library

    offenders = [
        (route.path, route.name)
        for route in library.router.routes
        if isinstance(route, APIRoute)
        and "DELETE" in (route.methods or set())
        and route.path.rstrip("/").endswith("/document/{document_id}")
    ]

    assert offenders == [], (
        "the library router must not declare a DELETE for the document API "
        "-- that is the duplicate that made the note guard in "
        f"library_delete dead code: {offenders}"
    )