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.

12.3 KB
Raw Blame
"""Cross-TENANT isolation: two independent users, two independent (in-memory)
DBs, two independent on-disk index directories, and — deliberately — COLLIDING
integer ``DocumentChunk.id`` values (each user's DB is a fresh autoincrement
sequence, so user A's first chunk and user B's first chunk both get id=1).

The facade's search-time rehydration (``VectorIndex.search`` in
``vector_stores/facade.py``) resolves a FAISS hit's int id to text by querying
the CALLER's own DB session for a ``DocumentChunk`` row. There is no
username/tenant column in that query — the isolation guarantee is structural:
each user's ``VectorIndex`` is wired to open (or receive) ONLY that user's own
DB session, so an id collision across tenants can never resolve to the wrong
person's text. If a future refactor ever made ``get_user_db_session`` return a
shared/global session, or made search fall back to some pooled session,
colliding ids would silently rehydrate the WRONG tenant's secret text — this
test is built so that regression fails loudly.

Mirrors the real-components fixture style of
``test_no_plaintext_production_path.py`` (fake deterministic network-free
embeddings, real ``Base.metadata.create_all`` sqlite engine, real on-disk
``VectorIndex``/``FaissVectorStore``), but parameterized per user: TWO
engines/sessions and TWO index directories, with
``vector_stores.facade.get_user_db_session`` patched to a small router that
sends each username to its OWN session — exactly mirroring how, in
production, each user's request is bound to their own SQLCipher-encrypted DB
file.
"""

import threading
from contextlib import contextmanager

import numpy as np
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.library import (
    DocumentChunk,
    EmbeddingProvider,
)
from local_deep_research.vector_stores.facade import ChunkInput, VectorIndex

# Distinctive secrets, one per tenant. If isolation is broken, user B's search
# results (or user B's on-disk index files) would contain user A's secret.
SECRET_A = "ZZZ_TENANT_A_ONLY_SECRET_8f13ad02"
SECRET_B = "ZZZ_TENANT_B_ONLY_SECRET_c4de9917"


class _FakeEmbeddings(Embeddings):
    """Deterministic, network-free embeddings so the marker can never survive
    into a vector as raw bytes (keeps any on-disk grep sound)."""

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

    def _vec(self, text: str):
        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)


def _noop(_p):
    return None


def _ok(_p):
    return (True, None)


def _grep_all(root, needle: bytes):
    """Every on-disk file under ``root`` that contains ``needle`` (raw bytes)."""
    return [
        str(f)
        for f in root.rglob("*")
        if f.is_file() and needle in f.read_bytes()
    ]


class _Tenant:
    """One independent user's world: its own in-memory DB/session and its own
    on-disk index directory, plumbed into a real ``VectorIndex``."""

    def __init__(self, tmp_path, username: str, collection_name: str):
        self.username = username
        self.db_password = f"pw-{username}"  # gitleaks:allow
        self.engine = None
        self.session = None
        try:
            self.engine = create_engine("sqlite:///:memory:")
            Base.metadata.create_all(self.engine)
            self.session = sessionmaker(bind=self.engine)()
            self.index_dir = tmp_path / username
            self.index_dir.mkdir()
            self.vi = VectorIndex(
                username=username,
                db_password=self.db_password,
                embeddings=_FakeEmbeddings(dim=8),
                embedding_model="fake-model",
                embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
                collection_name=collection_name,
                dimension=8,
                path=self.index_dir / f"{collection_name}.faiss",
                lock=threading.Lock(),
                integrity_record=_noop,
                integrity_verify=_ok,
                index_type="flat",
                metric="cosine",
                normalize=True,
            )
        except BaseException:
            # Construction happens before the fixture can register its
            # finalizer, so partial instances must release their own resources.
            self.close()
            raise

    def close(self):
        """Release the tenant's session and its pooled SQLite connection."""
        try:
            if self.session is not None:
                self.session.close()
        finally:
            if self.engine is not None:
                self.engine.dispose()


@pytest.fixture
def tenants(tmp_path, mocker, request):
    """Two tenants, SAME logical collection_name (so ids collide), each with
    its own DB session and its own on-disk directory. ``get_user_db_session``
    is patched at the facade's import site to route by username to the
    correct tenant's session — mirroring how production binds a request to
    the caller's own encrypted per-user DB.
    """
    a = _Tenant(tmp_path, "userA", collection_name="c")
    request.addfinalizer(a.close)
    b = _Tenant(tmp_path, "userB", collection_name="c")
    request.addfinalizer(b.close)
    by_username = {"userA": a.session, "userB": b.session}

    @contextmanager
    def _route_session(username=None, password=None):
        yield by_username[username]

    mocker.patch(
        "local_deep_research.vector_stores.facade.get_user_db_session",
        _route_session,
    )

    yield a, b


def test_colliding_ids_do_not_cross_tenant_boundary(tenants):
    """Sanity precondition: both tenants really do get DocumentChunk.id == 1
    (separate DBs => separate autoincrement sequences). Without this, the
    "isolation despite colliding ids" claim below would be vacuous."""
    a, b = tenants
    a.vi.index(
        source_type="document",
        source_id="doc-a",
        chunks=[ChunkInput(text=f"User A's private note: {SECRET_A}")],
    )
    b.vi.index(
        source_type="document",
        source_id="doc-b",
        chunks=[ChunkInput(text=f"User B's private note: {SECRET_B}")],
    )

    a_row = a.session.query(DocumentChunk).filter_by(source_id="doc-a").one()
    b_row = b.session.query(DocumentChunk).filter_by(source_id="doc-b").one()
    assert a_row.id == 1
    assert b_row.id == 1, (
        "precondition failed: tenants' chunk ids do not collide, so this "
        "test cannot exercise the cross-tenant rehydration boundary"
    )


