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.

19.7 KB
Raw Blame
"""Integrity-tamper recovery lifecycle, end to end, with a REAL
``FileIntegrityManager`` + REAL faiss (no fake checksum functions).

This drives the actual production wiring: ``LibraryRAGService`` constructs a
real ``FileIntegrityManager`` (registered with the real ``FAISSIndexVerifier``)
in its ``__init__`` — this test does NOT stub that class out, only its DB
session dependency (routed to a shared in-memory sqlite session, mirroring
``test_no_plaintext_production_path.py`` / ``test_legacy_rekey_orchestrator.py``).
The on-disk ``.faiss`` file and the ``FileIntegrityRecord`` checksum it is
checked against are both real.

Flow covered:

  1. Index a collection through the real service -> a real ``.faiss`` is
     written AND its checksum is recorded by the real integrity manager.
     Search returns the secret content.
  2. TAMPER: flip bytes in the on-disk ``.faiss`` so its checksum no longer
     matches the recorded one.
  3. WRITE-PATH recovery (``force_reindex=True`` -> ``reset_stale_state=True``):
     the tamper is DETECTED (``checksum_mismatch``), the file is quarantined
     to ``<path>.corrupt-*``, the index is rebuilt from the encrypted DB (the
     ``DocumentChunk`` rows), and search works again afterwards.
  4. READ-PATH fails closed (plain ``search()`` -> ``reset_stale_state=False``):
     with a freshly tampered file, search RAISES without renaming/deleting the
     file and without mutating the collection's indexed DB state -- a mere
     read must never destructively quarantine (a byte-flip cost-amplification
     vector: an attacker who can flip one bit of a world-adjacent file must
     not be able to force a full, expensive re-embed merely by triggering a
     search).

Each test below includes a CONTROL proving the assertion actually bites (not
a tautology): #3 checks no quarantine file exists on the healthy baseline
before tampering; #4 checks the pre-tamper search succeeds fine, so the
post-tamper raise is caused by the tamper, not some other test artifact.
"""

import hashlib
import os
import time
import uuid
from contextlib import contextmanager
from unittest.mock import MagicMock

import pytest
from langchain_core.embeddings import Embeddings
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.file_integrity import (
    FileIntegrityRecord,
)
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentChunk,
    DocumentCollection,
    DocumentStatus,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)

_RAG_MOD = "local_deep_research.research_library.services.library_rag_service"
_FACADE_MOD = "local_deep_research.vector_stores.facade"
_INTEGRITY_MOD = "local_deep_research.security.file_integrity.integrity_manager"
_THREAD_MOD = "local_deep_research.database.thread_local_session"

