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.

13.8 KB
Raw Blame
"""Bounded pandoc conversion for pandoc-backed uploads.

Five upload formats convert through a ``pandoc`` subprocess before
parsing: ``.rtf``/``.epub``/``.rst``/``.org`` through unstructured's
``convert_file_to_html_text_using_pandoc`` and ``.odt`` through
``partition_odt`` (odt -> docx), both ending in
``pypandoc.convert_file`` -> ``Popen(...)`` + ``communicate()`` with
**no timeout and no memory cap** — a hostile or pathological file pins
an indexing worker indefinitely or grows pandoc's heap without limit.

This module owns the conversion instead:

- the binary is resolved the way pypandoc resolves it (the bundled
  ``pypandoc-binary`` copy first), falling back to ``pandoc`` on PATH;
- a wall-clock timeout enforced with a process-group kill (mirroring
  the soffice wrapper in ``utilities/office_conversion.py``);
- a memory cap on the pandoc child: a GHC RTS heap ceiling
  (``+RTS -M<N>m -RTS``) when the binary accepts RTS options, otherwise
  ``RLIMIT_AS`` in the child on Linux;
- a ``--sandbox``ed argv with pandoc writing to an output file, whose
  size is checked before it is read; pandoc's stdout/stderr go to
  ``/dev/null`` so no diagnostic stream is buffered in the worker;
- typed failures and full temp cleanup in ``finally``.

Every extension converts straight to HTML, which the ``.html`` loader
then parses.
"""

from __future__ import annotations

import contextlib
import functools
import os
import shutil
import signal
import subprocess
import sys
import tempfile
import time
from pathlib import Path
from typing import Any, Callable, NamedTuple, Optional

from loguru import logger

try:  # POSIX only; imported here so the forked child imports nothing.
    import resource
except ImportError:  # pragma: no cover - Windows
    resource = None  # type: ignore[assignment]

#: Every upload extension whose registered loader converts through
#: pandoc; all of them are routed through this bounded wrapper instead.
#: (.docx/.pptx/.xlsx are parsed in-process by python-docx/-pptx/
#: openpyxl and never reach pandoc.)
PANDOC_BACKED_EXTENSIONS = frozenset({".rtf", ".epub", ".rst", ".org", ".odt"})

#: Conversion wall clock: real conversions of ordinary documents are
#: sub-second to a few seconds; generous headroom keeps
#: pathological-but-honest inputs from failing while a hung pandoc tree
#: cannot outlive the worker's patience.
DEFAULT_CONVERSION_TIMEOUT_S = 90

#: Converted-HTML ceiling: extraction only needs text, and a larger
#: "HTML" output signals amplification, not a real document.
DEFAULT_MAX_OUTPUT_BYTES = 64 * 1024 * 1024

#: pandoc heap ceiling in MiB. Measured with the bundled pandoc 3.9,
#: peak RSS is roughly 100-120x the document's text size (1 MB of rst
#: -> ~200 MB, 5 MB -> ~570 MB), and 20 MB of text needs ~2 GB *and*
#: more than the 90 s timeout. 2 GiB therefore admits what the timeout
#: admits, while a decompression bomb (pandoc's zip reader does not
#: trust declared entry sizes) becomes a prompt, typed failure instead
#: of an OOM of the host.
DEFAULT_MAX_HEAP_MB = 2048

#: Extra address space granted over the heap ceiling when the cap has
#: to be enforced with RLIMIT_AS (code, RTS bookkeeping, mmaps).
_RLIMIT_AS_HEADROOM_MB = 512

#: GHC runtimes exit with this status on "Heap exhausted" (and on
#: "out of memory" when RLIMIT_AS refuses an allocation).
_GHC_HEAP_EXHAUSTED_EXIT = 251

#: How long a *failed* RTS probe is remembered before it is retried.
#: Successes are remembered for the life of the process; failures
#: (including a timed-out probe on a loaded host) are retried, but at
#: most once per window so a broken binary does not add a probe to
#: every conversion.
_RTS_PROBE_FAILURE_TTL_S = 300.0

#: Timeout of one RTS probe (``pandoc +RTS -M64m -RTS --version``).
_RTS_PROBE_TIMEOUT_S = 30

#: binary path -> (probe succeeded, time.monotonic() of the probe)
_rts_probe_cache: dict[str, tuple[bool, float]] = {}


