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.

8.3 KB
Raw Blame
"""Tests for ODTExporter._sanitize_metadata.

This is a security-critical function that prevents Pandoc argument injection
via user-supplied metadata values. Zero prior direct test coverage.
"""

import pytest

from local_deep_research.exporters.odt_exporter import ODTExporter


@pytest.fixture
def exporter():
    """Create an ODTExporter instance."""
    return ODTExporter()


class TestSanitizeMetadataInjectionPrevention:
    """Tests for _sanitize_metadata argument injection prevention."""

    def test_normal_text_unchanged(self, exporter):
        assert (
            exporter._sanitize_metadata("My Research Report")
            == "My Research Report"
        )

    def test_double_dash_removed(self, exporter):
        assert (
            exporter._sanitize_metadata("--output=evil.sh") == "output=evil.sh"
        )

    def test_newlines_replaced_with_spaces(self, exporter):
        assert exporter._sanitize_metadata("line1\nline2") == "line1 line2"

    def test_multiple_double_dashes(self, exporter):
        result = exporter._sanitize_metadata("--flag1 --flag2 --flag3")
        assert "--" not in result

    def test_empty_string(self, exporter):
        assert exporter._sanitize_metadata("") == ""

    def test_only_dashes(self, exporter):
        assert exporter._sanitize_metadata("----") == ""

    def test_single_dash_preserved(self, exporter):
        assert exporter._sanitize_metadata("well-known") == "well-known"

    def test_unicode_preserved(self, exporter):
        assert exporter._sanitize_metadata("Ünïcödé Títlé") == "Ünïcödé Títlé"

    def test_triple_dash_removes_double_part(self, exporter):
        result = exporter._sanitize_metadata("---metadata")
        assert "--" not in result

    def test_mixed_injection_patterns(self, exporter):
        result = exporter._sanitize_metadata(
            "Title\n--variable=x\n--output=/tmp/evil"
        )
        assert "\n" not in result
        assert "--" not in result

    def test_embedded_single_dashes_preserved(self, exporter):
        assert (
            exporter._sanitize_metadata("state-of-the-art")
            == "state-of-the-art"
        )

    def test_tab_characters_preserved(self, exporter):
        result = exporter._sanitize_metadata("col1\tcol2")
        assert "\t" in result

    def test_extract_media_injection(self, exporter):
        result = exporter._sanitize_metadata("--extract-media=/tmp")
        assert "--" not in result

    def test_multiple_newlines(self, exporter):
        result = exporter._sanitize_metadata("a\nb\nc\nd")
        assert "\n" not in result
        assert "a b c d" == result


class TestSanitizeMetadataNulRemoval:
    """NUL (U+0000) must never reach a pandoc argument (#5995).

    Python's subprocess rejects any argv element containing NUL with
    ``ValueError: embedded null byte`` before Pandoc starts, so a NUL in
    title/author/date metadata would otherwise fail the whole export.
    """

    def test_nul_removed(self, exporter):
        assert exporter._sanitize_metadata("Ti\x00tle") == "Title"

    def test_multiple_nuls_removed(self, exporter):
        assert exporter._sanitize_metadata("a\x00b\x00c") == "abc"

    def test_only_nuls_becomes_empty(self, exporter):
        assert exporter._sanitize_metadata("\x00\x00") == ""

    def test_nul_combined_with_injection_patterns(self, exporter):
        result = exporter._sanitize_metadata("Ti\x00tle\n--output=evil")
        assert "\x00" not in result
        assert "\n" not in result
        assert "--" not in result

    def test_unicode_hyphen_2010_preserved(self, exporter):
        # U+2010 (HYPHEN) is a legitimate Unicode character, not a NUL
        value = "Well\u2010Known Title"
        assert exporter._sanitize_metadata(value) == value

    def test_sanitize_output_never_contains_nul(self, exporter):
        value = "\x00--\x00a\nb\x00\n--\x00"
        assert "\x00" not in exporter._sanitize_metadata(value)

    def test_nul_cannot_reconstruct_double_dash(self, exporter):
        # "-\x00-" must not sanitize to "--": NUL is stripped before the
        # injection-pattern pass so it cannot join two dashes afterwards.
        result = exporter._sanitize_metadata("-\x00-")
        assert "\x00" not in result
        assert "--" not in result
        assert result == ""

    def test_nul_split_injection_pattern_neutralized(self, exporter):
        # A "--" pattern split by NUL must not reassemble after sanitization
        result = exporter._sanitize_metadata("-\x00-output=evil")
        assert "\x00" not in result
        assert "--" not in result
        assert result == "output=evil"


