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.

15.1 KB
Raw Blame
"""Cross-user IDOR regression guards for the notes feature.

LDR is a multi-user app: each user has their own encrypted SQLite
database, and the per-user DB session is the structural isolation
primitive (Document rows have no user_id column). These tests guard
that primitive — if a future refactor causes NoteService to route
queries to a global / wrong-user session, the assertions here flip.

``tests/notes/`` previously had no cross-user coverage. This file
closes that gap with one targeted test per surface — both reads
(get / version read / list / outgoing-links / backlinks) and writes
(update / delete) — so a future refactor that introduces a
per-method routing bypass gets caught at the right call site.
"""

import uuid
from contextlib import contextmanager

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import (
    Base,
    Document,
    NoteLink,
    NoteVersion,
    SourceType,
)
from local_deep_research.research_library.notes.services.note_service import (
    NoteService,
)

from tests.notes.helpers import _generate_hash


@pytest.fixture
def two_user_dbs(monkeypatch, request):
    """Build two independent in-memory SQLite engines and route
    ``get_user_db_session`` to the right one based on username.

    The whole point of the test: each user has a structurally separate
    database. The shim mirrors what the production
    ``get_user_db_session`` does (returns a session bound to the
    user's encrypted DB file), just collapsed onto in-memory engines
    so we can populate them independently.
    """

    def _build_engine():
        engine = create_engine("sqlite:///:memory:")
        try:

            @event.listens_for(engine, "connect")
            def _set_sqlite_pragma(dbapi_conn, _connection_record):
                cursor = dbapi_conn.cursor()
                cursor.execute("PRAGMA foreign_keys=ON")
                cursor.close()

            Base.metadata.create_all(engine)
            return engine
        except BaseException:
            # The engine has not yet been returned to the fixture, so no
            # outer cleanup callback can own it on this failure path.
            engine.dispose()
            raise

    engine_a = _build_engine()
    request.addfinalizer(engine_a.dispose)
    engine_b = _build_engine()
    request.addfinalizer(engine_b.dispose)
    Session = sessionmaker()

    # Seed both DBs with their own source_type rows so service queries
    # that resolve note_source_type_id work. Context managers also close
    # the sessions when add/commit raises during fixture construction.
    type_id_a = str(uuid.uuid4())
    type_id_b = str(uuid.uuid4())
    with Session(bind=engine_a) as sess_a:
        sess_a.add(
            SourceType(
                id=type_id_a,
                name="note",
                display_name="Note",
                description="x",
                icon="sticky-note",
            )
        )
        sess_a.commit()
    with Session(bind=engine_b) as sess_b:
        sess_b.add(
            SourceType(
                id=type_id_b,
                name="note",
                display_name="Note",
                description="x",
                icon="sticky-note",
            )
        )
        sess_b.commit()

    @contextmanager
    def fake_session(username=None, password=None):
        engine = {"user_a": engine_a, "user_b": engine_b}.get(username)
        if engine is None:
            raise AssertionError(
                f"Unexpected username in cross-user test: {username!r}. "
                "The shim only knows user_a and user_b."
            )
        # Session is itself a context manager — close() on exit rolls back
        # any pending transaction, mirroring production get_user_db_session.
        with Session(bind=engine) as session:
            yield session

    monkeypatch.setattr(
        "local_deep_research.research_library.notes.services.note_service.get_user_db_session",
        fake_session,
    )

    yield {
        "engine_a": engine_a,
        "engine_b": engine_b,
        "Session": Session,
        "type_id_a": type_id_a,
        "type_id_b": type_id_b,
    }


def _seed_note_in_user_b(two_user_dbs):
    """Insert a note directly into user B's DB and return its id."""
    note_id = str(uuid.uuid4())
    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as session:
        session.add(
            Document(
                id=note_id,
                title="User B's private note",
                text_content="Secret content",
                file_type="note",
                file_size=14,
                source_type_id=two_user_dbs["type_id_b"],
                document_hash=_generate_hash(f"{note_id}:secret"),
                tags=["private"],
            )
        )
        session.commit()
    return note_id


def test_user_a_cannot_get_user_b_note(two_user_dbs):
    """``NoteService('user_a').get_note(note_b_id)`` must return None.

    If the per-user-DB routing breaks (e.g. NoteService falls back to a
    global session or uses the wrong username), user A would see user
    B's content. The structural guarantee: note_b_id only exists in
    user B's DB; user A's session can't find it.
    """
    note_b_id = _seed_note_in_user_b(two_user_dbs)

    service_a = NoteService(username="user_a")
    result = service_a.get_note(note_b_id)

    assert result is None, (
        "User A's NoteService returned user B's note. The per-user-DB "
        "isolation primitive is broken — either NoteService is using a "
        "global session, or `get_user_db_session` is mis-routing."
    )


