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.

17.4 KB
Raw Blame
"""Real websocket-*transport* coverage for socketio_asgi.py.

Every existing Socket.IO test in this suite --
test_socketio_handshake_auth.py, test_socketio_connect_gate.py,
test_socketio_asgi_user_scoping.py, tests/web/test_socketio_asgi_contracts.py
-- drives the Engine.IO *polling* transport (``transport=polling``)
end-to-end, or calls the async handlers (``connect``, ``on_subscribe``,
...) directly. As of this file, ``websocket_connect`` and
``transport=websocket`` had zero hits anywhere under ``tests/`` --  i.e.
the transport every real browser client actually negotiates
(``services/socket.js`` uses Socket.IO's default transport upgrade to
WebSocket) had no coverage running through the real wire at all.

connect()/on_subscribe()/on_unsubscribe() are the same Python coroutines
regardless of transport, but the transport upgrade itself -- the
"Upgrade: websocket" handshake and engine.io's frame handling over a
persistent connection instead of discrete polling requests -- is
mechanism that was completely unexercised.

Handshake recipe (Engine.IO v4 protocol; the ``Upgrade``/``Connection``
headers are required -- engine.io otherwise rejects the request with
"Invalid websocket upgrade" before ever reaching ``connect()``)::

    c.cookies.set("session", signed_cookie)
    with c.websocket_connect(
        "/ws/socket.io/?EIO=4&transport=websocket",
        headers={"Upgrade": "websocket", "Connection": "Upgrade"},
    ) as ws:
        ws.receive_text()   # engine.io OPEN packet: '0{"sid": ...}'
        ws.send_text("40")  # Socket.IO CONNECT packet
        ws.receive_text()   # '40{"sid": "..."}' ack, or '44...' refusal

This file has two parts:

1. ``TestHandshakeAcrossBothTransports`` -- the three canonical
   accept/refuse handshake cases that
   test_socketio_handshake_auth.py::TestEndToEndPollingHandshake already
   pins for polling, parametrized across both transports so websocket
   gets the same coverage through the same assertions.
2. ``TestRestoredUnsubscribeOwnershipGateRealTransport`` -- on_unsubscribe
   was restored to require the same ownership check as on_subscribe
   (this branch had dropped it). Drives that gate over the real
   websocket transport specifically, since it is the transport real
   clients use.
"""

import base64
import contextlib
import json
import time
from unittest.mock import MagicMock, Mock, patch

import itsdangerous
import pytest
from fastapi.testclient import TestClient

from local_deep_research.web.fastapi_app import SECRET_KEY, app
from local_deep_research.web.services import socketio_asgi

WS_UPGRADE_HEADERS = {"Upgrade": "websocket", "Connection": "Upgrade"}


def make_session_cookie(
    session_data: dict,
    key: str | bytes | None = None,
    timestamp: int | None = None,
    real_session: bool = True,
) -> str:
    """Build a session cookie value exactly like Starlette's
    SessionMiddleware: base64(json(session)) signed with an
    itsdangerous TimestampSigner over the app SECRET_KEY."""

    # The WebSocket handshake now validates session_id against the
    # server-side store (the revocation gate main's #5535 added and this
    # branch was missing). These helpers used to fabricate a session_id
    # like "s1", which no longer authenticates -- correctly. Mint a REAL
    # session for the username so these tests exercise the gate instead
    # of depending on its absence. Pass real_session=False to build a
    # deliberately-unauthenticated cookie.
    if real_session and session_data.get("username"):
        from local_deep_research.web.auth.session_manager import (
            session_manager,
        )

        session_data = dict(session_data)
        session_data["session_id"] = session_manager.create_session(
            session_data["username"]
        )

    payload = base64.b64encode(json.dumps(session_data).encode("utf-8"))
    signer = itsdangerous.TimestampSigner(
        key if key is not None else SECRET_KEY
    )
    if timestamp is not None:
        signer.get_timestamp = lambda: timestamp  # type: ignore[method-assign]
    return signer.sign(payload).decode("utf-8")


