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.2 KB
Raw Blame
"""End-to-end security battery: hostile Zotero item titles -> filesystem writes.

Complements:
  * tests/security/test_remote_filename_writes.py  (URL-derived filenames
    via DownloadService; this file covers the Zotero sync pipeline which
    uses a different generator).
  * tests/security/test_pdf_path_traversal.py  (covers only the
    _get_safe_file_path READ-path validator, no actual writes).
  * tests/security/test_filename_sanitizer.py  (covers only the bare
    sanitize_filename unit, with no ZoteroItem involvement).

The Zotero ingest pipeline:

  remote ZoteroItem.data["title"]
      -> ZoteroSyncService._filename_for(item, file_type)   sync_service.py:1658
           title = item.data.get("title").strip()
           if title.lower().endswith(f".{file_type}".lower()):
               title = title[: -(len(file_type) + 1)]       line 1662-1663
           candidate = f"{title[:120]}.{file_type}"        line 1664
           sanitize_filename(candidate)                    line 1667
             * on UnsafeFilenameError: fall back to
               f"{item.key}.{file_type}"                   line 1670
      -> Document.filename = self._filename_for(...)      sync_service.py:1304
      -> PDFStorageManager.save_pdf(pdf_content, document, session,
                                    filename=doc.filename) sync_service.py:1326

Two surfaces are exercised:

  (A) DIRECT unit-level: call ``ZoteroSyncService._filename_for`` with a
      real ``ZoteroItem`` whose ``data["title"]`` carries a hostile
      payload, and assert the OUTPUT cannot traverse the filesystem.

  (B) END-TO-END: drive the real ``_filename_for`` -> real
      ``PDFStorageManager.save_pdf`` (filesystem mode) chain with a
      tmp_path library root, plant canary files outside it, and assert
      the written file lands STRICTLY inside library_root and the
      canaries are untouched.

Failure classification:
  * Red because of vulnerability: any file lands outside library_root,
    any outside canary is touched, _filename_for returns a value
    containing "/", "\\", "\x00", or a ".." prefix, or a symlink is
    followed.
  * Broken-test: setup/import/expectation bugs.

Run with:
  .venv/bin/python -m pytest tests/security/test_zotero_ingest_writes.py \
      -v --no-header --timeout=120
"""

from __future__ import annotations

import os
from pathlib import Path
from typing import Optional

import pytest

from local_deep_research.research_library.zotero.client import ZoteroItem
from local_deep_research.research_library.zotero.sync_service import (
    ZoteroSyncService,
)
from local_deep_research.security.filename_sanitizer import UnsafeFilenameError


# Minimal bytes that pass "%PDF" sniffers — content is irrelevant, the
# write path is what we care about.  Mirrors the constant in
# tests/security/test_remote_filename_writes.py.
_MINIMAL_PDF = b"%PDF-1.4\n1 0 obj <<>>endobj\ntrailer<<>>\n%%EOF\n"


# ---------------------------------------------------------------------------
# Fixtures and helpers
# ---------------------------------------------------------------------------


# ``library_session``, ``fresh_document``, ``fs_storage`` and
# ``canary_paths`` now live in tests/security/conftest.py -- they were
# duplicated verbatim across four batteries in this directory.


def _zotero_item(title: str, key: str = "ITEM0001") -> ZoteroItem:
    """Minimal real ZoteroItem — same shape used in
    tests/research_library/zotero/test_sync_service.py.

    ``data`` mirrors what ZoteroSyncService._filename_for reads at
    sync_service.py:1659 (``item.data.get("title")``).
    """
    return ZoteroItem(
        key=key,
        version=1,
        item_type="journalArticle",
        data={"title": title},
    )


def _safe_name_assertions(name: str, *, where: str) -> None:
    """Assert a derived filename cannot escape library_root."""
    assert "/" not in name, (
        f"VULNERABILITY: {where} returned a path-containing value: {name!r}"
    )
    assert "\\" not in name, (
        f"VULNERABILITY: {where} returned a backslash-containing value: "
        f"{name!r}"
    )
    assert "\x00" not in name, (
        f"VULNERABILITY: {where} did not strip null bytes: {name!r}"
    )
    assert not name.startswith(".."), (
        f"VULNERABILITY: {where} returned a traversal-leading value: {name!r}"
    )
    # sanitize_filename caps at MAX_FILENAME_LENGTH=255.
    assert len(name) <= 255, (
        f"VULNERABILITY: {where} returned an over-long filename "
        f"({len(name)} chars): {name!r}"
    )
    # Resolves to a basename — never a path.
    assert Path(name).name == name, (
        f"VULNERABILITY: {where} returned a path, not a basename: {name!r}"
    )