SECRET = "ZZZ_TAMPER_LIFECYCLE_MARKER_9f13cd"


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings (mirrors the canonical harness)."""

    def __init__(self, dim: int = 8):
        self.dim = dim

    def _vec(self, text: str):
        import numpy as np

        rng = np.random.default_rng(abs(hash(text)) % (2**32))
        return rng.random(self.dim).tolist()

    def embed_documents(self, texts):
        return [self._vec(t) for t in texts]

    def embed_query(self, text):
        return self._vec(text)


@contextmanager
def _session_ctx(session):
    yield session


def _sha256(path) -> str:
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for block in iter(lambda: f.read(4096), b""):
            h.update(block)
    return h.hexdigest()


def _tamper(path) -> None:
    """Flip a byte in the middle of the file so its checksum changes, and
    force the mtime to be observably different (some filesystems have coarse
    mtime granularity that could otherwise make a same-second rewrite look
    unchanged to the smart-verification mtime check)."""
    data = bytearray(path.read_bytes())
    assert len(data) > 4, "index file too small to tamper meaningfully"
    mid = len(data) // 2
    data[mid] ^= 0xFF
    path.write_bytes(bytes(data))
    future = time.time() + 5
    os.utime(path, (future, future))


class _Rig:
    def __init__(self, svc, session, collection_id, document_id):
        self.svc = svc
        self.session = session
        self.collection_id = collection_id
        self.document_id = document_id
        self.collection_name = f"collection_{collection_id}"

    def rag_index_row(self):
        return (
            self.session.query(RAGIndex)
            .filter_by(collection_name=self.collection_name, is_current=True)
            .first()
        )

    def index_path(self):
        row = self.rag_index_row()
        assert row is not None
        from pathlib import Path

        return Path(row.index_path)

    def integrity_record(self, path):
        return (
            self.session.query(FileIntegrityRecord)
            .filter_by(file_path=str(path.resolve()))
            .first()
        )


@pytest.fixture
def rig(tmp_path, monkeypatch, mocker, request):
    """Real service, real FileIntegrityManager, real on-disk faiss index, a
    shared in-memory sqlite session standing in for the (encrypted, in
    production) per-user DB, and a real Document+Collection seeded with a
    secret marker -- already indexed once before the test body runs."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))

    engine = create_engine("sqlite:///:memory:")
    request.addfinalizer(engine.dispose)
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    request.addfinalizer(session.close)

    def _shared(*_a, **_k):
        return _session_ctx(session)

    # Route every get_user_db_session call site involved in this flow --
    # the service, the facade, AND the real FileIntegrityManager -- to the
    # SAME shared session so integrity records/verifications and RAGIndex/
    # DocumentChunk rows agree with each other.
    mocker.patch(f"{_RAG_MOD}.get_user_db_session", _shared)
    mocker.patch(f"{_FACADE_MOD}.get_user_db_session", _shared)
    mocker.patch(f"{_INTEGRITY_MOD}.get_user_db_session", _shared)
    # Force the nested-integrity-write path to fall back to
    # get_user_db_session (patched above) rather than a real thread-local
    # session (there is none outside a Flask/background-worker context here).
    mocker.patch(f"{_THREAD_MOD}.get_current_thread_session", return_value=None)

    username = "tamperuser"
    collection_id = uuid.uuid4().hex
    document_id = uuid.uuid4().hex
    source_type = SourceType(
        id=uuid.uuid4().hex, name="document", display_name="Document"
    )
    session.add(source_type)
    session.add(Collection(id=collection_id, name="Tamper test collection"))
    body = (
        f"This document's confidential body mentions {SECRET} several times. "
        * 5
    )
    session.add(
        Document(
            id=document_id,
            source_type_id=source_type.id,
            document_hash=hashlib.sha256(body.encode()).hexdigest(),
            title=f"Secret doc {SECRET}",
            text_content=body,
            original_url="http://example.com/secret",
            file_size=len(body),
            file_type="txt",
            status=DocumentStatus.COMPLETED,
        )
    )
    session.commit()

    emb_mgr = MagicMock()
    emb_mgr.embeddings = _FakeEmbeddings(dim=8)

    from local_deep_research.research_library.services.library_rag_service import (
        LibraryRAGService,
    )

    svc = LibraryRAGService(
        username=username,
        db_password="pw",
        embedding_model="fake-model",
        embedding_provider="sentence_transformers",
        embedding_manager=emb_mgr,
    )
    request.addfinalizer(svc.close)

    # Real FileIntegrityManager, not a mock/stub.
    from local_deep_research.security.file_integrity import FileIntegrityManager

    assert isinstance(svc.integrity_manager, FileIntegrityManager)

    result = svc.index_document(document_id, collection_id)
    assert result["status"] == "success", result
    assert result["chunk_count"] > 0

    r = _Rig(svc, session, collection_id, document_id)

    index_path = r.index_path()
    assert index_path.is_file(), "indexing did not write a real .faiss file"

    record = r.integrity_record(index_path)
    assert record is not None, (
        "the real FileIntegrityManager did not record an integrity checksum "
        "for the freshly indexed .faiss file"
    )
    assert record.checksum == _sha256(index_path)

    hits = svc.search(f"confidential body {SECRET}", collection_id, top_k=3)
    assert hits and any(SECRET in h.text for h in hits), (
        "baseline search did not return the indexed secret content"
    )

    yield r


def _quarantine_files(index_path):
    return [
        path
        for path in index_path.parent.glob(f"{index_path.name}.corrupt-*")
        if path.is_file()
    ]


