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.

12.2 KB
Raw Blame
"""Username trust boundary at the DatabaseManager connection layer.

Incident context (PR #5596, cached-connection login auth bypass): the
connection cache (``connections`` / ``_password_verifiers``) is keyed by the
EXACT username string, and every credential decision funnels through
``_get_user_db_path`` -> ``get_user_database_filename``
(config/paths.py:135), which sha256-hashes the username into
``ldr_user_{hash16}.db`` and raises ValueError for empty/None usernames
(the #5481 defense-in-depth chokepoint).

That makes the username an attacker-controlled input that reaches THREE
security-relevant surfaces this file pins at the real-SQLCipher level:

* F1 traversal containment: even usernames containing path separators,
  absolute paths, "..", NUL bytes, glob/metacharacters or extreme lengths
  must resolve to a file INSIDE the data directory (the hash destroys all
  structure), and neither open nor create may touch anything outside it;
* F2 the empty/None chokepoint: ValueError BEFORE any filesystem effect;
* F3 confusable-username cache-key exactness: byte-distinct usernames are
  distinct cache keys AND distinct files -- "Alice"/"alice "/"ALICE"/NFD
  lookalikes get nothing from alice's warm engine and disturb nothing of
  hers (the cache is keyed by exact string, the filename by exact hash;
  any normalization regression on either surface would let a lookalike
  share alice's engine or file).

Complements round 1's test_db_connection_cold_open_artifacts.py (which
fingerprinted failed opens for an EXISTING user) and
test_login_cached_connection_password_lifecycle.py::test_multi_user_
isolation (which used ordinary distinct usernames, not adversarial ones).
"""

import hashlib
import re
import unicodedata
import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager

# Module-level gate. Without a working SQLCipher build every test below
# is a no-op, and the dozens of in-body ``pytest.skip`` calls let a CI
# lane report this whole battery green having executed nothing. Skipping
# at import time makes the missing dependency one visible, uniform signal
# per module instead.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

# Every username here must be unusable as a path by the time it reaches the
# filesystem: the sha256[:16] hash keeps only [0-9a-f].
ADVERSARIAL_USERNAMES = [
    "../../etc/pwned",
    "/abs/path",
    "..",
    "a/../../b",
    "x\x00y",
    "Cafe\u0301-*?<>|",
    "u" * 1000,
]

HASHED_FILENAME_RE = re.compile(
    r"^(ldr_user_[0-9a-f]{16}\.db)(\.salt|-wal|-shm|-journal|\.rekey\.lock|\.rekey\.epoch|\.rekey\.pending)?$"
)


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager whose data dir is a SUBDIRECTORY of tmp_path.

    Same self-contained pattern as the round-1 files (KDF knob included);
    putting data_dir inside tmp_path makes "outside the data dir" an
    observable, fingerprintable region for the containment assertions.
    """
    data_dir = tmp_path / "encrypted_databases"
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = data_dir
    yield mgr
    mgr.close_all_databases()


def _tree_fingerprint(root) -> dict:
    """relative-posix-path -> (size, sha256, mode) for every file under root."""
    fingerprint = {}
    for path in sorted(root.rglob("*")):
        if path.is_file():
            rel = path.relative_to(root).as_posix()
            fingerprint[rel] = (
                path.stat().st_size,
                hashlib.sha256(path.read_bytes()).hexdigest(),
                path.stat().st_mode,
            )
    return fingerprint


def _outside(fingerprint: dict, data_rel: str) -> dict:
    """The subset of a tree fingerprint that lives outside the data dir."""
    prefix = data_rel + "/"
    return {k: v for k, v in fingerprint.items() if not k.startswith(prefix)}


def test_traversal_usernames_resolve_inside_data_dir_and_stay_there(
    manager, tmp_path
):
    """F1: for each adversarial username, the resolved DB path sits DIRECTLY
    inside data_dir; an open (no such file) writes nothing anywhere; a
    create materializes only hashed, owner-only files inside data_dir and
    never touches the sentinel region outside it."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    outside_dir = tmp_path / "outside"
    outside_dir.mkdir()
    (outside_dir / "keep.txt").write_text("sentinel-outside")
    (tmp_path / "sentinel-root.txt").write_text("sentinel-root")
    data_rel = "encrypted_databases"

    outside_before = _outside(_tree_fingerprint(tmp_path), data_rel)
    data_before = {
        k: v
        for k, v in _tree_fingerprint(tmp_path).items()
        if k.startswith(data_rel + "/")
    }

    seen_filenames = set()
    for username in ADVERSARIAL_USERNAMES:
        # --- OPEN: no file exists for this hashed name; the attempt must
        # return None and write NOTHING (the salt lookup only reads).
        whole_before = _tree_fingerprint(tmp_path)
        assert manager.open_user_database(username, "SomePassword1!") is None, (
            f"open() unexpectedly succeeded for adversarial username "
            f"{username[:24]!r}"
        )
        assert _tree_fingerprint(tmp_path) == whole_before, (
            f"open() for {username[:24]!r} wrote to the filesystem"
        )

        # --- Path resolution: contained, directly inside data_dir.
        db_path = manager._get_user_db_path(username)
        assert db_path.resolve().parent == manager.data_dir.resolve(), (
            f"resolved path for {username[:24]!r} escaped the data dir: "
            f"{db_path}"
        )
        assert HASHED_FILENAME_RE.match(db_path.name), (
            f"filename for {username[:24]!r} is not a plain hashed name: "
            f"{db_path.name}"
        )
        seen_filenames.add(db_path.name)

        # --- CREATE: succeeds (containment, not rejection, is the design);
        # the engine is usable; new files are hashed names inside data_dir.
        engine = manager.create_user_database(username, "SomePassword1!")
        assert engine is not None
        with engine.connect() as conn:
            assert conn.execute(text("SELECT 1")).scalar() == 1
        assert db_path.exists()
        assert manager.data_dir.resolve() in db_path.resolve().parents

        manager.close_user_database(username)

        after = _tree_fingerprint(tmp_path)
        assert _outside(after, data_rel) == outside_before, (
            f"create() for {username[:24]!r} touched files outside the "
            "data directory"
        )
        new_inside = {
            k: v
            for k, v in after.items()
            if k.startswith(data_rel + "/") and k not in data_before
        }
        assert new_inside, (
            f"create() for {username[:24]!r} left no artifacts (test bug)"
        )
        for rel in new_inside:
            assert HASHED_FILENAME_RE.match(rel.split("/", 1)[1]), (
                f"artifact for {username[:24]!r} is not a hashed-user "
                f"filename: {rel}"
            )
            # owner-only, exactly like every legitimate user artifact
            assert (
                manager.data_dir / rel.split("/", 1)[1]
            ).stat().st_mode & 0o777 == 0o600
        data_before = {
            k: v for k, v in after.items() if k.startswith(data_rel + "/")
        }

    # Distinct usernames must not have collided onto one shared file.
    assert len(seen_filenames) == len(ADVERSARIAL_USERNAMES), (
        "two adversarial usernames hashed to the same DB filename"
    )


