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.

test: cover cancellation ownership lookup failures (#6243)

LearningCircuit22cf131305d7
diff --git a/tests/security/test_research_terminate_cross_user.py b/tests/security/test_research_terminate_cross_user.py
index fde627d2c7..0270a601b1 100644
--- a/tests/security/test_research_terminate_cross_user.py
+++ b/tests/security/test_research_terminate_cross_user.py
@@ -1,7 +1,6 @@
"""Cross-user isolation for research termination.
-``cancel_research`` (reached from ``POST /research/api/terminate/<id>`` via
-``api_terminate_research``) drives the PROCESS-GLOBAL termination registry
+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,
@@ -15,8 +14,10 @@ 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 ownership gate directly, mocking the DB and the global
-registry so only the authorization ordering is under test.
+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
@@ -59,21 +60,27 @@ def patched(monkeypatch):
return set_flag, is_active, handle
[email protected]("active", [False, True])
def test_cancel_research_refuses_when_caller_does_not_own_it(
- monkeypatch, patched
+ 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.
- monkeypatch.setattr(
- rs, "get_user_db_session", MagicMock(return_value=_db_ctx(None))
- )
+ 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()
@@ -99,12 +106,32 @@ def test_cancel_research_terminates_your_own_active_research(
handle.assert_called_once()
-def test_cancel_research_fails_closed_on_db_error(monkeypatch, patched):
+ "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
- boom = MagicMock(side_effect=RuntimeError("db down"))
- monkeypatch.setattr(rs, "get_user_db_session", boom)
+ 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()