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.

36.9 KB
Raw Blame
# allow: no-sut-import — security battery for read-side poisoning of
# DB-stored paths (research report_path, library Document.file_path, backup
# metadata). Drives the real FastAPI routes through a real authenticated
# TestClient while stubbing the DB session so we can plant rows whose path
# columns point at a canary file under tmp_path, then asserts the canary
# marker NEVER appears in the response bytes/content.
"""Security battery: poisoned DB-stored paths on the READ side.

Complements
  * ``tests/security/test_report_path_deletion.py`` — pins the
    DELETE / clear_history unlink surface for ``ResearchHistory.report_path``
    (write/delete side: the unlink trusts the column verbatim).
  * ``tests/security/test_pdf_path_traversal.py`` — pins
    ``PDFStorageManager._get_safe_file_path`` as a unit; this file drives
    the same manager end-to-end via the real FastAPI route and ``load_pdf``.
  * ``tests/security/test_remote_filename_writes.py`` — pins the
    write-side of ``Document.file_path`` (``save_pdf``); this file pins
    the matching read-side (``load_pdf`` -> ``view_pdf_page``).

SURFACE VERIFICATION (re-verified against the FastAPI routers after
#3299 deleted the Flask blueprints this file used to drive):

1. Research export/report — GREEN PIN (report_path is never opened).
   * ``POST /api/v1/research/<id>/export/<format>``
     (``export_research_report`` in ``web/routers/research.py``) builds
     content via ``assemble_full_report``, which reads
     ``ResearchHistory.report_content`` from the DB
     (``web/services/report_assembly_service.py``:
     ``body = research.report_content or ""``) and streams the assembled
     bytes back — never opens a file on disk.
   * ``GET /api/report/<id>`` (``get_research_report`` in
     ``web/routers/research.py``) uses the same DB-content path.
   * ``report_path`` is only ever echoed back in JSON metadata elsewhere in
     the same router — never opened as a file.
   => Poisoning vector does not exist; the tests below pin that and turn
      red if a future change serves the file at ``report_path``.

2. Library PDF serving — GREEN PIN (validator-routed).
   * ``view_pdf_page`` / ``serve_pdf_api`` (``web/routers/library.py``,
     both delegating to the shared ``_serve_pdf``) call
     ``PDFStorageManager.load_pdf`` (pdf_storage_manager.py) ->
     ``_load_from_filesystem`` -> ``_get_safe_file_path`` ->
     ``_safe_path_in_root`` -> ``PathValidator.validate_safe_path``
     (path_validator.py; werkzeug's ``safe_join`` — retained deliberately
     as a security utility, not Flask residue — rejects absolute paths
     and ``..`` segments, plus encoded/unicode traversal) followed by an
     explicit ``is_symlink()`` block.
   * The other read consumer ``get_absolute_path_from_settings``
     (research_library/utils/__init__.py; used by
     download_service.py's ``_try_existing_pdf_extraction`` and
     library_service.py reads) routes through ``_resolve_within_root`` —
     the same validator + symlink check. The legacy shared-root read
     fallback is operator-gated OFF by default
     (``research_library.allow_legacy_read_fallback``).

3. Backup metadata — GREEN PIN (no restore exists; metadata is validated).
   * backup_service.py has NO restore function and no HTTP route accepts
     a user-supplied backup name (the only backup route,
     ``GET /api/backup-status`` in ``web/routers/settings.py``, globs).
   * ``list_backups`` globs ``ldr_backup_*.db`` filtered by
     ``is_safe_glob_result`` (rejects symlinks and entries whose
     ``resolve()`` leaves backup_dir) and exposes only the bare filename.
   * The single ``info["path"]`` consumer, ``purge_and_refresh``,
     iterates that validated list.

FAILURE CLASSIFICATION: red = canary marker appeared in served content,
a canary/symlink was deleted, or backup metadata described a foreign path
(vulnerability tripped). Broken-test = import/setup/expectation bugs.

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

from __future__ import annotations

import hashlib
import itertools
import uuid
from contextlib import contextmanager
from datetime import datetime, timezone
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from tests.test_utils import add_src_to_path

add_src_to_path()

from local_deep_research.database.models import Base, ResearchHistory  # noqa: E402
from local_deep_research.database.models.library import (  # noqa: E402
    Document,
    DocumentStatus,
    SourceType,
)

# The FastAPI migration (#3299) deleted the Flask blueprints this file used
# to drive (``web.routes.research_routes``, ``research_library.routes.
# library_routes``); both route bodies now live in FastAPI routers that
# import ``get_user_db_session`` / ``get_settings_manager`` at module level,
# so the same "patch it at its source" technique below still works.
_RR = "local_deep_research.web.routers.research"
_LR = "local_deep_research.web.routers.library"

# Unique ASCII markers — survive LaTeX templating unchanged and cannot be
# confused with framework noise.
_REPORT_POISON = "LDR_POISONED_REPORT_READ_7f3a9c"
_REPORT_SAFE = "SAFEDBCONTENT42"
_PDF_POISON = b"LDR_POISONED_PDF_READ_7f3a9c"
_PDF_SAFE = b"%PDF-1.4 SAFELIBPDF42\n%%EOF\n"

TEST_PASSWORD = "PoisonReadsPass123"  # noqa: S105

# Monotonic, never random: several clients register per test session and
# random /8 addresses can collide, producing a 429 from a bucket some
# earlier test filled (see test_save_raw_config_blocked_keys_fastapi.py
# for the same reasoning).
_FORWARDED_IP_SEQ = itertools.count(1)


# ---------------------------------------------------------------------------
# Fixtures / helpers
# ---------------------------------------------------------------------------


@pytest.fixture(autouse=True)
def _slowapi_off():
    """Take the per-IP HTTP rate limiter out of the picture.

    Every client below registers a fresh user (``/auth/register`` is rate limited
    per IP); nothing here is about rate limiting.
    """
    from local_deep_research.web.dependencies.rate_limit import limiter

    original = limiter.enabled
    limiter.enabled = False
    try:
        yield
    finally:
        limiter.enabled = original


def _csrf(client: TestClient) -> str:
    """Stamp the session with a CSRF token, then fetch it as a string."""
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _authenticated_fastapi_client(app, username_prefix: str) -> TestClient:
    """Register a fresh real user and return their logged-in TestClient.

    Real registration/login (not a faked session) is what lets these
    requests past the FastAPI routers' ``require_auth`` dependency; the DB
    session itself is separately patched per-test (see
    ``_patched_user_db_session``) so the real per-user database is never
    touched. The registered username is attached to the client as
    ``.username`` for tests that need to compute the per-user storage root.
    """
    n = next(_FORWARDED_IP_SEQ)
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update(
        {"X-Forwarded-For": f"10.{(n // 250) % 250 + 1}.{n % 250 + 1}.17"}
    )
    username = f"{username_prefix}_{uuid.uuid4().hex[:8]}"
    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "confirm_password": TEST_PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        "harness broken — could not register a user to drive the endpoint "
        f"with: {resp.status_code} {resp.text[:300]}"
    )
    client.username = username
    # Every state-changing request below needs a CSRF header (ASGI
    # middleware, no off switch); set a default so POSTs don't each have
    # to fetch their own.
    token = _csrf(client)
    if token:
        client.headers.update({"X-CSRFToken": token})
    return client


@pytest.fixture
def research_client(app):
    """Authenticated client for the REAL FastAPI research router."""
    return _authenticated_fastapi_client(app, "poison-reads-research")


@pytest.fixture
def library_client(app):
    """Authenticated client for the REAL FastAPI library router.

    ``client.username`` is the real registered username — ``TestViewPdfRoute``
    needs it to compute the per-user storage root the same way the route
    does (``apply_user_subdir``).
    """
    return _authenticated_fastapi_client(app, "library-pdf-user")


def _seed_research_db(rid, report_path_value, report_content=_REPORT_SAFE):
    """In-memory SQLite session with one ResearchHistory row.

    The row carries BOTH a poisoned ``report_path`` and legitimate
    ``report_content`` — an implementation that trusts the DB serves the
    safe marker; one that opens ``report_path`` leaks the poison marker.
    Returns ``(session, engine)``; caller must ``_close``.

    ``StaticPool`` + ``check_same_thread=False``: this session is patched
    into a real ``TestClient`` request for a sync route, dispatched
    through ``run_in_threadpool`` on a different thread than the one that
    seeded it here — without a shared, thread-agnostic connection,
    closing/rolling back this session later trips ``sqlite3.
    ProgrammingError: SQLite objects created in a thread can only be used
    in that same thread``. Same fix as
    ``test_socket_ownership_edges_fastapi.py``.
    """
    engine = create_engine(
        "sqlite:///:memory:",
        poolclass=StaticPool,
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    session.add(
        ResearchHistory(
            id=rid,
            query="q",
            mode="quick",
            status="completed",
            created_at=datetime.now(timezone.utc).isoformat(),
            report_path=report_path_value,
            report_content=report_content,
        )
    )
    session.commit()
    return session, engine


def _close(session, engine):
    session.close()
    engine.dispose()


@contextmanager
def _patched_user_db_session(module_target, session):
    """Patch ``get_user_db_session`` in ``module_target`` to yield
    ``session`` (used as a context manager by the route handlers)."""
    with patch(f"{module_target}.get_user_db_session") as get_sess:
        get_sess.return_value.__enter__ = lambda _s: session
        get_sess.return_value.__exit__ = MagicMock(return_value=False)
        yield


@pytest.fixture
def library_session():
    """In-memory SQLite session with the library models + a SourceType.

    Pattern from tests/security/test_remote_filename_writes.py. The
    fixture tears the session down, so tests need no manual cleanup.

    ``StaticPool`` + ``check_same_thread=False`` — see ``_seed_research_db``
    above: ``TestViewPdfRoute`` patches this session into a real
    ``TestClient`` request for a sync route, run in a threadpool worker
    thread.
    """
    engine = create_engine(
        "sqlite:///:memory:",
        poolclass=StaticPool,
        connect_args={"check_same_thread": False},
    )
    Base.metadata.create_all(engine)
    with sessionmaker(bind=engine)() as session:
        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
            description="x",
        )
        session.add(source_type)
        session.commit()
        yield session, source_type
    engine.dispose()


def _seed_document(session, source_type, file_path_value) -> Document:
    """Add a completed filesystem-mode Document with the given (possibly
    poisoned) ``file_path`` and return it."""
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        document_hash=hashlib.sha256(b"placeholder").hexdigest(),
        file_size=100,
        file_type="pdf",
        status=DocumentStatus.COMPLETED,
        title="t",
        filename="t.pdf",
        storage_mode="filesystem",
        file_path=file_path_value,
    )
    session.add(doc)
    session.commit()
    return doc


def _make_canary(directory, name, marker):
    """Plant a canary file with the marker content; returns its path."""
    directory.mkdir(parents=True, exist_ok=True)
    canary = directory / name
    if isinstance(marker, bytes):
        canary.write_bytes(marker)
    else:
        canary.write_text(marker)
    return canary


def _library_settings_mock(base_root: Path):
    """MagicMock settings manager pinning the library root for
    ``view_pdf_page`` (which calls ``get_settings_manager(db_session)``).

    ``apply_user_subdir`` (real code, utils/__init__.py) then resolves the
    effective per-user root to ``<base_root>/<session username>`` — the
    tests must plant payloads relative to THAT root.
    """
    settings_mgr = MagicMock()

    def _get(key, default=None):
        if key == "research_library.storage_path":
            return str(base_root)
        if key == "research_library.pdf_storage_mode":
            return "filesystem"
        if key == "research_library.shared_library":
            return False
        return default

    settings_mgr.get_setting.side_effect = _get
    return settings_mgr


# ---------------------------------------------------------------------------
# SURFACE 1 — POST /api/v1/research/<id>/export/<format>
# ---------------------------------------------------------------------------


class TestResearchExportReads:
    """Export must assemble from DB ``report_content`` and must never open
    the file at ``report_path`` (verified research_routes.py:2170-2254)."""

    def test_absolute_report_path_content_not_served(
        self, research_client, tmp_path
    ):
        canary = _make_canary(
            tmp_path / "export_outside_abs", "canary.txt", _REPORT_POISON
        )
        rid = "rid-export-abs"
        session, engine = _seed_research_db(rid, str(canary))
        try:
            with _patched_user_db_session(_RR, session):
                resp = research_client.post(
                    f"/api/v1/research/{rid}/export/latex"
                )
            assert resp.status_code in (200, 400, 500)
            assert _REPORT_POISON.encode() not in resp.data, (
                "POST /api/v1/research/<id>/export served the file at an "
                "absolute report_path outside the storage root — "
                "arbitrary file read"
            )
        finally:
            _close(session, engine)
        assert canary.read_text() == _REPORT_POISON

    def test_parent_traversal_report_path_content_not_served(
        self, research_client, tmp_path
    ):
        canary = _make_canary(
            tmp_path / "export_outside_traversal", "victim.txt", _REPORT_POISON
        )
        fake_storage = tmp_path / "research_outputs"
        (fake_storage / "sub").mkdir(parents=True)
        traversal = (
            fake_storage
            / "sub"
            / ".."
            / ".."
            / "export_outside_traversal"
            / "victim.txt"
        )
        # The payload must be stat-able, or a reader's .exists() guard
        # would skip the file and produce a false green.
        assert traversal.exists()
        assert traversal.resolve() == canary.resolve()

        rid = "rid-export-traversal"
        session, engine = _seed_research_db(rid, str(traversal))
        try:
            with _patched_user_db_session(_RR, session):
                resp = research_client.post(
                    f"/api/v1/research/{rid}/export/latex"
                )
            assert resp.status_code in (200, 400, 500)
            assert _REPORT_POISON.encode() not in resp.data, (
                "POST /api/v1/research/<id>/export followed a ..-laden "
                "report_path outside the storage root — arbitrary file read"
            )
        finally:
            _close(session, engine)
        assert canary.read_text() == _REPORT_POISON

    def test_symlink_report_path_content_not_served(
        self, research_client, tmp_path
    ):
        canary = _make_canary(
            tmp_path / "export_outside_target",
            "symlink_target.txt",
            _REPORT_POISON,
        )
        fake_storage = tmp_path / "research_outputs"
        fake_storage.mkdir()
        link = fake_storage / "evil_link"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")
        assert link.is_symlink()

        rid = "rid-export-symlink"
        session, engine = _seed_research_db(rid, str(link))
        try:
            with _patched_user_db_session(_RR, session):
                resp = research_client.post(
                    f"/api/v1/research/{rid}/export/latex"
                )
            assert resp.status_code in (200, 400, 500)
            assert _REPORT_POISON.encode() not in resp.data, (
                "POST /api/v1/research/<id>/export followed a symlink "
                "report_path outside the storage root — arbitrary file read"
            )
        finally:
            _close(session, engine)
        assert canary.read_text() == _REPORT_POISON
        assert link.is_symlink() or link.exists()

    def test_positive_control_db_content_is_served(self, research_client):
        """The export feature must keep working: report_content from
        the DB is exported. The row deliberately has no report_path, so
        a regression that starts reading from disk would find nothing
        to serve and these assertions would fail loudly."""
        rid = "rid-export-legit"
        session, engine = _seed_research_db(rid, None)
        try:
            with _patched_user_db_session(_RR, session):
                resp = research_client.post(
                    f"/api/v1/research/{rid}/export/latex"
                )
        finally:
            _close(session, engine)
        assert resp.status_code == 200, (
            f"POSITIVE CONTROL FAILED: legit export returned "
            f"{resp.status_code}; if a path-validation fix was just added "
            "it is over-restrictive."
        )
        assert _REPORT_SAFE.encode() in resp.data
        assert _REPORT_POISON.encode() not in resp.data


# ---------------------------------------------------------------------------
# SURFACE 1b — GET /api/report/<id>
# ---------------------------------------------------------------------------


class TestResearchReportEndpointReads:
    """/api/report must assemble from the DB, never open report_path
    (verified research_routes.py:2093-2163)."""

    def test_absolute_report_path_content_not_served(
        self, research_client, tmp_path
    ):
        canary = _make_canary(
            tmp_path / "report_outside_abs", "canary.txt", _REPORT_POISON
        )
        rid = "rid-report-abs"
        session, engine = _seed_research_db(rid, str(canary))
        try:
            with _patched_user_db_session(_RR, session):
                resp = research_client.get(f"/api/report/{rid}")
            assert resp.status_code in (200, 400, 500)
            assert _REPORT_POISON.encode() not in resp.data, (
                "GET /api/report/<id> served the file at an absolute "
                "report_path outside the storage root — arbitrary file read"
            )
        finally:
            _close(session, engine)
        assert canary.read_text() == _REPORT_POISON

    def test_positive_control_db_content_is_served(self, research_client):
        rid = "rid-report-legit"
        session, engine = _seed_research_db(rid, None)
        try:
            with _patched_user_db_session(_RR, session):
                resp = research_client.get(f"/api/report/{rid}")
        finally:
            _close(session, engine)
        assert resp.status_code == 200, resp.status_code
        assert _REPORT_SAFE.encode() in resp.data


# ---------------------------------------------------------------------------
# SURFACE 2 — PDFStorageManager.load_pdf (service level)
# ---------------------------------------------------------------------------


class TestPdfStorageLoadPdf:
    """``load_pdf`` must route filesystem reads through
    ``_get_safe_file_path`` (PathValidator + symlink check). Pinned
    unit-level by test_pdf_path_traversal.py; pinned here at the serving
    level with an in-memory library DB."""

    @pytest.fixture
    def fs_storage(self, tmp_path):
        from local_deep_research.research_library.services.pdf_storage_manager import (
            PDFStorageManager,
        )

        library_root = tmp_path / "library"
        (library_root / "pdfs").mkdir(parents=True)
        return PDFStorageManager(
            library_root=library_root,
            storage_mode="filesystem",
            username="testuser",
        )

    def test_absolute_file_path_not_served(self, fs_storage, library_session):
        session, source_type = library_session
        canary = _make_canary(
            fs_storage.library_root.parent / "outside_zone",
            "victim.pdf",
            _PDF_POISON,
        )
        doc = _seed_document(session, source_type, str(canary))
        result = fs_storage.load_pdf(doc, session)
        assert result is None or _PDF_POISON not in result, (
            "PDFStorageManager.load_pdf served bytes from an absolute "
            "Document.file_path outside the library root — arbitrary "
            "file read"
        )
        assert canary.read_bytes() == _PDF_POISON

    def test_parent_traversal_file_path_not_served(
        self, fs_storage, library_session
    ):
        session, source_type = library_session
        canary = _make_canary(
            fs_storage.library_root.parent / "outside_zone",
            "victim.pdf",
            _PDF_POISON,
        )
        # library_root/pdfs/../../outside_zone/victim.pdf -> the canary.
        traversal = "pdfs/../../outside_zone/victim.pdf"
        assert (fs_storage.library_root / traversal).resolve() == (
            canary.resolve()
        )
        doc = _seed_document(session, source_type, traversal)
        result = fs_storage.load_pdf(doc, session)
        assert result is None or _PDF_POISON not in result, (
            "PDFStorageManager.load_pdf followed a ..-laden "
            "Document.file_path outside the library root — arbitrary "
            "file read"
        )
        assert canary.read_bytes() == _PDF_POISON

    def test_symlink_file_path_not_served(self, fs_storage, library_session):
        session, source_type = library_session
        canary = _make_canary(
            fs_storage.library_root.parent / "outside_target",
            "victim.pdf",
            _PDF_POISON,
        )
        link = fs_storage.library_root / "pdfs" / "evil_link.pdf"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")
        assert link.is_symlink()
        doc = _seed_document(session, source_type, "pdfs/evil_link.pdf")
        result = fs_storage.load_pdf(doc, session)
        assert result is None or _PDF_POISON not in result, (
            "PDFStorageManager.load_pdf followed a symlink "
            "Document.file_path outside the library root — arbitrary "
            "file read"
        )
        assert canary.read_bytes() == _PDF_POISON
        assert link.is_symlink() or link.exists()

    def test_positive_control_legit_file_is_served(
        self, fs_storage, library_session
    ):
        session, source_type = library_session
        legit = fs_storage.library_root / "pdfs" / "legit.pdf"
        legit.write_bytes(_PDF_SAFE)
        doc = _seed_document(session, source_type, "pdfs/legit.pdf")
        result = fs_storage.load_pdf(doc, session)
        assert result == _PDF_SAFE, (
            "POSITIVE CONTROL FAILED: a legitimate in-root PDF was not "
            "served by load_pdf — containment is overly restrictive"
        )


# ---------------------------------------------------------------------------
# SURFACE 2b — GET /library/document/<id>/pdf (real route)
# ---------------------------------------------------------------------------


class TestViewPdfRoute:
    """The real serving route must never leak canary bytes for a poisoned
    ``Document.file_path``.

    The route computes its root as
    ``apply_user_subdir(base_root, session_username, shared_library=False)``
    = ``<base_root>/<the registered username>`` — all relative payloads are
    planted LIVE against that exact root so a validator regression actually
    trips. The username comes from ``library_client.username`` (the real
    user ``_authenticated_fastapi_client`` registered), not a hardcoded
    constant.
    """

    def _root(self, base_root: Path, library_client) -> Path:
        return base_root / library_client.username

    def _get_pdf(self, library_client, session, doc, base_root):
        with patch(
            f"{_LR}.get_settings_manager",
            return_value=_library_settings_mock(base_root),
        ):
            with _patched_user_db_session(_LR, session):
                return library_client.get(f"/library/document/{doc.id}/pdf")

    def test_absolute_file_path_not_served(
        self, library_client, library_session, tmp_path
    ):
        session, source_type = library_session
        base_root = tmp_path / "library"
        (self._root(base_root, library_client) / "pdfs").mkdir(parents=True)
        canary = _make_canary(tmp_path / "secrets", "canary.pdf", _PDF_POISON)
        doc = _seed_document(session, source_type, str(canary))
        resp = self._get_pdf(library_client, session, doc, base_root)
        assert resp.status_code in (200, 404, 500)
        assert _PDF_POISON not in resp.data, (
            "GET /library/document/<id>/pdf served bytes from an absolute "
            "Document.file_path outside the library root — arbitrary "
            "file read"
        )
        assert canary.read_bytes() == _PDF_POISON

    def test_parent_traversal_file_path_not_served(
        self, library_client, library_session, tmp_path
    ):
        session, source_type = library_session
        base_root = tmp_path / "library"
        root = self._root(base_root, library_client)
        (root / "pdfs").mkdir(parents=True)
        canary = _make_canary(tmp_path / "secrets", "canary.pdf", _PDF_POISON)
        # root/pdfs/../../../secrets/canary.pdf -> tmp_path/secrets/...
        traversal = "pdfs/../../../secrets/canary.pdf"
        assert (root / traversal).resolve() == canary.resolve()
        doc = _seed_document(session, source_type, traversal)
        resp = self._get_pdf(library_client, session, doc, base_root)
        assert resp.status_code in (200, 404, 500)
        assert _PDF_POISON not in resp.data, (
            "GET /library/document/<id>/pdf followed a ..-laden "
            "Document.file_path outside the library root — arbitrary "
            "file read"
        )
        assert canary.read_bytes() == _PDF_POISON

    def test_symlink_file_path_not_served(
        self, library_client, library_session, tmp_path
    ):
        session, source_type = library_session
        base_root = tmp_path / "library"
        root = self._root(base_root, library_client)
        (root / "pdfs").mkdir(parents=True)
        canary = _make_canary(tmp_path / "secrets", "canary.pdf", _PDF_POISON)
        link = root / "pdfs" / "evil_link.pdf"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")
        assert link.is_symlink()
        doc = _seed_document(session, source_type, "pdfs/evil_link.pdf")
        resp = self._get_pdf(library_client, session, doc, base_root)
        assert resp.status_code in (200, 404, 500)
        assert _PDF_POISON not in resp.data, (
            "GET /library/document/<id>/pdf followed a symlink "
            "Document.file_path outside the library root — arbitrary "
            "file read"
        )
        assert canary.read_bytes() == _PDF_POISON
        assert link.is_symlink() or link.exists()

    def test_positive_control_legit_file_is_served(
        self, library_client, library_session, tmp_path
    ):
        session, source_type = library_session
        base_root = tmp_path / "library"
        root = self._root(base_root, library_client)
        (root / "pdfs").mkdir(parents=True)
        legit = root / "pdfs" / "legit.pdf"
        legit.write_bytes(_PDF_SAFE)
        doc = _seed_document(session, source_type, "pdfs/legit.pdf")
        resp = self._get_pdf(library_client, session, doc, base_root)
        assert resp.status_code == 200, (
            f"POSITIVE CONTROL FAILED: legit PDF view returned "
            f"{resp.status_code}; if a containment fix was just added it "
            "is over-restrictive."
        )
        assert resp.data == _PDF_SAFE


# ---------------------------------------------------------------------------
# SURFACE 2c — get_absolute_path_from_settings (the resolver used by
# download_service._try_existing_pdf_extraction and library_service reads)
# ---------------------------------------------------------------------------


def _relative_escape(root: Path, canary: Path) -> str:
    """A ``..``-laden relative path from ``root`` to ``canary`` (pure pathlib)."""
    root_parts = root.resolve().parts
    canary_parts = canary.resolve().parts
    common = 0
    for a, b in zip(root_parts, canary_parts):
        if a != b:
            break
        common += 1
    ups = [".."] * (len(root_parts) - common)
    downs = list(canary_parts[common:])
    return "/".join(ups + downs)


class TestLibraryPathResolver:
    def test_poisoned_relative_paths_resolve_to_none(
        self, tmp_path, monkeypatch
    ):
        """absolute / ..-traversal / symlink inputs must all fail closed
        to ``None`` — the contract every filesystem read in the library
        services depends on.

        The explicit ``settings_manager`` argument is the exact
        mechanism ``DownloadService`` uses in production (it passes its
        captured per-user manager); the ambient manager would resolve
        the operator's real defaults-file storage_path, which is not
        writable test territory."""
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        # get_absolute_path_from_settings's storage_path lookup carries an
        # eagerly-evaluated str(get_library_directory()) default -- Python
        # evaluates it before get_setting() runs, regardless of the
        # explicit settings_manager passed below, so it unconditionally
        # mkdirs <data_dir>/library. Pin LDR_DATA_DIR so that lands under
        # tmp_path instead of the real platformdirs user data dir.
        monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "app_data"))
        base_root = tmp_path / "library"
        username = "poison-resolver-user"
        root = base_root / username  # apply_user_subdir, shared off
        root.mkdir(parents=True)
        settings = _library_settings_mock(base_root)
        canary = _make_canary(tmp_path / "secrets", "canary.pdf", _PDF_POISON)

        # Absolute input -> rejected by safe_join (isabs).
        assert (
            get_absolute_path_from_settings(
                str(canary), username, settings_manager=settings
            )
            is None
        ), "resolver returned a path for an ABSOLUTE input — traversal hole"

        # ..-laden relative input, derived from the ACTUAL root so the
        # payload is live (resolves onto the canary).
        traversal = _relative_escape(root, canary)
        assert traversal.startswith(".."), traversal
        assert (root / traversal).resolve() == canary.resolve()
        assert (
            get_absolute_path_from_settings(
                traversal, username, settings_manager=settings
            )
            is None
        ), "resolver returned a path for a ..-laden input — traversal hole"

        # Symlink inside the per-user root pointing outside.
        link = root / "evil_link.pdf"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")
        resolved = get_absolute_path_from_settings(
            "evil_link.pdf", username, settings_manager=settings
        )
        assert resolved is None, (
            f"resolver returned {resolved} for a symlink input — symlink "
            "escape hole"
        )
        assert canary.read_bytes() == _PDF_POISON


# ---------------------------------------------------------------------------
# SURFACE 3 — backup metadata (list_backups / purge_and_refresh)
# ---------------------------------------------------------------------------


class TestBackupMetadata:
    """No restore function exists and no HTTP route takes a backup name;
    the only ``info["path"]`` consumer is purge_and_refresh. Pin that
    backup metadata never describes anything outside the per-user backup
    dir — the invariant any future restore would have to trust."""

    USERNAME = "poison-backup-user"

    def _service(self, tmp_path, monkeypatch):
        from local_deep_research.database.backup.backup_service import (
            BackupService,
        )

        # LDR_DATA_DIR routes get_data_directory() (and therefore
        # get_user_backup_directory) under tmp_path.
        monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "app_data"))
        service = BackupService(self.USERNAME, "pw-not-used")
        service.backup_dir.mkdir(parents=True, exist_ok=True)
        return service

    def test_list_backups_excludes_symlinks_and_reports_only_in_root(
        self, tmp_path, monkeypatch
    ):
        service = self._service(tmp_path, monkeypatch)
        legit = service.backup_dir / "ldr_backup_20260101_000000.db"
        legit.write_bytes(b"fake-encrypted-backup")
        canary = _make_canary(
            tmp_path / "outside_backups", "victim.db", _PDF_POISON
        )
        link = service.backup_dir / "ldr_backup_evil.db"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")

        backups = service.list_backups()
        names = {info["path"] for info in backups}

        # The symlink entry must never be reported — a consumer (purge,
        # or any future restore) would follow it outside the backup dir.
        assert "ldr_backup_evil.db" not in names, (
            "list_backups reported a symlink backup entry pointing "
            "outside backup_dir"
        )
        assert "ldr_backup_20260101_000000.db" in names, (
            "POSITIVE CONTROL FAILED: a legitimate in-root backup was "
            "not listed"
        )
        # Every reported path is a bare, in-root, non-symlink filename.
        for info in backups:
            p = info["path"]
            assert p == Path(p).name, (
                f"backup metadata path {p!r} is not a bare filename"
            )
            full = (service.backup_dir / p).resolve()
            assert full.is_relative_to(service.backup_dir.resolve()), (
                f"backup metadata path {p!r} resolves outside backup_dir"
            )
            assert not (service.backup_dir / p).is_symlink()
        assert canary.read_bytes() == _PDF_POISON
        assert link.is_symlink() or link.exists()

    def test_purge_and_refresh_unlinks_only_validated_backups(
        self, tmp_path, monkeypatch
    ):
        from local_deep_research.database.backup.backup_service import (
            BackupResult,
        )

        service = self._service(tmp_path, monkeypatch)
        legit = service.backup_dir / "ldr_backup_20260101_000000.db"
        legit.write_bytes(b"fake-encrypted-backup")
        canary = _make_canary(
            tmp_path / "outside_backups", "victim.db", _PDF_POISON
        )
        link = service.backup_dir / "ldr_backup_evil.db"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")

        # Stub the fresh-backup creation (needs real SQLCipher); the
        # list_backups-driven unlink loop runs for real.
        with patch.object(
            service,
            "_create_backup_impl",
            return_value=BackupResult(success=True),
        ):
            result = service.purge_and_refresh()

        assert result.success
        # Positive control: the validated legit backup IS purged.
        assert not legit.exists(), (
            "POSITIVE CONTROL FAILED: purge_and_refresh did not remove a "
            "validated in-root backup"
        )
        # The symlink entry must survive untouched (never followed).
        assert link.is_symlink(), (
            "purge_and_refresh removed a symlink entry from the backup "
            "dir — the is_safe_glob_result filter regressed"
        )
        assert canary.read_bytes() == _PDF_POISON, (
            "purge_and_refresh followed a symlink backup entry and "
            "deleted/modified a file outside the backup dir"
        )