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.

22.2 KB
Raw Blame
"""Regression tests for WebSocket session validation.

Before the connect-time session gate was ported, the handshake checked only
that the cookie signature was valid and that
``db_manager.is_user_connected(username)`` was true. Neither establishes that
the originating session is still active.

* The signature only proves *we* issued the cookie, not that it is current.
* ``is_user_connected`` is username-scoped, so it is true whenever ANY of that
  user's sessions has the database open -- a second device, or an in-flight
  research run. Logout deliberately leaves the DB open while research is
  running, so this is a normal state, not a corner case.

The #5535 connect-time gate was therefore ported to validate the server-side
session before registering a socket. These tests preserve that corrected
behavior for logout, password change, and expiry cases.

Nothing else covers it: ``DatabaseMiddleware``'s ``_enforce_session_revocation``
returns early for non-HTTP scopes AND skips the ``/ws/`` prefix, so the HTTP
revocation work does not reach the socket path at all.

These tests are marked ``real_session_check`` so the autouse
``_legacy_bare_username_auth`` shim in tests/conftest.py cannot relax the gate
they exist to prove.
"""

import asyncio
from unittest.mock import AsyncMock, patch

import pytest

from local_deep_research.web.auth.session_manager import session_manager
from local_deep_research.web.services import socketio_asgi as sio_mod

pytestmark = pytest.mark.real_session_check

USER = "ws_gate_user"
_LOOP_HANDOFF_TIMEOUT = 5


def _connect(session_payload, sid="sid-test"):
    """Drive the real connect() handler with a decoded cookie payload.

    ``is_user_connected`` is forced True throughout: that is the state that
    made the old gate pass, so leaving it true is what keeps these tests
    honest about what is actually doing the rejecting.
    """
    sio_mod.init_lock()
    with (
        patch.object(
            sio_mod, "_decode_session_cookie", return_value=session_payload
        ),
        patch(
            "local_deep_research.database.encrypted_db.db_manager"
            ".is_user_connected",
            return_value=True,
        ),
    ):
        return asyncio.run(
            sio_mod.connect(sid, {"HTTP_COOKIE": "session=whatever"})
        )


@pytest.fixture(autouse=True)
def _clean_socket_state(monkeypatch):
    monkeypatch.setattr(sio_mod, "_sid_users", {})
    monkeypatch.setattr(sio_mod, "_sid_sessions", {})
    # set_main_loop() assigns directly rather than through monkeypatch. Record
    # both process-global loop primitives here so every test restores them.
    monkeypatch.setattr(sio_mod, "_main_loop", sio_mod._main_loop)
    monkeypatch.setattr(sio_mod, "_lock", sio_mod._lock)


def test_live_session_can_still_connect():
    """The gate must not break the normal case."""
    sid_token = session_manager.create_session(USER, remember_me=False)
    try:
        assert _connect({"username": USER, "session_id": sid_token}) is True
        assert sio_mod._sid_users == {"sid-test": USER}
        assert sio_mod._sid_sessions == {"sid-test": sid_token}
    finally:
        session_manager.destroy_session(sid_token)


def test_destroyed_session_cannot_connect():
    """Historical regression case: a destroyed session at handshake."""
    sid_token = session_manager.create_session(USER, remember_me=False)
    session_manager.destroy_session(sid_token)

    assert _connect({"username": USER, "session_id": sid_token}) is False
    assert sio_mod._sid_users == {}, "a revoked session registered a socket"


def test_forged_session_id_cannot_connect():
    assert _connect({"username": USER, "session_id": "never-issued"}) is False
    assert sio_mod._sid_users == {}


def test_cookie_without_session_id_cannot_connect():
    """A signed cookie carrying only a username is not enough -- that was
    precisely the pre-fix acceptance condition."""
    assert _connect({"username": USER}) is False
    assert sio_mod._sid_users == {}


def test_session_belonging_to_another_user_cannot_connect():
    """The stored session must match the username the cookie claims."""
    other = session_manager.create_session("someone_else", remember_me=False)
    try:
        assert _connect({"username": USER, "session_id": other}) is False
        assert sio_mod._sid_users == {}
    finally:
        session_manager.destroy_session(other)


