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.3 KB
Raw Blame
"""A stale session must be cleared when nothing can reopen its database.

Regression guard for a behaviour lost in the Flask->FastAPI migration. Flask ran
``cleanup_stale_sessions()`` as a before_request handler on EVERY request
(``web/auth/session_cleanup.py``); that module was deleted with no successor, so
``require_auth`` began raising 401 without ever clearing the cookie. The browser
kept believing it was logged in and every protected route 401'd indefinitely.
At the time only the root route cleared the session (``fastapi_app.py``
index()), so a user recovered by navigating to "/" while an API or XHR client
never did.

The clearing is deliberately conditional: a temp auth token or a stored session
password means ``ensure_user_database()`` can still reopen the connection, so
those sessions are stale, not dead, and must survive. Clearing them would log
users out mid-session — which is why these tests assert both directions.
"""

import errno
from pathlib import Path

import pytest
from fastapi import HTTPException

from local_deep_research.database.encrypted_db import UserConnectionStatus
from local_deep_research.database.rekey_lock import (
    RekeyEpochUnreadableError,
    RekeyLockContentionError,
    RekeySidecarIOError,
    RekeySidecarOperation,
)
from local_deep_research.web.dependencies import auth as auth_dep


class _FakeRequest:
    """Minimal stand-in — require_auth only touches ``request.session``."""

    def __init__(self, session):
        self.session = dict(session)


class _FakeDBManager:
    def __init__(self, connected=False, has_encryption=True, probe=None):
        self._connected = connected
        self.has_encryption = has_encryption
        # What probe_user_connection reports once is_user_connected has said
        # False. Defaults to the real manager's answer when no usable engine
        # is cached; UNAVAILABLE models a kept engine behind a sidecar fault.
        self._probe = probe or (
            UserConnectionStatus.CONNECTED
            if connected
            else UserConnectionStatus.NOT_CONNECTED
        )

    def is_user_connected(self, username):
        return self._connected

    def probe_user_connection(self, username):
        return self._probe


class _FakePasswordStore:
    def __init__(self, password=None):
        self._password = password

    def get_session_password(self, username, session_id):
        return self._password


class _FakeSessionManager:
    """Server-side session store — always holds a live session for these tests.

    ``require_auth`` validates the session id before it ever reaches the
    stale-credential logic under test here (see test_session_revocation.py).
    Without a live server-side session every test below would 401 for the wrong
    reason and prove nothing about credential recovery.
    """

    def __init__(self):
        self.destroyed = []

    def validate_session(self, session_id):
        if session_id in self.destroyed:
            return None
        return {"s1": "alice", "s-bob": "bob"}.get(session_id)

    def session_owner(self, session_id):
        return self.validate_session(session_id)

    def destroy_session(self, session_id):
        self.destroyed.append(session_id)


@pytest.fixture(autouse=True)
def _live_server_session(monkeypatch):
    sessions = _FakeSessionManager()
    monkeypatch.setattr(auth_dep, "session_manager", sessions)
    return sessions


@pytest.fixture
def disconnected(monkeypatch):
    """User is authenticated in the cookie but has no open database."""
    monkeypatch.setattr(auth_dep, "db_manager", _FakeDBManager(connected=False))
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore()
    )
    return


def test_unrecoverable_session_is_cleared(disconnected):
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)

    assert exc.value.status_code == 401
    assert request.session == {}, (
        "stale session survived with no way to reopen the database; the client "
        "keeps a username cookie that 401s on every protected route until "
        'someone loads "/" (the pre-migration before_request hook cleared it)'
    )


def test_unrecoverable_session_is_revoked_server_side(
    disconnected, _live_server_session, monkeypatch
):
    """Clearing the cookie must also end the server-side session.

    Once the cookie is gone the browser cannot reach logout, so without this
    the record would stay valid for its whole idle timeout and its sockets
    would stay connected.
    """
    from local_deep_research.web.services import socketio_asgi

    disconnected_sockets = []
    monkeypatch.setattr(
        socketio_asgi, "disconnect_session", disconnected_sockets.append
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException):
        auth_dep.require_auth(request)

    assert _live_server_session.destroyed == ["s1"]
    assert disconnected_sockets == ["s1"]


def test_revocation_never_touches_another_users_session(
    disconnected, _live_server_session, monkeypatch
):
    from local_deep_research.web.services import socketio_asgi

    disconnected_sockets = []
    monkeypatch.setattr(
        socketio_asgi, "disconnect_session", disconnected_sockets.append
    )
    request = _FakeRequest({"username": "alice", "session_id": "s-bob"})

    assert auth_dep.clear_session_if_unrecoverable(request, "alice") is True

    assert request.session == {}
    assert _live_server_session.destroyed == []
    assert disconnected_sockets == []


def test_revocation_failure_still_returns_401(disconnected, monkeypatch):
    """Teardown is best-effort: it must not turn the 401 into a 500."""

    def _explode(_session_id):
        raise RuntimeError("socket layer down")

    from local_deep_research.web.services import socketio_asgi

    monkeypatch.setattr(socketio_asgi, "disconnect_session", _explode)
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)

    assert exc.value.status_code == 401
    assert request.session == {}