def test_empty_and_none_usernames_fail_closed_before_filesystem(
    manager, tmp_path
):
    """F2: the sha256 chokepoint raises ValueError for empty/None usernames
    on both entry points, BEFORE any filesystem effect; and ``get_session``
    (a pure cache lookup) just returns None for such keys without raising
    and without side effects."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    (tmp_path / "sentinel-root.txt").write_text("sentinel-root")
    before = _tree_fingerprint(tmp_path)

    # The password is deliberately VALID, and the match= is pinned to the
    # username guard's own wording: both config/paths.py's
    # get_user_database_filename and _is_valid_encryption_key raise plain
    # ValueError, so a bare pytest.raises could not tell which guard
    # fired -- and a regression that started rejecting the password
    # instead would look identical.
    for bad in ("", None):
        with pytest.raises(ValueError, match="empty username"):
            manager.open_user_database(bad, "SomePassword1!")
        with pytest.raises(ValueError, match="empty username"):
            manager.create_user_database(bad, "SomePassword1!")

    assert _tree_fingerprint(tmp_path) == before, (
        "an empty/None username attempt touched the filesystem"
    )
    assert "" not in manager.connections
    assert "" not in manager._password_verifiers

    # get_session's actual contract (read from the code): a dict lookup
    # under _connections_lock -- no path resolution, no open, no raise.
    before_maps = (
        dict(manager.connections),
        dict(manager._password_verifiers),
        dict(manager._init_locks),
    )
    assert manager.get_session("") is None
    assert manager.get_session(None) is None
    assert _tree_fingerprint(tmp_path) == before
    assert (
        dict(manager.connections),
        dict(manager._password_verifiers),
        dict(manager._init_locks),
    ) == before_maps


def test_confusable_usernames_get_nothing_and_disturb_nothing(manager):
    """F3: alice is warm; byte-distinct lookalikes of her name each fail,
    cache nothing, and leave her engine, verifier, files and readability
    untouched. Two genuinely distinct users coexist with distinct files and
    engines as the positive control."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    alice_password = "AliceExactPassword1!"  # noqa: S105
    alice = "alice"
    alice_engine = manager.create_user_database(alice, alice_password)
    with alice_engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_f_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_f_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-F"},
        )
    files_before = {p.name for p in manager.data_dir.iterdir()}

    nfd_lookalike = unicodedata.normalize("NFD", "a\u0301lice")
    assert nfd_lookalike != alice
    attempts = [
        ("Alice", alice_password),
        ("alice ", alice_password),
        ("ALICE", "attacker-plain-guess"),
        (nfd_lookalike, alice_password),
    ]
    for name, pw in attempts:
        assert manager.open_user_database(name, pw) is None, (
            f"confusable username {name!r} opened a database"
        )
        assert name not in manager.connections, (
            f"confusable username {name!r} got a cache entry"
        )
        assert name not in manager._password_verifiers, (
            f"confusable username {name!r} got a verifier entry"
        )

    # alice's world is exactly as it was.
    assert manager.connections[alice] is alice_engine
    assert manager._password_matches_cached(alice, alice_password) is True
    with alice_engine.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_f_canary WHERE id = 1")
            ).scalar()
            == "gap-F"
        )
    assert {p.name for p in manager.data_dir.iterdir()} == files_before, (
        "a confusable-username attempt created or removed files"
    )

    # Positive control: two distinct real users, distinct files + engines.
    bob = f"bob_{uuid.uuid4().hex[:8]}"
    carol = f"carol_{uuid.uuid4().hex[:8]}"
    engine_bob = manager.create_user_database(bob, "BobPassword2!")
    engine_carol = manager.create_user_database(carol, "CarolPassword3!")
    assert engine_bob is not engine_carol
    assert manager._get_user_db_path(bob) != manager._get_user_db_path(carol)
    assert manager.get_connected_usernames() >= {alice, bob, carol}