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.0 KB
Raw Blame
"""Store-level scoping of ``get_any_session_password`` (round-9, C3).

Attack class: in-process username-confusion / cross-tenant credential
scavenging. ``get_any_session_password(username)`` is a username-wide
fallback accessor (``database/session_passwords.py:84``) used by FastAPI
routes where no Flask session id is available. Its blast radius is decided
entirely by the store's key scoping: entries are keyed by the exact
``(username, session_id)`` tuple, so a password stored for one account must
never be resolvable under another account's username -- not for a wholly
different user, not for a prefix-colliding username ("alice" vs "alice2"),
not after TTL expiry, and not after the user's sessions are cleared.

These tests stay at the STORE level on purpose: the ``require_auth`` web
gate that fronts real callers is another layer's test surface. The HTTP
replay boundary is not in scope here; what is in scope is that the store
itself refuses every shape of wrong-username retrieval.

Related upper-layer context (do not duplicate here):
``tests/security/test_sqlcipher_key_lifecycle.py`` pins the credential
LIFETIME findings around this accessor; this file pins only the scoping
primitives.
"""

from local_deep_research.database.session_passwords import SessionPasswordStore


class TestGetAnySessionPasswordScoping:
    """Username scoping of the username-wide password fallback accessor."""

    def test_no_stored_sessions_returns_none(self):
        """A username with nothing stored -- the attacker-username shape --
        gets ``None``, not a password scavenged from another user's
        sessions."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("victim", "s1", "victim-pw")  # noqa: S105

        assert store.get_any_session_password("attacker") is None

    def test_cross_username_isolation(self):
        """Storing ``(alice, s1)`` never yields a password for ``bob``, in
        either direction, even with both users live in the same store."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "alice-pw")  # noqa: S105

        assert store.get_any_session_password("bob") is None

        store.store_session_password("bob", "s2", "bob-pw")  # noqa: S105
        assert store.get_any_session_password("alice") == "alice-pw"
        assert store.get_any_session_password("bob") == "bob-pw"

    def test_prefix_usernames_do_not_collide(self):
        """Tuple-keyed exact matching: "alice2" is a different principal
        from "alice" even though it shares a prefix, for both the
        storage and the retrieval direction."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "alice-pw")  # noqa: S105
        store.store_session_password("alice2", "s1", "alice2-pw")  # noqa: S105

        assert store.get_any_session_password("alice") == "alice-pw"
        assert store.get_any_session_password("alice2") == "alice2-pw"

    def test_prefix_collision_attacker_direction_yields_none(self):
        """Attacker direction of the prefix case: with only ``alice2``'s
        entry in the store, ``alice`` (a mere prefix of the other
        principal) must get ``None`` -- a weakened username match
        (prefix or substring) must not let one account scavenge a
        password stored under a prefix-colliding username."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice2", "s1", "alice2-pw")  # noqa: S105

        assert store.get_any_session_password("alice") is None

    def test_expired_ttl_entries_yield_none_even_when_present(self):
        """An entry past its ``expires_at`` yields ``None`` even though it
        is still physically present in the store, and the scan cleans it
        up on the way past (``session_passwords.py:104-108``)."""
        import time

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "alice-pw")  # noqa: S105
        # Age the entry past its TTL without waiting for real time.
        store._store[("alice", "s1")]["expires_at"] = time.time() - 1

        assert store.get_any_session_password("alice") is None
        assert ("alice", "s1") not in store._store

    def test_mixed_expired_and_live_sessions_return_the_live_one(self):
        """With one expired and one live session for the same user, the
        scan skips the expired entry and returns the live password --
        expiry must not be bypassed by having a stale entry linger."""
        import time

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "stale", "stale-pw")  # noqa: S105
        store.store_session_password("alice", "live", "live-pw")  # noqa: S105
        store._store[("alice", "stale")]["expires_at"] = time.time() - 1

        assert store.get_any_session_password("alice") == "live-pw"
        assert ("alice", "stale") not in store._store

    def test_clearing_users_sessions_makes_subsequent_get_any_none(self):
        """After ``clear_all_for_user(alice)`` (the idle-sweeper/logout
        teardown path), no password is resolvable for alice -- while
        another user's entries survive untouched."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "alice-pw")  # noqa: S105
        store.store_session_password("bob", "s3", "bob-pw")  # noqa: S105

        store.clear_all_for_user("alice")

        assert store.get_any_session_password("alice") is None
        assert store.get_any_session_password("bob") == "bob-pw"

    def test_positive_control_live_session_returns_password(self):
        """Positive control for every negative above: a genuinely live
        session does satisfy the accessor, for the right username only."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "alice-pw")  # noqa: S105
        store.store_session_password("alice", "s2", "alice-pw")  # noqa: S105

        assert store.get_any_session_password("alice") == "alice-pw"