def test_gate_is_not_satisfied_by_is_user_connected_alone():
    """Pin the reasoning, not just the outcome: with is_user_connected forced
    True (a user with another device or a running research), a destroyed
    session must still be refused. If someone later reorders the checks so the
    connection short-circuits on is_user_connected, this fails."""
    sid_token = session_manager.create_session(USER, remember_me=False)
    session_manager.destroy_session(sid_token)

    with patch.object(
        session_manager, "validate_session", return_value=None
    ) as validate:
        result = _connect({"username": USER, "session_id": sid_token})

    assert result is False
    assert validate.called, (
        "connect() never consulted the server-side session store"
    )


class TestSubscribeRevalidation:
    """The connect gate is not enough on its own.

    Identity is captured at handshake and frozen for the socket's lifetime, so
    a session that idle-expires while the socket is open leaves it able to
    subscribe to research it had not subscribed to before. Logout and password
    change disconnect actively. Idle expiry can otherwise have bounded
    revocation latency until the periodic sweep, so subscribe revalidates the
    session before accepting new work.
    """

    def test_dead_session_severs_every_socket_of_that_session(
        self, monkeypatch
    ):
        """Not just the socket that asked.

        ``validate_session`` DELETES an expired session as a side effect of
        checking it. If only the calling sid is disconnected, a sibling socket
        on the same session stays connected AND becomes permanently
        un-revocable -- the idle sweep can never find that session again --
        so it keeps receiving the user's events indefinitely.
        """
        sio_mod.init_lock()
        monkeypatch.setattr(sio_mod, "_sid_users", {})
        monkeypatch.setattr(sio_mod, "_sid_sessions", {})

        disconnected = []

        async def _disconnect(sid, *a, **k):
            disconnected.append(sid)

        async def _emit(*a, **k):
            pass

        monkeypatch.setattr(sio_mod.sio, "disconnect", _disconnect)
        monkeypatch.setattr(sio_mod.sio, "emit", _emit)

        token = session_manager.create_session(USER, remember_me=False)
        for sid in ("tab-1", "tab-2"):
            sio_mod._sid_users[sid] = USER
            sio_mod._sid_sessions[sid] = token
        session_manager.destroy_session(token)

        async def _run():
            sio_mod.set_main_loop(asyncio.get_running_loop())
            with patch.object(
                sio_mod, "_user_owns_research", return_value=True
            ):
                await sio_mod.on_subscribe("tab-1", {"research_id": "r-1"})
            await asyncio.sleep(0.05)

        asyncio.run(_run())

        assert "tab-1" in disconnected
        assert "tab-2" in disconnected, (
            "sibling socket on the same dead session was orphaned"
        )
        assert sio_mod._subscriptions == {}

    def test_live_session_can_still_subscribe(self, monkeypatch):
        """The re-check must not break the normal case."""
        sio_mod.init_lock()
        monkeypatch.setattr(sio_mod, "_sid_users", {})
        monkeypatch.setattr(sio_mod, "_sid_sessions", {})
        monkeypatch.setattr(sio_mod, "_subscriptions", {})

        async def _emit(*a, **k):
            pass

        monkeypatch.setattr(sio_mod.sio, "emit", _emit)

        token = session_manager.create_session(USER, remember_me=False)
        try:
            sio_mod._sid_users["tab-1"] = USER
            sio_mod._sid_sessions["tab-1"] = token

            async def _run():
                sio_mod.set_main_loop(asyncio.get_running_loop())
                with patch.object(
                    sio_mod, "_user_owns_research", return_value=True
                ):
                    await sio_mod.on_subscribe("tab-1", {"research_id": "r-1"})

            asyncio.run(_run())
            assert sio_mod._subscriptions == {(USER, "r-1"): {"tab-1"}}
        finally:
            session_manager.destroy_session(token)

    def test_dead_session_is_evicted_even_when_error_frame_delivery_fails(
        self, monkeypatch
    ):
        """A broken transport cannot short-circuit session revocation."""
        token = session_manager.create_session(USER, remember_me=False)
        monkeypatch.setattr(sio_mod, "_lock", asyncio.Lock())
        monkeypatch.setattr(
            sio_mod,
            "_sid_users",
            {"tab-1": USER, "tab-2": USER},
        )
        monkeypatch.setattr(
            sio_mod,
            "_sid_sessions",
            {"tab-1": token, "tab-2": token},
        )
        monkeypatch.setattr(sio_mod, "_subscriptions", {})
        disconnected = []
        ownership = AsyncMock(return_value=True)
        failing_emit = AsyncMock(
            side_effect=RuntimeError("socket cannot receive error frame")
        )
        session_manager.destroy_session(token)

        async def _run():
            all_disconnected = asyncio.Event()

            async def _disconnect(sid, *args, **kwargs):
                disconnected.append(sid)
                if len(disconnected) == 2:
                    all_disconnected.set()

            monkeypatch.setattr(sio_mod.sio, "emit", failing_emit)
            monkeypatch.setattr(sio_mod.sio, "disconnect", _disconnect)
            sio_mod.set_main_loop(asyncio.get_running_loop())
            with patch.object(sio_mod, "_user_owns_research", ownership):
                await sio_mod.on_subscribe(
                    "tab-1", {"research_id": "r-expired"}
                )
            await asyncio.wait_for(
                all_disconnected.wait(), timeout=_LOOP_HANDOFF_TIMEOUT
            )

        asyncio.run(_run())

        ownership.assert_not_awaited()
        failing_emit.assert_awaited_once_with(
            "subscribe_error",
            {"error": "Session expired", "research_id": "r-expired"},
            room="tab-1",
        )
        assert set(disconnected) == {"tab-1", "tab-2"}


