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.

10.4 KB
Raw Blame
"""Read-only / permission-hostile filesystem behavior at open time.

Incident context (PR #5596): the fail-closed cache means every open is
backed by a real SQLCipher open, so hostile filesystem states surface on
the cold path. Two states matter for the at-rest threat model (a local
attacker who can chmod, or a read-only mount):

* R1 the .db file itself made owner-read-only (0o400): ACTUAL pinned
  behavior -- SQLite falls back to a read-only connection and the cold
  open SUCCEEDS with the correct password, publishing a read-only engine
  whose reads work and whose writes fail loudly; restoring 0o600 and
  reopening regains full read/write function;
* R2 the data directory made non-writable (0o500): WAL sidecars cannot be
  created next to the database, the creator fails, and the open refuses
  cleanly (None, nothing published); restoring 0o700 reopens.

Both tests pin the ACTUAL current behavior (verified empirically, not
assumed). Both restore permissions
in ``finally`` blocks so tmp_path cleanup never breaks, and both skip
when running as root (root ignores file modes).
"""

import os
import stat
import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.rekey_lock import (
    DatabaseKeyCheckUnavailableError,
)

# 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)")

# Both tests skip under root 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/2/3 files, with the
    sanctioned test-mode KDF knob.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    mgr.close_all_databases()


def _require_non_root():
    if os.geteuid() == 0:
        pytest.skip("permission tests are meaningless when running as root")


def _make_user(manager, note):
    username = f"rohost_{uuid.uuid4().hex[:8]}"
    password = "ReadOnlyHostilePw1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_r_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_r_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )
    manager.close_user_database(username)
    return username, password


def _assert_nothing_published(manager, username):
    assert username not in manager.connections, (
        "a refused read-only open published an engine"
    )
    assert username not in manager._password_verifiers, (
        "a refused read-only open published a verifier"
    )


