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.

36.9 KB
Raw Blame
"""Two real users, real HTTP: user B attacks user A's objects.

This file is the EXECUTION complement to the static cross-user census in
``tests/security/test_cross_user_isolation_census.py``. That census parsed
``web/routers/`` and matched accessor names across 314 routes and came back
clean -- yet it missed a live broken-access-control defect
(``cancel_research`` acting before its ownership check, GHSA-q48m), because
the vulnerable call is spelled ``cancel_research(...)`` in ``routers/api.py``:
neither a router-local registry access nor an accessor name. A census is
bounded by its own resolution rules. So nothing here is parsed. Two users are
registered through the real ``/auth/register`` endpoint, user A creates one of
every ownable object through the real routes, and user B -- a genuinely
separate authenticated principal with its own session, its own cookie jar and
its own encrypted database -- attacks A's object ids over real HTTP.

THE ISOLATION MODEL, AND WHERE IT DOES NOT REACH
------------------------------------------------
Almost every route in this application is isolated *structurally* rather
than by an ownership check: the handler opens
``get_user_db_session(<authenticated username>)`` and each user has their own
SQLCipher database. A foreign id is not refused so much as it is simply
absent, so it 404s. That is a strong boundary and the sweep below confirms it
holds for delete/restore/re-index/retry/unsubscribe across notes, chat
sessions, collections, library documents and news subscriptions.

The boundary does NOT structurally reach process-global state.
``web/research_state.py`` is the one piece of mutable state in the process
that is not per-user: two module-level dicts keyed by ``research_id`` alone,
with no owner recorded. Every accessor of those dicts is therefore only as
safe as the ownership check its caller performs *first*. ``cancel_research``
used to perform none -- GHSA-q48m, fixed by fa466ad13 ("cross-user isolation
for benchmark runs, research termination, and follow-up", #5600), hand-ported
into this branch at 76eed009b. It now opens ``get_user_db_session(username)``
and confirms the caller owns the research BEFORE touching
``set_termination_flag`` or ``is_research_active``, refusing (fail-closed)
otherwise. The unit-level pin for that ordering lives in
``tests/security/test_research_terminate_cross_user.py``;
:class:`TestGlobalResearchRegistryIsReachableCrossUser` below is this file's
end-to-end complement -- it proves the fix holds all the way through real
HTTP, not just at the ``cancel_research`` call boundary.

WHAT COUNTS AS A PASS HERE
--------------------------
``assert response.status_code != 200`` is not used anywhere in this file and
would be worthless if it were: a route that 500s for everybody satisfies it,
and the actual defect found below returns **200 OK** to the attacker. Every
attack assertion has three parts:

1. the SPECIFIC refusal (the exact error string the handler returns, not just
   "not a 2xx"),
2. that A's object is byte-for-byte unchanged afterwards, read back through
   A's own session -- because a refusal that still mutated state is the
   finding, and
3. a POSITIVE CONTROL in which A performs the very same verb on the very same
   object and it succeeds. Without (3) the test only shows the feature is
   broken for everyone.

``cancel_research`` wraps its whole body in ``except Exception``, and several
other handlers here do too, so a tripwire that merely raises is swallowed and
reads as a pass. Nothing in this file asserts by raising from inside a patched
callee. The registry tests assert on the observable contents of the global
dicts (via the public ``is_research_active`` /
``is_termination_requested`` accessors) before and after the attacker's HTTP
request; the rest assert on rows read back over HTTP.

DETERMINISM
-----------
``TestGlobalResearchRegistryIsReachableCrossUser`` puts A's research into the
active registry with ``set_active_research`` rather than racing A's real
background worker thread. The research id and the ``ResearchHistory`` row are
still real -- created by A through ``POST /api/start_research`` -- and the
ATTACK is real HTTP; only the victim's "currently active" precondition is
pinned, because the background thread for an unreachable LLM provider tears
its own registry entry down within milliseconds and the attack would
otherwise be a coin flip. No test in this file sleeps.

Every test takes the function-scoped ``app`` fixture from ``tests/conftest.py``,
which points ``LDR_DATA_DIR`` at a fresh temporary directory *before*
``fastapi_app`` is imported. Importing ``fastapi_app`` directly at module
scope instead (the pattern in many older files under ``tests/web/``) makes a
direct run of the file register users against the operator's real on-disk
install; this file never does that.
"""