class TestWritePathRecoversFromTamper:
    def test_reindex_detects_quarantines_and_rebuilds(self, rig):
        index_path = rig.index_path()
        record = rig.integrity_record(index_path)
        baseline_checksum = record.checksum

        # CONTROL: no quarantine artifact exists on the healthy baseline --
        # proves the assertion below is detecting the tamper, not some
        # pre-existing/incidental file from indexing itself.
        assert _quarantine_files(index_path) == []

        _tamper(index_path)
        tampered_checksum = _sha256(index_path)
        assert tampered_checksum != baseline_checksum, (
            "tamper helper did not actually change the file's checksum"
        )

        baseline_chunk_count = rig.rag_index_row().chunk_count
        assert baseline_chunk_count > 0

        # WRITE-path operation: force_reindex=True drives
        # _get_vector_index(reset_stale_state=True), which is the only path
        # authorized to quarantine + rebuild.
        result = rig.svc.index_document(
            rig.document_id, rig.collection_id, force_reindex=True
        )
        assert result["status"] == "success", result

        # 1) Detected + quarantined: a .corrupt-* file now exists holding the
        # TAMPERED bytes (not deleted -- preserved for inspection).
        quarantined = _quarantine_files(index_path)
        assert len(quarantined) == 1, (
            f"expected exactly one quarantined file, got {quarantined}"
        )
        assert _sha256(quarantined[0]) == tampered_checksum

        # 2) Rebuilt: a FRESH, valid .faiss now lives at the original path.
        assert index_path.is_file()
        rebuilt_checksum = _sha256(index_path)
        assert rebuilt_checksum != tampered_checksum
        # The authenticated segmented root must reopen through the provider.
        from local_deep_research.vector_stores.implementations.segmented_faiss import (
            is_segmented,
        )

        assert is_segmented(index_path)

        # 3) The rebuilt file's checksum was re-recorded by the real
        # integrity manager (not left pointing at stale/tampered bytes).
        new_record = rig.integrity_record(index_path)
        assert new_record.checksum == rebuilt_checksum

        # 4) Rebuilt FROM THE ENCRYPTED DB: the DocumentChunk rows for this
        # document/collection still hold the secret text (the store itself
        # never held it), and the vector ids in the rebuilt index match the
        # DB's live chunk ids exactly.
        chunk_rows = (
            rig.session.query(DocumentChunk)
            .filter_by(
                source_type="document",
                source_id=rig.document_id,
                collection_name=rig.collection_name,
            )
            .all()
        )
        assert chunk_rows
        assert any(SECRET in c.chunk_text for c in chunk_rows)

        from local_deep_research.vector_stores import get_vector_store_class

        store_cls = get_vector_store_class()
        rebuilt_dim = rig.rag_index_row().embedding_dimension
        store = store_cls.load(
            index_path,
            dimension=rebuilt_dim,
            index_type="flat",
            metric="cosine",
            normalize=True,
        )
        assert set(store.live_ids()) == {c.id for c in chunk_rows}

        # 5) Search works again on the rebuilt content.
        hits = rig.svc.search(
            f"confidential body {SECRET}", rig.collection_id, top_k=3
        )
        assert hits and any(SECRET in h.text for h in hits), (
            "search did not recover the secret content after rebuild"
        )


class TestReadPathFailsClosed:
    def test_search_raises_without_quarantine_or_db_mutation(self, rig):
        index_path = rig.index_path()
        record_before = rig.integrity_record(index_path)
        baseline_checksum = record_before.checksum

        # CONTROL: the pre-tamper baseline search already succeeded in the
        # fixture -- re-run it here too, immediately before tampering, so the
        # post-tamper raise below is attributable to the tamper itself, not a
        # search path that always raises regardless of file state.
        hits = rig.svc.search(
            f"confidential body {SECRET}", rig.collection_id, top_k=3
        )
        assert hits and any(SECRET in h.text for h in hits)

        # Snapshot the DB state a mere read must not mutate.
        rag_row = rig.rag_index_row()
        chunk_count_before = rag_row.chunk_count
        total_docs_before = rag_row.total_documents
        doc_collection_before = (
            rig.session.query(DocumentCollection)
            .filter_by(
                document_id=rig.document_id, collection_id=rig.collection_id
            )
            .first()
        )
        assert doc_collection_before.indexed is True
        rag_status_before = (
            rig.session.query(RagDocumentStatus)
            .filter_by(
                document_id=rig.document_id, collection_id=rig.collection_id
            )
            .first()
        )
        assert rag_status_before is not None

        _tamper(index_path)
        tampered_checksum = _sha256(index_path)
        assert tampered_checksum != baseline_checksum

        # READ-path operation: plain search() -> reset_stale_state=False.
        with pytest.raises(RuntimeError, match="checksum_mismatch"):
            rig.svc.search(
                f"confidential body {SECRET}", rig.collection_id, top_k=3
            )

        # 1) NOT quarantined: no .corrupt-* file was created.
        assert _quarantine_files(index_path) == [], (
            "a read-path search destructively quarantined the file -- a "
            "byte-flip cost-amplification vector"
        )

        # 2) NOT renamed/deleted: the (tampered) bytes are still at the
        # original path, unchanged by the failed read.
        assert index_path.is_file()
        assert _sha256(index_path) == tampered_checksum

        # 3) DB state (chunk counts / indexed flags / status row) is
        # untouched -- a mere read must never reset indexed state.
        rag_row_after = rig.rag_index_row()
        assert rag_row_after.chunk_count == chunk_count_before
        assert rag_row_after.total_documents == total_docs_before
        doc_collection_after = (
            rig.session.query(DocumentCollection)
            .filter_by(
                document_id=rig.document_id, collection_id=rig.collection_id
            )
            .first()
        )
        assert doc_collection_after.indexed is True
        rag_status_after = (
            rig.session.query(RagDocumentStatus)
            .filter_by(
                document_id=rig.document_id, collection_id=rig.collection_id
            )
            .first()
        )
        assert rag_status_after is not None

        # 4) The integrity record's baseline checksum is unchanged (a failed
        # verification records a FAILURE in the audit trail, but must not
        # silently adopt the tampered bytes as the new "good" checksum).
        record_after = rig.integrity_record(index_path)
        assert record_after.checksum == baseline_checksum