def test_session_with_stored_password_is_kept(monkeypatch):
    """ensure_user_database() can still recover this one — do not log them out."""
    monkeypatch.setattr(auth_dep, "db_manager", _FakeDBManager(connected=False))
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException):
        auth_dep.require_auth(request)

    assert request.session.get("username") == "alice", (
        "session was cleared even though the password store can still reopen "
        "the database — this logs the user out mid-session"
    )


def test_session_with_temp_auth_token_is_kept(
    disconnected, _live_server_session
):
    """The post-login bootstrap token opens the DB on the next request."""
    request = _FakeRequest(
        {"username": "alice", "session_id": "s1", "temp_auth_token": "tok"}
    )

    with pytest.raises(HTTPException):
        auth_dep.require_auth(request)

    assert request.session.get("username") == "alice", (
        "cleared a session holding a live temp auth token, which "
        "ensure_user_database() would have consumed to open the database"
    )
    assert _live_server_session.destroyed == []


def test_unencrypted_database_session_is_kept(monkeypatch):
    """Without encryption a missing connection is not a credential problem."""
    monkeypatch.setattr(
        auth_dep,
        "db_manager",
        _FakeDBManager(connected=False, has_encryption=False),
    )
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore()
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException):
        auth_dep.require_auth(request)

    assert request.session.get("username") == "alice"


def test_session_without_session_id_is_cleared(disconnected):
    """No session_id means the password store cannot be consulted at all."""
    request = _FakeRequest({"username": "alice"})

    with pytest.raises(HTTPException):
        auth_dep.require_auth(request)

    assert request.session == {}


def test_connected_user_is_untouched(monkeypatch):
    monkeypatch.setattr(auth_dep, "db_manager", _FakeDBManager(connected=True))
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore()
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    assert auth_dep.require_auth(request) == "alice"
    assert request.session.get("username") == "alice"


def test_no_username_raises_without_touching_session(monkeypatch):
    monkeypatch.setattr(auth_dep, "db_manager", _FakeDBManager(connected=False))
    request = _FakeRequest({})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)

    assert exc.value.status_code == 401
    assert exc.value.detail == "Authentication required"


class _SidecarReleaseFaultDBManager(_FakeDBManager):
    """``is_user_connected`` raising the one sidecar fault it lets escape.

    The probe reports a sidecar that cannot be opened or locked, and an
    epoch read that fails, as a plain False (``probe_user_connection``
    tells those apart). A failure to RELEASE the sidecar after the claim
    raises ``RekeySidecarIOError`` instead: a ``RekeyOperationalError``,
    not an OSError, so it reaches the caller.
    """

    def is_user_connected(self, username):
        raise RekeySidecarIOError(
            Path("alice.db"), RekeySidecarOperation.UNLOCK, errno.EIO
        )


def _assert_retryable_503_keeping_the_session(exc, request, sessions):
    assert exc.value.status_code == 503
    assert exc.value.headers == {
        "Retry-After": str(auth_dep.DATABASE_UNAVAILABLE_RETRY_AFTER_SECONDS)
    }
    assert request.session == {"username": "alice", "session_id": "s1"}, (
        "a transient sidecar fault cleared a recoverable session"
    )
    assert sessions.destroyed == []


@pytest.mark.real_session_check
def test_sidecar_release_fault_on_probe_answers_503_and_keeps_the_session(
    monkeypatch, _live_server_session
):
    """A rekey-sidecar fault is not a verdict on the session.

    Since the rekey-serialization work the probe takes the sidecar
    (filesystem + blocking lock). Unhandled, its typed fault escaped
    ``require_auth`` as a 500 on every request; degrading it to the 401
    path instead let ``/`` (``end_rejected_session``) destroy the session
    and its stored password, logging the user out after a transient
    fault. It must be a retryable 503 that leaves the session alone.
    """
    monkeypatch.setattr(auth_dep, "db_manager", _SidecarReleaseFaultDBManager())
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)

    _assert_retryable_503_keeping_the_session(
        exc, request, _live_server_session
    )


@pytest.mark.real_session_check
def test_unverifiable_cached_engine_answers_503_and_keeps_the_session(
    monkeypatch, _live_server_session
):
    """is_user_connected says False, the manager says the engine is kept.

    That is a sidecar or epoch-read fault (an fd-exhaustion spike), not a
    missing engine, so the request is retryable rather than a 401.
    """
    monkeypatch.setattr(
        auth_dep,
        "db_manager",
        _FakeDBManager(connected=False, probe=UserConnectionStatus.UNAVAILABLE),
    )
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)

    _assert_retryable_503_keeping_the_session(
        exc, request, _live_server_session
    )