class TestUnsubscribeRevalidation:
    """Unsubscribe is also a session-validity boundary."""

    def test_unauthenticated_sid_cannot_reach_ownership_or_bookkeeping(
        self, monkeypatch
    ):
        research_id = "r-unauthenticated"
        existing = {(USER, research_id): {"another-tab"}}
        monkeypatch.setattr(sio_mod, "_lock", asyncio.Lock())
        monkeypatch.setattr(
            sio_mod,
            "_subscriptions",
            {(USER, research_id): {"another-tab"}},
        )
        ownership = AsyncMock(return_value=True)

        with patch.object(sio_mod, "_user_owns_research", ownership):
            asyncio.run(
                sio_mod.on_unsubscribe(
                    "unknown-tab", {"research_id": research_id}
                )
            )

        ownership.assert_not_awaited()
        assert sio_mod._subscriptions == existing

    def test_dead_session_unsubscribe_severs_every_sibling_socket(
        self, monkeypatch
    ):
        """Eviction must use the recorded session, not only the caller sid.

        Session validation deletes an expired token as a side effect.  If the
        unsubscribe handler disconnected only its caller, a sibling tab would
        become permanently invisible to the periodic session sweep and keep
        receiving events.
        """
        research_id = "r-expired-unsubscribe"
        token = session_manager.create_session(USER, remember_me=False)
        monkeypatch.setattr(sio_mod, "_lock", asyncio.Lock())
        monkeypatch.setattr(
            sio_mod,
            "_sid_users",
            {"tab-1": USER, "tab-2": USER},
        )
        monkeypatch.setattr(
            sio_mod,
            "_sid_sessions",
            {"tab-1": token, "tab-2": token},
        )
        existing = {(USER, research_id): {"tab-1", "tab-2"}}
        monkeypatch.setattr(
            sio_mod,
            "_subscriptions",
            {(USER, research_id): {"tab-1", "tab-2"}},
        )
        disconnected = []
        ownership = AsyncMock(return_value=True)
        session_manager.destroy_session(token)

        async def _run():
            all_disconnected = asyncio.Event()

            async def _disconnect(sid, *args, **kwargs):
                disconnected.append(sid)
                if len(disconnected) == 2:
                    all_disconnected.set()

            monkeypatch.setattr(sio_mod.sio, "disconnect", _disconnect)
            sio_mod.set_main_loop(asyncio.get_running_loop())
            with patch.object(sio_mod, "_user_owns_research", ownership):
                await sio_mod.on_unsubscribe(
                    "tab-1", {"research_id": research_id}
                )
            await asyncio.wait_for(
                all_disconnected.wait(), timeout=_LOOP_HANDOFF_TIMEOUT
            )

        asyncio.run(_run())

        ownership.assert_not_awaited()
        assert set(disconnected) == {"tab-1", "tab-2"}
        assert sio_mod._subscriptions == existing


