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.

9.7 KB
Raw Blame
"""Bounded conversion of legacy office formats (.doc/.ppt).

``.doc``/``.ppt`` uploads are OLE binaries; the loader path converts
them to modern containers via a ``soffice`` subprocess before parsing.
The upstream conversion (unstructured's ``convert_office_doc``) invokes
``soffice`` with **no timeout**, so a hostile or pathological file can
pin an indexing worker forever — for content up to the 3 GB upload cap.

This module owns the conversion instead: only input carrying the OLE
compound-file signature is handed to soffice, a real wall-clock timeout
enforced with a process-group kill (``soffice`` spawns children that a
bare ``subprocess.run(timeout=...)`` would leave alive), typed
failures, output validation (expected file, size ceiling), soffice's
stdio sent to ``/dev/null`` rather than buffered, a per-call
``UserInstallation`` profile so concurrent conversions cannot collide
on soffice's singleton profile lock, and full temp cleanup in
``finally``. soffice's memory is not capped (it is bounded by the
timeout only); the OLE-signature check keeps renamed zip containers
and other formats from reaching it.
"""

from __future__ import annotations

import contextlib
import os
import shutil
import signal
import subprocess
import tempfile
import uuid
from pathlib import Path
from typing import Optional

from loguru import logger

#: Legacy extensions -> the modern container soffice produces.
_LEGACY_TARGETS = {".doc": ".docx", ".ppt": ".pptx"}

#: OLE compound-file signature: every genuine .doc/.ppt starts with it.
OLE_MAGIC = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1"

#: soffice binary candidates, resolved once per call via shutil.which.
_SOFFICE_CANDIDATES = ("soffice", "libreoffice")

#: Conversion wall clock. Generous for real documents (a 3 GB file is
#: rejected by the upload cap long before this; honest conversions of
#: large-but-legitimate documents finish in tens of seconds) while
#: keeping a hung process tree from pinning a worker forever.
DEFAULT_CONVERSION_TIMEOUT_S = 120

#: Converted-output ceiling. A conversion that legitimately needs more
#: than this was never going to be indexed usefully.
DEFAULT_MAX_OUTPUT_BYTES = 512 * 1024 * 1024


class OfficeConversionError(ValueError):
    """A legacy-office conversion failed, timed out, or was refused."""


def _normalized_ext(extension: str) -> str:
    return (
        extension.lower()
        if extension.startswith(".")
        else f".{extension.lower()}"
    )


def _kill_process_group(pid: int) -> None:
    """Best-effort SIGKILL of *pid*'s whole process group.

    On platforms without process groups (Windows) this falls back to
    terminating *pid* alone: soffice's helper processes survive there.
    """
    with contextlib.suppress(ProcessLookupError, PermissionError):
        if hasattr(os, "killpg"):
            os.killpg(pid, signal.SIGKILL)
        else:  # pragma: no cover - Windows has no process groups
            os.kill(pid, signal.SIGTERM)


