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.

8.8 KB
Raw Blame
"""Cross-user isolation for the in-memory benchmark run registry.

``BenchmarkService.active_runs`` is process-global, but ``BenchmarkRun.id`` is a
PER-USER autoincrement integer -- so two different users can each own a run with
the same id (both have id == 1). The registry is therefore keyed by
``(username, BenchmarkRun.id)``: a user-facing lookup can only ever reach the
caller's OWN run, so a request that names another user's per-user id simply
misses. Without that binding one user could:

  * set another user's in-flight run to "cancelled" and stop it (cross-user DoS);
  * trigger a read/persist of another user's in-memory results (disclosure, and
    -- via the run's stored password -- a credential crossover between DBs);
  * observe another user's run persistence-error state;
  * clobber another user's live run by starting their own colliding id.

This is the same bug class as the cached-connection auth bypass: an identifier
(here the per-user integer id) trusted for authorization without being bound to
the authenticated principal -- fixed here by binding the id to its owner in the
key itself. These tests exercise the isolation directly on ``active_runs``
state, mocking the database layer so only the in-memory logic is under test.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.benchmarks.web_api.benchmark_service import (
    BenchmarkService,
)


@pytest.fixture
def service():
    """A fresh BenchmarkService with an empty in-memory registry."""
    return BenchmarkService()


def _seed_run(
    service, run_id, owner, *, results=None, persistence_failed=False
):
    """Put an in-memory run owned by ``owner`` under the ``(owner, run_id)`` key
    start_benchmark would use (the owner also lives at
    run['data']['username'])."""
    key = service._run_key(owner, run_id)
    service.active_runs[key] = {
        "data": {"username": owner, "user_password": f"{owner}-pw"},
        "status": "running",
        "results": list(results or []),
    }
    if persistence_failed:
        service.active_runs[key]["result_persistence_failed"] = True
    return service.active_runs[key]


# --------------------------------------------------------------------------- #
# cancel_benchmark
# --------------------------------------------------------------------------- #
def test_cancel_does_not_stop_another_users_run(service):
    """A different user's cancel must NOT flip the in-memory run to cancelled.

    The colliding-id scenario: alice owns the in-memory run at id 1; bob (who
    also has his own id 1 in his own DB) POSTs /benchmark/api/cancel/1.
    """
    run = _seed_run(service, 1, "alice")

    with patch.object(service, "update_benchmark_status") as upd:
        # update_benchmark_status is DB-scoped to the caller (bob) and is
        # irrelevant to the in-memory DoS -- mock it away.
        assert service.cancel_benchmark(1, "bob") is True

    assert run["status"] == "running", (
        "bob's cancel must not stop alice's in-flight benchmark run"
    )
    # The DB update still runs, scoped to the caller's own database.
    upd.assert_called_once()


def test_cancel_stops_your_own_run(service):
    run = _seed_run(service, 1, "alice")
    with patch.object(service, "update_benchmark_status"):
        assert service.cancel_benchmark(1, "alice") is True
    assert run["status"] == "cancelled", (
        "the owner's cancel must stop their own run"
    )


def test_cancel_unknown_run_is_harmless(service):
    with patch.object(service, "update_benchmark_status"):
        assert service.cancel_benchmark(999, "alice") is True
    assert service._run_key("alice", 999) not in service.active_runs


# --------------------------------------------------------------------------- #
# sync_pending_results
# --------------------------------------------------------------------------- #
def test_sync_pending_results_refuses_cross_user(service):
    """bob syncing alice's run must be refused (0) and must NOT open any DB or
    read alice's results."""
    _seed_run(service, 1, "alice", results=[{"secret": "alice-only"}])

    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as gud:
        saved = service.sync_pending_results(1, "bob")

    assert saved == 0, "a cross-user result sync must persist nothing"
    # A refused cross-user sync must never open a database session -- no read of
    # alice's results, and no credential crossover via the run's stored password.
    gud.assert_not_called()


