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.

10.4 KB
Raw Blame
"""Unit tests for the WebSocket connect-time authentication gate.

Parity with main's Flask-SocketIO handler: unauthenticated sockets must be
rejected at the handshake (connect returns False). The FastAPI port
initially accepted them, which handed every roomless broadcast — e.g.
parallel_search_started, carrying the searching user's query text — to
clients that never logged in. These tests pin the gate's branches with the
session-cookie decoding and DB seams patched.
"""

import asyncio
import threading
from pathlib import Path
from unittest.mock import AsyncMock, Mock, patch

import pytest

from local_deep_research.database.rekey_lock import (
    RekeySidecarIOError,
    RekeySidecarOperation,
)
from local_deep_research.web.dependencies.threadpool import (
    run_db_sync as real_run_db_sync,
)
from local_deep_research.web.services import socketio_asgi
from local_deep_research.web.services.socketio_asgi import connect

MOD = "local_deep_research.web.services.socketio_asgi"


@pytest.fixture
def sid_users():
    """Fresh _sid_users + a usable lock for this test, restored after."""
    saved_users = dict(socketio_asgi._sid_users)
    saved_lock = socketio_asgi._lock
    socketio_asgi._sid_users.clear()
    socketio_asgi._lock = asyncio.Lock()
    yield socketio_asgi._sid_users
    socketio_asgi._sid_users.clear()
    socketio_asgi._sid_users.update(saved_users)
    socketio_asgi._lock = saved_lock


def _connect(
    session_data, db_manager=None, password=None, session_valid_for="alice"
):
    """Run the async connect handler with the decode/db seams patched.

    ``session_valid_for`` stubs the server-side session lookup the handshake
    now performs (the revocation gate). These tests are about the DB/password
    seams, so the session is treated as live unless a test says otherwise;
    the gate itself has dedicated coverage in
    test_socket_connect_session_gate.py.
    """
    dbm = db_manager or Mock()
    store = Mock()
    store.get_session_password.return_value = password
    with (
        patch(f"{MOD}._decode_session_cookie", return_value=session_data),
        patch(
            "local_deep_research.web.auth.session_manager.session_manager"
            ".validate_session",
            return_value=session_valid_for,
        ),
        patch("local_deep_research.database.encrypted_db.db_manager", dbm),
        patch(
            "local_deep_research.database.session_passwords"
            ".session_password_store",
            store,
        ),
    ):
        return asyncio.run(connect("sid-1", {"HTTP_COOKIE": "session=x"})), (
            dbm,
            store,
        )


class TestConnectGate:
    def test_unauthenticated_connection_is_rejected(self, sid_users):
        """No username in the session cookie → handshake refused."""
        result, _ = _connect(session_data=None)

        assert result is False
        assert sid_users == {}

    def test_session_without_username_is_rejected(self, sid_users):
        result, _ = _connect(session_data={"session_id": "s1"})

        assert result is False
        assert sid_users == {}

    def test_authenticated_user_with_open_db_is_accepted(self, sid_users):
        dbm = Mock()
        dbm.is_user_connected.return_value = True

        result, _ = _connect(
            session_data={"username": "alice", "session_id": "s1"},
            db_manager=dbm,
        )

        assert result is True
        assert sid_users == {"sid-1": "alice"}
        dbm.open_user_database.assert_not_called()

    def test_closed_db_without_stored_password_is_rejected(self, sid_users):
        """Valid cookie but no session password (e.g. store expired) —
        the socket could never pass the subscribe ownership check, and
        main rejected exactly this case."""
        dbm = Mock()
        dbm.is_user_connected.return_value = False

        result, _ = _connect(
            session_data={"username": "alice", "session_id": "s1"},
            db_manager=dbm,
            password=None,
        )

        assert result is False
        assert sid_users == {}

    def test_closed_db_is_lazily_opened_with_session_password(self, sid_users):
        dbm = Mock()
        dbm.is_user_connected.return_value = False

        result, (dbm, store) = _connect(
            session_data={"username": "alice", "session_id": "s1"},
            db_manager=dbm,
            password="pw",
        )

        assert result is True
        dbm.open_user_database.assert_called_once_with("alice", "pw")
        store.get_session_password.assert_called_once_with("alice", "s1")
        assert sid_users == {"sid-1": "alice"}

    def test_lazy_db_open_sweeps_session_on_executor_worker(self, sid_users):
        """Regression test for reverting run_db_sync to bare asyncio.to_thread.

        A cold open can create a thread-local DB session during phase-two rekey.
        The connect path must sweep that session on the same pooled executor
        worker before returning, rather than leave it attached for a later user.

        Premise updated for the probe offload: the handshake now makes TWO
        run_db_sync calls when it lazily opens — is_user_connected first, then
        open_user_database — and run_db_sync sweeps its worker after EVERY
        call, so a second sweep boundary (the probe's worker) is correct new
        behavior, not a leak: the probe itself routes through the per-user
        lifecycle lock and rekey sidecar and can leave thread-local state on
        its worker. What this fence still pins, in call order: each offloaded
        call sweeps on the very worker that ran it, and above all the open's
        session is swept on the SAME thread that ran the open, before the
        handshake returns. (Reverting run_db_sync to bare asyncio.to_thread
        still fails: no sweep happens at all.)
        """
        dbm = Mock()
        probe_threads = []
        open_threads = []
        cleanup_threads = []

        def record_probe_thread(username):
            probe_threads.append(threading.get_ident())
            return False

        def record_open_thread(username, password):
            open_threads.append(threading.get_ident())

        dbm.is_user_connected.side_effect = record_probe_thread
        dbm.open_user_database.side_effect = record_open_thread

        with patch(
            "local_deep_research.database.thread_local_session.cleanup_current_thread",
            side_effect=lambda: cleanup_threads.append(threading.get_ident()),
        ):
            result, _ = _connect(
                session_data={"username": "alice", "session_id": "s1"},
                db_manager=dbm,
                password="pw",
            )

        assert result is True
        # Probe first, then the open — and each sweep lands on the worker
        # that ran its own call (executor may reuse one worker for both,
        # hence list equality rather than a thread-uniqueness claim).
        assert cleanup_threads == probe_threads + open_threads
        # THE fence: the open's thread-local session is swept on the same
        # executor worker that ran the open, before connect() returns.
        assert cleanup_threads[-1] == open_threads[0]
        # No sweep (and so no open) ever ran on the event-loop thread.
        assert threading.get_ident() not in cleanup_threads

    def test_lazy_db_open_failure_rejects_connection(self, sid_users):
        dbm = Mock()
        dbm.is_user_connected.return_value = False
        dbm.open_user_database.side_effect = RuntimeError("cipher error")

        result, _ = _connect(
            session_data={"username": "alice", "session_id": "s1"},
            db_manager=dbm,
            password="pw",
        )

        assert result is False
        assert sid_users == {}


