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.

13.7 KB
Raw Blame
"""Extended GREEN disk-write guardrail battery.

Complements ``test_disk_write_guards.py`` without duplicating its first-pin
coverage.  Every test uses only ``tmp_path`` and asserts current secure
behavior: a failure is a regression, not a reason to weaken the assertion.

Surfaces covered:
1. legacy_rekey cleanup/quarantine mutations stay scoped to their cache paths.
2. FAISS stale-temp sweeping stays scoped to the target index directory/name.
3. Server-mode open-file-location routes refuse before any subprocess spawn.
4. Failed/crashed backup creation leaves no partial or outside-root artifacts.
"""

import os
import re
import sys
import threading
import time
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock

import numpy as np
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


# --------------------------------------------------------------------------
# Surface 1: login-time legacy re-key cleanup containment
# (src/local_deep_research/vector_stores/legacy_rekey.py)
# --------------------------------------------------------------------------
def test_purge_redundant_pkl_only_unlinks_its_own_companion(tmp_path):
    from local_deep_research.vector_stores.legacy_rekey import (
        _purge_redundant_pkl,
    )

    cache_root = tmp_path / "rag_indices"
    cache_root.mkdir()
    faiss_path = cache_root / "index.faiss"
    pkl_path = cache_root / "index.pkl"
    faiss_path.write_bytes(b"index")
    pkl_path.write_bytes(b"legacy plaintext")

    outside = tmp_path / "outside"
    outside.mkdir()
    outside_canary = outside / "index.pkl"
    outside_canary.write_bytes(b"outside plaintext")
    in_root_canary = cache_root / "keep.txt"
    in_root_canary.write_text("keep")

    assert _purge_redundant_pkl(faiss_path, "password") is True

    assert faiss_path.exists()
    assert not pkl_path.exists()
    assert outside_canary.read_bytes() == b"outside plaintext"
    assert in_root_canary.read_text() == "keep"


@contextmanager
def _session_context(session):
    yield session


@pytest.fixture
def rekey_session(monkeypatch):
    from local_deep_research.database.models import Base
    import local_deep_research.vector_stores.legacy_rekey as rekey_module

    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    monkeypatch.setattr(
        rekey_module,
        "get_user_db_session",
        lambda *args, **kwargs: _session_context(session),
    )
    monkeypatch.setattr(rekey_module, "FileIntegrityManager", MagicMock())
    yield session
    session.close()
    engine.dispose()


def _add_rag_index(session, faiss_path):
    from local_deep_research.database.models.library import (
        EmbeddingProvider,
        RAGIndex,
    )

    row = RAGIndex(
        collection_name="collection_scoped",
        embedding_model="test-model",
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=2,
        index_path=str(faiss_path),
        index_hash="scoped-index-hash",
        chunk_size=500,
        chunk_overlap=50,
        index_type="flat",
        distance_metric="cosine",
        normalize_vectors=False,
        is_current=True,
    )
    session.add(row)
    session.commit()


def test_orphaned_sidecar_cleanup_stays_inside_cache_root(
    tmp_path, rekey_session
):
    from local_deep_research.vector_stores.legacy_rekey import (
        rekey_user_indexes,
    )

    cache_root = tmp_path / "rag_indices"
    index_dir = cache_root / "user_indexes"
    index_dir.mkdir(parents=True)
    faiss_path = index_dir / "index.faiss"
    sidecar_path = index_dir / "index.idmap.json"
    pkl_path = index_dir / "index.pkl"
    sidecar_path.write_text("{}")
    pkl_path.write_bytes(b"legacy plaintext")

    outside = tmp_path / "outside"
    outside.mkdir()
    outside_canary = outside / "index.pkl"
    outside_canary.write_bytes(b"outside plaintext")
    in_root_canary = index_dir / "unrelated.txt"
    in_root_canary.write_text("keep")
    _add_rag_index(rekey_session, faiss_path)

    stats = rekey_user_indexes("scoped-user", "password")

    assert stats["checked"] == 1
    assert stats["skipped_no_faiss"] == 1
    assert not sidecar_path.exists()
    assert not pkl_path.exists()
    assert outside_canary.read_bytes() == b"outside plaintext"
    assert in_root_canary.read_text() == "keep"