def _assert_files_inside(
    library_root: Path,
    written_relative: Optional[str],
    canaries: dict,
) -> None:
    """Strict safety assertion: any file written MUST be inside library_root
    AND must not have modified any canary outside it."""
    library_root = library_root.resolve()

    if written_relative is not None:
        written = (library_root / written_relative).resolve()
        try:
            written.relative_to(library_root)
        except ValueError as exc:
            pytest.fail(
                f"VULNERABILITY: save_pdf wrote {written} OUTSIDE library_root "
                f"{library_root} (relative_to raised: {exc})"
            )

    for name, canary in canaries.items():
        assert canary.exists(), (
            f"VULNERABILITY: canary {name} at {canary} was DELETED"
        )
        assert canary.read_text(encoding="utf-8") == "UNTOUCHED-CANARY\n", (
            f"VULNERABILITY: canary {name} at {canary} was MODIFIED "
            f"(content: {canary.read_text(encoding='utf-8')!r})"
        )


# ---------------------------------------------------------------------------
# Direct _filename_for battery — narrowest pipeline test
# ---------------------------------------------------------------------------
#
# These directly exercise ZoteroSyncService._filename_for (the choke-point
# between remote Zotero data and every consumer of Document.filename)
# with the same remote-payload shapes the task brief enumerates.  At
# least one of the battery must exercise _filename_for itself — these
# are that one.


DIRECT_HOSTILE_TITLES = [
    pytest.param("../../etc/passwd", id="dotdot-traversal"),
    pytest.param("/etc/passwd", id="absolute-path"),
    pytest.param("evil\x00nullbyte", id="null-byte"),
    pytest.param("\uff0e\uff0e\uff0fescape", id="fullwidth-unicode-traversal"),
    pytest.param("a" * 5000, id="extremely-long"),
    pytest.param("con.pdf", id="windows-reserved-name"),
    pytest.param("..\\windows\\sys", id="backslash-traversal"),
]


class TestFilenameForHostileTitles:
    """Drive the real ``_filename_for`` @staticmethod with a real
    ``ZoteroItem`` whose ``data["title"]`` is hostile and assert the
    OUTPUT cannot escape a filesystem.  A failure here means the
    Zotero pipeline emits an unsafe filename even before save_pdf sees
    it."""

    @pytest.mark.parametrize("title", DIRECT_HOSTILE_TITLES)
    def test_filename_for_output_is_safe(self, title):
        item = _zotero_item(title)
        result = ZoteroSyncService._filename_for(item, "pdf")
        _safe_name_assertions(result, where="_filename_for")


# ---------------------------------------------------------------------------
# Empty / fallback path — _filename_for's UnsafeFilenameError branch
# ---------------------------------------------------------------------------


class TestFilenameForEmptyTitleFallback:
    """When the candidate is empty (no title) or sanitize_filename rejects
    it, _filename_for falls back to ``{item.key}.{file_type}``.  An
    attacker-controlled Zotero server could set item.key to a hostile
    value too — this exercises the fallback."""

    @pytest.mark.parametrize(
        "title,expected", [pytest.param("", "ITEM0001.pdf", id="empty-title")]
    )
    def test_empty_or_unrenderable_title_falls_back_or_raises(
        self, title, expected
    ):
        item = _zotero_item(title)
        try:
            result = ZoteroSyncService._filename_for(item, "pdf")
        except UnsafeFilenameError:
            assert expected is None, (
                f"Unexpected UnsafeFilenameError for title {title!r}"
            )
            return
        if expected is not None:
            assert result == expected, (
                f"expected fallback {expected!r}, got {result!r}"
            )
        _safe_name_assertions(result, where="_filename_for/key-fallback")


# ---------------------------------------------------------------------------
# End-to-end battery: _filename_for -> save_pdf filesystem write
# ---------------------------------------------------------------------------


# Smaller than DIRECT_HOSTILE_TITLES: end-to-end is more expensive and
# the direct battery above already pins _filename_for's output shape.
# Here we only need to prove the WHOLE chain cannot escape library_root.
E2E_HOSTILE_TITLES = [
    pytest.param("../../etc/passwd", id="dotdot-traversal"),
    pytest.param("/tmp/abs.pdf", id="absolute-path"),
    pytest.param("evil\x00nullbyte", id="null-byte"),
    pytest.param("\uff0e\uff0e\uff0fescape.pdf", id="fullwidth-unicode"),
]


