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.

fix(export): strip NUL from ODT metadata before invoking pandoc (#5997)

LearningCircuit67f3a81c953f
diff --git a/src/local_deep_research/exporters/odt_exporter.py b/src/local_deep_research/exporters/odt_exporter.py
index eca548929f..74c4333fa8 100644
--- a/src/local_deep_research/exporters/odt_exporter.py
+++ b/src/local_deep_research/exporters/odt_exporter.py
@@ -178,11 +178,18 @@ class ODTExporter(BaseExporter):
Removes potential pandoc argument injection patterns from user-supplied
metadata values.
+ Also strips NUL characters (U+0000): Python's subprocess rejects any
+ argv element containing NUL with ``ValueError: embedded null byte``
+ before Pandoc starts, so a NUL in the title, author, or date would
+ otherwise fail the whole export (#5995).
+
Args:
value: The metadata value to sanitize
Returns:
Sanitized metadata value safe for pandoc arguments
"""
- # Remove potential argument injection patterns
- return value.replace("--", "").replace("\n", " ")
+ # Remove potential argument injection patterns. NUL is stripped
+ # first so it cannot join two dashes into a fresh "--" pair after
+ # the injection-pattern pass has already run.
+ return value.replace("\x00", "").replace("--", "").replace("\n", " ")
diff --git a/tests/exporters/test_odt_sanitize_metadata.py b/tests/exporters/test_odt_sanitize_metadata.py
index 65b00b4947..997ddb7fac 100644
--- a/tests/exporters/test_odt_sanitize_metadata.py
+++ b/tests/exporters/test_odt_sanitize_metadata.py
@@ -77,3 +77,137 @@ class TestSanitizeMetadataInjectionPrevention:
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)
+
+ 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"