from __future__ import annotations

import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web import research_state

TEST_PASSWORD = "AttackSimPass123!"  # noqa: S105

# Not registered with any LLM provider factory, so if the background
# research thread ever runs far enough to build a model it dies with a
# synchronous ValueError before opening a socket. The SEARCH engine, by
# contrast, must be a real registered name: the request-boundary egress
# precheck fail-closed rejects an unknown engine with 400 *before* the
# ResearchHistory row is created, and these tests need that row.
_UNREACHABLE_PROVIDER = "no_such_llm_provider_xyz"
_ACCEPTED_ENGINE = "searxng"


# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------


def _new_client(app) -> TestClient:
    """A client with its own cookie jar and its own rate-limit bucket.

    ``/auth/register`` is capped per-IP; the TestClient peer address is
    treated as private so ``X-Forwarded-For`` is honoured for the slowapi
    key. Without a unique one, user B's registration lands in user A's
    bucket.
    """
    client = TestClient(app, raise_server_exceptions=False)
    octet_a = uuid.uuid4().int % 254 + 1
    octet_b = uuid.uuid4().int % 254 + 1
    client.headers.update({"X-Forwarded-For": f"10.{octet_a}.{octet_b}.1"})
    return client


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


def _drop_stale_csrf_header(client: TestClient) -> None:
    """CSRFMiddleware prefers the header over the form field.

    ``_register_and_login`` attaches a persistent ``X-CSRFToken`` default
    header; once the session rotates its token that stale header shadows the
    correct ``csrf_token`` form field and every form POST 403s.
    """
    for name in ("X-CSRFToken", "X-CSRF-Token"):
        client.headers.pop(name, None)


def _register_and_login(client: TestClient, username: str) -> None:
    _drop_stale_csrf_header(client)
    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "confirm_password": TEST_PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"register failed for {username!r}: "
        f"{resp.status_code} {resp.text[:300]}"
    )

    _drop_stale_csrf_header(client)
    resp = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"login failed for {username!r}: {resp.status_code} {resp.text[:300]}"
    )

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


def _two_users(app):
    """Register A and B through the real endpoints. Returns clients+names."""
    name_a = f"atk_a_{uuid.uuid4().hex[:10]}"
    name_b = f"atk_b_{uuid.uuid4().hex[:10]}"
    client_a = _new_client(app)
    client_b = _new_client(app)
    _register_and_login(client_a, name_a)
    _register_and_login(client_b, name_b)
    return client_a, name_a, client_b, name_b


def _create_note(client: TestClient, title: str) -> str:
    resp = client.post(
        "/notes/api/notes", json={"title": title, "content": f"body of {title}"}
    )
    assert resp.status_code == 201, resp.text[:300]
    return resp.json()["id"]


def _create_collection(client: TestClient, name: str) -> str:
    resp = client.post("/library/api/collections", json={"name": name})
    assert resp.status_code == 200, resp.text[:300]
    return resp.json()["collection"]["id"]


def _create_chat_session(client: TestClient, title: str) -> str:
    resp = client.post("/api/chat/sessions", json={"title": title})
    assert resp.status_code == 200, resp.text[:300]
    return resp.json()["session_id"]


def _create_subscription(client: TestClient) -> str:
    resp = client.post(
        "/news/api/subscribe",
        json={
            "query": f"topic {uuid.uuid4().hex[:8]}",
            "subscription_type": "search",
        },
    )
    assert resp.status_code == 200, resp.text[:300]
    return resp.json()["subscription_id"]


