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.

22.2 KB
Raw Blame
"""Bounds for the legacy-office conversion path (.doc/.ppt uploads).

``.doc``/``.ppt`` uploads are OLE binaries that get converted via a
``soffice`` subprocess before parsing. The conversion runs on an
indexing worker for content up to the 3 GB upload cap; an unbounded
subprocess (unstructured's ``convert_office_doc`` calls
``subprocess.run(command, capture_output=True)`` with **no timeout**)
lets a hostile or pathological file pin the worker forever — and a
bare per-child kill would leave soffice's grandchildren alive.

These contracts pin the bounded seam: a wall-clock timeout that
actually reaches the subprocess layer, a process-group kill on hang,
typed failure, output-size validation, per-call isolation of
soffice's singleton profile, temp hygiene, and correct ordering with
the zip-container guard on the *converted* bytes.
"""

from __future__ import annotations

import io
import os
import shutil
import signal
import subprocess
import sys
import time
import zipfile
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import patch

import pytest


def _conv():
    from local_deep_research.utilities import office_conversion

    return office_conversion


#: A payload carrying the OLE compound-file signature, which the wrapper
#: requires before it launches soffice.
_OLE = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" + b"\x00" * 504


@contextmanager
def _soffice_available(conv):
    """Resolve the binary even on soffice-less boxes."""
    with patch.object(conv.shutil, "which", return_value="soffice"):
        yield


class _FakeProc:
    """Minimal Popen double: records the timeout, optional side effect."""

    pid = 4242
    returncode = 0

    def __init__(self, on_communicate=None, command=None, kwargs=None):
        self._on_communicate = on_communicate
        self.communicate_timeout = None
        self.waited = False
        self.command = command
        self.kwargs = kwargs

    def communicate(self, timeout=None):
        self.communicate_timeout = timeout
        if self._on_communicate is not None:
            self._on_communicate()
        return b"", b""

    def wait(self):
        self.waited = True
        return 0


def _recording_popen(on_communicate=None, write_output=None):
    """Popen double recording argv + kwargs; ``write_output`` gets the
    path soffice would write (``<stem>.docx`` beside the input)."""
    launched: list[_FakeProc] = []

    def fake_popen(command, **kwargs):
        in_path = Path([a for a in command if str(a).endswith(".doc")][0])

        def communicate():
            if write_output is not None:
                write_output(in_path.with_suffix(".docx"))
            if on_communicate is not None:
                on_communicate()

        proc = _FakeProc(
            on_communicate=communicate, command=command, kwargs=kwargs
        )
        launched.append(proc)
        return proc

    return fake_popen, launched


class TestConversionBounds:
    def test_the_timeout_reaches_the_subprocess(self, tmp_path):
        """No timeout -> a hang pins the worker forever."""
        conv = _conv()
        proc = _FakeProc()

        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", return_value=proc),
        ):
            try:
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )
            except conv.OfficeConversionError:
                pass  # the fake writes no output; only timing matters

        assert (
            proc.communicate_timeout is not None
            and proc.communicate_timeout >= 30
        ), "soffice runs without a real wall-clock timeout"

    def test_hanging_conversion_is_killed_by_group_and_fails_typed(
        self, tmp_path
    ):
        """TimeoutExpired triggers a process-group kill and a typed
        rejection — not a bare child kill that leaks grandchildren."""
        conv = _conv()

        def hang(timeout=None):
            raise subprocess.TimeoutExpired(cmd="soffice", timeout=timeout)

        proc = _FakeProc(on_communicate=hang)
        kills: list = []

        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", return_value=proc),
            patch.object(conv, "_kill_process_group", side_effect=kills.append),
        ):
            with pytest.raises(conv.OfficeConversionError) as excinfo:
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )

        assert "timed out" in str(excinfo.value).lower()
        assert kills == [proc.pid], (
            f"hung conversion not killed by process group: {kills}"
        )
        assert proc.waited, "killed process was never reaped"

    def test_runaway_output_is_refused(self, tmp_path):
        """A conversion whose output exceeds the ceiling is refused."""
        conv = _conv()

        def write_big():
            (tmp_path / "upload.docx").write_bytes(b"\x00" * 1000)

        with (
            _soffice_available(conv),
            patch.object(
                conv.subprocess,
                "Popen",
                return_value=_FakeProc(on_communicate=write_big),
            ),
        ):
            with pytest.raises(conv.OfficeConversionError) as excinfo:
                conv.convert_legacy_office_bytes(
                    _OLE,
                    ".doc",
                    output_dir=tmp_path,
                    input_stem="upload",
                    max_output_bytes=100,
                )

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

    def test_each_call_gets_its_own_soffice_profile(self, tmp_path):
        """soffice is a singleton per profile; concurrent conversions
        must not serialize or corrupt each other via a shared one."""
        conv = _conv()
        commands: list = []

        def fake_popen(command, **kwargs):
            commands.append(command)
            return _FakeProc()

        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            for payload in (_OLE + b"a", _OLE + b"b"):
                try:
                    conv.convert_legacy_office_bytes(
                        payload, ".doc", output_dir=tmp_path
                    )
                except conv.OfficeConversionError:
                    pass  # the fake writes no output; only calls matter

        profiles = [
            " ".join(a for a in command if "UserInstallation" in str(a))
            for command in commands
        ]
        assert len(commands) == 2, commands
        assert all(profiles), f"no -env:UserInstallation passed: {commands}"
        assert profiles[0] != profiles[1], (
            "conversions share one soffice profile (singleton lock)"
        )

    def test_temp_input_is_cleaned_up(self, tmp_path):
        """The OLE bytes written to disk for soffice must not linger."""
        conv = _conv()
        seen: list = []

        def check_input():
            seen.append(list(tmp_path.rglob("*.doc")))
            assert seen[-1], "input file missing while soffice runs"

        with (
            _soffice_available(conv),
            patch.object(
                conv.subprocess,
                "Popen",
                return_value=_FakeProc(on_communicate=check_input),
            ),
        ):
            with pytest.raises(conv.OfficeConversionError):
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )

        leftovers = list(tmp_path.rglob("*"))
        assert not leftovers, f"conversion left files behind: {leftovers}"

    def test_traversal_shaped_input_stem_is_refused(self, tmp_path):
        """An input_stem carrying separators must never escape the
        work dir."""
        conv = _conv()

        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", return_value=_FakeProc()),
        ):
            with pytest.raises(conv.OfficeConversionError):
                conv.convert_legacy_office_bytes(
                    _OLE,
                    ".doc",
                    output_dir=tmp_path,
                    input_stem="../../evil",
                )


