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.7 KB
Raw Blame
"""GREEN identity and index filesystem guardrail battery.

Pins secure path derivation and write scopes with real ``tmp_path`` roots.
Failures indicate regressions and must not weaken these assertions.

Surfaces covered (third green-pin battery; complements
``test_disk_write_guards.py`` and ``test_disk_write_guards_extended.py``):

1. Collection-name -> FAISS index path hashing in
   ``research_library/services/library_rag_service.py::_get_index_path``.
2. Per-user backup directory containment in
   ``config/paths.py::get_user_backup_directory``.
3. Installation secret-key file permissions in
   ``web/fastapi_app.py::_load_secret_key`` (the FastAPI migration, #3299,
   replaced Flask's ``web/app_factory.py::create_app`` with a module-level
   secret-key load; see that surface's test for why it drives
   ``_load_secret_key`` directly rather than the whole app).
4. Uploaded filename sanitization before spool/target composition in
   ``security/filename_sanitizer.py`` + the ``PDFStorageManager`` spool
   layer the upload route (``web/routers/rag.py``) drives.
5. Report metadata sidecar containment and deletion scoping in
   ``storage/file.py::FileReportStorage``.
6. Plaintext filesystem PDF storage remains default-denied at
   ``research_library/services/pdf_storage_manager.py``.

Offline, tmp_path-only, no LLM/network.
"""

import re
import sys
from unittest.mock import MagicMock

import pytest


# --------------------------------------------------------------------------
# Surface 1: collection identity -> hashed, per-user FAISS path
# (research_library/services/library_rag_service.py)
# --------------------------------------------------------------------------
def _bare_rag_service(username: str):
    from local_deep_research.research_library.services.library_rag_service import (
        LibraryRAGService,
    )

    service = object.__new__(LibraryRAGService)
    service.username = username
    service.chunk_overlap = 200
    service.chunk_size = 1000
    service.distance_metric = "cosine"
    service.index_type = "flat"
    service.normalize_vectors = True
    service.splitter_type = "recursive"
    service.text_separators = ["\n\n", "\n", ". ", " ", ""]
    return service


@pytest.mark.parametrize(
    "collection_name",
    ["../../etc", "unicode/秘密", "nul\x00name", "x" * 4096],
)
def test_hostile_collection_identity_hashes_to_in_cache_faiss_path(
    tmp_path, monkeypatch, collection_name
):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    service = _bare_rag_service("index-owner")

    index_hash = service._get_index_hash(
        collection_name, "model/name", "sentence_transformers"
    )
    index_path = service._get_index_path(index_hash)

    assert re.fullmatch(r"[0-9a-f]{64}", index_hash)
    assert index_path.resolve().is_relative_to((tmp_path / "cache").resolve())
    assert index_path.name == f"{index_hash}.faiss"
    # No raw collection-name fragment leaks into the resolved path: even a
    # 4KiB name cannot push bytes into the directory tree.
    assert collection_name not in str(index_path.resolve())


def test_collection_names_differing_only_by_case_get_distinct_index_paths(
    tmp_path, monkeypatch
):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    service = _bare_rag_service("index-owner")

    upper = service._get_index_path(
        service._get_index_hash("Collection", "model", "sentence_transformers")
    )
    lower = service._get_index_path(
        service._get_index_hash("collection", "model", "sentence_transformers")
    )

    assert upper.resolve().is_relative_to((tmp_path / "cache").resolve())
    assert lower.resolve().is_relative_to((tmp_path / "cache").resolve())
    assert upper != lower


# --------------------------------------------------------------------------
# Surface 2: per-user backup directory containment
# (config/paths.py)
# --------------------------------------------------------------------------
@pytest.mark.parametrize(
    "username", ["../escape", "/absolute/path", "user/../../etc"]
)
def test_hostile_backup_username_is_hashed_inside_data_directory(
    tmp_path, monkeypatch, username
):
    from local_deep_research.config.paths import get_user_backup_directory

    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    backup_dir = get_user_backup_directory(username)
    backup_root = tmp_path / "encrypted_databases" / "backups"

    assert backup_dir.resolve().is_relative_to(backup_root.resolve())
    assert backup_dir.parent == backup_root
    assert re.fullmatch(r"[0-9a-f]{16}", backup_dir.name)
    if sys.platform != "win32":
        # Windows ignores mkdir/chmod modes and reports 0o777; the
        # containment assertions above are the portable half.
        assert backup_dir.stat().st_mode & 0o777 == 0o700