def _start_research(client: TestClient) -> str:
    resp = client.post(
        "/api/start_research",
        json={
            "query": f"attack sim query {uuid.uuid4().hex[:8]}",
            "mode": "quick",
            "model_provider": _UNREACHABLE_PROVIDER,
            "model": "no-such-model-xyz",
            "search_engine": _ACCEPTED_ENGINE,
            "iterations": 1,
            "questions_per_iteration": 1,
        },
    )
    assert resp.status_code == 200, resp.text[:300]
    body = resp.json()
    assert body.get("status") == "success", body
    return body["research_id"]


def _upload_document(client: TestClient, collection_id: str, tag: str) -> str:
    resp = client.post(
        f"/library/api/collections/{collection_id}/upload",
        files=[
            (
                "files",
                (
                    f"{tag}.txt",
                    f"document body for {tag}".encode(),
                    "text/plain",
                ),
            )
        ],
    )
    assert resp.status_code == 200, resp.text[:300]
    uploaded = resp.json()["uploaded"]
    assert len(uploaded) == 1, resp.text[:300]
    return uploaded[0]["id"]


def _note_titles(client: TestClient) -> list[str]:
    resp = client.get("/notes/api/notes")
    assert resp.status_code == 200, resp.text[:300]
    body = resp.json()
    return [n["title"] for n in body.get("notes", [])]


def _collection_names(client: TestClient) -> list[str]:
    resp = client.get("/library/api/collections")
    assert resp.status_code == 200, resp.text[:300]
    return [c["name"] for c in resp.json().get("collections", [])]


# ---------------------------------------------------------------------------
# THE FINDING: the process-global research registry has no owner
# ---------------------------------------------------------------------------


