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.

5.5 KB
Raw Blame
"""Email uploads index the message body only, never its attachments.

unstructured's ``partition_email`` (and ``partition_msg``, which
``UnstructuredEmailLoader`` dispatches to when the content sniffs as an
Outlook .msg) default to ``process_attachments=True`` and hand every
attachment to ``unstructured.partition.auto.partition``. That recursion runs
outside ``load_from_bytes`` and therefore outside every bound this module
family adds: a .doc/.ppt attachment reaches soffice with no timeout, an
.rtf/.rst/.org/.odt/.epub attachment reaches ``pypandoc.convert_file`` with
no timeout or heap cap, and a .docx/.pptx/.xlsx attachment reaches its parser
without the zip-container guard. The registry therefore passes
``process_attachments=False`` for ``.eml``.
"""

from __future__ import annotations

import io
import json
import struct
from email.message import EmailMessage
from unittest.mock import patch

import pytest

BODY_MARKER = "BODYMARKER quarterly numbers attached"


def _minimal_ole_doc() -> bytes:
    """A minimal well-formed CFB file holding a ``WordDocument`` stream.

    unstructured's file-type sniffer opens the compound file and classifies
    it as a legacy .doc by that stream name, routing it to ``partition_doc``
    and from there to ``convert_office_doc`` (soffice).
    """
    end, free, fatsect = 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFD
    header = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" + b"\x00" * 16
    header += struct.pack("<HHHHH", 0x3E, 3, 0xFFFE, 9, 6) + b"\x00" * 6
    header += struct.pack("<IIIIIIIII", 0, 1, 1, 0, 4096, end, 0, end, 0)
    header += struct.pack("<I", 0) + struct.pack("<I", free) * 108
    # FAT: sector 0 is the FAT, 1 the directory, 2-9 the 4096-byte stream.
    fat = [fatsect, end, *range(3, 10), end]
    fat += [free] * (128 - len(fat))

    def entry(name: str, kind: int, child: int, start: int, size: int):
        raw = (name + "\x00").encode("utf-16-le")
        return (
            raw.ljust(64, b"\x00")
            + struct.pack("<HBB", len(raw), kind, 1)
            + struct.pack("<III", free, free, child)
            + b"\x00" * 36
            + struct.pack("<II", start, size)
            + b"\x00" * 4
        )

    unused = b"\x00" * 64 + struct.pack("<HBBIII", 0, 0, 0, free, free, free)
    unused += b"\x00" * 48
    directory = (
        entry("Root Entry", 5, 1, end, 0)
        + entry("WordDocument", 2, free, 2, 4096)
        + unused * 2
    )
    return header + struct.pack("<128I", *fat) + directory + b"\x00" * 4096


def _docx_bytes() -> bytes:
    docx = pytest.importorskip("docx")
    document = docx.Document()
    document.add_paragraph("DOCXATTACHMENT")
    buf = io.BytesIO()
    document.save(buf)
    return buf.getvalue()


def _eml(attachment: bytes, subtype: str, filename: str) -> bytes:
    msg = EmailMessage()
    msg["From"] = "[email protected]"
    msg["To"] = "[email protected]"
    msg["Subject"] = "Report"
    msg.set_content(BODY_MARKER)
    msg.add_attachment(
        attachment, maintype="application", subtype=subtype, filename=filename
    )
    return msg.as_bytes()


class _Called(AssertionError):
    pass


def test_registry_disables_attachment_processing():
    from local_deep_research.document_loaders.loader_registry import (
        LOADER_REGISTRY,
    )

    assert (
        LOADER_REGISTRY[".eml"]["loader_kwargs"].get("process_attachments")
        is False
    )


@pytest.mark.parametrize("kind", ["rtf", "doc", "docx"])
def test_attachment_converters_and_parsers_never_run(kind, tmp_path):
    """Each attachment type reaches its unbounded converter on revert."""
    pytest.importorskip("unstructured.partition.email")
    pypandoc = pytest.importorskip("pypandoc")
    docx = pytest.importorskip("docx")
    udoc = pytest.importorskip("unstructured.partition.doc")
    utext = pytest.importorskip("unstructured.partition.text")

    from local_deep_research.security import isolated_loader, isolated_worker

    attachments = {
        "rtf": (b"{\\rtf1\\ansi RTFATTACHMENT}", "rtf", "notes.rtf"),
        "doc": (_minimal_ole_doc(), "msword", "legacy.doc"),
        "docx": (
            _docx_bytes(),
            "vnd.openxmlformats-officedocument.wordprocessingml.document",
            "modern.docx",
        ),
    }
    payload = _eml(*attachments[kind])
    source = tmp_path / "report.eml"
    source.write_bytes(payload)

    calls: list[str] = []

    def _record(name):
        def _spy(*_a, **_k):
            calls.append(name)
            raise _Called(f"{name} was called for an email attachment")

        return _spy

    with (
        # Classification is unrelated to attachment dispatch. Without this
        # patch it would need NLTK data, which tests get only from the
        # nltk_parser_data fixture; with it, the parse needs none.
        patch.object(utext, "is_possible_narrative_text", return_value=True),
        patch.object(pypandoc, "convert_file", _record("convert_file")),
        patch.object(udoc, "convert_office_doc", _record("convert_office_doc")),
        patch.object(docx, "Document", _record("docx.Document")),
    ):
        # Spies must run inside the parser boundary. The upload routing and
        # normal real-child round trip are covered in test_isolated_text_formats.
        result = isolated_worker._result(str(source), ".eml")
        assert result["status"] == "ok"
        docs = isolated_loader._documents_from_output(
            json.dumps(result).encode(), extension=".eml"
        )

    assert calls == []
    text = "\n".join(d.page_content for d in docs)
    assert BODY_MARKER in text
    assert "ATTACHMENT" not in text