# --------------------------------------------------------------------------
# Surface 3: installation secret-key file permissions
# (web/fastapi_app.py)
#
# Flask's ``app_factory.create_app()`` ran ``_load_secret_key()``-equivalent
# logic every time an app was built. The FastAPI migration (#3299) moved
# the SECRET_KEY load to ``web/fastapi_app.py::_load_secret_key`` and calls
# it exactly once, at MODULE IMPORT time (``SECRET_KEY =
# _load_secret_key()``) -- so calling ``create_app()``-equivalent a second
# time in a test process would not re-run it, and reloading the whole
# ``fastapi_app`` module to force a re-run risks corrupting every other
# test's view of the app singleton. Driving ``_load_secret_key()`` directly
# instead exercises the exact same file-permission logic without either
# hazard; ``config.paths.get_data_directory()`` reads ``LDR_DATA_DIR`` live
# (no caching), so the ``monkeypatch.setenv`` below is still honoured.
# --------------------------------------------------------------------------
def test_generated_secret_key_is_private_and_under_configured_data_dir(
    tmp_path, monkeypatch
):
    from local_deep_research.web import fastapi_app

    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    fastapi_app._load_secret_key()

    secret_key_file = tmp_path / ".secret_key"
    assert secret_key_file.is_file()
    assert secret_key_file.resolve().is_relative_to(tmp_path.resolve())
    if sys.platform != "win32":
        # Windows ignores the 0o600 chmod and reports 0o666.
        assert secret_key_file.stat().st_mode & 0o077 == 0


# --------------------------------------------------------------------------
# Surface 4: uploaded filename sanitization before spool/target composition
# (web/routers/rag.py's upload route + security/filename_sanitizer.py)
# --------------------------------------------------------------------------
def test_hostile_upload_filenames_cannot_escape_spool_target(tmp_path):
    from local_deep_research.security.filename_sanitizer import (
        sanitize_filename,
    )

    spool_root = tmp_path / "data" / "upload_spool"
    spool_root.mkdir(parents=True)
    for raw_filename in (
        "../escape.pdf",
        "/absolute/escape.pdf",
        "nul\x00name.pdf",
        r"..\escape.pdf",
    ):
        safe_filename = sanitize_filename(raw_filename)
        target = (spool_root / safe_filename).resolve()

        assert target.is_relative_to(spool_root.resolve())
        assert not {"/", "\\", "\x00"}.intersection(safe_filename)


def test_upload_pdf_storage_layer_keeps_spool_inside_library_root(
    tmp_path,
):
    # Depth pin on the storage layer the upload route drives
    # (rag_routes.upload_to_collection only ever constructs
    # PDFStorageManager with storage_mode="database", passing filenames
    # that already went through sanitize_filename). Database mode must
    # leave NO plaintext file under the library root, and even the
    # operator-gated filesystem mode must refuse traversal joins before
    # any write (PathValidator raises, nothing lands outside the root).
    from local_deep_research.research_library.services.pdf_storage_manager import (
        PDFStorageManager,
    )

    library_root = tmp_path / "library"
    library_root.mkdir()
    document = MagicMock(id=1)
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = None

    database_manager = PDFStorageManager(
        library_root=library_root, storage_mode="database"
    )
    result = database_manager.save_pdf(
        b"%PDF-1.4 bytes", document, session, "paper.pdf"
    )

    assert result[0] == "database"
    assert document.file_path is None
    assert not [path for path in tmp_path.rglob("*") if path.is_file()]

    outside_canary = tmp_path / "escape.pdf"
    outside_canary.write_bytes(b"canary")
    filesystem_manager = PDFStorageManager(
        library_root=library_root, storage_mode="filesystem"
    )
    with pytest.raises(ValueError):
        filesystem_manager.save_pdf(
            b"%PDF-1.4 bytes", document, session, "../../escape.pdf"
        )

    assert outside_canary.read_bytes() == b"canary"
    assert not [path for path in library_root.rglob("*") if path.is_file()]