class TestExportNulSafety:
    """The pandoc argv built by export() must be NUL-free for every
    metadata path (title, author, date), or subprocess.run raises
    ``ValueError: embedded null byte`` before Pandoc starts (#5995).

    These tests mock subprocess and pypandoc so they run without a
    real Pandoc install; the pinned property is the argv content.
    """

    def _export_with_mocked_pandoc(self, monkeypatch, options):
        """Run export() with faked pandoc; return (result, argv)."""
        from unittest.mock import MagicMock

        import local_deep_research.exporters.odt_exporter as odt_module

        mock_pypandoc = MagicMock()
        mock_pypandoc.get_pandoc_path.return_value = "/fake/pandoc"
        monkeypatch.setattr(odt_module, "pypandoc", mock_pypandoc)
        monkeypatch.setattr(odt_module, "PYPANDOC_AVAILABLE", True)
        # The sandbox probe would otherwise parse the MagicMock's
        # version string (or reuse a value another test cached) and
        # refuse the export; these tests pin the argv, not the probe.
        monkeypatch.setattr(
            odt_module.ODTExporter,
            "_pandoc_supports_sandbox",
            MagicMock(return_value=True),
        )

        fake_odt = b"PK\x03\x04fake-odt-bytes"
        mock_run = MagicMock(
            return_value=type("R", (), {"stdout": fake_odt, "stderr": b""})()
        )
        monkeypatch.setattr(odt_module.subprocess, "run", mock_run)

        result = odt_module.ODTExporter().export("# Test", options)
        argv = mock_run.call_args.args[0]
        return result, argv

    def test_title_with_nul_never_reaches_argv(self, monkeypatch):
        from local_deep_research.exporters import ExportOptions

        result, argv = self._export_with_mocked_pandoc(
            monkeypatch, ExportOptions(title="Ti\x00tle --safe")
        )
        assert result.content[:2] == b"PK"
        assert all("\x00" not in arg for arg in argv)
        assert "--metadata=title:Title safe" in argv

    def test_author_with_nul_never_reaches_argv(self, monkeypatch):
        from local_deep_research.exporters import ExportOptions

        result, argv = self._export_with_mocked_pandoc(
            monkeypatch, ExportOptions(metadata={"author": "Au\x00thor"})
        )
        assert result.content[:2] == b"PK"
        assert all("\x00" not in arg for arg in argv)
        assert "--metadata=author:Author" in argv

    def test_date_with_nul_never_reaches_argv(self, monkeypatch):
        from local_deep_research.exporters import ExportOptions

        result, argv = self._export_with_mocked_pandoc(
            monkeypatch,
            ExportOptions(metadata={"date": "2024\x0001\x0015"}),
        )
        assert result.content[:2] == b"PK"
        assert all("\x00" not in arg for arg in argv)
        assert "--metadata=date:20240115" in argv

    def test_all_metadata_paths_with_nul(self, monkeypatch):
        from local_deep_research.exporters import ExportOptions

        options = ExportOptions(
            title="Re\x00port",
            metadata={"author": "A\x00b", "date": "2024\x0001"},
        )
        result, argv = self._export_with_mocked_pandoc(monkeypatch, options)
        assert result.content[:2] == b"PK"
        assert all("\x00" not in arg for arg in argv)
        assert "--metadata=title:Report" in argv
        assert "--metadata=author:Ab" in argv
        assert "--metadata=date:202401" in argv

    def test_subprocess_invocation_stays_list_form(self, monkeypatch):
        """The fix must not change the injection-safe invocation shape."""
        from local_deep_research.exporters import ExportOptions

        _, argv = self._export_with_mocked_pandoc(
            monkeypatch, ExportOptions(title="Ti\x00tle")
        )
        assert isinstance(argv, list)
        assert argv[0] == "/fake/pandoc"