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.

4.5 KB
Raw Blame
"""Control-character contracts for exporter filenames.

``_generate_safe_filename`` deliberately keeps the ``\\s`` class so the
HTTP download route can percent-encode CR/LF/TAB in the
Content-Disposition (``%0D%0A``) -- a behavior locked by the
hostile-input matrix and the promoted export contracts. The one
consumer that embeds the stem RAW in another container is the Quarto
export's zip entry names, so the control-character stripping lives at
that sink. These tests pin it there, plus the empty-stem fallback in
the shared helper.
"""

from __future__ import annotations

import io
import re
import zipfile

import pytest

from local_deep_research.exporters.base import ExportOptions
from local_deep_research.exporters.quarto_exporter import QuartoExporter
from local_deep_research.exporters.ris_exporter import RISExporter


def _zip_entry_names(export_result) -> list[str]:
    with zipfile.ZipFile(io.BytesIO(export_result.content)) as archive:
        return archive.namelist()


# Every code point that is both (a) matched by the base helper's ``\s``
# (so it survives ``_generate_safe_filename`` unstripped) and (b) matched
# by the sink's ``is_line_breaking_char`` class -- i.e. every control
# character actually reachable at the sink. Narrowing the sink's class
# by even one of these must fail test_every_live_sink_codepoint_is_stripped
# below.
_LIVE_SINK_CODEPOINTS = [
    ("\t", "TAB"),
    ("\n", "LF"),
    ("\v", "VT"),
    ("\f", "FF"),
    ("\r", "CR"),
    ("\x1c", "FS"),
    ("\x1d", "GS"),
    ("\x1e", "RS"),
    ("\x1f", "US"),
    ("\x85", "NEL"),
    ("\u2028", "LINE_SEPARATOR"),
    ("\u2029", "PARAGRAPH_SEPARATOR"),
]


class TestQuartoEntryNamesStripControlCharacters:
    """Control characters must not ride raw into archive entry names."""

    @pytest.mark.parametrize(
        "char,name",
        _LIVE_SINK_CODEPOINTS,
        ids=[codepoint_name for _, codepoint_name in _LIVE_SINK_CODEPOINTS],
    )
    def test_every_live_sink_codepoint_is_stripped(self, char, name):
        """Each live code point must be absent from both archive
        surfaces: the entry names and the download filename.
        """
        exporter = QuartoExporter()

        result = exporter.export(
            "Body text",
            options=ExportOptions(title=f"Report{char}X-Evil: 1"),
        )

        for entry in _zip_entry_names(result):
            assert char not in entry, (
                f"{name} ({char!r}) survived raw in entry name {entry!r}"
            )
        assert char not in result.filename, (
            f"{name} ({char!r}) survived raw in download filename "
            f"{result.filename!r}"
        )

    def test_crlf_title_yields_clean_two_entry_archive(self):
        exporter = QuartoExporter()

        result = exporter.export(
            "Body text",
            options=ExportOptions(title="a\r\nX-Evil: 1"),
        )

        entries = _zip_entry_names(result)
        # The archive always holds exactly the document and the
        # bibliography, regardless of title content -- pinning the
        # structural fact, not a specific threat.
        assert len(entries) == 2, entries
        for entry in entries:
            assert not re.search(r"[\x00-\x1f\x7f]", entry), (
                f"archive entry name {entry!r} carries control characters"
            )

    def test_tab_is_stripped_from_entries(self):
        """TAB is live at the sink: the helper keeps ``\\s``, which
        includes TAB, so it is the sink's own class removing it here --
        unlike NUL, which the helper already strips before the sink
        ever runs.
        """
        exporter = QuartoExporter()

        result = exporter.export(
            "Body text",
            options=ExportOptions(title="Re\tport_final"),
        )

        for entry in _zip_entry_names(result):
            assert not re.search(r"[\x00-\x1f\x7f]", entry), (
                f"archive entry name {entry!r} carries control characters"
            )


class TestEmptyStemFallback:
    """Punctuation/whitespace-only titles fall back to the default stem."""

    def test_all_punctuation_title_falls_back(self):
        exporter = RISExporter()

        filename = exporter._generate_safe_filename("@#$%^&*()")

        assert filename == "research_report.ris"

    def test_quarto_empty_stem_still_names_its_entries(self):
        exporter = QuartoExporter()

        result = exporter.export(
            "Body text", options=ExportOptions(title="@#$%^&*()")
        )

        entries = _zip_entry_names(result)
        assert entries and all(not e.startswith(".") for e in entries), entries