# --------------------------------------------------------------------------
# Surface 5: report metadata sidecar containment and deletion scope
# (storage/file.py)
# --------------------------------------------------------------------------
def test_hostile_research_id_cannot_escape_metadata_sidecar_root(tmp_path):
    from local_deep_research.storage.file import FileReportStorage

    outputs = tmp_path / "outputs"
    storage = FileReportStorage(outputs)
    for research_id in ("../escape", "/tmp/escape", "bad\x00id"):
        with pytest.raises(ValueError):
            storage._get_metadata_path(research_id)

    assert list(outputs.iterdir()) == []


def test_save_report_rejects_hostile_id_before_report_or_sidecar_write(
    tmp_path,
):
    from local_deep_research.storage.file import FileReportStorage

    outputs = tmp_path / "outputs"
    canary = outputs / "unrelated.txt"
    storage = FileReportStorage(outputs)
    canary.write_text("keep")

    assert storage.save_report("../escape", "report", {"query": "q"}) is False
    assert canary.read_text() == "keep"
    assert list(outputs.iterdir()) == [canary]


def test_delete_report_removes_own_sidecar_but_spares_outputs_sibling(
    tmp_path,
):
    from local_deep_research.storage.file import FileReportStorage

    outputs = tmp_path / "outputs"
    storage = FileReportStorage(outputs)
    report = outputs / "report.md"
    sidecar = outputs / "report_metadata.json"
    canary = outputs / "unrelated.txt"
    report.write_text("report")
    sidecar.write_text("{}")
    canary.write_text("keep")

    assert storage.delete_report("report") is True
    assert not report.exists()
    assert not sidecar.exists()
    assert canary.read_text() == "keep"


# --------------------------------------------------------------------------
# Surface 6: plaintext filesystem PDF storage remains default-denied
# (research_library/services/pdf_storage_manager.py)
# --------------------------------------------------------------------------
def test_pdf_filesystem_storage_env_var_completely_unset(monkeypatch):
    """The env var MUST be absent (not just falsy) for the gate to
    default-deny; this guards against any future change that interprets a
    present-but-empty value as an opt-in."""
    from local_deep_research.research_library.services.pdf_storage_manager import (
        filesystem_pdf_storage_allowed,
        resolve_pdf_storage_mode,
    )

    monkeypatch.delenv(
        "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE", raising=False
    )

    assert filesystem_pdf_storage_allowed() is False
    assert resolve_pdf_storage_mode("filesystem") == "database"


def test_pdf_filesystem_storage_explicit_truthy_opt_in_honors_filesystem(
    monkeypatch,
):
    """Positive control: an explicit truthy opt-in is honored (filesystem
    is NOT coerced). If a future regression tightens the gate past the env
    var, this turns red."""
    from local_deep_research.research_library.services.pdf_storage_manager import (
        filesystem_pdf_storage_allowed,
        resolve_pdf_storage_mode,
    )

    monkeypatch.setenv(
        "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE", "true"
    )

    assert filesystem_pdf_storage_allowed() is True
    assert resolve_pdf_storage_mode("filesystem") == "filesystem"


@pytest.mark.parametrize("value", ["", "not-an-opt-in"])
def test_pdf_filesystem_storage_falsy_env_values_default_deny(
    monkeypatch, value
):
    """A present-but-falsy env var still reads as off; the gate must not
    be confused by empty strings or non-truthy literals into honoring
    filesystem."""
    from local_deep_research.research_library.services.pdf_storage_manager import (
        filesystem_pdf_storage_allowed,
        resolve_pdf_storage_mode,
    )

    monkeypatch.setenv(
        "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE", value
    )

    assert filesystem_pdf_storage_allowed() is False
    assert resolve_pdf_storage_mode("filesystem") == "database"