class TestGlobalResearchRegistryIsReachableCrossUser:
    """``POST /research/api/terminate/{id}`` checks the owner before it acts.

    Historically (GHSA-q48m) ``routers/api.py::api_terminate_research`` took
    ``research_id`` straight off the path under ``Depends(require_auth)`` and
    handed it to ``services/research_service.cancel_research``, whose first
    two statements were unconditional global operations::

        set_termination_flag(research_id)     # global write, no owner check
        if is_research_active(research_id):   # global read,  no owner check
            handle_termination(research_id, username)
            return True                       # <-- returned BEFORE the gate
        with get_user_db_session(username):   # the 404 gate, below the return

    ``handle_termination`` calls ``cleanup_research_resources``, which calls
    ``cleanup_research(research_id)`` -- popping the id out of BOTH global
    dicts. For a research that was currently ACTIVE the owner-scoped lookup
    was never reached at all, so any authenticated user who knew a victim's
    research id could evict that research from the live registry and set its
    termination flag.

    Fixed by fa466ad13 (#5600, hand-ported into this branch at 76eed009b):
    ``cancel_research`` now opens ``get_user_db_session(username)`` and
    confirms the caller owns the research FIRST, before either global dict is
    touched, refusing (fail-closed) otherwise -- see
    ``web/services/research_service.py::cancel_research``. That closes the
    gap between this route and its sibling, which always checked ownership
    first: ``research.py::terminate_research`` (``POST /api/terminate/{id}``)
    -- see :meth:`test_the_sibling_terminate_route_refuses_the_same_foreign_id`.
    Two routes, same job, now the same order.

    The tests below are the end-to-end negative controls for that fix: they
    prove, over real HTTP, that a foreign user's attack is refused and the
    victim's registry entry survives untouched. The unit-level ordering pin
    lives in ``tests/security/test_research_terminate_cross_user.py``.

    Mitigating even before the fix: research ids are ``str(uuid.uuid4())`` at
    every minting site, so an attacker would have had to already know the
    victim's uuid4.
    """

    def test_foreign_user_cannot_evict_victims_active_research(self, app):
        """B's HTTP call is refused; A's research survives in the registry."""
        client_a, name_a, client_b, _name_b = _two_users(app)

        # A's research is REAL: a genuine ResearchHistory row in A's own
        # encrypted database, with a real uuid4 id, created over HTTP.
        research_id = _start_research(client_a)

        # Pin the "currently active" precondition rather than racing A's
        # background worker (see the module docstring on determinism). The
        # id and the row remain A's real ones.
        research_state.set_active_research(
            research_id,
            {
                "status": "in_progress",
                "settings": {"username": name_a},
                "log": [],
            },
        )
        assert research_state.is_research_active(research_id) is True

        # --- THE (NOW REFUSED) ATTACK: B, over real HTTP, against A's id.
        attack = client_b.post(f"/research/api/terminate/{research_id}")

        # B is refused with the same "not found" answer an unowned id always
        # gets -- not a 403 that would confirm the id exists, just a clean
        # failure -- because the ownership lookup in B's own database comes
        # back empty before either global dict is ever touched.
        assert attack.status_code == 200, attack.text[:300]
        assert attack.json() == {
            "status": "success",
            "message": "Research not found or already completed",
            "result": False,
        }, attack.text[:300]

        # --- A's registry entry survived the refusal untouched, observed on
        # the global registry itself rather than by any tripwire that could
        # be swallowed by cancel_research's blanket `except Exception`.
        assert research_state.is_research_active(research_id) is True, (
            "SECURITY REGRESSION: user B's refused attempt evicted user A's "
            "active research from the process-global registry via POST "
            f"/research/api/terminate/{research_id}"
        )

    def test_a_can_terminate_their_own_research(self, app):
        """POSITIVE CONTROL: the same verb works for the owner.

        Without this, the test above would only show that the route is
        broken for everybody rather than that B performed a real action.
        """
        client_a, name_a, _client_b, _name_b = _two_users(app)
        research_id = _start_research(client_a)
        research_state.set_active_research(
            research_id,
            {
                "status": "in_progress",
                "settings": {"username": name_a},
                "log": [],
            },
        )
        assert research_state.is_research_active(research_id) is True

        owner = client_a.post(f"/research/api/terminate/{research_id}")

        assert owner.status_code == 200, owner.text[:300]
        assert owner.json()["result"] is True
        assert research_state.is_research_active(research_id) is False

    def test_unregistered_id_is_reported_as_not_found(self, app):
        """NEGATIVE CONTROL: the 200/True above is not returned universally.

        An id that was never registered as active falls through to the
        owner-scoped DB lookup and comes back ``result: False``. So the
        ``result: True`` that B received in the attack really did come from
        the ``is_research_active`` branch -- i.e. from A's registry entry --
        and not from the route answering 200 to everything.
        """
        _client_a, _name_a, client_b, _name_b = _two_users(app)
        never_registered = str(uuid.uuid4())

        resp = client_b.post(f"/research/api/terminate/{never_registered}")

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json()["result"] is False
        assert (
            resp.json()["message"] == "Research not found or already completed"
        )

    def test_termination_flag_is_not_planted_for_an_unowned_id(self, app):
        """The ownership check now runs before ``set_termination_flag``.

        Before the fix, the global write happened unconditionally and the
        owner-scoped lookup only refused afterwards, so the flag was left
        planted in a process-global dict for the lifetime of the process
        (``clear_termination_flag`` has zero production call sites, and
        ``cleanup_research`` only ran on the active branch) -- an
        authenticated user could plant a permanent termination flag under any
        id they chose, owned or not. Now the ownership lookup runs first and
        ``set_termination_flag`` is never reached for a foreign id.
        """
        _client_a, _name_a, client_b, _name_b = _two_users(app)
        target = str(uuid.uuid4())
        assert research_state.is_termination_requested(target) is False

        resp = client_b.post(f"/research/api/terminate/{target}")
        assert resp.status_code == 200, resp.text[:300]
        # The route reports failure...
        assert resp.json()["result"] is False

        # ...and, unlike before the fix, the global write never happened.
        assert research_state.is_termination_requested(target) is False, (
            "SECURITY REGRESSION: set_termination_flag executed for an id "
            "the caller does not own -- the ownership check should run "
            "before any global registry write"
        )

    def test_the_sibling_terminate_route_refuses_the_same_foreign_id(self, app):
        """The other terminate route checks the owner FIRST, and 404s.

        ``research.py::terminate_research`` opens ``get_user_db_session``
        before it touches the registry. Same job, same caller-supplied id,
        opposite order -- and the correct outcome. This is what
        ``/research/api/terminate/{id}`` should be doing.
        """
        client_a, name_a, client_b, _name_b = _two_users(app)
        research_id = _start_research(client_a)
        research_state.set_active_research(
            research_id,
            {
                "status": "in_progress",
                "settings": {"username": name_a},
                "log": [],
            },
        )

        attack = client_b.post(f"/api/terminate/{research_id}")

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Research not found"

        # And crucially: A's registry entry survived the refusal. The
        # refusal did not mutate state on its way to saying no.
        assert research_state.is_research_active(research_id) is True

        # POSITIVE CONTROL: the owner can use this route on this object.
        owner = client_a.post(f"/api/terminate/{research_id}")
        assert owner.status_code == 200, owner.text[:300]