class TestWiring:
    def test_doc_upload_routes_through_conversion_then_zip_guard(
        self, tmp_path
    ):
        """.doc bytes are converted, and the CONVERTED container is
        zip-guarded — a bomb in the conversion output cannot pass."""
        from local_deep_research.document_loaders.bytes_loader import (
            load_from_bytes,
        )
        from local_deep_research.document_loaders.zip_container_guard import (
            DecompressionBombError,
        )

        conv = _conv()
        bomb = io.BytesIO()
        with zipfile.ZipFile(bomb, "w") as zf:
            for i in range(10_001):
                zf.writestr(f"p{i}.xml", "x")

        def fake_popen(command, **kwargs):
            # Emulate soffice: <stem>.doc -> <stem>.docx beside it.
            in_path = Path([a for a in command if str(a).endswith(".doc")][0])
            in_path.with_suffix(".docx").write_bytes(bomb.getvalue())
            return _FakeProc()

        import local_deep_research.document_loaders.bytes_loader as bl

        with (
            _soffice_available(conv),
            patch.object(bl, "is_extension_supported", return_value=True),
            patch.object(
                bl, "get_loader_class_for_extension", return_value=None
            ),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(DecompressionBombError):
                load_from_bytes(_OLE, ".doc", "hostile.doc")

    @pytest.mark.skipif(
        not (shutil.which("soffice") or shutil.which("libreoffice")),
        reason="soffice not installed",
    )
    def test_live_round_trip(self, tmp_path):
        """Control: garbage OLE fails cleanly (typed error), never hangs."""
        conv = _conv()

        with pytest.raises((conv.OfficeConversionError, ValueError)):
            conv.convert_legacy_office_bytes(
                _OLE + b"not really OLE", ".doc", output_dir=tmp_path
            )


class TestProcessIsolation:
    """A hang must be killable as a tree, and nothing may be buffered."""

    def _launch(self, conv, tmp_path, **popen_opts):
        fake_popen, launched = _recording_popen(**popen_opts)
        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            try:
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )
            except conv.OfficeConversionError:
                pass
        assert launched, "soffice never launched"
        return launched[0]

    def test_soffice_runs_in_its_own_session(self, tmp_path):
        """Without a new session, proc.pid is not a process-group id:
        the group kill misses and ``proc.wait()`` blocks forever."""
        conv = _conv()
        kwargs = self._launch(conv, tmp_path).kwargs

        assert (
            kwargs.get("start_new_session") is True
            or kwargs.get("process_group") == 0
        ), f"soffice not started in its own process group: {kwargs}"

    def test_stdio_goes_to_devnull_not_a_pipe(self, tmp_path):
        conv = _conv()
        kwargs = self._launch(conv, tmp_path).kwargs

        for stream in ("stdout", "stderr"):
            assert kwargs.get(stream) == subprocess.DEVNULL, (
                f"soffice {stream} is buffered: {kwargs.get(stream)!r}"
            )

    @pytest.mark.skipif(
        not hasattr(os, "killpg"), reason="no process groups here"
    )
    def test_timeout_kill_targets_the_process_group(self, tmp_path):
        """soffice forks helpers; a bare child kill leaves them alive."""
        conv = _conv()

        def hang():
            raise subprocess.TimeoutExpired(cmd="soffice", timeout=1)

        fake_popen, launched = _recording_popen(on_communicate=hang)
        group_kills: list = []
        pid_kills: list = []

        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
            patch.object(
                conv.os, "killpg", side_effect=lambda *a: group_kills.append(a)
            ),
            patch.object(
                conv.os, "kill", side_effect=lambda *a: pid_kills.append(a)
            ),
        ):
            with pytest.raises(conv.OfficeConversionError, match="timed out"):
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )

        assert group_kills == [(launched[0].pid, signal.SIGKILL)], (
            f"hung soffice not killed as a group: killpg={group_kills} "
            f"kill={pid_kills}"
        )
        assert launched[0].waited, "killed process was never reaped"


