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.

6.3 KB
Raw Blame
"""One user's progress must never reach another user's socket.

Benchmark ids come from ``BenchmarkRun.id``, which autoincrements inside each
user's own encrypted database — so every user's first benchmark run is id 1.
The per-research subscription map used to be keyed by that bare id, and the
socket layer's ownership check could not catch it: ``_owns_research_sync``
asks "does THIS user own this id in THEIR database?", and for two different
users' run 1 the honest answer is yes for both. Both sids therefore landed in
``_subscriptions["1"]`` and one user's benchmark progress was emitted to the
other's browser.

That shape — a correct per-user check plus a process-global dict keyed by a
per-user id — is ADR-0009's trap, and it is invisible in either file alone:
the handler looks right, and the dict has no idea where its keys came from.
Four separate review passes found it, none by reading a single file.

These tests pin the two properties that make it impossible by construction:
delivery is scoped to the owner, and an emit that cannot name an owner
reaches nobody rather than everybody.
"""

import asyncio

import pytest

from local_deep_research.web.services import socketio_asgi as sio_mod


@pytest.fixture
def socket_state(monkeypatch):
    """Isolated subscription state plus a capturing ``sio.emit``."""
    sio_mod.init_lock()
    monkeypatch.setattr(sio_mod, "_subscriptions", {})
    monkeypatch.setattr(sio_mod, "_sid_users", {})

    delivered = []

    async def _capture(event, data, room=None):
        delivered.append((room, event, data))

    monkeypatch.setattr(sio_mod.sio, "emit", _capture)
    return delivered


def _subscribe(username, research_id, sid):
    sio_mod._sid_users[sid] = username
    # Build the key the way production does. #5600's numeric-id
    # normalization means a benchmark id seeded as "1" would otherwise
    # never be found by an emit that resolves it from the DB as 1.
    sio_mod._subscriptions.setdefault(
        sio_mod._subscription_key(username, research_id), set()
    ).add(sid)


def test_two_users_same_run_id_do_not_see_each_other(socket_state):
    """The original defect, as reproduced: Alice and Bob both hold run 1."""
    _subscribe("alice", "1", "sid-alice")
    _subscribe("bob", "1", "sid-bob")

    asyncio.run(
        sio_mod._async_emit_to_subscribers(
            "research_progress", "1", {"secret": "alice-only"}, "alice"
        )
    )

    rooms = [room for room, _, _ in socket_state]
    assert rooms == ["sid-alice"]
    assert "sid-bob" not in rooms


def test_each_owner_receives_their_own_run(socket_state):
    """Scoping must not break the normal case for either user."""
    _subscribe("alice", "1", "sid-alice")
    _subscribe("bob", "1", "sid-bob")

    asyncio.run(
        sio_mod._async_emit_to_subscribers(
            "research_progress", "1", {"whose": "bob"}, "bob"
        )
    )

    assert [room for room, _, _ in socket_state] == ["sid-bob"]
    assert socket_state[0][2] == {"whose": "bob"}


def test_unknown_owner_reaches_nobody(socket_state):
    """Fail closed. A caller that loses track of whose research it is
    emitting must deliver nothing — the alternative is the original leak."""
    _subscribe("alice", "1", "sid-alice")
    _subscribe("bob", "1", "sid-bob")

    asyncio.run(
        sio_mod._async_emit_to_subscribers(
            "research_progress", "1", {"secret": "x"}, "nobody"
        )
    )

    assert socket_state == []


def test_emit_to_subscribers_requires_owner():
    """``owner`` is keyword-only and required so a call site that forgets it
    raises here instead of silently reverting to an id-only lookup."""
    with pytest.raises(TypeError):
        sio_mod.emit_to_subscribers("research_progress", "1", {})


def test_removal_is_scoped_to_the_owner(socket_state):
    """One user's run finishing must not tear down another user's
    subscription to their own run of the same numeric id."""
    _subscribe("alice", "1", "sid-alice")
    _subscribe("bob", "1", "sid-bob")

    asyncio.run(sio_mod._async_remove_subscriptions("1", "alice"))

    # Same normalization as the seed: "1" is stored as ("owner", 1).
    assert sio_mod._subscription_key("alice", "1") not in sio_mod._subscriptions
    assert sio_mod._subscriptions[sio_mod._subscription_key("bob", "1")] == {
        "sid-bob"
    }


def test_disconnect_drops_the_sid_from_every_owner_entry(socket_state):
    """A sid may hold subscriptions under its own user only, but the sweep
    must not assume the key shape and skip cleanup."""
    _subscribe("alice", "1", "sid-alice")
    _subscribe("alice", "2", "sid-alice")

    asyncio.run(sio_mod.disconnect("sid-alice"))

    assert sio_mod._subscriptions == {}
    assert "sid-alice" not in sio_mod._sid_users


def test_string_and_int_benchmark_ids_are_one_subscription(socket_state):
    """Subscribing with "1" and emitting with 1 must hit the SAME bucket.

    Ported from upstream #5600's
    ``test_string_and_int_benchmark_ids_are_one_subscription``, which had no
    equivalent here. It is the assertion that stops someone "simplifying"
    ``_subscription_key`` by dropping the ``int()`` cast.

    Why it matters: a subscribe arrives from a JSON socket payload, so a
    benchmark run comes in as the STRING "1"; the matching emit resolves the
    id from the database, where it is the INTEGER 1. Keyed raw those are two
    different dict keys, so the subscriber is correctly registered and then
    silently never delivered to -- a failure with no error anywhere.

    UUID research ids are left untouched, which the second half asserts.
    """
    # Subscribe as the wire does: a string.
    _subscribe("alice", "1", "sid-alice")

    # Emit as the database does: an int. Same bucket, or the event is lost.
    assert sio_mod._subscriptions[sio_mod._subscription_key("alice", 1)] == {
        "sid-alice"
    }
    assert (
        sio_mod._subscription_key("alice", "1")
        == sio_mod._subscription_key("alice", 1)
        == ("alice", 1)
    )

    # Control: the normalization must NOT collapse different owners...
    assert sio_mod._subscription_key("bob", 1) != sio_mod._subscription_key(
        "alice", 1
    )
    # ...nor touch a UUID research id, which is globally unique already.
    uuid_id = "0f9c2f1e-1c2a-4a5b-9d3e-2b6a7c8d9e01"
    assert sio_mod._subscription_key("alice", uuid_id) == ("alice", uuid_id)