def test_quarantine_file_effects_stay_inside_cache_root(tmp_path, monkeypatch):
    from local_deep_research.database.models.library import (
        DocumentCollection,
        RAGIndex,
        RagDocumentStatus,
    )
    import local_deep_research.vector_stores.legacy_rekey as rekey_module

    cache_root = tmp_path / "rag_indices"
    index_dir = cache_root / "user_indexes"
    index_dir.mkdir(parents=True)
    faiss_path = index_dir / "index.faiss"
    sidecar_path = index_dir / "index.idmap.json"
    pkl_path = index_dir / "index.pkl"
    faiss_path.write_bytes(b"broken index")
    sidecar_path.write_text("{}")
    pkl_path.write_bytes(b"legacy plaintext")

    outside = tmp_path / "outside"
    outside.mkdir()
    outside_canary = outside / "index.pkl"
    outside_canary.write_bytes(b"outside plaintext")
    in_root_canary = index_dir / "unrelated.txt"
    in_root_canary.write_text("keep")

    rag_index = MagicMock(id=7, collection_name="collection_scoped")
    idx = MagicMock(id=7, is_current=True)
    q_ragindex = MagicMock()
    q_ragindex.filter_by.return_value.first.return_value = idx
    q_status = MagicMock()
    q_collection = MagicMock()
    session = MagicMock()
    session.query.side_effect = lambda model: {
        RAGIndex: q_ragindex,
        RagDocumentStatus: q_status,
        DocumentCollection: q_collection,
    }[model]
    monkeypatch.setattr(
        rekey_module,
        "get_user_db_session",
        lambda *args, **kwargs: _session_context(session),
    )

    rekey_module._quarantine_and_reset(
        "scoped-user",
        "password",
        rag_index,
        faiss_path,
        sidecar_path,
        threading.Lock(),
    )

    assert not faiss_path.exists()
    assert not sidecar_path.exists()
    assert not pkl_path.exists()
    assert list(index_dir.glob("index.faiss.corrupt-*"))
    assert list(index_dir.glob("index.idmap.json.corrupt-*"))
    assert outside_canary.read_bytes() == b"outside plaintext"
    assert in_root_canary.read_text() == "keep"


# --------------------------------------------------------------------------
# Surface 2: FAISS stale-temp sweep containment
# (src/local_deep_research/vector_stores/implementations/faiss_store.py)
# --------------------------------------------------------------------------
def _make_store():
    from local_deep_research.vector_stores.implementations.faiss_store import (
        FaissVectorStore,
    )

    store = FaissVectorStore.create(
        dimension=2,
        index_type="flat",
        metric="l2",
        normalize=False,
    )
    store.add([1], np.array([[1.0, 2.0]], dtype="float32"))
    return store


def test_faiss_stale_tmp_sweep_is_confined_to_index_directory(tmp_path):
    from local_deep_research.vector_stores.implementations.faiss_store import (
        _STALE_TMP_AGE_SECONDS,
    )

    index_dir = tmp_path / "index_dir"
    sibling_dir = tmp_path / "sibling_dir"
    nested_dir = index_dir / "nested"
    for directory in (index_dir, sibling_dir, nested_dir):
        directory.mkdir(parents=True, exist_ok=True)
    sibling_canary = sibling_dir / "index.faiss.orphan.tmp"
    nested_canary = nested_dir / "index.faiss.orphan.tmp"
    for canary in (sibling_canary, nested_canary):
        canary.write_bytes(b"stale elsewhere")
        old_time = time.time() - _STALE_TMP_AGE_SECONDS - 1
        os.utime(canary, (old_time, old_time))

    _make_store().persist(index_dir / "index.faiss")

    assert (index_dir / "index.faiss").is_file()
    assert sibling_canary.read_bytes() == b"stale elsewhere"
    assert nested_canary.read_bytes() == b"stale elsewhere"


def test_faiss_sweep_spares_stale_foreign_prefix_tmp_in_index_dir(tmp_path):
    from local_deep_research.vector_stores.implementations.faiss_store import (
        _STALE_TMP_AGE_SECONDS,
    )

    index_dir = tmp_path / "index_dir"
    index_dir.mkdir()
    foreign_canary = index_dir / "other-index.orphan.tmp"
    foreign_canary.write_bytes(b"not this index")
    old_time = time.time() - _STALE_TMP_AGE_SECONDS - 1
    os.utime(foreign_canary, (old_time, old_time))

    _make_store().persist(index_dir / "index.faiss")

    assert foreign_canary.read_bytes() == b"not this index"


