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
"""A live connection must not hide missing web-session credentials."""

import uuid

import pytest

from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.database.session_passwords import (
    session_password_store,
)
from tests.web.test_auth_session_lifecycle import (
    _client,
    _login,
    _register,
    _session_payload,
    live_app as _lifecycle_app,
)

live_app = _lifecycle_app
pytestmark = pytest.mark.real_session_check


@pytest.mark.parametrize("path", ["/auth/check", "/settings/api"])
@pytest.mark.parametrize("other_device", [False, True])
@pytest.mark.parametrize(
    "stale_bootstrap", [False, True], ids=["current_cookie", "stale_bootstrap"]
)
def test_missing_credential_requires_login_even_with_an_open_database(
    live_app, path, other_device, stale_bootstrap
):
    if not db_manager.has_encryption:
        pytest.skip("Requires SQLCipher")
    client = _client(live_app)
    username = "missing_credential_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    bootstrap_cookies = type(client.cookies)(client.cookies)
    assert client.get(path).status_code == 200
    sibling = _client(live_app) if other_device else None
    if sibling:
        assert _login(sibling, username).status_code == 302
        assert sibling.get("/auth/check").status_code == 200
    if stale_bootstrap:
        # A concurrent response can retain the initial cookie after another
        # request has already consumed its one-time bootstrap credential.
        client.cookies = bootstrap_cookies
        assert _session_payload(client).get("temp_auth_token")
    session_password_store.clear_session(
        username, _session_payload(client)["session_id"]
    )
    assert db_manager.is_user_connected(username)

    response = client.get(path, headers={"Accept": "application/json"})

    assert response.status_code == 401
    assert "username" not in _session_payload(client)
    assert client.get("/auth/check").status_code == 401
    if sibling:
        assert sibling.get("/auth/check").status_code == 200


def test_consumed_bootstrap_cookie_keeps_a_usable_session(live_app):
    client = _client(live_app)
    username = "consumed_bootstrap_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    bootstrap_cookies = type(client.cookies)(client.cookies)
    session_id = _session_payload(client)["session_id"]
    assert client.get("/auth/check").status_code == 200
    client.cookies = bootstrap_cookies

    response = client.get("/settings/api")

    assert response.status_code == 200
    assert _session_payload(client)["session_id"] == session_id
    assert "temp_auth_token" not in _session_payload(client)


@pytest.mark.parametrize("lose_credential", [False, True])
def test_json_logout_requires_csrf_and_recovers_missing_credentials(
    live_app, lose_credential
):
    client = _client(live_app)
    username = "logout_recovery_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    identity = client.get("/auth/csrf-token").json()
    headers = {
        "Accept": "application/json",
        "X-LDR-Auth-Context": identity["auth_context"],
    }
    assert client.post("/auth/logout", headers=headers).status_code == 403
    assert client.get("/auth/check").status_code == 200
    if lose_credential:
        session_password_store.clear_all_for_user(username)
        assert db_manager.is_user_connected(username)
    refreshed = client.get("/auth/csrf-token", headers=headers)
    assert refreshed.status_code == 200
    headers["X-CSRFToken"] = refreshed.json()["csrf_token"]

    response = client.post("/auth/logout", headers=headers)

    assert response.status_code == 200
    assert response.json() == {"success": True}
    assert client.get("/auth/check").status_code == 401


def test_old_page_cannot_log_out_a_replacement_login(live_app):
    client = _client(live_app)
    username = "replaced_logout_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    original = client.get("/auth/csrf-token").json()
    assert _login(client, username).status_code == 302
    replacement = client.get("/auth/csrf-token").json()
    assert replacement["auth_context"] != original["auth_context"]

    rejected = client.post(
        "/auth/logout",
        headers={
            "Accept": "application/json",
            "X-CSRFToken": replacement["csrf_token"],
            "X-LDR-Auth-Context": original["auth_context"],
        },
    )

    assert rejected.status_code == 409
    assert client.get("/auth/check").json() == {
        "authenticated": True,
        "username": username,
    }


