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.

5.4 KB
Raw Blame
"""``clear_history`` must confine report-file unlinks to the reports root.

``delete_research`` resolves each row's ``report_path`` and refuses to
unlink anything outside ``get_research_outputs_directory()`` — the
documented reason is that the DB column is server-written but a
corrupted row (or a future buggy writer) could point it at
``../../etc/passwd``. Before this fix, ``clear_history`` deleted report
files from the same column with a bare ``Path(report_path).unlink()`` and
no confinement check, so the bulk path could delete arbitrary files the
corrupted-row threat model names.

These tests pin parity with the single-delete path: a report file
outside the reports root survives the bulk clear (rows still deleted),
and a report file inside the root is still unlinked.
"""

from contextlib import contextmanager
from unittest.mock import patch

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

_RR = "local_deep_research.web.routers.research"
_PATHS = "local_deep_research.config.paths"
_LIFECYCLE_CLEANUP = (
    "local_deep_research.web.queue.lifecycle_cleanup."
    "cleanup_queued_research_state"
)

_ENGINE_KW = {
    "connect_args": {"check_same_thread": False},
    "poolclass": StaticPool,
}


@pytest.fixture
def db():
    """A real in-memory database with the full schema; yields a session."""
    from local_deep_research.database.models import Base

    engine = create_engine("sqlite:///:memory:", **_ENGINE_KW)
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    try:
        yield session
    finally:
        session.close()
        engine.dispose()


def _patch_session(session):
    @contextmanager
    def _ctx(*_args, **_kwargs):
        yield session

    return patch(f"{_RR}.get_user_db_session", _ctx)


def _add_completed_research(session, rid, report_path):
    from local_deep_research.database.models import ResearchHistory

    session.add(
        ResearchHistory(
            id=rid,
            query="q",
            mode="quick",
            status="completed",
            created_at="2025-01-01T00:00:00+00:00",
            report_path=report_path,
        )
    )
    session.commit()


def _ids(session):
    from local_deep_research.database.models import ResearchHistory

    return {row.id for row in session.query(ResearchHistory).all()}


def _clear_history(authenticated_client, db, reports_root):
    with (
        _patch_session(db),
        patch(f"{_RR}.get_active_research_ids", return_value=[]),
        patch(_LIFECYCLE_CLEANUP),
        patch(
            f"{_PATHS}.get_research_outputs_directory",
            return_value=reports_root,
        ),
    ):
        return authenticated_client.post("/api/clear_history")


def test_clear_history_refuses_to_unlink_report_path_outside_the_reports_root(
    authenticated_client, db, tmp_path
):
    """A corrupted-row report_path outside the reports root survives.

    The row is still deleted — the file confinement must stop the
    unlink, not the history clear.
    """
    sentinel = tmp_path / "sentinel.txt"
    sentinel.write_text("do not delete", encoding="utf-8")
    reports_root = tmp_path / "reports"
    reports_root.mkdir()
    _add_completed_research(db, "row-1", report_path=str(sentinel))

    response = _clear_history(authenticated_client, db, reports_root)

    assert response.status_code == 200, response.text[:300]
    assert sentinel.exists(), (
        "clear_history unlinked a report_path outside the reports root — "
        "the bulk path lacks the confinement delete_research enforces"
    )
    assert _ids(db) == set(), (
        "clear_history left the history row behind even though the "
        "confinement refused only the file unlink"
    )


def test_clear_history_still_unlinks_report_path_inside_the_reports_root(
    authenticated_client, db, tmp_path
):
    """The confinement must not stop legitimate report cleanup."""
    reports_root = tmp_path / "reports"
    reports_root.mkdir()
    report = reports_root / "report.md"
    report.write_text("stale report", encoding="utf-8")
    _add_completed_research(db, "row-1", report_path=str(report))

    response = _clear_history(authenticated_client, db, reports_root)

    assert response.status_code == 200, response.text[:300]
    assert not report.exists(), "legitimate report file inside the root must go"


def test_clear_history_refuses_to_unlink_report_path_in_name_prefix_sibling(
    authenticated_client, db, tmp_path
):
    """A name-prefix sibling directory is not inside the reports root.

    ``research_outputs-evil`` starts with ``research_outputs`` as a
    string, but is not inside it as a path. A
    ``str(resolved).startswith(str(reports_root))`` containment check
    would treat it as contained and delete the file below;
    ``Path.is_relative_to`` must not.
    """
    reports_root = tmp_path / "research_outputs"
    reports_root.mkdir()
    evil_file = tmp_path / "research_outputs-evil" / "f.md"
    evil_file.parent.mkdir()
    evil_file.write_text("do not delete", encoding="utf-8")
    _add_completed_research(db, "row-1", report_path=str(evil_file))

    response = _clear_history(authenticated_client, db, reports_root)

    assert response.status_code == 200, response.text[:300]
    assert evil_file.exists(), (
        "clear_history unlinked a report_path in a name-prefix sibling "
        "directory — a str.startswith containment check would delete it"
    )