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.

7.8 KB
Raw Blame
"""Direct unit test for the load-bearing ordering inside
``ensure_user_database`` (``web/dependencies/auth.py:217-294``).

Background
----------
``ensure_user_database`` consumes the post-login ``temp_auth_token`` (a
one-time, 10s-TTL bootstrap credential) BEFORE checking the
``db_manager.is_user_connected(username)`` fast path. That ordering is
called out explicitly in the function's own comment
(``web/dependencies/auth.py`` around line 234): if the fast path ran
first, the token would never be consumed on any request after the very
first one (login itself already opens the connection, so
``is_user_connected()`` is True from the second request onward) -- it
would stay valid in ``temp_auth_store`` for its full TTL *and* stay
embedded in the client's already-issued signed cookie, so a cookie
captured in that 10s window could re-authenticate later, including after
the legitimate user has logged out (logout clears
``session_password_store`` but cannot reach into a cookie already handed
out).

``tests/web/test_long_integration_flows.py::TestSessionLifecycle::
test_cookie_captured_before_logout_is_rejected_after_logout`` was written
to guard exactly this, end-to-end through a real login/logout/replay
flow. Its docstring still claims "reverting [the fix] fails this test" --
that is no longer true. ``DatabaseMiddleware`` (``fastapi_app.py``) now
calls ``_enforce_session_revocation(session)`` on every request BEFORE
``ensure_user_database`` runs; by the time a post-logout replay of the
captured cookie reaches ``ensure_user_database``, the session has already
been wiped (the revoked ``session_id`` fails ``session_manager
.validate_session``), so ``ensure_user_database`` returns at its very
first line (``username = request.session.get("username")`` is None) and
never reaches the code whose ordering is actually under test. The
integration test still passes, but no longer because the ordering is
correct -- it would pass identically if the ordering bug were
reintroduced. (Out of scope here: the orchestrator will correct that
docstring's claim separately.)

This file closes that gap by calling ``ensure_user_database`` directly,
bypassing ``DatabaseMiddleware``/``_enforce_session_revocation``
entirely, so the ordering is exercised whether or not some *other* layer
happens to also catch the scenario this particular integration test used.
"""

import types
import uuid

from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.database.session_passwords import (
    session_password_store,
)
from local_deep_research.database.temp_auth import temp_auth_store
from local_deep_research.web.dependencies.auth import ensure_user_database


def _fake_request(session: dict):
    """Minimal request double exposing only ``.session`` (a plain dict).

    ``ensure_user_database`` only ever touches ``request.session`` (via
    ``.get``/``.pop``), so a plain dict satisfies it -- this mirrors
    ``DatabaseMiddleware``'s own inline ``_MinimalRequest`` shim
    (``fastapi_app.py``), which calls this exact function the same way
    outside of a real FastAPI ``Request``.
    """
    return types.SimpleNamespace(session=session)


class TestTempAuthTokenConsumedBeforeFastPath:
    def test_token_is_consumed_even_when_connection_already_open(
        self, tmp_path, monkeypatch
    ):
        """The temp_auth_token must be consumed (removed from
        temp_auth_store, popped from the session, promoted into
        session_password_store) even when
        ``db_manager.is_user_connected(username)`` is ALREADY True --
        i.e. even when the fast path, if checked first, would return
        before ever looking at the token.

        ``is_user_connected()`` is True for essentially every request
        after the one that performed the login (login already opened the
        connection), so this is not an edge case -- it is the normal
        state for request #2 onward within the 10s token TTL. Forcing it
        to True here and asserting the token is still consumed is what
        pins the ordering: token-consumption-before-fast-path, not the
        reverse.
        """
        username = f"tokorder_{uuid.uuid4().hex[:10]}"
        session_id = f"sess_{uuid.uuid4().hex[:10]}"
        password = "correct-horse-battery-staple"  # noqa: S105

        # Make "the connection is already open" REAL -- true for every
        # request after login. is_user_connected() is no longer a dict
        # membership check: it claims the cached generation, re-reads the
        # durable rekey epoch beside the database file and detaches any
        # entry whose recorded epoch is not the committed one. A planted
        # sentinel (the old ``connections[username] = object()``) has no
        # epoch file behind it, is detached on that first claim, and turns
        # the fast path False -- after which the token block runs whatever
        # the ordering is, and this test passes vacuously. So open the
        # connection through the real create path (isolated data dir, the
        # sanctioned low-KDF test knob) and assert the premise below.
        monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
        # Save/restore the RAW override: ``monkeypatch.setattr(db_manager,
        # "data_dir", ...)`` would, on undo, write the getter's resolved
        # path back through the setter and pin ``_data_dir_override`` for
        # the rest of the worker (same as tests/connected/conftest.py).
        original_override = db_manager._data_dir_override
        db_manager._data_dir_override = tmp_path
        token = None
        try:
            db_manager.create_user_database(username, password)
            # Store AFTER the slow create (SQLCipher + migrations) so the
            # 10 s token TTL cannot expire on a loaded CI runner.
            token = temp_auth_store.store_auth(username, password)
            assert db_manager.is_user_connected(username) is True, (
                "premise: the fast path must be live for this user, or "
                "the token block runs regardless of its ordering and "
                "nothing below pins it"
            )
            fake_session = {
                "username": username,
                "session_id": session_id,
                "temp_auth_token": token,
            }
            request = _fake_request(fake_session)

            ensure_user_database(request)

            assert "temp_auth_token" not in fake_session, (
                "temp_auth_token must be popped from the session once "
                "consumed -- it wasn't, meaning the fast path skipped "
                "the token block instead of running after it"
            )
            assert temp_auth_store.peek_auth(token) is None, (
                "temp_auth_token must be consumed (single-use) even "
                "though is_user_connected() was already True -- it is "
                "still sitting in the store, live for its full TTL and "
                "replayable by anyone holding the captured cookie"
            )
            assert (
                session_password_store.get_session_password(
                    username, session_id
                )
                == password
            ), (
                "consuming the token must promote the recovered "
                "password into session_password_store -- it wasn't "
                "written, meaning the token block never ran"
            )
        finally:
            db_manager.close_user_database(username)
            session_password_store.clear_session(username, session_id)
            # Idempotent no-op in the passing case (ensure_user_database
            # already consumed it via retrieve_auth); cleans up after a
            # failed run that left the token live.
            if token is not None:
                temp_auth_store.retrieve_auth(token)
            db_manager._data_dir_override = original_override