class TestStaleIdentityWithoutRecordedSession:
    """Fail closed when identity survives but its handshake session does not.

    This defensive state represents an identity left by code predating the
    ``_sid_sessions`` connect gate.  There is no session id to pass to
    ``disconnect_session``, so both handlers must sever the calling sid
    directly and return before authorization or subscription bookkeeping.
    """

    def test_subscribe_disconnects_directly_without_authorizing_or_mutating(
        self, monkeypatch
    ):
        sid = "legacy-tab"
        research_id = "r-legacy"
        key = (USER, research_id)
        expected = {key: {"existing-tab"}}

        monkeypatch.setattr(sio_mod, "_lock", asyncio.Lock())
        monkeypatch.setattr(sio_mod, "_subscriptions", {key: {"existing-tab"}})
        sio_mod._sid_users[sid] = USER
        assert sid not in sio_mod._sid_sessions

        ownership = AsyncMock(return_value=True)
        disconnected = []
        emitted = []

        async def _disconnect(disconnected_sid, *args, **kwargs):
            disconnected.append(disconnected_sid)
            raise RuntimeError("transport already closed")

        async def _emit(event, data=None, room=None, **kwargs):
            emitted.append((room, event, data))

        with (
            patch.object(sio_mod, "_user_owns_research", ownership),
            patch.object(sio_mod.sio, "disconnect", _disconnect),
            patch.object(sio_mod.sio, "emit", _emit),
        ):
            # The direct-disconnect failure is deliberately contained by the
            # production handler; reaching the assertions proves it escaped
            # neither the handler nor asyncio.run().
            asyncio.run(sio_mod.on_subscribe(sid, {"research_id": research_id}))

        ownership.assert_not_awaited()
        assert sio_mod._subscriptions == expected
        assert disconnected == [sid]
        assert emitted == [
            (
                sid,
                "subscribe_error",
                {"error": "Session expired", "research_id": research_id},
            )
        ]

    def test_unsubscribe_disconnects_directly_without_authorizing_or_mutating(
        self, monkeypatch
    ):
        sid = "legacy-tab"
        research_id = "r-legacy"
        key = (USER, research_id)
        expected = {key: {sid, "existing-tab"}}

        monkeypatch.setattr(sio_mod, "_lock", asyncio.Lock())
        monkeypatch.setattr(
            sio_mod, "_subscriptions", {key: {sid, "existing-tab"}}
        )
        sio_mod._sid_users[sid] = USER
        assert sid not in sio_mod._sid_sessions

        ownership = AsyncMock(return_value=True)
        disconnected = []

        async def _disconnect(disconnected_sid, *args, **kwargs):
            disconnected.append(disconnected_sid)
            raise RuntimeError("transport already closed")

        with (
            patch.object(sio_mod, "_user_owns_research", ownership),
            patch.object(sio_mod.sio, "disconnect", _disconnect),
        ):
            asyncio.run(
                sio_mod.on_unsubscribe(sid, {"research_id": research_id})
            )

        ownership.assert_not_awaited()
        assert sio_mod._subscriptions == expected
        assert disconnected == [sid]