# ---------------------------------------------------------------------------
# The per-user-database boundary, exercised verb by verb
# ---------------------------------------------------------------------------


class TestDestructiveVerbsAgainstAForeignObject:
    """B runs every destructive verb against A's ids, and A checks after.

    Each test asserts the specific refusal, then reads A's object back
    through A's own session to prove nothing moved, then has A perform the
    identical verb successfully.
    """

    def test_delete_note(self, app):
        client_a, _name_a, client_b, _name_b = _two_users(app)
        note_id = _create_note(client_a, "A private note")

        attack = client_b.delete(f"/notes/api/notes/{note_id}")

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json() == {"success": False, "error": "Note not found"}

        # A's note is untouched.
        after = client_a.get(f"/notes/api/notes/{note_id}")
        assert after.status_code == 200, after.text[:300]
        assert after.json()["note"]["title"] == "A private note"

        # POSITIVE CONTROL: A deletes the same note through the same route.
        owner = client_a.delete(f"/notes/api/notes/{note_id}")
        assert owner.status_code == 200, owner.text[:300]
        assert client_a.get(f"/notes/api/notes/{note_id}").status_code == 404

    def test_restore_note_version(self, app):
        """The ``restore`` verb -- a mutation the static census cannot model."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        note_id = _create_note(client_a, "A restorable note")

        # Create a second version so a real version id exists to restore to.
        edit = client_a.put(
            f"/notes/api/notes/{note_id}",
            json={"title": "A restorable note", "content": "edited content"},
        )
        assert edit.status_code == 200, edit.text[:300]
        versions = client_a.get(f"/notes/api/notes/{note_id}/versions")
        assert versions.status_code == 200, versions.text[:300]
        version_list = versions.json().get("versions", [])
        assert version_list, versions.text[:300]
        version_id = version_list[-1]["id"]

        attack = client_b.post(
            f"/notes/api/notes/{note_id}/versions/{version_id}/restore",
            json={},
        )

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Note not found"

        after = client_a.get(f"/notes/api/notes/{note_id}")
        assert after.status_code == 200, after.text[:300]
        assert after.json()["note"]["content"] == "edited content"

        # POSITIVE CONTROL: A restores the same version successfully.
        owner = client_a.post(
            f"/notes/api/notes/{note_id}/versions/{version_id}/restore",
            json={},
        )
        assert owner.status_code == 200, owner.text[:300]
        assert owner.json()["success"] is True

    def test_reindex_note(self, app):
        """The ``re-index`` verb, with A's collection id in the BODY."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        note_id = _create_note(client_a, "A indexable note")
        collection_id = _create_collection(
            client_a, f"A coll {uuid.uuid4().hex[:6]}"
        )

        attack = client_b.post(
            f"/notes/api/notes/{note_id}/index",
            json={"collection_id": collection_id},
        )

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Note not found"

        after = client_a.get(f"/notes/api/notes/{note_id}")
        assert after.status_code == 200, after.text[:300]
        assert after.json()["note"]["is_indexed"] is False

    def test_delete_chat_session(self, app):
        client_a, _name_a, client_b, _name_b = _two_users(app)
        session_id = _create_chat_session(client_a, "A private chat")

        attack = client_b.delete(f"/api/chat/sessions/{session_id}")

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json() == {
            "success": False,
            "error": "Session not found",
        }

        after = client_a.get(f"/api/chat/sessions/{session_id}")
        assert after.status_code == 200, after.text[:300]
        assert after.json()["session"]["title"] == "A private chat"

        # POSITIVE CONTROL.
        owner = client_a.delete(f"/api/chat/sessions/{session_id}")
        assert owner.status_code == 200, owner.text[:300]
        assert (
            client_a.get(f"/api/chat/sessions/{session_id}").status_code == 404
        )

    def test_retry_chat_attempt(self, app):
        """The ``retry`` verb: deletes a turn and re-spawns a research run."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        session_id = _create_chat_session(client_a, "A retryable chat")
        research_id = _start_research(client_a)

        attack = client_b.post(
            f"/api/chat/sessions/{session_id}/attempts/{research_id}/retry",
            json={},
        )

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Session not found"

        # A's chat session is still there and still ACTIVE -- retry's first
        # destructive step (delete_attempt) never ran.
        after = client_a.get(f"/api/chat/sessions/{session_id}")
        assert after.status_code == 200, after.text[:300]
        assert after.json()["session"]["status"] == "active"

    def test_delete_collection(self, app):
        client_a, _name_a, client_b, _name_b = _two_users(app)
        name = f"A coll {uuid.uuid4().hex[:6]}"
        collection_id = _create_collection(client_a, name)

        attack = client_b.delete(f"/library/api/collections/{collection_id}")

        assert attack.status_code == 404, attack.text[:300]
        body = attack.json()
        assert body["deleted"] is False
        assert body["error"] == "Collection not found"

        assert name in _collection_names(client_a)

        # POSITIVE CONTROL.
        owner = client_a.delete(f"/library/api/collections/{collection_id}")
        assert owner.status_code == 200, owner.text[:300]
        assert name not in _collection_names(client_a)

    def test_delete_collection_index(self, app):
        """The ``delete index`` verb -- destroys the vector store."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        name = f"A coll {uuid.uuid4().hex[:6]}"
        collection_id = _create_collection(client_a, name)

        attack = client_b.delete(
            f"/library/api/collections/{collection_id}/index"
        )

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Collection not found"
        assert name in _collection_names(client_a)

    def test_unsubscribe(self, app):
        """The ``unsubscribe`` verb."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        subscription_id = _create_subscription(client_a)

        attack = client_b.delete(f"/news/api/subscriptions/{subscription_id}")

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error_code"] == "SUBSCRIPTION_NOT_FOUND"

        after = client_a.get(f"/news/api/subscriptions/{subscription_id}")
        assert after.status_code == 200, after.text[:300]

        # POSITIVE CONTROL.
        owner = client_a.delete(f"/news/api/subscriptions/{subscription_id}")
        assert owner.status_code == 200, owner.text[:300]
        assert (
            client_a.get(
                f"/news/api/subscriptions/{subscription_id}"
            ).status_code
            == 404
        )

    def test_export_research(self, app):
        """The ``export`` verb -- exfiltration rather than destruction."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        research_id = _start_research(client_a)

        attack = client_b.post(f"/api/v1/research/{research_id}/export/ris")

        assert attack.status_code == 404, attack.text[:300]
        assert "not found" in attack.text.lower(), attack.text[:300]

    def test_delete_research(self, app):
        client_a, _name_a, client_b, _name_b = _two_users(app)
        research_id = _start_research(client_a)

        attack = client_b.delete(f"/api/delete/{research_id}")

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Research not found"

        # A's research row survived.
        after = client_a.get(f"/history/status/{research_id}")
        assert after.status_code == 200, after.text[:300]
        assert after.json()["id"] == research_id