def _sidecar_io_error() -> RekeySidecarIOError:
    """A fault the rekey sidecar can really produce (fd exhaustion)."""
    return RekeySidecarIOError(
        Path("alice.db"), RekeySidecarOperation.ACQUIRE, 23
    )


class TestConnectProbeOffloadAndGuard:
    """The is_user_connected probe must not run on the event loop.

    Since the rekey-serialization work, the probe routes through
    ``_acquire_current_connection_generation``: per-user lifecycle lock +
    cross-process sidecar (filesystem I/O, blocking acquire). Called
    directly inside ``async def connect`` — a single-worker deployment —
    a rekey in progress would stall every user's sockets and async
    routes for the rekey's duration, and a sidecar fault (not an OSError)
    would raise straight out of the handshake. The probe therefore has to
    travel through ``run_db_sync`` like the adjacent open_user_database
    call, and a ``RekeyOperationalError`` has to degrade to "not
    connected" (fail closed) rather than escape.
    """

    def test_probe_runs_off_the_event_loop_via_run_db_sync(self, sid_users):
        dbm = Mock()
        dbm.is_user_connected.return_value = True

        spy = AsyncMock(wraps=real_run_db_sync)
        with patch(
            "local_deep_research.web.dependencies.threadpool.run_db_sync",
            spy,
        ):
            result, _ = _connect(
                session_data={"username": "alice", "session_id": "s1"},
                db_manager=dbm,
            )

        assert result is True
        # Connected, so the probe is the ONLY run_db_sync call the
        # handshake makes — one await whose first arg is the probe.
        spy.assert_awaited_once()
        fn, arg = spy.await_args.args
        # Identity, not __name__: production hands run_db_sync the
        # manager's ``is_user_connected`` bound method unwrapped, but this
        # test stands the manager in as a Mock, and a Mock child has no
        # ``__name__`` (Mock refuses dunder attribute auto-creation) —
        # so a name assertion can only ever see ''. Asserting identity
        # is the mock analog of asserting the real bound method was
        # passed, and also catches a wrapped/partial probe.
        assert fn is dbm.is_user_connected
        assert arg == "alice"

    def test_sidecar_fault_on_probe_rejects_instead_of_raising(self, sid_users):
        """A raising probe must fail closed through the no-connection
        path, not blow up the handshake (or the event loop)."""
        dbm = Mock()
        dbm.is_user_connected.side_effect = _sidecar_io_error()

        result, _ = _connect(
            session_data={"username": "alice", "session_id": "s1"},
            db_manager=dbm,
            password=None,
        )

        assert result is False
        assert sid_users == {}