class PandocConversionError(ValueError):
    """A pandoc conversion failed, timed out, or was refused."""


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: grandchildren pandoc spawned 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 _normalized_ext(extension: str) -> str:
    return (
        extension.lower()
        if extension.startswith(".")
        else f".{extension.lower()}"
    )


def is_pandoc_backed_extension(extension: str) -> bool:
    """True when *extension*'s loader path converts through pandoc."""
    return _normalized_ext(extension) in PANDOC_BACKED_EXTENSIONS


def resolve_pandoc_binary() -> Optional[str]:
    """Locate the pandoc binary the way the rest of the app does.

    The shipped dependency is ``pypandoc-binary``, which bundles pandoc
    inside the package (``pypandoc/files/pandoc``) and puts nothing on
    PATH, so ``pypandoc.get_pandoc_path()`` is the primary resolver
    (it also honours ``PYPANDOC_PANDOC`` and picks the newest of the
    bundled and PATH copies). ``shutil.which`` is the fallback for
    installs without pypandoc.
    """
    try:
        import pypandoc

        path = pypandoc.get_pandoc_path()
        if path:
            return str(path)
    except (ImportError, OSError, RuntimeError):
        pass
    return shutil.which("pandoc")


def _probe_rts_heap_cap(pandoc: str) -> bool:
    """Run one ``+RTS -M`` probe against *pandoc* (uncached)."""
    try:
        probe = subprocess.run(  # noqa: S603 fixed argv, no shell
            [pandoc, "+RTS", "-M64m", "-RTS", "--version"],
            stdin=subprocess.DEVNULL,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            timeout=_RTS_PROBE_TIMEOUT_S,
            check=False,
        )
    except (OSError, subprocess.SubprocessError):
        return False
    return probe.returncode == 0


def _pandoc_accepts_rts_heap_cap(pandoc: str) -> bool:
    """Whether *pandoc* honours ``+RTS -M``.

    Official pandoc builds (including the one pypandoc-binary bundles)
    link with ``-rtsopts``; a build without it exits non-zero with
    "Most RTS options are disabled". A success is cached for the
    process; a failure only for ``_RTS_PROBE_FAILURE_TTL_S``, so a
    transient failure (e.g. a probe timing out under load) does not
    pin the process to the fallback cap for good.
    """
    now = time.monotonic()
    cached = _rts_probe_cache.get(pandoc)
    if cached is not None:
        ok, probed_at = cached
        if ok or now - probed_at < _RTS_PROBE_FAILURE_TTL_S:
            return ok
    ok = _probe_rts_heap_cap(pandoc)
    _rts_probe_cache[pandoc] = (ok, now)
    return ok


def _set_address_space_limit(limit_bytes: int) -> None:
    """Runs in the forked child before exec: ``setrlimit`` and nothing
    else (``resource`` is imported by the parent at module load)."""
    resource.setrlimit(resource.RLIMIT_AS, (limit_bytes, limit_bytes))


class _MemoryCap(NamedTuple):
    rts_args: list[str]
    preexec_fn: Optional[Callable[[], None]]
    #: Human-readable effective limit, used in the failure message.
    description: str


def _memory_cap(pandoc: str, max_heap_mb: int) -> _MemoryCap:
    """Return how pandoc's memory is capped for this binary.

    Preferred: a GHC RTS heap ceiling on the argv — no Python code runs
    in the forked child, so it is safe in this multi-threaded server.
    Fallback for binaries without ``-rtsopts``: ``RLIMIT_AS`` of the
    heap ceiling plus ``_RLIMIT_AS_HEADROOM_MB`` set in a
    ``preexec_fn`` that does nothing else; Linux only, since macOS does
    not enforce RLIMIT_AS and Windows has neither mechanism.

    A probe that wrongly reports RTS support (the binary then rejects
    ``+RTS``) costs availability only: the conversion fails typed and
    pandoc never runs uncapped.
    """
    if _pandoc_accepts_rts_heap_cap(pandoc):
        return _MemoryCap(
            ["+RTS", f"-M{max_heap_mb}m", "-RTS"],
            None,
            f"{max_heap_mb} MiB heap cap",
        )
    if resource is not None and sys.platform.startswith("linux"):
        limit_mb = max_heap_mb + _RLIMIT_AS_HEADROOM_MB
        return _MemoryCap(
            [],
            functools.partial(_set_address_space_limit, limit_mb * 1024 * 1024),
            f"{limit_mb} MiB address-space limit "
            f"({max_heap_mb} MiB heap + {_RLIMIT_AS_HEADROOM_MB} MiB "
            "headroom)",
        )
    logger.warning(
        "pandoc rejects RTS options and RLIMIT_AS is unavailable on this "
        "platform; pandoc conversions are bounded by the timeout only"
    )
    return _MemoryCap([], None, "no memory cap")


