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
"""Cross-user isolation for research termination.

The ``cancel_research`` service drives the PROCESS-GLOBAL termination registry
(``_termination_flags`` / ``_active_research`` in ``web/research_state.py``),
which is keyed by ``research_id`` alone and shared across all users. Before the
fix it called ``set_termination_flag`` and, for an active run,
``handle_termination`` *before* any ownership check (the only pre-existing DB
ownership query lived in the not-active branch). So a signed-in user who knew
another user's research id could terminate that user's in-progress research
(cross-user denial of service).

Same bug class as the cached-connection auth bypass (#5596): an identifier
acted on against shared global state without being bound to the authenticated
principal. The fix confirms the caller owns the research in their OWN database
before touching any global termination state.

These tests exercise the service's ownership gate directly, mocking the DB and
the global registry so only the authorization ordering is under test. The
FastAPI termination route has a separate implementation and requires its own
route-level coverage.
"""

from unittest.mock import MagicMock

import pytest

from local_deep_research.web.services import research_service as rs

# This branch moved the process-global termination registry from
# web/routes/globals.py to web/research_state.py; routes/globals.py is now
# only a re-export shim. cancel_research imports the names from
# research_state directly, so patching the shim would leave the real
# functions in place and the ownership assertions below would never fire.
import local_deep_research.web.research_state as gl


def _db_ctx(first_return):
    """A ``with get_user_db_session(...) as session`` mock whose
    ``session.query(...).filter_by(...).first()`` yields ``first_return``."""
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        first_return
    )
    ctx = MagicMock()
    ctx.__enter__.return_value = session
    ctx.__exit__.return_value = False
    return ctx


@pytest.fixture
def patched(monkeypatch):
    """Patch the global termination registry + handle_termination and return
    the mocks so tests can assert whether they were touched."""
    set_flag = MagicMock()
    is_active = MagicMock(return_value=True)
    handle = MagicMock()
    monkeypatch.setattr(gl, "set_termination_flag", set_flag)
    monkeypatch.setattr(gl, "is_research_active", is_active)
    monkeypatch.setattr(rs, "handle_termination", handle)
    return set_flag, is_active, handle


@pytest.mark.parametrize("active", [False, True])
def test_cancel_research_refuses_when_caller_does_not_own_it(
    monkeypatch, patched, active
):
    """The security fix: a research id not in the caller's own database must be
    refused BEFORE any global termination state is touched -- otherwise a user
    could terminate another user's active research by id."""
    set_flag, is_active, handle = patched
    is_active.return_value = active
    # research id is NOT in alice's database.
    ctx = _db_ctx(None)
    get_session = MagicMock(return_value=ctx)
    monkeypatch.setattr(rs, "get_user_db_session", get_session)

    result = rs.cancel_research("bob-research-uuid", "alice")

    assert result is False
    get_session.assert_called_once_with("alice")
    ctx.__enter__.return_value.query.return_value.filter_by.assert_called_once_with(
        id="bob-research-uuid"
    )
    set_flag.assert_not_called()  # never sets the global termination flag
    is_active.assert_not_called()  # never even consults the shared registry
    handle.assert_not_called()


def test_cancel_research_terminates_your_own_active_research(
    monkeypatch, patched
):
    """The owner's terminate still works: an owned, active research sets the
    flag and calls handle_termination."""
    set_flag, is_active, handle = patched
    # research id IS in alice's database (truthy row).
    monkeypatch.setattr(
        rs,
        "get_user_db_session",
        MagicMock(return_value=_db_ctx(MagicMock())),
    )

    result = rs.cancel_research("alice-research-uuid", "alice")

    assert result is True
    set_flag.assert_called_once_with("alice-research-uuid")
    handle.assert_called_once()


@pytest.mark.parametrize(
    "failure_stage", ["factory", "enter", "query", "first", "exit"]
)
def test_cancel_research_fails_closed_on_db_error(
    monkeypatch, patched, failure_stage
):
    """If the caller's ownership lookup cannot even be performed, refuse --
    never fall through to touching the global registry."""
    set_flag, is_active, handle = patched
    # Use a truthy ownership row: failure while leaving the context must
    # still prevent action, even after the query found the caller's row.
    ctx = _db_ctx(MagicMock())
    session = ctx.__enter__.return_value
    get_session = MagicMock(return_value=ctx)
    operations = {
        "factory": get_session,
        "enter": ctx.__enter__,
        "query": session.query,
        "first": session.query.return_value.filter_by.return_value.first,
        "exit": ctx.__exit__,
    }
    operations[failure_stage].side_effect = RuntimeError("db down")
    monkeypatch.setattr(rs, "get_user_db_session", get_session)

    assert rs.cancel_research("some-uuid", "alice") is False
    get_session.assert_called_once_with("alice")
    set_flag.assert_not_called()
    is_active.assert_not_called()
    handle.assert_not_called()