# ---------------------------------------------------------------------------
# A's id somewhere other than the path
# ---------------------------------------------------------------------------


class TestForeignIdInBodyRatherThanPath:
    """Ownership checks that key off the path can miss ids in the body."""

    def test_foreign_collection_id_in_json_body(self, app):
        """B links B's OWN note to A's collection, naming it in the body.

        The path segment here is B's own note -- entirely legitimate. Only
        the body carries A's id, so any check that validates "the path id
        belongs to the caller" would wave this through.
        """
        client_a, _name_a, client_b, _name_b = _two_users(app)
        a_collection = _create_collection(
            client_a, f"A coll {uuid.uuid4().hex[:6]}"
        )
        b_note = _create_note(client_b, "B own note")

        attack = client_b.post(
            f"/notes/api/notes/{b_note}/collections",
            json={"collection_id": a_collection},
        )

        assert attack.status_code == 404, attack.text[:300]
        assert attack.json()["error"] == "Collection not found"

        # POSITIVE CONTROL: the same call with B's OWN collection works, so
        # the 404 above is about ownership and not about the route being
        # broken for everyone.
        b_collection = _create_collection(
            client_b, f"B coll {uuid.uuid4().hex[:6]}"
        )
        owner = client_b.post(
            f"/notes/api/notes/{b_note}/collections",
            json={"collection_id": b_collection},
        )
        assert owner.status_code == 200, owner.text[:300]
        assert owner.json()["success"] is True

    def test_foreign_collection_id_in_scheduler_body(self, app):
        """A's collection id in the body of the scheduler's run-now verb."""
        client_a, _name_a, client_b, _name_b = _two_users(app)
        a_collection = _create_collection(
            client_a, f"A coll {uuid.uuid4().hex[:6]}"
        )

        attack = client_b.post(
            "/api/scheduler/run-now", json={"collection_id": a_collection}
        )

        # Refused -- and specifically, not a 2xx claiming work was done on
        # A's collection.
        assert attack.status_code == 400, attack.text[:300]
        assert "Failed to trigger" in attack.json()["error"]