def test_user_a_cannot_restore_user_b_note_version(two_user_dbs):
    """Same guarantee for the version surface — driven through the
    PRODUCTION path, not a raw session query.

    ``NoteService('user_a').restore_with_bookends(note_b_id,
    version_b_id)`` must refuse: the service resolves the note/version
    through user A's session, where neither row exists. Every sibling
    test here drives NoteService for exactly this reason — a raw
    engine_a query would pass by construction no matter how the
    production code routes sessions, guarding nothing.
    """
    note_b_id = _seed_note_in_user_b(two_user_dbs)
    version_b_id = str(uuid.uuid4())

    # Seed a version in user B's DB.
    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as session:
        session.add(
            NoteVersion(
                id=version_b_id,
                document_id=note_b_id,
                title="User B v1",
                content="Secret content v1",
                tags=[],
                change_type="initial",
                content_hash="hashb1",
            )
        )
        session.commit()

    service_a = NoteService(username="user_a")
    ok, error = service_a.restore_with_bookends(note_b_id, version_b_id)

    assert ok is False, (
        "User A restored user B's note version — per-user-DB isolation "
        "broken. This would let user A restore-to (and thereby read) "
        "user B's content from a learned id."
    )
    assert error is not None

    # And nothing about user B's note changed.
    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as session:
        note_b = session.query(Document).filter_by(id=note_b_id).one()
        assert note_b.text_content == "Secret content"


def test_user_a_list_notes_does_not_include_user_b(two_user_dbs):
    """``NoteService('user_a').list_notes()`` returns only user A's notes."""
    _seed_note_in_user_b(two_user_dbs)

    service_a = NoteService(username="user_a")
    result = service_a.list_notes()

    notes = result.get("notes", []) if isinstance(result, dict) else result
    assert notes == [], (
        "User A's list_notes returned user B's note(s). Per-user-DB "
        f"isolation broken — got: {notes!r}"
    )


# ---------------------------------------------------------------------------
# Mutation surfaces — write paths must also honor per-user routing.
#
# The read tests above already prove the routing primitive holds; the
# tests below pin the same guarantee at each write surface, so a future
# refactor that introduces a per-method bypass (e.g. a helper that uses
# a global session for "convenience") gets caught on the right
# call site instead of cascading silently.
# ---------------------------------------------------------------------------


def test_user_a_cannot_update_user_b_note(two_user_dbs):
    """``update_note`` must return False — user A can't see user B's row.

    ``update_note`` looks up the Document in the caller's session; with
    per-user routing, user A's session simply does not contain user B's
    note_id, so the method returns False without writing anything. If
    this assertion ever flips True, the structural isolation has broken
    upstream of the title/content update path and user A could overwrite
    user B's content.
    """
    note_b_id = _seed_note_in_user_b(two_user_dbs)

    service_a = NoteService(username="user_a")
    result = service_a.update_note(
        note_b_id, title="A overwriting B", content="attacker content"
    )

    assert result is False, (
        "update_note succeeded against another user's note. Per-user-DB "
        "isolation broken on the update path."
    )

    # And verify user B's row was untouched.
    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as sess_b:
        doc_b = sess_b.query(Document).filter_by(id=note_b_id).first()
    assert doc_b is not None
    assert doc_b.title == "User B's private note"
    assert doc_b.text_content == "Secret content"


