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.

3.0 KB
Raw Blame
"""Unit tests for export Content-Disposition filename encoding.

Starlette encodes response headers as latin-1 at Response construction, so
interpolating a raw research title into Content-Disposition raised
UnicodeEncodeError and 500'd the export whenever the title contained CJK,
Cyrillic, or any other non-latin-1 text (the exporter's
_generate_safe_filename keeps Unicode word characters — \\w is
Unicode-aware). Flask's send_file handled this via RFC 2231/5987 encoding;
the route now emits filename*=UTF-8'' plus a plain ASCII fallback (matching
library.py's PDF route).
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
from urllib.parse import quote

import pytest

R = "local_deep_research.web.routers.research"


def _research_row(title):
    row = Mock()
    row.title = title
    row.query = title
    return row


def _call_export(filename):
    from local_deep_research.web.routers.research import (
        export_research_report,
    )

    query = MagicMock()
    query.filter_by.return_value.first.return_value = _research_row("t")
    session = MagicMock()
    session.query.return_value = query

    @contextmanager
    def fake_db_session(*a, **kw):
        yield session

    with (
        patch(f"{R}.get_user_db_session", side_effect=fake_db_session),
        patch(
            "local_deep_research.web.services.report_assembly_service"
            ".assemble_full_report",
            return_value="# report",
        ),
        patch(
            f"{R}.export_report_to_memory",
            return_value=(b"content", filename, "application/x-latex"),
        ),
    ):
        return export_research_report(
            Mock(), "rid-1", "latex", username="alice"
        )


@pytest.mark.parametrize(
    "filename",
    [
        "研究报告_2026.tex",  # CJK
        "исследование.tex",  # Cyrillic
        "Bericht über Käfer.tex",  # latin-1 but with umlauts + space
    ],
)
def test_non_latin1_filenames_export_without_crashing(filename):
    """Raw interpolation raised UnicodeEncodeError at Response
    construction; RFC 5987 encoding must survive any title."""
    resp = _call_export(filename)

    assert resp.status_code == 200
    disposition = resp.headers["content-disposition"]
    assert disposition.startswith('attachment; filename="')
    assert "filename*=UTF-8''" in disposition
    assert quote(filename, safe="") in disposition


def test_ascii_filename_round_trips():
    resp = _call_export("report.tex")

    assert resp.status_code == 200
    assert "report.tex" in resp.headers["content-disposition"]


def test_header_injection_is_neutralized():
    """A quote/CRLF in the stored title must not break out of the header."""
    resp = _call_export('evil".tex\r\nX-Injected: 1')

    assert resp.status_code == 200
    disposition = resp.headers["content-disposition"]
    assert "\r" not in disposition and "\n" not in disposition
    assert '"' not in disposition.split("''", 1)[1]
    assert "x-injected" not in resp.headers