class TestForeignIdInBulkOperation:
    """Partial-success paths are where ownership checks get skipped.

    A bulk endpoint that loops over ids and reports per-id outcomes has a
    natural failure mode: the loop body forgets the ownership filter because
    "the request already passed auth". The test below mixes one of B's own
    ids with one of A's in the SAME request, so B's own id succeeding proves
    the loop really executed -- an all-failed response would prove nothing.
    """

    def test_bulk_delete_mixing_own_and_foreign_document_ids(self, app):
        client_a, _name_a, client_b, _name_b = _two_users(app)

        a_collection = _create_collection(
            client_a, f"A coll {uuid.uuid4().hex[:6]}"
        )
        a_document = _upload_document(client_a, a_collection, "alpha")

        b_collection = _create_collection(
            client_b, f"B coll {uuid.uuid4().hex[:6]}"
        )
        b_document = _upload_document(client_b, b_collection, "beta")

        # --- One request, two ids: B's own, and A's.
        attack = client_b.request(
            "DELETE",
            "/library/api/documents/bulk",
            json={"document_ids": [b_document, a_document]},
        )

        assert attack.status_code == 200, attack.text[:300]
        body = attack.json()
        assert body["total"] == 2, body

        # The loop DID run -- B's own document was deleted. This is the
        # in-request positive control: without it, `deleted == 0` for A's id
        # would be indistinguishable from the endpoint erroring out wholesale.
        assert body["deleted"] == 1, body
        assert body["failed"] == 1, body
        failed_ids = [e["document_id"] for e in body["errors"]]
        assert failed_ids == [a_document], body

        # --- A's document is still there, read back through A's session.
        a_docs = client_a.get(
            f"/library/api/collections/{a_collection}/documents"
        )
        assert a_docs.status_code == 200, a_docs.text[:300]
        a_doc_ids = [d["id"] for d in a_docs.json().get("documents", [])]
        assert a_document in a_doc_ids, (
            "SECURITY: user B's bulk delete removed user A's document"
        )

    def test_bulk_collection_unlink_mixing_own_and_foreign_ids(self, app):
        """Same shape, against the collection-scoped bulk unlink route."""
        client_a, _name_a, client_b, _name_b = _two_users(app)

        a_collection = _create_collection(
            client_a, f"A coll {uuid.uuid4().hex[:6]}"
        )
        a_document = _upload_document(client_a, a_collection, "alpha")

        b_collection = _create_collection(
            client_b, f"B coll {uuid.uuid4().hex[:6]}"
        )
        b_document = _upload_document(client_b, b_collection, "beta")

        attack = client_b.request(
            "DELETE",
            f"/library/api/collection/{b_collection}/documents/bulk",
            json={"document_ids": [b_document, a_document]},
        )

        assert attack.status_code == 200, attack.text[:300]
        body = attack.json()
        assert body["total"] == 2, body
        # B's own id processed (loop ran), A's id refused.
        assert body["failed"] == 1, body
        failed_ids = [e["document_id"] for e in body["errors"]]
        assert failed_ids == [a_document], body

        a_docs = client_a.get(
            f"/library/api/collections/{a_collection}/documents"
        )
        assert a_docs.status_code == 200, a_docs.text[:300]
        a_doc_ids = [d["id"] for d in a_docs.json().get("documents", [])]
        assert a_document in a_doc_ids


