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.

12.2 KB
Raw Blame
"""Bounds for the ODT export's pandoc subprocess.

The ODT exporter pipes the user's report markdown into a pandoc
subprocess: ``subprocess.run(cmd, input=..., capture_output=True,
check=True)`` with **no timeout** and **unbounded captured stdout**,
and it logs the *full* pandoc stderr on failure — which quotes input
lines, i.e. report content. A pathological report can pin an export
worker indefinitely, and file and URL references in the markdown are
resolved by pandoc (local reads, network fetches) unless sandboxed.

These contracts pin: a wall-clock timeout that reaches the subprocess,
typed failure on hang, an output-size ceiling, a sandboxed argv with a
GHC heap cap, refusal when sandbox support is unavailable or unknown,
and truncated stderr in both the raised error and the log call.
"""

from __future__ import annotations

import io
import subprocess
import threading
import zipfile
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

import pytest

from local_deep_research.exporters.odt_exporter import ODTExporter


@pytest.fixture(autouse=True)
def _fresh_sandbox_probe():
    """The version probe is lru_cached per process; never let a value
    cached by one test (or another module's pypandoc mock) leak."""
    cached_probe = ODTExporter._pandoc_supports_sandbox
    cached_probe.cache_clear()
    yield
    cached_probe.cache_clear()


class _RecordingLogger:
    def __init__(self):
        self.calls: list[tuple[str, tuple, dict]] = []

    def __getattr__(self, level):
        def record(*args, **kwargs):
            self.calls.append((level, args, kwargs))

        return record

    def rendered(self, level: str) -> list[str]:
        return [
            " ".join(str(a) for a in args)
            for lvl, args, _ in self.calls
            if lvl == level
        ]


def _patched_logger(monkeypatch) -> _RecordingLogger:
    from local_deep_research.exporters import odt_exporter as mod

    recorder = _RecordingLogger()
    monkeypatch.setattr(mod, "logger", recorder)
    return recorder


def _exporter() -> ODTExporter:
    return ODTExporter()


def _patched_run(monkeypatch, **behavior):
    from local_deep_research.exporters import odt_exporter as mod

    recorded: dict = {}

    def fake_run(cmd, **kwargs):
        recorded.update(kwargs)
        recorded["cmd"] = cmd
        if "raise_timeout" in behavior:
            raise subprocess.TimeoutExpired(cmd, kwargs.get("timeout", 0))
        if "raise_called" in behavior:
            raise subprocess.CalledProcessError(
                1, cmd, stderr=behavior["raise_called"]
            )
        return subprocess.CompletedProcess(
            cmd, 0, stdout=behavior.get("stdout", b"PK-odt-bytes")
        )

    monkeypatch.setattr(mod.subprocess, "run", fake_run)
    monkeypatch.setattr(mod, "PYPANDOC_AVAILABLE", True)
    monkeypatch.setattr(
        mod.pypandoc, "get_pandoc_path", lambda: "/usr/bin/pandoc"
    )
    return recorded