class TestDefaultsAndHygiene:
    def test_default_timeout_is_bounded_and_reaches_the_subprocess(
        self, tmp_path
    ):
        conv = _conv()
        fake_popen, launched = _recording_popen()

        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.OfficeConversionError):
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )

        assert launched[0].communicate_timeout == (
            conv.DEFAULT_CONVERSION_TIMEOUT_S
        )
        assert 30 <= conv.DEFAULT_CONVERSION_TIMEOUT_S <= 300

    def test_default_output_ceiling_refuses_one_byte_over(self, tmp_path):
        """The default ceiling is 512 MiB and applies without kwargs; a
        sparse file keeps the check cheap."""
        conv = _conv()
        assert conv.DEFAULT_MAX_OUTPUT_BYTES == 512 * 1024 * 1024

        def sparse(out):
            with out.open("wb") as fh:
                fh.truncate(conv.DEFAULT_MAX_OUTPUT_BYTES + 1)

        fake_popen, _ = _recording_popen(write_output=sparse)
        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.OfficeConversionError, match="larger"):
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )

    def test_oversized_output_is_never_read(self, tmp_path):
        """The size check must come BEFORE the read."""
        conv = _conv()
        reads: list = []
        real_read_bytes = Path.read_bytes

        def spy(self):
            reads.append(self.suffix)
            return real_read_bytes(self)

        fake_popen, _ = _recording_popen(
            write_output=lambda out: out.write_bytes(b"x" * 5000)
        )
        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
            patch.object(Path, "read_bytes", spy),
        ):
            with pytest.raises(conv.OfficeConversionError, match="larger"):
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path, max_output_bytes=100
                )

        assert ".docx" not in reads, "oversized output was read"

    @pytest.mark.parametrize("succeed", [True, False])
    def test_owned_work_dir_is_removed(self, tmp_path, succeed):
        """With no output_dir the wrapper owns a mkdtemp dir; it must be
        gone afterwards on success and on failure."""
        conv = _conv()
        created: list[Path] = []
        real_mkdtemp = conv.tempfile.mkdtemp

        def recording_mkdtemp(*args, **kwargs):
            path = real_mkdtemp(*args, dir=tmp_path, **kwargs)
            created.append(Path(path))
            return path

        fake_popen, _ = _recording_popen(
            write_output=(
                (lambda out: out.write_bytes(b"PK")) if succeed else None
            )
        )
        with (
            _soffice_available(conv),
            patch.object(conv.tempfile, "mkdtemp", recording_mkdtemp),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            if succeed:
                assert conv.convert_legacy_office_bytes(_OLE, ".doc")
            else:
                with pytest.raises(conv.OfficeConversionError):
                    conv.convert_legacy_office_bytes(_OLE, ".doc")

        assert len(created) == 1, created
        assert not created[0].exists(), "owned work dir left behind"


_FAKE_BINARY = """#!/bin/sh
sleep 20 &
echo $! > "{pidfile}"
wait
"""


def _pid_alive(pid: int) -> bool:
    try:
        with open(f"/proc/{pid}/stat", encoding="utf-8") as fh:
            state = fh.read().rsplit(")", 1)[1].split()[0]
    except (FileNotFoundError, ProcessLookupError, IndexError):
        return False
    return state not in ("Z", "X")


def _require_exec(directory: Path) -> None:
    """Skip when *directory* is mounted noexec."""
    probe = directory / "exec-probe"
    probe.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8")
    probe.chmod(0o755)
    try:
        subprocess.run([str(probe)], check=True, timeout=10)
    except (OSError, subprocess.SubprocessError):
        pytest.skip("tmp_path does not allow executing scripts")


def _wait_for_pid(pidfile: Path, timeout_s: float = 5.0) -> int:
    deadline = time.monotonic() + timeout_s
    while time.monotonic() < deadline:
        try:
            text = pidfile.read_text(encoding="utf-8").strip()
        except FileNotFoundError:
            text = ""
        if text:
            return int(text)
        time.sleep(0.05)
    pytest.skip("child never reached its spawn point (loaded host)")


@pytest.mark.skipif(
    not sys.platform.startswith("linux"), reason="uses /proc and /bin/sh"
)
class TestRealProcessTree:
    """A real child that spawns a sleeping grandchild: the timeout must
    return promptly AND take the grandchild down with it."""

    def test_timeout_kills_the_whole_tree(self, tmp_path):
        conv = _conv()
        pidfile = tmp_path / "grandchild.pid"
        bin_dir = tmp_path / "bin"
        bin_dir.mkdir()
        _require_exec(bin_dir)
        # The wrapper launches soffice by name, so the fake goes on PATH.
        binary = bin_dir / "soffice"
        binary.write_text(
            _FAKE_BINARY.format(pidfile=pidfile), encoding="utf-8"
        )
        binary.chmod(0o755)
        work = tmp_path / "work"
        work.mkdir()
        path = f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}"

        grandchild = None
        try:
            with patch.dict(os.environ, {"PATH": path}):
                started = time.monotonic()
                with pytest.raises(conv.OfficeConversionError, match="timed"):
                    conv.convert_legacy_office_bytes(
                        _OLE, ".doc", output_dir=work, timeout_s=2
                    )
                elapsed = time.monotonic() - started

            assert elapsed < 12, f"timeout took {elapsed:.1f}s to return"
            grandchild = _wait_for_pid(pidfile)
            deadline = time.monotonic() + 5
            while _pid_alive(grandchild) and time.monotonic() < deadline:
                time.sleep(0.05)
            assert not _pid_alive(grandchild), "grandchild survived the kill"
        finally:
            if grandchild is None and pidfile.exists():
                text = pidfile.read_text(encoding="utf-8").strip()
                grandchild = int(text) if text else None
            if grandchild is not None:
                try:
                    os.kill(grandchild, signal.SIGKILL)
                except (ProcessLookupError, PermissionError):
                    pass


