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.7 KB
Raw Blame
"""An encrypted database must never be opened with the unencrypted placeholder.

`get_user_db_session` resolves a password from, in order: the caller's argument,
the session store for a given session id, any active session for that user, and
the background-worker thread context. When all of those come back empty it makes
a decision that matters:

    if not password and db_manager.has_encryption:
        raise DatabaseSessionError(...)      # fail closed
    if not password:
        password = UNENCRYPTED_DB_PLACEHOLDER   # unencrypted deployments only

Getting that order wrong — or losing the `has_encryption` condition — means an
encrypted database is opened with the literal string ``"unencrypted-mode"`` as
its key instead of refusing. The refusal is the only thing standing between "the
user's session expired" and "open the encrypted store with a hardcoded value".

Provenance: `origin/main` covered this through two tests in
`tests/database/test_session_context_extended.py`
(`test_unencrypted_db_not_connected_reopens`,
`test_unencrypted_reopen_fails_gracefully`). Both drove the Flask
`@ensure_db_session` decorator through `g.db_session` and `flask_session`, and
were dropped with that decorator. The ADR-0010 audit recorded them as the only
two predecessor cases in this area. The surviving encrypted fail-closed
invariant is security-relevant, so its executable evidence is kept here rather
than in the database suite.

What the branch had instead: `test_session_context.py::TestUnencryptedDbPlaceholder`
asserts `UNENCRYPTED_DB_PLACEHOLDER == "unencrypted-mode"`. That is a statement
about a literal, and it passes whatever the surrounding logic does with it.
"""

from unittest.mock import Mock, patch

import pytest

from local_deep_research.database.session_context import (
    UNENCRYPTED_DB_PLACEHOLDER,
    DatabaseSessionError,
    get_user_db_session,
)

USER = "fallback_probe_user"


def _no_password_anywhere(monkeypatch):
    """Silence every password source, so the fallback branch is the one under
    test rather than an accident of a leftover session."""
    # `session_password_store` is imported INSIDE get_user_db_session, so it is
    # not an attribute of session_context and must be patched at its source
    # module. Patching the wrong target here fails loudly (AttributeError)
    # rather than silently leaving a real password source live — which would
    # make these tests pass for the wrong reason.
    from local_deep_research.database import session_context as sc
    from local_deep_research.database import session_passwords as sp

    monkeypatch.setattr(
        sp.session_password_store,
        "get_session_password",
        lambda *a, **k: None,
    )
    monkeypatch.setattr(
        sp.session_password_store,
        "get_any_session_password",
        lambda *a, **k: None,
    )
    monkeypatch.setattr(sc, "get_search_context", lambda: None)


class TestEncryptedDatabaseFailsClosed:
    """The half that matters. An encrypted store must refuse, not substitute."""

    def test_encrypted_db_with_no_password_raises(self, monkeypatch):
        _no_password_anywhere(monkeypatch)

        with patch(
            "local_deep_research.database.session_context.db_manager"
        ) as mock_db:
            mock_db.has_encryption = True

            with pytest.raises(DatabaseSessionError) as exc:
                with get_user_db_session(USER):
                    pass

        assert USER in str(exc.value)
        assert "password" in str(exc.value).lower()

    def test_encrypted_db_never_reaches_the_session_layer(self, monkeypatch):
        """The stronger assertion: it must not merely raise *eventually*.

        A version that opened the session and then raised would still satisfy
        `pytest.raises`, while having already handed the placeholder to the
        database layer. Assert nothing was opened at all.
        """
        _no_password_anywhere(monkeypatch)

        with patch(
            "local_deep_research.database.session_context.db_manager"
        ) as mock_db:
            mock_db.has_encryption = True
            with patch(
                "local_deep_research.database.thread_local_session.get_metrics_session"
            ) as mock_get:
                with pytest.raises(DatabaseSessionError):
                    with get_user_db_session(USER):
                        pass

        mock_get.assert_not_called()

    def test_placeholder_is_never_used_as_an_encrypted_key(self, monkeypatch):
        """Pins the exact failure this guard exists to prevent."""
        _no_password_anywhere(monkeypatch)

        with patch(
            "local_deep_research.database.session_context.db_manager"
        ) as mock_db:
            mock_db.has_encryption = True
            with patch(
                "local_deep_research.database.thread_local_session.get_metrics_session"
            ) as mock_get:
                with pytest.raises(DatabaseSessionError):
                    with get_user_db_session(USER):
                        pass

        used_passwords = [
            call.args[1]
            for call in mock_get.call_args_list
            if len(call.args) > 1
        ]
        assert UNENCRYPTED_DB_PLACEHOLDER not in used_passwords, (
            "the unencrypted placeholder was passed as the key for an "
            "ENCRYPTED database — the has_encryption guard is gone"
        )


class TestUnencryptedDatabaseStillWorks:
    """The allow counterpart.

    Without this, a build that raised for *every* passwordless open would
    satisfy every assertion above while breaking unencrypted deployments
    entirely — the guard would be "secure" and useless.
    """

    def test_unencrypted_db_with_no_password_uses_the_placeholder(
        self, monkeypatch
    ):
        _no_password_anywhere(monkeypatch)
        sentinel = Mock(name="session")

        with patch(
            "local_deep_research.database.session_context.db_manager"
        ) as mock_db:
            mock_db.has_encryption = False
            with patch(
                "local_deep_research.database.thread_local_session.get_metrics_session",
                return_value=sentinel,
            ) as mock_get:
                with get_user_db_session(USER) as session:
                    assert session is sentinel

        mock_get.assert_called_once()
        assert mock_get.call_args.args[0] == USER
        assert mock_get.call_args.args[1] == UNENCRYPTED_DB_PLACEHOLDER

    def test_unencrypted_reopen_failure_raises_rather_than_yielding_none(
        self, monkeypatch
    ):
        """`main`'s ``test_unencrypted_reopen_fails_gracefully``.

        When the session layer cannot produce a session, the caller must get an
        error — not a ``None`` that every downstream `.query(...)` turns into an
        AttributeError far from the cause.
        """
        _no_password_anywhere(monkeypatch)

        with patch(
            "local_deep_research.database.session_context.db_manager"
        ) as mock_db:
            mock_db.has_encryption = False
            with patch(
                "local_deep_research.database.thread_local_session.get_metrics_session",
                return_value=None,
            ):
                with pytest.raises(DatabaseSessionError) as exc:
                    with get_user_db_session(USER):
                        pass

        assert "Could not establish session" in str(exc.value)


class TestNoUsername:
    def test_missing_username_is_refused(self):
        """`get_user_db_session(None)` is the #4526 fallback this codebase
        keeps closing; it must never resolve to some other user's store."""
        with pytest.raises(DatabaseSessionError) as exc:
            with get_user_db_session(None):
                pass

        assert "No authenticated user" in str(exc.value)