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.

6.3 KB
Raw Blame
"""The library document API must not disclose the server's own file paths.

``LibraryService.get_documents`` (the list behind ``GET /library/api/documents``)
and ``LibraryService.get_document_by_id`` (the document details page) both
returned ``file_absolute_path``: the resolved absolute path of the stored file,
which is the server's directory layout rather than anything about the document.

The codebase already treats exactly this as a bug on a neighbouring endpoint.
``/library/api/check-downloads`` withholds it in so many words -- "it's the
absolute server path, which leaks directory layout to any authenticated user.
The client fetches via ``/document/{id}/pdf`` ... so the path is never needed
client-side" -- and #6443 removes the same class of value from the research
status, details and report endpoints (#6462).

``file_path`` stays. It is the path *relative to the library root*, which is
what tells a reader where a document sits inside their own library, and the
resolution still happens internally so the availability flag is unchanged; only
the resolved value stops being returned.
"""

import re
from contextlib import contextmanager
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import patch

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

_SERVICE = "local_deep_research.research_library.services.library_service"

# A path no test data can produce by accident, so finding it anywhere in a
# payload means the resolved absolute path was carried out of the service.
SENTINEL_ROOT = "/srv/ldr-private-root-9d41c2"
SENTINEL_ABS = f"{SENTINEL_ROOT}/pdfs/42.pdf"
RELATIVE_PATH = "pdfs/42.pdf"


@pytest.fixture
def session():
    from local_deep_research.database.models import Base

    engine = create_engine(
        "sqlite:///:memory:",
        connect_args={"check_same_thread": False},
        poolclass=StaticPool,
    )
    Base.metadata.create_all(engine)
    db = sessionmaker(bind=engine)()
    try:
        yield db
    finally:
        db.close()
        engine.dispose()


def _seed(db):
    """One completed document in the default collection, stored on disk."""
    from local_deep_research.database.models import (
        Collection,
        Document,
        DocumentCollection,
    )

    now = datetime.now(timezone.utc)
    collection = Collection(
        id="col-1", name="Library", created_at=now, updated_at=now
    )
    document = Document(
        id="doc-1",
        source_type_id=1,
        document_hash="hash-1",
        title="A Paper",
        filename="42.pdf",
        file_path=RELATIVE_PATH,
        file_size=1234,
        file_type="pdf",
        status="completed",
        processed_at=now,
        created_at=now,
        updated_at=now,
    )
    db.add_all(
        [
            collection,
            document,
            DocumentCollection(
                document_id="doc-1", collection_id="col-1", added_at=now
            ),
        ]
    )
    db.commit()


@contextmanager
def _service(db):
    """A LibraryService whose DB is `db` and whose library root is the sentinel."""
    from local_deep_research.research_library.services.library_service import (
        LibraryService,
    )

    @contextmanager
    def _session(*_args, **_kwargs):
        yield db

    with (
        patch(f"{_SERVICE}.get_user_db_session", _session),
        patch(
            f"{_SERVICE}.get_absolute_path_from_settings",
            return_value=Path(SENTINEL_ABS),
        ),
        patch(f"{_SERVICE}.get_settings_manager", return_value=object()),
        patch(
            "local_deep_research.database.library_init.get_default_library_id",
            return_value="col-1",
        ),
    ):
        yield LibraryService(username="alice")


def _documents(db):
    with _service(db) as service:
        return service.get_documents(collection_id="col-1")


def _document(db):
    with _service(db) as service:
        return service.get_document_by_id("doc-1")


class TestTheResolvedPathIsNotReturned:
    def test_the_document_list_carries_no_absolute_path(self, session):
        _seed(session)

        documents = _documents(session)

        assert len(documents) == 1
        assert "file_absolute_path" not in documents[0]
        # Not only the key: the value must be absent from the payload however
        # it is spelled or nested.
        assert SENTINEL_ROOT not in repr(documents)

    def test_the_details_payload_carries_no_absolute_path(self, session):
        _seed(session)

        document = _document(session)

        assert document is not None
        assert "file_absolute_path" not in document
        assert SENTINEL_ROOT not in repr(document)


class TestWhatTheReaderStillGets:
    """Accept controls. Returning an empty payload would satisfy every
    assertion above, and so would a change that stopped resolving the path at
    all and broke the availability flag with it.
    """

    def test_the_library_relative_path_still_identifies_the_file(self, session):
        _seed(session)

        documents = _documents(session)
        document = _document(session)

        for payload in (documents[0], document):
            assert payload["file_path"] == RELATIVE_PATH
            assert payload["file_name"] == "42.pdf"

    def test_availability_still_depends_on_resolving_the_path(self, session):
        """``has_pdf`` in the list is computed FROM the resolved absolute path.
        It has to stay true, which is what separates "stopped returning it"
        from "stopped resolving it".
        """
        _seed(session)

        documents = _documents(session)

        assert documents[0]["has_pdf"] is True


class TestTheDocumentPageDoesNotRenderItEither:
    def test_the_template_has_no_absolute_path_field(self):
        """The service is one of two readers; the details template printed the
        same value under an "Absolute Path" label.
        """
        template = (
            Path(__file__).resolve().parents[2]
            / "src/local_deep_research/web/templates/pages/document_details.html"
        )
        body = template.read_text(encoding="utf-8")

        assert "file_absolute_path" not in body
        assert not re.search(r"Absolute\s+Path", body)
        # The relative path is still shown, so this is a removal of one field
        # rather than of the File Location section.
        assert "document.file_path" in body