class TestConnectTeardownRace:
    """The connect-time gate is not atomic with registration.

    Ported from main's #5572, which fixed this in
    ``web/services/socket_service.py``. At the migration-review snapshot the
    replacement had no successor for that check; this class preserves the
    restored post-registration revalidation.

    ``disconnect_session`` severs a session's sockets by enumerating
    ``_sid_sessions``. If a concurrent logout enumerated that dict BEFORE
    ``connect`` registered this sid, the teardown never sees the socket, and
    it stays registered against a dead session -- still receiving that user's
    events, including ``settings_changed``, which carries plaintext secrets.

    ``connect`` therefore re-validates AFTER registering and fails closed.
    """

    def test_session_invalidated_during_registration_is_rejected(self):
        """The race itself: the gate passes, then the session dies before the
        post-registration re-check."""
        sid_token = session_manager.create_session(USER, remember_me=False)
        calls = {"n": 0}
        real_validate = session_manager.validate_session

        def racing_validate(session_id):
            # Pass the connect gate, then behave as though a concurrent
            # logout destroyed the session while this socket was registering.
            calls["n"] += 1
            if calls["n"] == 1:
                return real_validate(session_id)
            return None

        try:
            with patch.object(
                session_manager, "validate_session", side_effect=racing_validate
            ):
                result = _connect({"username": USER, "session_id": sid_token})

            assert result is False, (
                "socket survived a mid-registration teardown"
            )
            assert sio_mod._sid_users == {}, (
                "a socket whose session died while registering stayed "
                "registered, so disconnect_session can never reach it"
            )
            assert sio_mod._sid_sessions == {}
            assert calls["n"] >= 2, (
                "connect() never re-validated after registering -- the race "
                "window is still open"
            )
        finally:
            session_manager.destroy_session(sid_token)

    def test_no_race_keeps_the_socket_registered(self):
        """The re-check must not break the normal path."""
        sid_token = session_manager.create_session(USER, remember_me=False)
        try:
            assert _connect({"username": USER, "session_id": sid_token}) is True
            assert sio_mod._sid_users == {"sid-test": USER}
            assert sio_mod._sid_sessions == {"sid-test": sid_token}
        finally:
            session_manager.destroy_session(sid_token)


class TestSocketActivityRefreshesSession:
    """Watching a run over the socket must count as activity.

    Second half of main's #5535 (778688295). At the migration-review snapshot,
    the disconnect helpers had been ported but activity refresh had not, and
    ``touch_session`` had no call sites. The test below preserves the restored
    call. The consequence is not cosmetic: a user who starts a long
    research and then only watches the progress UI issues no HTTP requests,
    so nothing refreshes their idle timer, the session is reaped mid-run,
    and the idle sweep then disconnects the socket -- the run appears to die
    in front of them.
    """

    def test_emit_refreshes_the_watching_session(self, monkeypatch):
        sio_mod.init_lock()
        monkeypatch.setattr(sio_mod, "_sid_users", {})
        monkeypatch.setattr(sio_mod, "_sid_sessions", {})
        monkeypatch.setattr(sio_mod, "_subscriptions", {})

        async def _emit(*a, **k):
            pass

        monkeypatch.setattr(sio_mod.sio, "emit", _emit)

        token = session_manager.create_session(USER, remember_me=False)
        try:
            sio_mod._sid_users["watcher"] = USER
            sio_mod._sid_sessions["watcher"] = token
            sio_mod._subscriptions[(USER, "r-1")] = {"watcher"}

            touched = []
            real_touch = session_manager.touch_session
            monkeypatch.setattr(
                session_manager,
                "touch_session",
                lambda sid: (touched.append(sid), real_touch(sid))[1],
            )

            asyncio.run(
                sio_mod._async_emit_to_subscribers(
                    "research_progress", "r-1", {"p": 1}, owner=USER
                )
            )

            assert touched == [token], (
                "socket activity did not refresh the watcher's session, so a "
                "long run watched only over the socket will idle-expire"
            )
        finally:
            session_manager.destroy_session(token)

    def test_emit_still_delivers_when_the_refresh_fails(self, monkeypatch):
        """A refresh failure must not strand delivery to other subscribers --
        the reason main wrapped this in its own try/except."""
        sio_mod.init_lock()
        monkeypatch.setattr(sio_mod, "_sid_users", {})
        monkeypatch.setattr(sio_mod, "_sid_sessions", {})
        monkeypatch.setattr(sio_mod, "_subscriptions", {})

        delivered = []

        async def _emit(event, data, room=None, **k):
            delivered.append(room)

        monkeypatch.setattr(sio_mod.sio, "emit", _emit)

        def _boom(_sid):
            raise RuntimeError("session store unavailable")

        monkeypatch.setattr(session_manager, "touch_session", _boom)

        sio_mod._sid_sessions["a"] = "tok-a"
        sio_mod._sid_sessions["b"] = "tok-b"
        sio_mod._subscriptions[(USER, "r-1")] = {"a", "b"}

        asyncio.run(
            sio_mod._async_emit_to_subscribers(
                "research_progress", "r-1", {"p": 1}, owner=USER
            )
        )

        assert sorted(delivered) == ["a", "b"]