def _connected_db_manager() -> Mock:
    dbm = Mock()
    dbm.is_user_connected.return_value = True
    return dbm


def _wait_until(
    predicate, timeout: float = 2.0, interval: float = 0.02
) -> bool:
    """Poll `predicate` until truthy or `timeout` elapses.

    on_subscribe/on_unsubscribe run as coroutines on the ASGI app's own
    background event-loop thread (driven by TestClient's websocket
    portal) and, on their success paths, send no reply packet -- there
    is nothing to block a `receive_text()` on. Polling module state from
    the test thread is the only externally observable completion signal
    available here.
    """
    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        if predicate():
            return True
        time.sleep(interval)
    return predicate()


@pytest.fixture(autouse=True)
def _isolated_socketio_state():
    """Snapshot/restore module state so tests never see another test's
    leftover sids/subscriptions, and _lock is fresh for each test's
    event loop."""
    saved_users = dict(socketio_asgi._sid_users)
    saved_subs = {k: set(v) for k, v in socketio_asgi._subscriptions.items()}
    saved_lock = socketio_asgi._lock
    socketio_asgi._sid_users.clear()
    socketio_asgi._subscriptions.clear()
    socketio_asgi._lock = None
    yield
    socketio_asgi._sid_users.clear()
    socketio_asgi._sid_users.update(saved_users)
    socketio_asgi._subscriptions.clear()
    socketio_asgi._subscriptions.update(saved_subs)
    socketio_asgi._lock = saved_lock


def _polling_handshake(
    client: TestClient, cookie_value: str | None, on_connected=None
) -> str:
    """Drive one real Engine.IO POLLING handshake through the mounted
    ASGI app. Returns the ack/refusal packet text.

    `on_connected`, if given, is called with the ack text before this
    function returns -- see `_websocket_handshake` for why that timing
    matters."""
    if cookie_value is not None:
        client.cookies.set("session", cookie_value)
    r_open = client.get("/ws/socket.io/?EIO=4&transport=polling")
    assert r_open.status_code == 200
    eio_sid = json.loads(r_open.text[1:])["sid"]
    r_connect = client.post(
        f"/ws/socket.io/?EIO=4&transport=polling&sid={eio_sid}",
        content="40",
        headers={"Content-Type": "text/plain"},
    )
    assert r_connect.status_code == 200
    r_poll = client.get(f"/ws/socket.io/?EIO=4&transport=polling&sid={eio_sid}")
    assert r_poll.status_code == 200
    if on_connected is not None:
        on_connected(r_poll.text)
    return r_poll.text


def _websocket_handshake(
    client: TestClient, cookie_value: str | None, on_connected=None
) -> str:
    """Drive one real Engine.IO WEBSOCKET handshake through the mounted
    ASGI app. Returns the ack/refusal packet text.

    `on_connected`, if given, is called with the ack text BEFORE the
    websocket context exits. This matters for a websocket in a way it
    doesn't for polling: closing the websocket fires the server's
    disconnect() handler synchronously, which pops the sid from
    `_sid_users` -- so a caller that only inspects module state *after*
    this function returns (post-close) would race against that cleanup
    and observe an empty map even on a successful handshake.
    """
    if cookie_value is not None:
        client.cookies.set("session", cookie_value)
    with client.websocket_connect(
        "/ws/socket.io/?EIO=4&transport=websocket", headers=WS_UPGRADE_HEADERS
    ) as ws:
        ws.receive_text()  # engine.io OPEN packet
        ws.send_text("40")
        ack = ws.receive_text()
        if on_connected is not None:
            on_connected(ack)
        return ack


_HANDSHAKE_BY_TRANSPORT = {
    "polling": _polling_handshake,
    "websocket": _websocket_handshake,
}