def test_search_in_tenant_b_never_returns_or_rehydrates_tenant_a_secret(
    tenants,
):
    a, b = tenants
    a.vi.index(
        source_type="document",
        source_id="doc-a",
        chunks=[ChunkInput(text=f"User A's private note: {SECRET_A}")],
    )
    b.vi.index(
        source_type="document",
        source_id="doc-b",
        chunks=[ChunkInput(text=f"User B's private note: {SECRET_B}")],
    )
    # Confirm the id collision this test is meant to stress.
    a_row = a.session.query(DocumentChunk).filter_by(source_id="doc-a").one()
    b_row = b.session.query(DocumentChunk).filter_by(source_id="doc-b").one()
    assert a_row.id == b_row.id == 1

    # A broad query run entirely inside user B's world (B's VectorIndex, which
    # is wired only to B's own DB session and B's own on-disk store).
    results = b.vi.search("private note", top_k=10)

    assert results, "expected user B's own chunk to be found"
    assert all(SECRET_A not in r.text for r in results), (
        "user A's secret leaked into user B's search results despite a "
        "colliding DocumentChunk.id across the two tenants' separate DBs"
    )
    assert all(SECRET_B in r.text or r.text == "" for r in results)
    # And it really did surface user B's own content (the assertion above
    # isn't vacuously true because search returned nothing relevant).
    assert any(SECRET_B in r.text for r in results)

    # Sanity check the assertion actually bites: if B's rehydration query were
    # NOT scoped to B's own session (e.g. searched a shared/global session
    # containing both rows), asking A's session for the same id DOES return
    # A's secret-bearing row — proving the guard is doing real work, not
    # trivially passing because the row doesn't exist anywhere.
    cross_row = a.session.query(DocumentChunk).filter_by(id=b_row.id).one()
    assert SECRET_A in cross_row.chunk_text


def test_tenant_a_secret_never_appears_on_disk_under_tenant_b_index_dir(
    tenants,
):
    a, b = tenants
    a.vi.index(
        source_type="document",
        source_id="doc-a",
        chunks=[ChunkInput(text=f"User A's private note: {SECRET_A}")],
    )
    b.vi.index(
        source_type="document",
        source_id="doc-b",
        chunks=[ChunkInput(text=f"User B's private note: {SECRET_B}")],
    )
    b.vi.search("private note", top_k=10)  # exercise B's read path too

    # B's on-disk store directory must contain no trace of A's secret. (The
    # store only ever holds vectors + int ids regardless of tenant, so this
    # mainly guards against a future regression that writes text/metadata
    # sidecars per tenant directory.)
    leaked = _grep_all(b.index_dir, SECRET_A.encode())
    assert leaked == [], (
        f"user A's secret leaked onto disk under user B's index dir: {leaked}"
    )

    # Sanity: the grep itself is capable of finding a real hit (proves the
    # helper isn't silently vacuous) — user B's OWN secret is not expected to
    # be on disk either (store holds no text), but B's index FILE must exist.
    faiss_file = b.index_dir / "c.faiss"
    assert faiss_file.is_file()
    assert faiss_file.stat().st_size > 0


def test_delete_ids_cross_tenant_never_deletes_other_tenants_row_on_colliding_id(
    tenants,
):
    """``delete_ids`` is the destructive path a caller uses to batch-clean
    vectors after already deleting DocumentChunk rows elsewhere (see its
    docstring in ``facade.py``). It re-runs ``_finalize_db`` -> a raw
    ``DocumentChunk.id.in_(ids)`` delete with NO tenant/username column in the
    filter — isolation depends entirely on the routed session (``self._db``)
    only ever being the CALLING VectorIndex's own session/store. Both tenants'
    first chunk lands on the SAME numeric id (fresh autoincrement per DB), so
    if ``delete_ids`` were ever called against the wrong session — or a future
    refactor made ``get_user_db_session`` return something shared — user B's
    call would destroy user A's row/vector too.
    """
    a, b = tenants
    a.vi.index(
        source_type="document",
        source_id="doc-a",
        chunks=[ChunkInput(text=f"User A's private note: {SECRET_A}")],
    )
    b.vi.index(
        source_type="document",
        source_id="doc-b",
        chunks=[ChunkInput(text=f"User B's private note: {SECRET_B}")],
    )
    a_row = a.session.query(DocumentChunk).filter_by(source_id="doc-a").one()
    b_row = b.session.query(DocumentChunk).filter_by(source_id="doc-b").one()
    assert a_row.id == b_row.id == 1, (
        "precondition failed: tenants' chunk ids do not collide, so this "
        "test cannot exercise the cross-tenant delete_ids boundary"
    )
    assert a.vi.count() == 1
    assert b.vi.count() == 1

    # User B deletes ITS OWN chunk id (which numerically collides with A's).
    stats = b.vi.delete_ids([b_row.id])
    assert stats.rows_deleted == 1
    assert stats.removed == 1

    # B's own row/vector is gone.
    assert (
        b.session.query(DocumentChunk).filter_by(id=b_row.id).one_or_none()
        is None
    )
    assert b.vi.count() == 0
    assert b.vi.search("private note", top_k=10) == []

    # User A's row, with the SAME numeric id, must be completely untouched:
    # still in A's DB, still text-intact, still vector-searchable, and A's
    # on-disk store still holds it.
    surviving_a_row = (
        a.session.query(DocumentChunk).filter_by(id=a_row.id).one()
    )
    assert SECRET_A in surviving_a_row.chunk_text
    assert a.vi.count() == 1
    a_results = a.vi.search("private note", top_k=10)
    assert any(SECRET_A in r.text for r in a_results), (
        "user B's delete_ids([1]) deleted user A's colliding-id row/vector"
    )