def pandoc_convert_bytes_to_html(
    content: bytes,
    extension: str,
    *,
    work_dir: Optional[Path] = None,
    timeout_s: int = DEFAULT_CONVERSION_TIMEOUT_S,
    max_output_bytes: int = DEFAULT_MAX_OUTPUT_BYTES,
    max_heap_mb: int = DEFAULT_MAX_HEAP_MB,
) -> bytes:
    """Convert a pandoc-backed document to HTML bytes, bounded.

    Returns the converted HTML. Raises ``PandocConversionError`` (a
    ``ValueError``) on timeout, launch failure, memory-cap breach,
    non-zero exit, missing output, or output over ``max_output_bytes``.
    All temporary files are removed whether the conversion succeeds or
    fails.
    """
    ext = _normalized_ext(extension)
    if ext not in PANDOC_BACKED_EXTENSIONS:
        raise PandocConversionError(f"not a pandoc-backed extension: {ext}")

    pandoc = resolve_pandoc_binary()
    if pandoc is None:
        raise PandocConversionError("pandoc binary not available")

    cap = _memory_cap(pandoc, max_heap_mb)

    owned_dir = work_dir is None
    scratch = (
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: mkdtemp creates a private server-owned directory
        Path(tempfile.mkdtemp(prefix="ldr-pandoc-"))
        if owned_dir
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: upload caller omits this optional trusted-code path
        else Path(work_dir).resolve()
    )
    in_path = scratch / f"in_doc{ext}"
    out_path = scratch / "out.html"

    try:
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Fixed filename and allowlisted extension inside a private conversion directory; upload callers cannot choose work_dir
        in_path.write_bytes(content)
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Fixed out.html inside this call's conversion directory
        out_path.unlink(missing_ok=True)

        # --sandbox restricts pandoc's own file access (same posture
        # as pypandoc's sandbox=True); output goes to a file so the
        # size ceiling can be checked before anything is read, and
        # --quiet plus /dev/null keeps warning floods out of memory.
        command = [
            pandoc,
            *cap.rts_args,
            "--sandbox",
            "--quiet",
            "--from",
            ext.lstrip("."),
            "--to",
            "html",
            "-o",
            str(out_path),
            str(in_path),
        ]
        popen_kwargs: dict[str, Any] = {}
        if cap.preexec_fn is not None:
            popen_kwargs["preexec_fn"] = cap.preexec_fn
        try:
            proc = subprocess.Popen(  # noqa: S603 fixed argv, no shell
                command,
                stdin=subprocess.DEVNULL,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
                start_new_session=True,
                **popen_kwargs,
            )
            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 PandocConversionError(
                    f"pandoc conversion timed out after {timeout_s}s"
                ) from None
        except PandocConversionError:
            raise
        except (OSError, subprocess.SubprocessError) as exc:
            raise PandocConversionError(
                f"could not launch pandoc ({type(exc).__name__})"
            ) from exc

        if proc.returncode == _GHC_HEAP_EXHAUSTED_EXIT:
            raise PandocConversionError(
                f"pandoc conversion ran out of memory ({cap.description})"
            )
        if proc.returncode != 0:
            raise PandocConversionError(
                f"pandoc conversion failed (exit {proc.returncode})"
            )

        if not out_path.exists():
            raise PandocConversionError("pandoc conversion produced no output")

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

        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Read the size-checked fixed out.html from this call's conversion directory
        converted = out_path.read_bytes()
        logger.debug(
            "pandoc conversion ok: {} -> html ({} bytes)",
            ext,
            output_size,
        )
        return converted
    finally:
        for path in (in_path, out_path):
            try:
                # nosemgrep: semgrep.rules.path-traversal-risk, reason: Cleanup is limited to this conversion's fixed input and output filenames
                path.unlink(missing_ok=True)
            except OSError:
                logger.warning("could not remove conversion temp file")
        if owned_dir:
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: Only this call's mkdtemp directory is recursively removed; caller-owned work directories are retained
            shutil.rmtree(scratch, ignore_errors=True)