def test_readonly_db_file_opens_readonly_writes_fail_restore_reopens(
    manager,
):
    """R1 (ACTUAL behavior, pinned empirically): with the .db at 0o400 the
    cold open SUCCEEDS -- SQLite falls back to a read-only connection and
    the already-WAL file verifies with the correct key -- so the manager
    publishes a working read-only engine. Pinned: reads work, WRITES fail
    loudly, nothing is corrupted; restoring only the .db restores reads
    but NOT writes (SQLite inherits the db mode for the -wal/-shm
    sidecars it creates during the read-only era, and the stale 0o400
    sidecars keep blocking writers); restoring the sidecars too brings
    full function back."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
    _require_non_root()

    username, password = _make_user(manager, "gap-R1")
    db_path = manager._get_user_db_path(username)
    original_mode = stat.S_IMODE(os.stat(db_path).st_mode)

    os.chmod(db_path, 0o400)
    try:
        assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o400
        readonly_engine = manager.open_user_database(username, password)
        assert readonly_engine is not None, (
            "expected the empirically-pinned read-only open to succeed"
        )
        assert manager.connections[username] is readonly_engine
        assert manager._password_matches_cached(username, password) is True

        # Reads work through the read-only engine.
        with readonly_engine.connect() as conn:
            assert (
                conn.execute(
                    text("SELECT note FROM gap_r_canary WHERE id = 1")
                ).scalar()
                == "gap-R1"
            )

        # Writes fail loudly -- an attacker who chmod-ed the file read-only
        # cannot use the manager to mutate the data either.
        with pytest.raises(Exception, match="readonly|read-only"):
            with readonly_engine.begin() as conn:
                conn.execute(
                    text(
                        "INSERT INTO gap_r_canary (id, note) "
                        "VALUES (2, 'must-not-exist')"
                    )
                )
        with readonly_engine.connect() as conn:
            rows = conn.execute(
                text("SELECT COUNT(*) FROM gap_r_canary")
            ).scalar()
        assert rows == 1, "a failed write on the read-only engine mutated data"
    finally:
        os.chmod(db_path, original_mode)

    # Partial recovery (ACTUAL, pinned): restoring only the .db to 0o600
    # brings READS back, but WRITES keep failing: SQLite created the
    # -wal/-shm sidecars during the read-only era INHERITING the db's
    # 0o400 mode, and a writer needs to write the (still 0o400) -wal.
    # The manager surfaces only the generic "attempt to write a readonly
    # database" -- nothing identifies the stale sidecars. See the FINDING
    # in the round-4 summary.
    stale_sidecars = []
    for suffix in ("-wal", "-shm"):
        sidecar = db_path.with_name(db_path.name + suffix)
        if sidecar.exists():
            stale_sidecars.append(sidecar)
            assert stat.S_IMODE(os.stat(sidecar).st_mode) == 0o400, (
                f"setup assumption wrong: {sidecar.name} is not 0o400"
            )
    manager.close_user_database(username)
    reopened = manager.open_user_database(username, password)
    assert reopened is not None, "restore of 0o600 failed to reopen"
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_r_canary WHERE id = 1")
            ).scalar()
            == "gap-R1"
        ), "reads must work again once the .db itself is restored"
    with pytest.raises(Exception, match="readonly|read-only"):
        with reopened.begin() as conn:
            conn.execute(
                text(
                    "INSERT INTO gap_r_canary (id, note) "
                    "VALUES (2, 'blocked-by-sidecar')"
                )
            )

    # Full recovery (ACTUAL): once the stale sidecars are restored too,
    # a fresh open regains full write function and the data is intact.
    try:
        for sidecar in stale_sidecars:
            os.chmod(sidecar, 0o600)
        manager.close_user_database(username)
        writable = manager.open_user_database(username, password)
        assert writable is not None, "full restore failed to reopen"
        with writable.begin() as conn:
            conn.execute(
                text("INSERT INTO gap_r_canary (id, note) VALUES (2, :note)"),
                {"note": "gap-R1-after-full-restore"},
            )
        with writable.connect() as conn:
            assert (
                conn.execute(
                    text("SELECT note FROM gap_r_canary WHERE id = 2")
                ).scalar()
                == "gap-R1-after-full-restore"
            )
            assert (
                conn.execute(
                    text("SELECT note FROM gap_r_canary WHERE id = 1")
                ).scalar()
                == "gap-R1"
            )
    finally:
        for sidecar in stale_sidecars:
            if sidecar.exists():
                os.chmod(sidecar, 0o600)
    assert manager._password_matches_cached(username, password) is True


def test_nonwritable_data_dir_refuses_open_and_restore_reopens(manager):
    """R2: data dir at 0o500 (no write) -- WAL sidecars cannot be created,
    the creator fails, the open refuses cleanly; restoring 0o700 reopens.

    The refusal is the typed ``DatabaseKeyCheckUnavailableError`` (a
    ``RekeyOperationalError``: login answers 503 without a lockout strike),
    never ``None``: ``None`` means "wrong password", and the password here
    is correct -- only the directory is unusable.

    Permissions are restored in ``finally`` so pytest's tmp_path cleanup
    (which must delete files inside this dir) never breaks."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
    _require_non_root()

    username, password = _make_user(manager, "gap-R2")
    db_path = manager._get_user_db_path(username)
    data_dir = db_path.parent
    original_mode = stat.S_IMODE(os.stat(data_dir).st_mode)

    os.chmod(data_dir, 0o500)
    try:
        assert stat.S_IMODE(os.stat(data_dir).st_mode) == 0o500
        with pytest.raises(DatabaseKeyCheckUnavailableError):
            manager.open_user_database(username, password)
        _assert_nothing_published(manager, username)
    finally:
        os.chmod(data_dir, original_mode)

    reopened = manager.open_user_database(username, password)
    assert reopened is not None, "restore of 0o700 failed to reopen"
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_r_canary WHERE id = 1")
            ).scalar()
            == "gap-R2"
        )
    assert manager._password_matches_cached(username, password) is True