@pytest.mark.parametrize("other_device", [False, True])
def test_rejecting_a_session_without_its_credential_revokes_it_server_side(
    live_app, monkeypatch, other_device
):
    """Clearing only the cookie would strand the server-side session.

    Once the cookie is gone the browser cannot reach logout, so the record
    would stay valid for its whole idle timeout, and a replayed copy of the
    cookie would keep renewing it. The rejection must destroy the record and
    disconnect that session's sockets, and must leave other sessions alone.
    """
    if not db_manager.has_encryption:
        pytest.skip("Requires SQLCipher")
    from local_deep_research.web.auth.session_manager import session_manager
    from local_deep_research.web.services import socketio_asgi

    disconnected = []
    monkeypatch.setattr(
        socketio_asgi, "disconnect_session", disconnected.append
    )
    client = _client(live_app)
    username = "revoked_credential_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    assert client.get("/auth/check").status_code == 200
    session_id = _session_payload(client)["session_id"]
    sibling = _client(live_app) if other_device else None
    if sibling:
        assert _login(sibling, username).status_code == 302
        sibling_id = _session_payload(sibling)["session_id"]
    captured = type(client.cookies)(client.cookies)
    session_password_store.clear_session(username, session_id)
    assert session_manager.validate_session(session_id) == username

    assert client.get("/auth/check").status_code == 401

    assert session_manager.validate_session(session_id) is None, (
        "the rejected session's server-side record is still live"
    )
    assert disconnected == [session_id]
    client.cookies = captured
    assert client.get("/auth/check").status_code == 401
    if sibling:
        assert session_manager.validate_session(sibling_id) == username
        assert sibling.get("/auth/check").status_code == 200


@pytest.mark.parametrize("other_device", [False, True])
def test_root_page_requires_this_sessions_own_credential(
    live_app, other_device
):
    """``/`` reads the user's settings, so it needs the same gate.

    Without a session-scoped password, ``get_user_db_session`` falls back to
    any other live session's password, so the page would render this
    session's view of the user's saved settings from a sibling's credential.
    """
    if not db_manager.has_encryption:
        pytest.skip("Requires SQLCipher")
    client = _client(live_app)
    username = "root_credential_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    assert client.get("/", follow_redirects=False).status_code == 200
    sibling = _client(live_app) if other_device else None
    if sibling:
        assert _login(sibling, username).status_code == 302
    session_password_store.clear_session(
        username, _session_payload(client)["session_id"]
    )
    assert db_manager.is_user_connected(username)

    response = client.get("/", follow_redirects=False)

    assert response.status_code == 302
    assert response.headers["location"] == "/auth/login"
    assert "username" not in _session_payload(client)
    if sibling:
        assert sibling.get("/", follow_redirects=False).status_code == 200


@pytest.mark.parametrize("start", ["/", "/settings/"])
def test_unreopenable_database_signs_out_instead_of_looping(
    live_app, monkeypatch, start
):
    """A 401 that keeps the cookie must not loop ``/`` <-> ``/auth/login``.

    When the database is closed and will not reopen while this session still
    holds its own password, ``require_auth`` answers 401 and leaves the
    cookie in place (the session still looks recoverable). ``/auth/login``
    sends any cookie with a username back to ``/``, so ``/`` has to end the
    session before redirecting, or the browser bounces until it gives up.
    Every protected page's HTML 401 funnels through that same pair.
    """
    from local_deep_research.web.auth.session_manager import session_manager
    from local_deep_research.web.services import socketio_asgi

    disconnected = []
    monkeypatch.setattr(
        socketio_asgi, "disconnect_session", disconnected.append
    )
    client = _client(live_app)
    username = "unreopenable_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    assert client.get("/", follow_redirects=False).status_code == 200
    session_id = _session_payload(client)["session_id"]
    assert session_password_store.get_session_password(username, session_id)
    sibling = _client(live_app)
    assert _login(sibling, username).status_code == 302
    sibling_id = _session_payload(sibling)["session_id"]

    # The database file is gone, corrupt, or failing migration: every reopen
    # fails while this session's stored password is still present.
    db_manager.close_user_database(username)
    monkeypatch.setattr(
        db_manager, "open_user_database", lambda *args, **kwargs: None
    )
    assert not db_manager.is_user_connected(username)

    location = start
    hops = []
    response = None
    for _ in range(6):
        response = client.get(location, follow_redirects=False)
        hops.append((location, response.status_code))
        if response.status_code != 302:
            break
        location = response.headers["location"]

    assert response is not None and response.status_code == 200, hops
    assert location.split("?")[0] == "/auth/login", hops
    assert "username" not in _session_payload(client)
    assert session_manager.validate_session(session_id) is None
    assert disconnected == [session_id]
    assert (
        session_password_store.get_session_password(username, session_id)
        is None
    )
    # Only this browser is signed out.
    assert session_manager.validate_session(sibling_id) == username
    assert session_password_store.get_session_password(username, sibling_id)