def test_user_a_cannot_accept_suggested_link_into_user_b_note(two_user_dbs):
    """``accept_suggested_link`` must not create a link or mutate content
    across the per-user DB boundary.

    ``accept_suggested_link`` resolves BOTH the source and target Documents
    through ``get_user_db_session(self.username)`` (note_service.py
    ~L2379-2383) and returns None if either is missing
    (``if not source or not target: return None`` ~L2384-2385). With
    per-user routing, user A's session can't see user B's notes, so:

      * both ids in B  → source missing in user A's session → None
      * source in A, target in B → target missing in user A's session → None

    The same-user duplicate-title test in ``test_note_service.py`` exercises
    the in-session path; the existing 7 cross-user tests cover get / version
    / list / update / delete / outgoing / backlinks but never
    ``accept_suggested_link``'s two-document lookup. This is the highest-risk
    cross-user WRITE surface (it both appends ``[[...]]`` content and inserts
    a NoteLink), so a routing regression here must be caught.
    """
    # Two distinct notes in user B's DB (a link source + target).
    note_b_source = _seed_note_in_user_b(two_user_dbs)
    note_b_target = str(uuid.uuid4())
    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as sess_b:
        sess_b.add(
            Document(
                id=note_b_target,
                title="User B link target",
                text_content="Target body.",
                file_type="note",
                file_size=11,
                source_type_id=two_user_dbs["type_id_b"],
                document_hash=_generate_hash(f"{note_b_target}:target"),
                tags=["private"],
            )
        )
        sess_b.commit()

    # One note in user A's DB.
    note_a_id = str(uuid.uuid4())
    with two_user_dbs["Session"](bind=two_user_dbs["engine_a"]) as sess_a:
        sess_a.add(
            Document(
                id=note_a_id,
                title="User A's own note",
                text_content="A body.",
                file_type="note",
                file_size=7,
                source_type_id=two_user_dbs["type_id_a"],
                document_hash=_generate_hash(f"{note_a_id}:a"),
                tags=["mine"],
            )
        )
        sess_a.commit()

    service_a = NoteService(username="user_a")

    # Both ids live in user B's DB — invisible to user A's session.
    result = service_a.accept_suggested_link(note_b_source, note_b_target)
    # Source in user A, target in user B — target invisible to user A.
    result2 = service_a.accept_suggested_link(note_a_id, note_b_target)

    assert result is None, (
        "accept_suggested_link resolved another user's notes — per-user-DB "
        "isolation broken on the link-accept path."
    )
    assert result2 is None, (
        "accept_suggested_link created a link into another user's note — "
        "per-user-DB isolation broken on the link-accept path."
    )

    # User B's notes are untouched: no NoteLink and no appended [[...]].
    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as sess_b:
        assert (
            sess_b.query(NoteLink)
            .filter_by(source_document_id=note_b_source)
            .count()
            == 0
        )
        src_b = sess_b.query(Document).filter_by(id=note_b_source).first()
        tgt_b = sess_b.query(Document).filter_by(id=note_b_target).first()
        assert src_b.text_content == "Secret content"
        assert tgt_b.text_content == "Target body."

    # User A's note gained no link and no content mutation.
    with two_user_dbs["Session"](bind=two_user_dbs["engine_a"]) as sess_a:
        assert (
            sess_a.query(NoteLink)
            .filter_by(source_document_id=note_a_id)
            .count()
            == 0
        )
        doc_a = sess_a.query(Document).filter_by(id=note_a_id).first()
        assert doc_a.text_content == "A body."


def test_user_a_cannot_delete_user_b_note(two_user_dbs):
    """``delete_note`` must return False and leave user B's row intact."""
    note_b_id = _seed_note_in_user_b(two_user_dbs)

    service_a = NoteService(username="user_a")
    result = service_a.delete_note(note_b_id)

    assert result is False, (
        "delete_note succeeded against another user's note. Per-user-DB "
        "isolation broken on the delete path."
    )

    with two_user_dbs["Session"](bind=two_user_dbs["engine_b"]) as sess_b:
        assert (
            sess_b.query(Document).filter_by(id=note_b_id).first() is not None
        )


def test_user_a_get_outgoing_links_of_user_b_note_is_empty(two_user_dbs):
    """Outgoing-links lookup must not surface user B's links to user A.

    Even if user A guesses user B's note_id, the lookup runs in user A's
    DB where the matching ``note_links`` rows do not exist, so the result
    is an empty list.
    """
    note_b_id = _seed_note_in_user_b(two_user_dbs)

    service_a = NoteService(username="user_a")
    result = service_a.get_outgoing_links(note_b_id)

    assert result == [], (
        "get_outgoing_links returned user B's links to user A. Per-user-DB "
        f"isolation broken — got: {result!r}"
    )


def test_user_a_get_backlinks_of_user_b_note_is_empty(two_user_dbs):
    """Backlinks lookup must not surface user B's backlinks to user A."""
    note_b_id = _seed_note_in_user_b(two_user_dbs)

    service_a = NoteService(username="user_a")
    result = service_a.get_backlinks(note_b_id)

    assert result == [], (
        "get_backlinks returned user B's backlinks to user A. Per-user-DB "
        f"isolation broken — got: {result!r}"
    )