class TestPandocBounds:
    def test_the_timeout_reaches_the_subprocess(self, monkeypatch):
        recorded = _patched_run(monkeypatch)

        try:
            _exporter().export("# T\n\nbody")
        except RuntimeError:
            pass  # the fake's output may fail later checks; shape first

        assert recorded.get("timeout") is not None and (
            recorded["timeout"] >= 30
        ), f"pandoc runs without a wall-clock timeout: {recorded}"

    def test_hang_surfaces_as_typed_error(self, monkeypatch):
        _patched_run(monkeypatch, raise_timeout=True)

        with pytest.raises(RuntimeError) as excinfo:
            _exporter().export("# T\n\nbody")

        assert "timed out" in str(excinfo.value).lower()

    def test_runaway_stdout_is_refused(self, monkeypatch):
        _patched_run(monkeypatch, stdout=b"\x00" * 1000)

        with pytest.raises(RuntimeError) as excinfo:
            ODTExporter(max_output_bytes=100).export("# T\n\nbody")

        assert "larger than" in str(excinfo.value).lower()

    def test_pandoc_argv_is_sandboxed_when_supported(self, monkeypatch):
        recorded = _patched_run(monkeypatch)
        monkeypatch.setattr(
            ODTExporter,
            "_pandoc_supports_sandbox",
            staticmethod(lambda: True),
        )

        try:
            _exporter().export("# T\n\nbody")
        except RuntimeError:
            pass

        assert "--sandbox" in recorded.get("cmd", []), (
            f"pandoc argv not sandboxed: {recorded.get('cmd')}"
        )

    def test_heap_cap_reaches_pandoc(self, monkeypatch):
        recorded = _patched_run(monkeypatch)
        monkeypatch.setattr(
            ODTExporter,
            "_pandoc_supports_sandbox",
            staticmethod(lambda: True),
        )

        _exporter().export("# T\n\nbody")

        assert recorded["cmd"][1:4] == ["+RTS", "-M1024m", "-RTS"]
        assert "--sandbox" in recorded["cmd"]

    @pytest.mark.parametrize("max_heap_mb", [0, -1, 1025, "1024"])
    def test_heap_limit_cannot_be_disabled(self, max_heap_mb):
        with pytest.raises(ValueError, match="max_heap_mb"):
            ODTExporter(max_heap_mb=max_heap_mb)

    def test_unsupported_rts_fails_without_uncapped_retry(self, monkeypatch):
        from local_deep_research.exporters import odt_exporter as mod

        _patched_run(monkeypatch)
        monkeypatch.setattr(
            ODTExporter,
            "_pandoc_supports_sandbox",
            staticmethod(lambda: True),
        )
        calls = []

        def reject_rts(cmd, **kwargs):
            calls.append(cmd)
            raise subprocess.CalledProcessError(
                1, cmd, stderr=b"Most RTS options are disabled"
            )

        monkeypatch.setattr(mod.subprocess, "run", reject_rts)
        with pytest.raises(RuntimeError, match="Pandoc conversion failed"):
            _exporter().export("# T\n\nbody")
        assert len(calls) == 1
        assert "+RTS" in calls[0]

    def test_older_pandoc_refuses_export(self, monkeypatch):
        """An older pandoc must never receive untrusted report markdown."""
        recorded = _patched_run(monkeypatch)
        monkeypatch.setattr(
            ODTExporter,
            "_pandoc_supports_sandbox",
            staticmethod(lambda: False),
        )

        with pytest.raises(RuntimeError, match="requires pandoc 2.15"):
            _exporter().export("# T\n\nbody")
        assert "cmd" not in recorded

    def test_stderr_is_bounded_in_failures(self, monkeypatch, tmp_path):
        huge_stderr = b"E" * 100_000
        _patched_run(monkeypatch, raise_called=huge_stderr)

        with pytest.raises(RuntimeError) as excinfo:
            _exporter().export("# T\n\nbody")

        assert len(str(excinfo.value)) < 10_000, (
            "unbounded pandoc stderr propagates into error surfaces"
        )

    def test_stderr_is_truncated_to_its_tail_in_the_log(self, monkeypatch):
        """pandoc's stderr quotes report lines; the log call must carry
        only the truncated tail, never the head of a large stderr."""
        stderr = (
            b"HEAD-SENTINEL-REPORT-LINE " + b"E" * 50_000 + b" TAIL-SENTINEL"
        )
        _patched_run(monkeypatch, raise_called=stderr)
        log = _patched_logger(monkeypatch)

        with pytest.raises(RuntimeError) as excinfo:
            _exporter().export("# T\n\nbody")

        logged = log.rendered("exception") + log.rendered("error")
        assert logged, f"pandoc failure was not logged: {log.calls}"
        for line in logged:
            assert "HEAD-SENTINEL" not in line, "full stderr reached logs"
            assert len(line) < 10_000, "unbounded stderr reached logs"
        assert any("TAIL-SENTINEL" in line for line in logged), (
            "the diagnostic tail of stderr should still be logged"
        )
        assert "HEAD-SENTINEL" not in str(excinfo.value)