# ---------------------------------------------------------------------------
# 1. The three canonical handshake outcomes, across both real transports.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("transport", ["polling", "websocket"])
class TestHandshakeAcrossBothTransports:
    """Mirrors test_socketio_handshake_auth.py::TestEndToEndPollingHandshake's
    three cases, but parametrized so ``websocket`` -- the transport real
    clients use and the one with zero prior coverage -- is pinned by the
    exact same assertions as ``polling``."""

    def test_valid_cookie_accepted_and_maps_identity(self, transport):
        socketio_asgi.init_lock()
        dbm = _connected_db_manager()
        cookie = make_session_cookie({"username": "alice", "session_id": "s1"})
        identity_while_connected = {}

        def _snapshot_identity(ack: str) -> None:
            # Must run WHILE the socket is still open: for the websocket
            # transport, closing the connection fires disconnect(), which
            # pops the sid from _sid_users -- checking after the
            # handshake helper returns would race that cleanup.
            if ack.startswith("40"):
                sio_sid = json.loads(ack[2:])["sid"]
                identity_while_connected["sid"] = sio_sid
                identity_while_connected["user"] = socketio_asgi._sid_users.get(
                    sio_sid
                )

        with patch("local_deep_research.database.encrypted_db.db_manager", dbm):
            client = TestClient(app, raise_server_exceptions=False)
            ack = _HANDSHAKE_BY_TRANSPORT[transport](
                client, cookie, on_connected=_snapshot_identity
            )

        assert ack.startswith("40"), f"{transport}: {ack!r}"
        assert identity_while_connected.get("user") == "alice", (
            f"{transport}: sid {identity_while_connected.get('sid')!r} was "
            f"not mapped to 'alice' while the socket was still connected "
            f"(_sid_users snapshot: {identity_while_connected!r})"
        )
        assert dbm.is_user_connected.call_args[0][0] == "alice"

    def test_tampered_cookie_refused(self, transport):
        socketio_asgi.init_lock()
        cookie = make_session_cookie({"username": "alice", "session_id": "s1"})
        suffix = "AAAA" if not cookie.endswith("AAAA") else "BBBB"
        tampered = cookie[:-4] + suffix

        with patch(
            "local_deep_research.database.encrypted_db.db_manager",
            _connected_db_manager(),
        ):
            client = TestClient(app, raise_server_exceptions=False)
            ack = _HANDSHAKE_BY_TRANSPORT[transport](client, tampered)

        assert ack.startswith("44"), f"{transport}: {ack!r}"
        assert socketio_asgi._sid_users == {}

    def test_missing_cookie_refused(self, transport):
        socketio_asgi.init_lock()

        with patch(
            "local_deep_research.database.encrypted_db.db_manager",
            _connected_db_manager(),
        ):
            client = TestClient(app, raise_server_exceptions=False)
            ack = _HANDSHAKE_BY_TRANSPORT[transport](client, None)

        assert ack.startswith("44"), f"{transport}: {ack!r}"
        assert socketio_asgi._sid_users == {}


# ---------------------------------------------------------------------------
# 2. Restored on_unsubscribe ownership gate, driven over the real
#    websocket transport.
# ---------------------------------------------------------------------------


def _patch_ownership(owned_usernames: set):
    """Patch the true DB boundary (get_user_db_session) so ownership
    resolves per-username, and record which usernames the check
    actually ran for.

    The returned call log is the point: it proves the ownership check
    executed at all, which is the property these tests exist to pin. For
    unsubscribe specifically, ``_subscriptions`` staying unchanged is
    NOT sufficient proof the gate ran -- ``subs.discard(sid)`` can only
    ever remove the caller's own sid, so the subscription map looks
    identical whether the (now-restored) gate ran and rejected, or
    whether no gate existed at all. The call log is what distinguishes
    "gate ran and rejected" from "gate doesn't exist".
    """
    captured: list = []

    @contextlib.contextmanager
    def fake(username, *_a, **_kw):
        captured.append(username)
        db = MagicMock()
        row = ("row",) if username in owned_usernames else None
        db.query.return_value.filter_by.return_value.first.return_value = row
        db.query.return_value.filter.return_value.first.return_value = row
        yield db

    return (
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            fake,
        ),
        captured,
    )