class TestFilenameForPipelineSavePdf:
    """Drive the REAL ``_filename_for`` -> REAL ``PDFStorageManager.save_pdf``
    chain (filesystem mode, tmp_path library root) with a hostile title
    and assert the write is contained.

    Either clean rejection (PathValidator raising ValueError) OR a write
    strictly inside library_root is acceptable; the test FAILS only when
    a file lands outside the root or a canary is touched."""

    @pytest.mark.parametrize("title", E2E_HOSTILE_TITLES)
    def test_hostile_title_filename_stays_inside_library_root(
        self,
        fs_storage,
        fresh_document,
        library_session,
        canary_paths,
        title,
    ):
        item = _zotero_item(title, key="ZKEY_E2E")

        # The narrowest real chain: _filename_for -> save_pdf.
        derived = ZoteroSyncService._filename_for(item, "pdf")
        _safe_name_assertions(derived, where="_filename_for")

        # Reset between parametrize iterations.
        fresh_document.storage_mode = None
        fresh_document.file_path = None

        try:
            relative, _size = fs_storage.save_pdf(
                pdf_content=_MINIMAL_PDF,
                document=fresh_document,
                session=library_session,
                filename=derived,
            )
        except (ValueError, FileNotFoundError, OSError):
            # PathValidator rejecting traversal, OS rejecting a non-existent
            # intermediate dir, etc. — all valid "rejection" outcomes.
            _assert_files_inside(fs_storage.library_root, None, canary_paths)
            return

        # save_pdf accepted the filename — assert it stayed strictly inside.
        _assert_files_inside(fs_storage.library_root, relative, canary_paths)


# ---------------------------------------------------------------------------
# Symlink escape — defense shared with test_remote_filename_writes
# ---------------------------------------------------------------------------


class TestSymlinkWriteEscape:
    """A symlink planted at the intended PDF filename must not redirect writes.

    The verified writer refuses the link where O_NOFOLLOW is available.
    The outside canary must survive, and legitimate in-root saves remain
    covered by the positive controls."""

    @pytest.mark.xfail(
        not getattr(os, "O_NOFOLLOW", 0),
        strict=True,
        reason="The verified writer cannot refuse leaf symlinks without O_NOFOLLOW",
    )
    def test_symlink_inside_pdf_subdir_does_not_let_write_escape(
        self,
        fs_storage,
        fresh_document,
        library_session,
        tmp_path,
        canary_paths,
    ):
        library_root = fs_storage.library_root
        target_name = "zotero_evil_link_target.pdf"

        # Real canary file outside the library root.
        outside_target = tmp_path / "outside_canary_zone" / target_name
        outside_target.write_text("UNTOUCHED-CANARY\n", encoding="utf-8")
        canary_paths["zotero_symlink_target"] = outside_target

        # Use a successful save to find the content-hashed destination;
        # the display filename itself is no longer the path being written.
        saved_relative, _ = fs_storage.save_pdf(
            _MINIMAL_PDF, fresh_document, library_session, target_name
        )
        link = library_root / saved_relative
        assert link.read_bytes() == _MINIMAL_PDF
        link.unlink()
        try:
            link.symlink_to(outside_target)
        except (OSError, NotImplementedError):
            pytest.skip("Cannot create symlinks in this environment")
        assert link.resolve() == outside_target.resolve()

        fresh_document.storage_mode = None
        fresh_document.file_path = None

        try:
            relative, _size = fs_storage.save_pdf(
                pdf_content=_MINIMAL_PDF,
                document=fresh_document,
                session=library_session,
                filename=target_name,
            )
        except (ValueError, OSError):
            _assert_files_inside(library_root, None, canary_paths)
            return

        assert relative == saved_relative
        assert (
            outside_target.read_text(encoding="utf-8") == "UNTOUCHED-CANARY\n"
        ), (
            f"VULNERABILITY: Zotero-derived save_pdf wrote through symlink "
            f"and overwrote outside-root canary at {outside_target}; "
            f"relative returned was {relative!r}"
        )
        _assert_files_inside(library_root, relative, canary_paths)


# ---------------------------------------------------------------------------
# Happy path — clean Zotero title saves inside pdfs/
# ---------------------------------------------------------------------------


class TestHappyPath:
    def test_clean_zotero_title_saves_inside_pdfs(
        self,
        fs_storage,
        fresh_document,
        library_session,
        canary_paths,
    ):
        """A clean remote Zotero title flows through _filename_for and
        save_pdf to land inside ``<library_root>/pdfs/`` — guards against
        over-blocking (a sanitization regression that rejects valid titles
        would manifest here)."""
        item = _zotero_item("A Clean Zotero Paper Title", key="ZKEY_CLEAN")
        derived = ZoteroSyncService._filename_for(item, "pdf")
        _safe_name_assertions(derived, where="_filename_for/clean")

        relative, _size = fs_storage.save_pdf(
            pdf_content=_MINIMAL_PDF,
            document=fresh_document,
            session=library_session,
            filename=derived,
            url="http://example.com/clean.pdf",
            resource_id=1,
        )

        written = fs_storage.library_root / relative
        assert written.is_file()
        # Must be inside library_root/pdfs/ — the intended location.
        assert written.resolve().is_relative_to(
            (fs_storage.library_root / "pdfs").resolve()
        ), f"clean file landed at unexpected path: {written}"
        assert written.read_bytes() == _MINIMAL_PDF
        assert fresh_document.file_path == relative
        assert fresh_document.storage_mode == "filesystem"
        _assert_files_inside(fs_storage.library_root, relative, canary_paths)