class TestSandboxVersionProbe:
    """The real ``_pandoc_supports_sandbox`` probe, driven through
    ``pypandoc.get_pandoc_version`` (not monkeypatched away)."""

    @pytest.mark.parametrize(
        ("version", "supported"),
        [
            ("3.9", True),
            ("2.15", True),
            ("2.14", False),
            ("2.13", False),
            ("2.12", False),
            # Lexically ">= 2.15" but numerically older: catches a
            # string comparison of version components.
            ("2.9.2.1", False),
        ],
    )
    def test_sandbox_flag_follows_pandoc_version(
        self, monkeypatch, version, supported
    ):
        from local_deep_research.exporters import odt_exporter as mod

        recorded = _patched_run(monkeypatch)
        monkeypatch.setattr(mod.pypandoc, "get_pandoc_version", lambda: version)

        if supported:
            result = _exporter().export("# T\n\nbody")
            assert "--sandbox" in recorded["cmd"]
            assert result.content == b"PK-odt-bytes"
        else:
            with pytest.raises(RuntimeError, match="requires pandoc 2.15"):
                _exporter().export("# T\n\nbody")
            assert "cmd" not in recorded

    def test_probe_failure_refuses_export_without_leaking_path(
        self, monkeypatch
    ):
        from local_deep_research.exporters import odt_exporter as mod

        secret = "/home/someone/private/path"

        def broken_probe():
            raise OSError(f"cannot run {secret}")

        recorded = _patched_run(monkeypatch)
        monkeypatch.setattr(mod.pypandoc, "get_pandoc_version", broken_probe)
        log = _patched_logger(monkeypatch)

        with pytest.raises(RuntimeError, match="requires pandoc 2.15"):
            _exporter().export("# T\n\nbody")
        assert "cmd" not in recorded
        warnings = log.rendered("warning")
        assert any("OSError" in w for w in warnings), (
            f"probe failure was not logged: {log.calls}"
        )
        assert not any(secret in w for w in warnings), (
            "warning must carry the exception type name only"
        )

    def test_probe_failure_is_not_cached(self, monkeypatch):
        """A transient probe failure must not disable the sandbox for
        the rest of the process."""
        from local_deep_research.exporters import odt_exporter as mod

        versions = iter([OSError("transient"), "3.1"])

        def flaky_probe():
            value = next(versions)
            if isinstance(value, Exception):
                raise value
            return value

        recorded = _patched_run(monkeypatch)
        monkeypatch.setattr(mod.pypandoc, "get_pandoc_version", flaky_probe)
        _patched_logger(monkeypatch)

        with pytest.raises(RuntimeError, match="requires pandoc 2.15"):
            _exporter().export("# T\n\nbody")
        assert "cmd" not in recorded

        _exporter().export("# T\n\nbody")
        assert "--sandbox" in recorded["cmd"]

    def test_real_pandoc_cannot_fetch_local_or_loopback_images(self, tmp_path):
        """Exercise the actual converter, not just the constructed argv."""
        from local_deep_research.exporters import odt_exporter as mod

        if not mod.PYPANDOC_AVAILABLE:
            pytest.skip("pypandoc is unavailable")
        if not ODTExporter._pandoc_supports_sandbox():
            pytest.skip("installed pandoc cannot run safely")

        marker = b"ODT-PRIVATE-FILE-MARKER"
        private = tmp_path / "private.gif"
        private.write_bytes(marker)
        hits = []

        class Handler(BaseHTTPRequestHandler):
            def do_GET(self):
                hits.append(self.path)
                self.send_response(200)
                self.send_header("Content-Type", "image/gif")
                self.end_headers()
                self.wfile.write(b"GIF89a")

            def log_message(self, *args):
                pass

        server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
        thread = threading.Thread(target=server.serve_forever, daemon=True)
        thread.start()
        try:
            report = (
                f"![local](file://{private})\n\n"
                f"![remote](http://127.0.0.1:{server.server_port}/image.gif)\n"
            )
            result = _exporter().export(report)
        finally:
            server.shutdown()
            server.server_close()
            thread.join(timeout=2)

        assert hits == []
        with zipfile.ZipFile(io.BytesIO(result.content)) as archive:
            assert marker not in b"".join(
                archive.read(name) for name in archive.namelist()
            )