# --------------------------------------------------------------------------
# Surface 3: server-mode file-manager routes and utility subprocess guard
# (web/routers/research.py, web/routers/settings.py,
#  research_library/utils/__init__.py)
#
# The two route-level refusal tests that used to live here called the
# Flask handlers directly through a bare ``app.test_request_context()``.
# The FastAPI migration (#3299) deleted ``web.routes.research_routes`` and
# ``web.routes.settings_routes`` entirely, and both replacement routers
# (``web/routers/research.py`` / ``web/routers/settings.py``) now refuse
# ``POST /open_file_location`` and ``POST /settings/open_file_location``
# unconditionally with a 403 and never touch ``subprocess`` at all — so
# there is no "unwrap the handler and call it directly" shape left to
# port here. That refusal is already pinned end to end (auth + the real
# FastAPI route) by
# ``tests/web/routers/test_research_route_error_envelopes.py::test_open_file_location_is_always_refused``,
# which is a strict superset of what stood here. The utility-level guard
# below is independent of the route layer and is unaffected.
# --------------------------------------------------------------------------


@pytest.mark.parametrize(
    "hostile_path", ["/etc/passwd", "/etc/shadow", "../../etc/passwd"]
)
def test_open_file_location_utility_never_spawns_for_hostile_path(
    hostile_path, monkeypatch
):
    import subprocess
    from local_deep_research.research_library.utils import open_file_location

    calls = []

    def record_run(*args, **kwargs):
        calls.append((args, kwargs))
        return MagicMock(returncode=0)

    monkeypatch.setattr(subprocess, "run", record_run)

    assert open_file_location(hostile_path) is False
    assert calls == []


# --------------------------------------------------------------------------
# Surface 4: failed backup crash cleanup
# (src/local_deep_research/database/backup/backup_service.py)
# --------------------------------------------------------------------------
@pytest.fixture
def backup_service(tmp_path, monkeypatch):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    from local_deep_research.database.backup.backup_service import BackupService

    service = BackupService("crash-guard-user", "password", max_backups=3)
    service.db_path.parent.mkdir(parents=True, exist_ok=True)
    service.db_path.write_bytes(b"source database bytes")
    service.db_path.chmod(0o600)
    return service


def _patch_backup_export(monkeypatch, service, *, crash_during_export):
    import local_deep_research.database.backup.backup_service as backup_module

    temp_paths = []
    cursor = MagicMock()

    def execute(sql, *args, **kwargs):
        if sql.startswith("ATTACH DATABASE"):
            match = re.search(r"ATTACH DATABASE '([^']+)'", sql)
            assert match is not None
            temp_path = Path(match.group(1).replace("''", "'"))
            temp_path.write_bytes(b"partial encrypted backup")
            temp_paths.append(temp_path)
        if "sqlcipher_export" in sql and crash_during_export:
            raise RuntimeError("simulated crash during backup export")

    cursor.execute.side_effect = execute
    connection = MagicMock()
    connection.cursor.return_value = cursor
    monkeypatch.setattr(
        backup_module,
        "create_sqlcipher_connection",
        lambda *args, **kwargs: connection,
    )
    monkeypatch.setattr(
        backup_module,
        "get_key_from_password",
        lambda *args, **kwargs: b"\xab" * 32,
    )
    return temp_paths


def _assert_no_crash_artifacts(service, tmp_path, outside_canary):
    assert list(service.backup_dir.glob("ldr_backup_*.db.tmp")) == []
    assert list(service.backup_dir.glob("ldr_backup_*.db")) == []
    assert outside_canary.read_bytes() == b"outside canary"
    assert not [
        path
        for path in tmp_path.rglob("*")
        if path.is_file() and path.stat().st_mode & 0o022
    ]


@pytest.mark.skipif(
    sys.platform == "win32", reason="POSIX permission bits required"
)
def test_crashed_backup_export_leaves_no_partial_or_outside_artifacts(
    backup_service, tmp_path, monkeypatch
):
    outside_canary = tmp_path / "outside-canary.bin"
    outside_canary.write_bytes(b"outside canary")
    outside_canary.chmod(0o600)
    temp_paths = _patch_backup_export(
        monkeypatch, backup_service, crash_during_export=True
    )

    result = backup_service.create_backup(force=True)

    assert result.success is False
    assert temp_paths
    _assert_no_crash_artifacts(backup_service, tmp_path, outside_canary)


@pytest.mark.skipif(
    sys.platform == "win32", reason="POSIX permission bits required"
)
def test_failed_backup_verification_deletes_partial_tmp(
    backup_service, tmp_path, monkeypatch
):
    outside_canary = tmp_path / "outside-canary.bin"
    outside_canary.write_bytes(b"outside canary")
    outside_canary.chmod(0o600)
    temp_paths = _patch_backup_export(
        monkeypatch, backup_service, crash_during_export=False
    )
    monkeypatch.setattr(
        type(backup_service), "_verify_backup", lambda self, path: False
    )

    result = backup_service.create_backup(force=True)

    assert result.success is False
    assert temp_paths
    _assert_no_crash_artifacts(backup_service, tmp_path, outside_canary)