class TestRestoredUnsubscribeOwnershipGateRealTransport:
    """on_unsubscribe was restored to require the same ownership check as
    on_subscribe (this branch's version had dropped it, arguing in its
    docstring that no check was needed).

    Honest severity: this is an authorization-boundary *consistency* fix,
    not the closing of an exploitable hole. ``subs.discard(sid)`` can
    only ever remove the CALLER's own sid, so an unauthorized unsubscribe
    for someone else's research_id was already a silent no-op -- the
    victim's subscription was never actually reachable through it. These
    tests prove the restored gate is wired into the real transport (the
    ownership check runs, and a non-owner is rejected before any
    subscription-map mutation is even attempted), matching on_subscribe.
    """

    def test_non_owner_unsubscribe_over_real_ws_runs_the_ownership_check(
        self,
    ):
        socketio_asgi.init_lock()
        rid = "rid-gate-check"
        cookie = make_session_cookie(
            {"username": "mallory", "session_id": "s1"}
        )
        patcher, captured = _patch_ownership(owned_usernames=set())

        # Seed a subscription "belonging" to someone else. Even under the
        # pre-fix (unguarded) code, discard(sid) on a foreign sid is a
        # structural no-op -- so the assertion that actually matters is
        # that the ownership check itself executed for mallory.
        socketio_asgi._subscriptions[("victim", rid)] = {"victim-sid"}

        with (
            patch(
                "local_deep_research.database.encrypted_db.db_manager",
                _connected_db_manager(),
            ),
            patcher,
        ):
            client = TestClient(app, raise_server_exceptions=False)
            client.cookies.set("session", cookie)
            with client.websocket_connect(
                "/ws/socket.io/?EIO=4&transport=websocket",
                headers=WS_UPGRADE_HEADERS,
            ) as ws:
                ws.receive_text()
                ws.send_text("40")
                ack = ws.receive_text()
                assert ack.startswith("40"), ack

                payload = json.dumps(
                    ["unsubscribe_from_research", {"research_id": rid}]
                )
                ws.send_text(f"42{payload}")

                assert _wait_until(lambda: captured == ["mallory"]), (
                    "on_unsubscribe never consulted ownership for the "
                    f"unsubscribing user over the real ws transport -- "
                    f"captured usernames: {captured!r}"
                )
                # Give the (now-synchronous, non-yielding) rest of the
                # handler a moment to finish before asserting on the
                # subscription map.
                time.sleep(0.05)

        assert socketio_asgi._subscriptions.get(("victim", rid)) == {
            "victim-sid"
        }, "a non-owner's unsubscribe mutated another user's subscription"

    def test_owner_unsubscribe_over_real_ws_still_removes_their_own_sid(
        self,
    ):
        """The restored gate must not regress the legitimate case: an
        owner can still unsubscribe from their own research over the
        real transport, pruning the now-empty research_id key."""
        socketio_asgi.init_lock()
        rid = "rid-owner-unsub"
        cookie = make_session_cookie({"username": "alice", "session_id": "s1"})
        patcher, captured = _patch_ownership(owned_usernames={"alice"})

        with (
            patch(
                "local_deep_research.database.encrypted_db.db_manager",
                _connected_db_manager(),
            ),
            patcher,
        ):
            client = TestClient(app, raise_server_exceptions=False)
            client.cookies.set("session", cookie)
            with client.websocket_connect(
                "/ws/socket.io/?EIO=4&transport=websocket",
                headers=WS_UPGRADE_HEADERS,
            ) as ws:
                ws.receive_text()
                ws.send_text("40")
                assert ws.receive_text().startswith("40")

                sub_payload = json.dumps(
                    ["subscribe_to_research", {"research_id": rid}]
                )
                ws.send_text(f"42{sub_payload}")
                assert _wait_until(
                    lambda: ("alice", rid) in socketio_asgi._subscriptions
                ), "owned subscribe over real ws transport never registered"

                unsub_payload = json.dumps(
                    ["unsubscribe_from_research", {"research_id": rid}]
                )
                ws.send_text(f"42{unsub_payload}")
                assert _wait_until(
                    lambda: ("alice", rid) not in socketio_asgi._subscriptions
                ), (
                    "owner's unsubscribe over real ws transport never "
                    "pruned the research_id entry"
                )

        assert "alice" in captured