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.

8.3 KB
Raw Blame
"""Real SQLCipher backup permissions and recovery-input independence.

Temporary and completed database files use owner-only permissions. Salt
sidecars travel with backups, so removing the original salt cannot strand
an independently verified backup. This module is serial/nonroot because
its permission probes temporarily change the process-global umask.
"""

import hashlib
import os
import shutil
import stat
import threading
import time
from pathlib import Path

import pytest

# Module-level gate: without a working SQLCipher build every test below
# is a no-op; skipping at import time makes that one visible signal.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

pytestmark = [pytest.mark.serial, pytest.mark.nonroot]

REALISTIC_UMASKS = [0o022, 0o002]

SQLITE_MAGIC = b"SQLite format 3\x00"

# Rows x ~1KB: enough export time for the poller to reliably sample the
# mid-flight .tmp without making the test slow (~10MB source).
_SOURCE_ROWS = 10000
_ROW_PAYLOAD = "x" * 1000


@pytest.fixture
def hunt_env(tmp_path, monkeypatch):
    """Isolated data dir with the sanctioned fast-KDF test knob."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    return tmp_path


def _create_source_db(username: str, password: str) -> Path:
    """Create a real salted SQLCipher source DB at its true service path."""
    from local_deep_research.config.paths import (
        get_encrypted_database_path,
        get_user_database_filename,
    )
    from local_deep_research.database.sqlcipher_utils import (
        create_database_salt,
        create_sqlcipher_connection,
    )

    db_path = get_encrypted_database_path() / get_user_database_filename(
        username
    )
    create_database_salt(db_path)
    conn = create_sqlcipher_connection(
        str(db_path), password, creation_mode=True
    )
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE blob (id INTEGER PRIMARY KEY, data TEXT)")
    cursor.executemany(
        "INSERT INTO blob (data) VALUES (?)",
        [(_ROW_PAYLOAD,) for _ in range(_SOURCE_ROWS)],
    )
    conn.commit()
    cursor.close()
    conn.close()
    return db_path


def _digest(path: Path) -> str:
    return hashlib.sha256(path.read_bytes()).hexdigest()


class TestBackupTmpPermissionWindow:
    """C2: mid-flight ``.tmp`` posture and final-artifact hardening."""

    @pytest.mark.parametrize("umask", REALISTIC_UMASKS)
    def test_tmp_window_ciphertext_only_and_final_owner_only(
        self, hunt_env, umask
    ):
        """Under realistic umasks the ``.tmp`` is never group/world
        WRITABLE mid-flight, never exposes a plaintext SQLite header, the
        final backup is exactly 0600, the backups dir exactly 0700, and
        no ``.tmp`` survives."""
        if os.geteuid() == 0:
            pytest.skip("permission bits are not enforced for root")

        from local_deep_research.database.backup.backup_service import (
            BackupService,
        )

        username = f"c2probe_{umask:o}"
        password = "C2Probe-Pw-1!"  # noqa: S105
        _create_source_db(username, password)

        service = BackupService(username=username, password=password)
        backup_dir = service.backup_dir
        observed: list[tuple[int, bytes]] = []
        stop = threading.Event()

        def poll():
            while not stop.is_set():
                for entry in os.scandir(backup_dir):
                    if entry.name.endswith(".tmp"):
                        try:
                            mode = stat.S_IMODE(entry.stat().st_mode)
                            with open(entry.path, "rb") as fh:
                                head = fh.read(len(SQLITE_MAGIC))
                            observed.append((mode, head))
                        except (FileNotFoundError, PermissionError):
                            pass
                time.sleep(0.001)

        original = os.umask(umask)
        try:
            poller = threading.Thread(target=poll)
            poller.start()
            try:
                result = service.create_backup(force=True)
            finally:
                stop.set()
                poller.join(timeout=5)
        finally:
            os.umask(original)

        assert result.success, f"backup failed: {result.error}"
        assert result.backup_path is not None

        # The window was actually observed (fail loud if the poll never
        # saw the .tmp -- the assertions below would silently vacate).
        assert observed, (
            "poller never sampled the .tmp; widen the export or fix the test"
        )

        # Window posture: never group/world-writable under this umask...
        for mode, _ in observed:
            assert mode & 0o022 == 0, (
                f"mid-flight .tmp is group/world-writable ({oct(mode)}) "
                f"under umask {umask:o}"
            )
        # ...and never plaintext: no sample carries the SQLite magic.
        for _, head in observed:
            assert head != SQLITE_MAGIC, (
                "mid-flight .tmp exposes a plaintext SQLite header -- the "
                "documented ciphertext-only window assumption is FALSE"
            )

        # Final posture.
        assert stat.S_IMODE(os.stat(result.backup_path).st_mode) == 0o600, (
            "final backup is not exactly owner-only"
        )
        assert stat.S_IMODE(os.stat(backup_dir).st_mode) == 0o700, (
            "per-user backups directory is not exactly 0o700"
        )
        assert list(backup_dir.glob("*.tmp")) == [], "orphaned .tmp files"

    def test_final_artifact_owner_only_under_default_umask(self, hunt_env):
        """Whatever the runner's own umask, the final backup lands exactly
        0600 (explicit chmod after verify -- not umask-masked) in a 0700
        directory."""
        if os.geteuid() == 0:
            pytest.skip("permission bits are not enforced for root")

        from local_deep_research.database.backup.backup_service import (
            BackupService,
        )

        username = "c2default"
        password = "C2Default-Pw-1!"  # noqa: S105
        _create_source_db(username, password)

        service = BackupService(username=username, password=password)
        result = service.create_backup(force=True)

        assert result.success, f"backup failed: {result.error}"
        assert result.backup_path is not None
        assert stat.S_IMODE(os.stat(result.backup_path).st_mode) == 0o600, (
            "final backup mode depends on umask -- explicit chmod regressed"
        )
        assert stat.S_IMODE(os.stat(service.backup_dir).st_mode) == 0o700


class TestBackupSaltStranding:
    """Deleting the source salt must not strand a completed backup."""

    def test_missing_source_salt_does_not_strand_backup(self, hunt_env):
        """Verify and reopen a real backup after losing the source salt."""
        from local_deep_research.database.backup.backup_service import (
            BackupService,
        )
        from local_deep_research.database.sqlcipher_utils import (
            create_sqlcipher_connection,
            get_salt_file_path,
        )

        username = "c5strand"
        password = "C5Strand-Pw-1!"
        db_path = _create_source_db(username, password)
        salt_path = get_salt_file_path(db_path)
        service = BackupService(username=username, password=password)
        result = service.create_backup(force=True)
        assert result.success, result.error
        backup_path = result.backup_path
        assert backup_path is not None
        assert (
            get_salt_file_path(backup_path).read_bytes()
            == salt_path.read_bytes()
        )
        before_digest = _digest(backup_path)
        artifacts_before = sorted(p.name for p in service.backup_dir.iterdir())

        salt_path.unlink()
        assert service._verify_backup(backup_path)
        recovered = hunt_env / "recovered.db"
        shutil.copy2(backup_path, recovered)
        shutil.copy2(
            get_salt_file_path(backup_path), get_salt_file_path(recovered)
        )
        conn = create_sqlcipher_connection(recovered, password)
        try:
            assert conn.execute("SELECT COUNT(*) FROM blob").fetchone() == (
                _SOURCE_ROWS,
            )
        finally:
            conn.close()
        assert _digest(backup_path) == before_digest
        assert (
            sorted(p.name for p in service.backup_dir.iterdir())
            == artifacts_before
        )