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.5 KB
Raw Blame
"""Revoking a session must actually revoke it.

Regression guard for a migration-introduced auth defect. Both checks
``require_auth`` previously made were username-scoped: ``is_user_connected``
is true whenever ANY session for that user has the database open, and the
password resolver falls back to ``get_any_session_password(username)``, which
returns whichever session's password happens to be live. Nothing consulted the
server-side session store, so ``destroy_session`` had no effect on the request
path.

The observable failure: log in, capture the cookie, log out (correctly 401),
then log in again from anywhere — and the ORIGINAL, supposedly-destroyed
cookie is accepted again, returning real data.

Flask did not have this hole. Its ``get_user_db_session`` resolved the password
from ``flask_session["session_id"]``, so a replayed cookie whose session had
been destroyed found no password and died at the database layer.
``get_any_session_password`` (added on this branch, absent from main) removed
that protection, because 154 router call sites call
``get_user_db_session(username)`` without threading a session_id through.

These tests assert at the ``require_auth`` chokepoint rather than end-to-end,
so they stay fast and cannot be defeated by a route happening not to touch the
database. The critical case is ``test_destroyed_session_rejected_even_when_user_
reconnected`` — it holds ``is_user_connected`` TRUE, which is exactly the state
a later login produces.
"""

import pytest
from fastapi import HTTPException

from local_deep_research.web.dependencies import auth as auth_dep

# These tests exist to prove a destroyed session IS rejected, so they must
# run against the real server-side-session gate. Opt out of the autouse
# ``_legacy_bare_username_auth`` shim in tests/conftest.py, which patches
# ``_server_session_valid`` to accept unconditionally so the many legacy
# route tests that authenticate with a bare username keep working. Without
# this marker that shim would silently defeat exactly what is under test.
pytestmark = pytest.mark.real_session_check


class _FakeRequest:
    """require_auth only touches ``request.session``."""

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


class _FakeDBManager:
    def __init__(self, connected=True, has_encryption=True):
        self._connected = connected
        self.has_encryption = has_encryption

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


class _FakeSessionManager:
    """Server-side session store: maps session_id -> username, or nothing."""

    def __init__(self, sessions=None):
        self._sessions = dict(sessions or {})

    def validate_session(self, session_id):
        return self._sessions.get(session_id)

    def session_owner(self, session_id):
        return self._sessions.get(session_id)


@pytest.fixture
def connected_db(monkeypatch):
    """The user's database is open — the state a later login produces."""
    monkeypatch.setattr(auth_dep, "db_manager", _FakeDBManager(connected=True))
    # Keep the credential usable so every rejection below is caused by the
    # owner/revocation check, rather than by missing database credentials.
    monkeypatch.setattr(
        auth_dep.session_password_store,
        "get_session_password",
        lambda _username, _session_id: "test-password",
    )
    return


def test_live_session_is_accepted(connected_db, monkeypatch):
    monkeypatch.setattr(
        auth_dep, "session_manager", _FakeSessionManager({"s1": "alice"})
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    assert auth_dep.require_auth(request) == "alice"


def test_destroyed_session_rejected_even_when_user_reconnected(
    connected_db, monkeypatch
):
    """The actual vulnerability: a dead session_id with the DB open.

    This is the state after: log out (session destroyed), then log in again
    from another device (database reopened). The replayed cookie must NOT be
    honoured.
    """
    # Session store knows about the NEW session only; the captured cookie's
    # session was destroyed at logout.
    monkeypatch.setattr(
        auth_dep, "session_manager", _FakeSessionManager({"s2_new": "alice"})
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1_destroyed"})

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

    assert exc.value.status_code == 401, (
        "a cookie whose server-side session was destroyed was accepted because "
        "the user had since logged in again — session revocation does not work"
    )
    assert request.session == {}, (
        "the dead session should also be cleared so the client stops "
        "presenting it"
    )


def test_session_id_missing_is_rejected(connected_db, monkeypatch):
    """A cookie carrying only a username claim must not authenticate."""
    monkeypatch.setattr(
        auth_dep, "session_manager", _FakeSessionManager({"s1": "alice"})
    )
    request = _FakeRequest({"username": "alice"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)
    assert exc.value.status_code == 401


def test_session_belonging_to_another_user_is_rejected(
    connected_db, monkeypatch
):
    """A valid session id must match the username claimed in the cookie."""
    monkeypatch.setattr(
        auth_dep, "session_manager", _FakeSessionManager({"s1": "bob"})
    )
    request = _FakeRequest({"username": "alice", "session_id": "s1"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)
    assert exc.value.status_code == 401


def test_expired_session_is_rejected(connected_db, monkeypatch):
    """validate_session returning None (timeout) must reject.

    This is what gives session_timeout_hours / remember_me_days real effect on
    the request path — previously nothing consulted them at all.
    """
    monkeypatch.setattr(auth_dep, "session_manager", _FakeSessionManager({}))
    request = _FakeRequest({"username": "alice", "session_id": "s_expired"})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)
    assert exc.value.status_code == 401


def test_no_username_still_rejected_before_session_lookup(monkeypatch):
    monkeypatch.setattr(auth_dep, "db_manager", _FakeDBManager(connected=True))
    monkeypatch.setattr(auth_dep, "session_manager", _FakeSessionManager({}))
    request = _FakeRequest({})

    with pytest.raises(HTTPException) as exc:
        auth_dep.require_auth(request)
    assert exc.value.detail == "Authentication required"