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.

5.0 KB
Raw Blame
"""``create_user_database`` in a read-only data directory.

Incident context (PR #5596): the cache only ever publishes engines for
creates that fully succeeded; a create that dies mid-way must leave the
filesystem in a state that cannot be mistaken for a usable (or
half-usable) account. A non-writable data directory is the simplest
hostile state that kills a create at its FIRST write -- before any
ciphertext exists.

AA1 (ACTUAL, probed): with the data dir at 0o500, the salt's
``os.open(..., O_CREAT)`` raises PermissionError out of
``create_user_database``; NO partial ``.db`` and NO partial ``.salt``
are left behind (the failure precedes every on-disk write); existing
users' files are untouched; restoring 0o700 lets the same username
create successfully. Complements round-2 H (duplicate/concurrent create
safety) by adding the permission-hostile dimension.
"""

import os
import stat
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 test below starts with `if os.geteuid() == 0: pytest.skip(...)`
# because permission bits are not enforced for root -- under the
# root-default docker-tests.yml container this whole module would
# silently no-op. Unlike ``serial`` this does NOT mutate process-global
# state (no shared state to race), it just needs a genuinely unprivileged
# process, so it opts into the dedicated non-root CI step instead of the
# parallel lane (see the ``nonroot`` marker in pyproject.toml and the
# dedicated step in .github/workflows/docker-tests.yml).
pytestmark = pytest.mark.nonroot


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the round-1..5 files, with the
    sanctioned test-mode KDF knob. One seed user is created and closed so
    the data dir is fully initialized (and its files give the "existing
    artifacts must be untouched" assertion something to protect).
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    seed = f"roseed_{uuid.uuid4().hex[:8]}"
    mgr.create_user_database(seed, "SeedPw1!")  # noqa: S105
    mgr.close_user_database(seed)
    yield mgr
    # Belt and suspenders: restore writability so the fixture teardown
    # (close_all_databases -> WAL checkpoints) and tmp_path cleanup work.
    os.chmod(mgr.data_dir, 0o700)
    mgr.close_all_databases()


def test_create_in_readonly_data_dir_fails_cleanly_with_no_orphans(manager):
    """AA1: 0o500 data dir -> PermissionError, zero partial artifacts,
    existing files untouched, cache untouched; 0o700 restore -> the same
    username creates successfully."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
    if os.geteuid() == 0:
        pytest.skip("permission bits are not enforced for root")

    data_dir = manager.data_dir
    before = sorted(p.name for p in data_dir.iterdir())
    assert before, "setup: the seed user's artifacts must exist"

    username = f"rocreate_{uuid.uuid4().hex[:8]}"
    os.chmod(data_dir, 0o500)
    try:
        assert stat.S_IMODE(os.stat(data_dir).st_mode) == 0o500
        with pytest.raises(PermissionError):
            manager.create_user_database(username, "RoCreatePw1!")  # noqa: S105
    finally:
        os.chmod(data_dir, 0o700)

    # Zero orphans from the failed create; seed artifacts untouched.
    after = sorted(p.name for p in data_dir.iterdir())
    assert after == before, (
        f"the read-only-dir create left orphans or touched existing "
        f"files: new={set(after) - set(before)} gone={set(before) - set(after)}"
    )
    for name in after:
        assert username.split("_")[0] not in name  # none belong to the new user
    assert username not in manager.connections
    assert username not in manager._password_verifiers

    # Restore proven: the same username now creates cleanly.
    engine = manager.create_user_database(username, "RoCreatePw1!")  # noqa: S105
    assert engine is not None
    with engine.begin() as conn:
        conn.execute(
            text("CREATE TABLE gap_aa_canary (id INTEGER PRIMARY KEY, v TEXT)")
        )
        conn.execute(text("INSERT INTO gap_aa_canary VALUES (1, 'gap-AA')"))
    manager.close_user_database(username)
    reopened = manager.open_user_database(username, "RoCreatePw1!")  # noqa: S105
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT v FROM gap_aa_canary WHERE id = 1")
            ).scalar()
            == "gap-AA"
        )