class TestOnlyOleInputReachesSoffice:
    """soffice sniffs content, not the extension: a zip container (or an
    RTF, or HTML) renamed .doc/.ppt would otherwise reach it unguarded."""

    @pytest.mark.parametrize("ext", [".doc", ".ppt"])
    @pytest.mark.parametrize(
        "payload",
        [
            pytest.param(b"PK\x03\x04" + b"\x00" * 64, id="zip"),
            pytest.param(b"{\\rtf1 hello}", id="rtf"),
            pytest.param(b"<html>hello</html>", id="html"),
            pytest.param(b"", id="empty"),
        ],
    )
    def test_non_ole_input_never_launches_soffice(self, tmp_path, ext, payload):
        conv = _conv()
        launched: list = []

        with (
            _soffice_available(conv),
            patch.object(
                conv.subprocess,
                "Popen",
                side_effect=lambda *a, **k: launched.append(a) or _FakeProc(),
            ),
        ):
            with pytest.raises(conv.OfficeConversionError, match="OLE"):
                conv.convert_legacy_office_bytes(
                    payload, ext, output_dir=tmp_path
                )

        assert launched == [], "non-OLE input was handed to soffice"

    def test_ole_input_still_launches_soffice(self, tmp_path):
        conv = _conv()
        fake_popen, launched = _recording_popen()
        with (
            _soffice_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.OfficeConversionError, match="no output"):
                conv.convert_legacy_office_bytes(
                    _OLE, ".doc", output_dir=tmp_path
                )

        assert len(launched) == 1

    def test_upload_path_refuses_a_zip_renamed_doc(self):
        from local_deep_research.document_loaders.bytes_loader import (
            extract_text_from_bytes,
            load_from_bytes,
        )

        import local_deep_research.document_loaders.bytes_loader as bl

        conv = _conv()
        bomb = io.BytesIO()
        with zipfile.ZipFile(bomb, "w", zipfile.ZIP_DEFLATED) as zf:
            zf.writestr("word/document.xml", b"\x00" * 100_000)

        with (
            _soffice_available(conv),
            patch.object(bl, "is_extension_supported", return_value=True),
            patch.object(
                conv.subprocess,
                "Popen",
                side_effect=AssertionError("soffice launched"),
            ),
        ):
            with pytest.raises(conv.OfficeConversionError, match="OLE"):
                load_from_bytes(bomb.getvalue(), ".doc", "renamed.doc")
            assert (
                extract_text_from_bytes(bomb.getvalue(), ".doc", "renamed.doc")
                is None
            )