@pytest.mark.parametrize(
    "interrupted", [False, True], ids=["rollback", "process-exit"]
)
def test_uncommitted_manifest_recovers_the_previous_searchable_generation(
    rig, interrupted
):
    from local_deep_research.vector_stores.facade import ChunkInput
    from local_deep_research.vector_stores.segment_files import checkpoint_path

    path = rig.index_path()
    original = path.read_bytes()
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    original_ids = set(index.live_ids())
    index.index(
        source_type="document",
        source_id=rig.document_id,
        chunks=[ChunkInput(text="uncommitted replacement")],
        session=rig.session,
    )
    assert path.read_bytes() != original
    assert checkpoint_path(path).is_file()
    if interrupted:
        # Simulate a process exit: SQLite rolls back, Python callbacks do not run.
        rig.session.info.pop("ldr_vector_transactions")
    rig.session.rollback()
    hits = rig.svc.search(SECRET, rig.collection_id, top_k=5)
    assert hits and any(SECRET in hit.text for hit in hits)
    assert path.read_bytes() == original
    assert (
        set(
            rig.svc._get_vector_index(
                rig.collection_id, rig.collection_name
            ).live_ids()
        )
        == original_ids
    )
    assert not checkpoint_path(path).exists()
    assert _quarantine_files(path) == []


def test_committed_manifest_is_not_reverted_by_an_old_checkpoint(rig):
    from local_deep_research.vector_stores.facade import ChunkInput
    from local_deep_research.vector_stores.segment_files import checkpoint_path

    path = rig.index_path()
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    index.index(
        source_type="document",
        source_id=rig.document_id,
        chunks=[ChunkInput(text="committed replacement")],
        session=rig.session,
    )
    new_manifest = path.read_bytes()
    rig.session.info.pop("ldr_vector_transactions")
    rig.session.commit()  # Simulate exit after commit but before retirement.
    assert checkpoint_path(path).exists()
    hits = rig.svc.search("committed replacement", rig.collection_id, top_k=5)
    assert [hit.text for hit in hits] == ["committed replacement"]
    assert path.read_bytes() == new_manifest


def test_recovery_refuses_a_checkpoint_not_authenticated_by_the_database(rig):
    from local_deep_research.vector_stores.facade import ChunkInput
    from local_deep_research.vector_stores.segment_files import checkpoint_path

    path = rig.index_path()
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    index.index(
        source_type="document",
        source_id=rig.document_id,
        chunks=[ChunkInput(text="uncommitted")],
        session=rig.session,
    )
    rig.session.info.pop("ldr_vector_transactions")
    rig.session.rollback()
    checkpoint_path(path).write_bytes(b"untrusted replacement")
    new_manifest = path.read_bytes()
    with pytest.raises(RuntimeError, match="Integrity"):
        rig.svc.search(SECRET, rig.collection_id, top_k=5)
    assert path.read_bytes() == new_manifest


def test_nested_write_is_refused_before_changing_vector_files(rig):
    from local_deep_research.vector_stores.facade import ChunkInput

    path = rig.index_path()
    original = path.read_bytes()
    index = rig.svc._get_vector_index(rig.collection_id, rig.collection_name)
    with pytest.raises(RuntimeError, match="outer database transaction"):
        with rig.session.begin_nested():
            index.index(
                source_type="document",
                source_id=rig.document_id,
                chunks=[ChunkInput(text="nested replacement")],
                session=rig.session,
            )
    rig.session.commit()
    assert path.read_bytes() == original
    assert any(
        SECRET in hit.text
        for hit in rig.svc.search(SECRET, rig.collection_id, top_k=5)
    )