def convert_legacy_office_bytes(
    content: bytes,
    extension: str,
    *,
    output_dir: Optional[Path] = None,
    timeout_s: int = DEFAULT_CONVERSION_TIMEOUT_S,
    max_output_bytes: int = DEFAULT_MAX_OUTPUT_BYTES,
    input_stem: Optional[str] = None,
) -> bytes:
    """Convert legacy OLE office bytes to their modern container.

    Returns the converted container's bytes. Raises
    ``OfficeConversionError`` (a ``ValueError``) on timeout, non-zero
    exit, missing/unexpected output, or an output over
    ``max_output_bytes``. All temporary files are removed whether the
    conversion succeeds or fails.
    """
    ext = _normalized_ext(extension)
    target = _LEGACY_TARGETS.get(ext)
    if target is None:
        raise OfficeConversionError(f"not a legacy office extension: {ext}")

    # soffice sniffs content, not the extension: anything else renamed
    # .doc/.ppt (a zip bomb, an RTF, an HTML page) would reach a filter
    # outside every other guard, with no memory cap on soffice. Only
    # the OLE compound files these extensions denote are converted.
    if not content.startswith(OLE_MAGIC):
        raise OfficeConversionError(f"{ext} upload is not an OLE compound file")

    soffice = next(
        (name for name in _SOFFICE_CANDIDATES if shutil.which(name)), None
    )
    if soffice is None:
        raise OfficeConversionError("soffice binary not available")

    # mkdtemp is absolute by construction; a caller-supplied output_dir
    # may be relative, which would break profile_dir.as_uri() below.
    work_dir = (
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: mkdtemp creates a private server-owned directory
        Path(tempfile.mkdtemp(prefix="ldr-office-"))
        if output_dir is None
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: upload caller omits this optional trusted-code path
        else Path(output_dir).resolve()
    )

    if input_stem is None:
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: validated local filename stem; no SQL sink
        stem = f"in_{uuid.uuid4().hex}"
    elif input_stem and all(c.isalnum() or c in "-_" for c in input_stem):
        stem = input_stem
    else:
        # A traversal-shaped stem must never escape the work dir.
        raise OfficeConversionError("invalid conversion input stem")

    in_path = work_dir / f"{stem}{ext}"
    out_path = work_dir / f"{stem}{target}"
    # Per-call profile: soffice serializes on a shared UserInstallation
    # and concurrent conversions would block or corrupt each other.
    profile_dir = work_dir / f"profile_{uuid.uuid4().hex}"

    try:
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Validated stem and allowlisted extension inside a private conversion directory; upload callers cannot choose output_dir
        in_path.write_bytes(content)
        # Defensive pre-clean: a stale file at out_path from an earlier
        # call in a shared output_dir must not fake a successful run.
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Fixed converted filename derived from the validated stem and allowlisted target extension in the conversion directory
        out_path.unlink(missing_ok=True)

        command = [
            soffice,
            "--headless",
            "--norestore",
            "--nolockcheck",
            f"-env:UserInstallation={profile_dir.as_uri()}",
            "--convert-to",
            target.lstrip("."),
            "--outdir",
            str(work_dir),
            str(in_path),
        ]
        try:
            proc = subprocess.Popen(  # noqa: S603 fixed argv, no shell
                command,
                # Nothing is read from soffice's streams: /dev/null
                # keeps a chatty or hostile conversion from growing
                # an in-memory pipe buffer in the worker.
                stdin=subprocess.DEVNULL,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
                start_new_session=True,
            )
            try:
                proc.communicate(timeout=timeout_s)
            except subprocess.TimeoutExpired:
                # start_new_session makes proc.pid the process-group
                # id: kill the whole tree, then reap, then fail typed.
                _kill_process_group(proc.pid)
                proc.wait()
                raise OfficeConversionError(
                    f"legacy office conversion timed out after {timeout_s}s"
                ) from None
        except OfficeConversionError:
            raise
        except OSError as exc:
            raise OfficeConversionError(
                f"could not launch soffice ({type(exc).__name__})"
            ) from exc

        if proc.returncode != 0:
            raise OfficeConversionError(
                f"legacy office conversion failed (exit {proc.returncode})"
            )

        if not out_path.exists():
            raise OfficeConversionError(
                "legacy office conversion produced no output"
            )

        output_size = out_path.stat().st_size
        if output_size > max_output_bytes:
            raise OfficeConversionError(
                f"converted output is larger than {max_output_bytes} bytes"
            )

        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Read the size-checked converter output with the fixed target extension from this call's conversion directory
        converted = out_path.read_bytes()
        logger.debug(
            "legacy office conversion ok: {} -> {} ({} bytes)",
            ext,
            target,
            output_size,
        )
        return converted
    finally:
        # Full temp hygiene on every path, success or failure: the
        # caller's content, soffice's output, and the profile tree.
        for path in (in_path, out_path):
            try:
                # nosemgrep: semgrep.rules.path-traversal-risk, reason: Cleanup is limited to this conversion's validated input and output filenames
                path.unlink(missing_ok=True)
            except OSError:
                logger.warning("could not remove conversion temp file")
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Profile directory uses a fresh UUID beneath this call's conversion directory
        shutil.rmtree(profile_dir, ignore_errors=True)
        if output_dir is None:
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: Only this call's mkdtemp directory is recursively removed; caller-owned output directories are retained
            shutil.rmtree(work_dir, ignore_errors=True)


def is_legacy_office_extension(extension: str) -> bool:
    """True when *extension* is one of the OLE formats needing conversion."""
    return _normalized_ext(extension) in _LEGACY_TARGETS


def converted_extension(extension: str) -> str:
    """The modern container extension a legacy *extension* converts to."""
    return _LEGACY_TARGETS[_normalized_ext(extension)]