# ---------------------------------------------------------------------------
# Not cross-user, but found by the same sweep
# ---------------------------------------------------------------------------


class TestIndexStartAcceptsACollectionThatDoesNotExist:
    """``index/start`` never checks the collection exists. NOT a cross-user leak.

    ``rag.py::_start_background_index_sync`` opens
    ``get_user_db_session(username)`` -- so it cannot read another user's
    collection, and no cross-user data is exposed. But it never looks the
    ``collection_id`` up at all: it scans for an in-progress task with the
    same id, then unconditionally inserts a ``TaskMetadata`` row and spawns
    a background indexer thread. Consequences, both real:

    * It answers ``200 {"success": true, "message": "Indexing started"}``
      for a collection the caller does not own and that does not exist in
      their database -- a false success that tells an attacker probing ids
      nothing useful, but tells a legitimate user their index is building
      when it is not.
    * The 409 "already in progress" guard keys on ``collection_id``, so a
      caller can spawn an unbounded number of concurrent background indexer
      threads and task rows simply by posting distinct random ids.

    Recorded here rather than fixed (no ``src/`` changes in scope).
    """

    def test_success_is_reported_for_a_nonexistent_collection(self, app):
        _client_a, _name_a, client_b, _name_b = _two_users(app)
        nonexistent = str(uuid.uuid4())

        resp = client_b.post(
            f"/library/api/collections/{nonexistent}/index/start", json={}
        )

        assert resp.status_code == 200, resp.text[:300]
        body = resp.json()
        assert body["success"] is True, body
        assert body["task_id"], body

    def test_success_is_reported_for_another_users_collection(self, app):
        client_a, _name_a, client_b, _name_b = _two_users(app)
        name = f"A coll {uuid.uuid4().hex[:6]}"
        a_collection = _create_collection(client_a, name)

        resp = client_b.post(
            f"/library/api/collections/{a_collection}/index/start", json={}
        )

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json()["success"] is True

        # A's collection itself is untouched: nothing was indexed, because
        # the worker read B's database and found no such collection.
        a_view = client_a.get(
            f"/library/api/collections/{a_collection}/documents"
        )
        assert a_view.status_code == 200, a_view.text[:300]
        assert a_view.json()["collection"]["name"] == name


if __name__ == "__main__":  # pragma: no cover
    raise SystemExit(pytest.main([__file__, "-q"]))