def test_sync_pending_results_owner_passes_the_gate(service):
    """The owner is allowed past the gate and opens THEIR OWN db session."""
    _seed_run(service, 1, "alice", results=[])

    ctx = MagicMock()
    ctx.__enter__.return_value = MagicMock()
    ctx.__exit__.return_value = False
    with patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=ctx,
    ) as gud:
        with patch.object(service, "_persist_unsaved_results", return_value=[]):
            service.sync_pending_results(1, "alice")

    gud.assert_called_once()
    # Opened as alice, with alice's stored password -- never crossed.
    args, kwargs = gud.call_args
    called_with = list(args) + list(kwargs.values())
    assert "alice" in called_with and "alice-pw" in called_with


def test_sync_pending_results_unknown_run_returns_zero(service):
    with patch(
        "local_deep_research.database.session_context.get_user_db_session"
    ) as gud:
        assert service.sync_pending_results(1234, "alice") == 0
    gud.assert_not_called()


# --------------------------------------------------------------------------- #
# get_result_persistence_error
# --------------------------------------------------------------------------- #
def test_persistence_error_not_disclosed_cross_user(service):
    _seed_run(service, 1, "alice", persistence_failed=True)

    assert service.get_result_persistence_error(1, "bob") is None, (
        "bob must not observe alice's run persistence-error state"
    )
    # The owner still sees their own error.
    assert service.get_result_persistence_error(1, "alice") is not None


def test_persistence_error_none_when_not_failed(service):
    _seed_run(service, 1, "alice", persistence_failed=False)
    assert service.get_result_persistence_error(1, "alice") is None


# --------------------------------------------------------------------------- #
# write-collision: colliding per-user ids must not clobber each other
# --------------------------------------------------------------------------- #
def test_two_users_colliding_ids_coexist(service):
    """The core write-collision fix. alice and bob can each own run id 1 at the
    same time; keying by (username, id) keeps both entries distinct instead of
    the second start_benchmark overwriting the first -- an overwrite that would
    cross the two users' results (and, via the run's stored password, their
    credentials)."""
    alice_run = _seed_run(service, 1, "alice", results=[{"r": "alice-1"}])
    bob_run = _seed_run(service, 1, "bob", results=[{"r": "bob-1"}])

    # Neither seed overwrote the other: two distinct entries remain. Assert
    # against the REGISTRY (re-fetched by key), never the captured references --
    # a captured ref survives even if its entry was evicted, so reading it back
    # would pass under a regression that collapsed both users onto one key.
    # `len == 2` and the identity re-fetch both fail if the keys collapse.
    assert len(service.active_runs) == 2
    stored_alice = service.active_runs[service._run_key("alice", 1)]
    stored_bob = service.active_runs[service._run_key("bob", 1)]
    assert stored_alice is alice_run
    assert stored_bob is bob_run
    assert alice_run is not bob_run

    # Each key holds its own owner's results and password -- never crossed.
    assert stored_alice["results"] == [{"r": "alice-1"}]
    assert stored_bob["results"] == [{"r": "bob-1"}]
    assert stored_alice["data"]["user_password"] == "alice-pw"
    assert stored_bob["data"]["user_password"] == "bob-pw"


def test_cancel_with_colliding_ids_targets_only_the_caller(service):
    """With both users holding id 1, bob's cancel stops only bob's run."""
    _seed_run(service, 1, "alice")
    _seed_run(service, 1, "bob")
    # Two distinct entries to begin with -- guards against a collapsed key that
    # would make the assertions below vacuous.
    assert len(service.active_runs) == 2

    with patch.object(service, "update_benchmark_status"):
        assert service.cancel_benchmark(1, "bob") is True

    # Re-fetch each entry by key (not a captured reference): bob's is cancelled,
    # alice's is untouched.
    assert (
        service.active_runs[service._run_key("bob", 1)]["status"] == "cancelled"
    )
    assert (
        service.active_runs[service._run_key("alice", 1)]["status"] == "running"
    ), "cancelling bob's id-1 run must not touch alice's id-1 run"