@pytest.mark.real_session_check
@pytest.mark.parametrize(
    ("session_id", "password"),
    [("s-revoked", "pw"), ("s1", None)],
    ids=["revoked-server-side", "no-recovery-credential"],
)
def test_unverifiable_cached_engine_does_not_keep_a_dead_session(
    monkeypatch, session_id, password
):
    """The 503 is only for a session that would otherwise be accepted."""
    monkeypatch.setattr(
        auth_dep,
        "db_manager",
        _FakeDBManager(connected=False, probe=UserConnectionStatus.UNAVAILABLE),
    )
    monkeypatch.setattr(
        auth_dep,
        "session_password_store",
        _FakePasswordStore(password=password),
    )
    request = _FakeRequest({"username": "alice", "session_id": session_id})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)

    assert exc.value.status_code == 401
    assert request.session == {}


def test_engine_found_by_the_second_probe_is_accepted(monkeypatch):
    """The fault cleared, or another request reopened the database,
    between the two probes. The later answer counts: the request is
    served instead of 401ing into a logout at "/"."""
    monkeypatch.setattr(
        auth_dep,
        "db_manager",
        _FakeDBManager(connected=False, probe=UserConnectionStatus.CONNECTED),
    )
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    assert auth_dep.require_auth(request) == "alice"
    assert request.session == {"username": "alice", "session_id": "s1"}


class _ReopenFaultDBManager(_FakeDBManager):
    """No engine is cached, and reopening the database meets ``fault``.

    That is the shape of a cold open that cannot take the user's rekey
    sidecar (another holder kept it past the wait bound, an fd-exhaustion
    spike) or read its epoch, after the user's other session logged out or
    the idle sweep closed the engine.
    """

    def __init__(self, fault):
        super().__init__(connected=False)
        self._fault = fault
        self.opened = 0

    def open_user_database(self, username, password):
        self.opened += 1
        raise self._fault


_REOPEN_FAULTS = [
    pytest.param(
        lambda: RekeyLockContentionError(Path("alice.db.rekey.lock")),
        id="sidecar-held-past-the-bound",
    ),
    pytest.param(
        lambda: RekeySidecarIOError(
            Path("alice.db"), RekeySidecarOperation.ACQUIRE, errno.EMFILE
        ),
        id="sidecar-emfile",
    ),
    pytest.param(
        lambda: RekeyEpochUnreadableError(Path("alice.db"), errno.EIO),
        id="epoch-unreadable",
    ),
]


@pytest.mark.real_session_check
@pytest.mark.parametrize("make_fault", _REOPEN_FAULTS)
def test_a_reopen_stopped_by_a_rekey_fault_answers_503_and_keeps_the_session(
    monkeypatch, _live_server_session, make_fault
):
    """The missing engine is the reopen's fault, not the credential's.

    ``ensure_user_database`` swallowed the fault, so ``require_auth`` saw
    "not connected" with a recoverable session and answered 401, which
    "/" turns into ``end_rejected_session``: a logout over a transient
    fault. Recorded for the request, it is the retryable 503 instead.
    """
    manager = _ReopenFaultDBManager(make_fault())
    monkeypatch.setattr(auth_dep, "db_manager", manager)
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with auth_dep.request_database_scope():
        auth_dep.ensure_user_database(request)
        with pytest.raises(HTTPException) as exc:
            auth_dep.require_auth(request)

    assert manager.opened == 1, "the reopen was never attempted"
    _assert_retryable_503_keeping_the_session(
        exc, request, _live_server_session
    )


@pytest.mark.real_session_check
def test_a_reopen_fault_does_not_keep_a_revoked_session(monkeypatch):
    """The 503 is only for a session that would otherwise be accepted."""
    manager = _ReopenFaultDBManager(
        RekeyLockContentionError(Path("alice.db.rekey.lock"))
    )
    monkeypatch.setattr(auth_dep, "db_manager", manager)
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )
    request = _FakeRequest({"username": "alice", "session_id": "s-revoked"})

    with auth_dep.request_database_scope():
        auth_dep.ensure_user_database(request)
        with pytest.raises(HTTPException) as exc:
            auth_dep.require_auth(request)

    assert manager.opened == 1
    assert exc.value.status_code == 401
    assert request.session == {}


def test_a_reopen_fault_stays_with_its_own_request(monkeypatch):
    """The next request starts clean: its own reopen decides."""
    manager = _ReopenFaultDBManager(
        RekeyLockContentionError(Path("alice.db.rekey.lock"))
    )
    monkeypatch.setattr(auth_dep, "db_manager", manager)
    monkeypatch.setattr(
        auth_dep, "session_password_store", _FakePasswordStore(password="pw")
    )

    with auth_dep.request_database_scope():
        auth_dep.ensure_user_database(
            _FakeRequest({"username": "alice", "session_id": "s1"})
        )

    request = _FakeRequest({"username": "alice", "session_id": "s1"})
    with auth_dep.request_database_scope():
        with pytest.raises(HTTPException) as exc:
            auth_dep.require_auth(request)

    assert exc.value.status_code == 401