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.

11.2 KB
Raw Blame
"""Untrusted report math must not become executable TeX on export."""

import re

import pytest

from local_deep_research.text_optimization.citation_formatter import (
    LaTeXExporter,
    QuartoExporter,
)


def _export_body(math: str, delimiter: str) -> str:
    report = f"# Report\n\nBefore {delimiter}{math}{delimiter} after."
    result = LaTeXExporter().export_to_latex(report)
    return result.split("\\maketitle\n", 1)[1].split("\\end{document}", 1)[0]


def _has_control_word(body: str, command: str) -> bool:
    r"""Whether TeX reads ``command`` as a control word somewhere in ``body``.

    A control word ends at the first non-letter, so the ``\text`` at the
    start of the escaper's own ``\textbackslash{}`` does not count.
    """
    return re.search(re.escape(command) + "(?![A-Za-z])", body) is not None


@pytest.mark.parametrize(
    ("math", "text"),
    [
        (r"\input{/etc/passwd}", "passwd"),
        (r"\include{chapter}", "chapter"),
        (r"\write18{touch probe}", "probe"),
        (r"\immediate\write18{touch probe}", "probe"),
        (r"\openin1=/etc/passwd", "passwd"),
        (r"\read16 to\pi", "read16"),
        (r"\catcode 124=0", "catcode"),
        (r"\global\catcode`\|=0\relax", "catcode"),
        (r"\def\alpha{x}", "alpha"),
        (r"\newcommand{\pi}{3}", "newcommand"),
        (r"\csname input\endcsname{/etc/passwd}", "passwd"),
        (r"\usepackage{shellesc}", "shellesc"),
        (r"x% comment", "comment"),
        (r"\begin{document}", "document"),
        (r"\unknown{1}", "unknown"),
        (r"x^{unclosed", "unclosed"),
        ("^^5cinput{/etc/passwd}", "passwd"),
        # An allowed command (\in) followed by letters spelled with ^^:
        # TeX would read the whole name as \input.
        (r"\in^^70ut{/etc/passwd}", "passwd"),
        # ``\^`` must stay out of _SAFE_MATH_CONTROL_SYMBOLS. Were it
        # allowed, ``\^`` would consume the first caret and hide the ``^^``
        # from the encoding check, leaving a lone second caret. TeX decodes
        # the hex form ``^^69`` to ``i``, so the span is
        # ``\input{/etc/passwd}``. Pinned for both caret spellings:
        (r"\^^69nput{/etc/passwd}", "passwd"),
        # ...and the single-character form, where ``^^)`` decodes to ``i``
        # (``0x29 + 0x40``):
        (r"\^^)nput{/etc/passwd}", "passwd"),
        # The ``^^`` check must reject every ``^^``, not only the two-hex
        # form. ``^^0`` is the single-character encoding of ``p``
        # (``0x30 + 0x40``), so ``\in^^0ut`` is ``\input``; here the digit
        # after ``^^`` is followed by a non-hex letter, which a hex-only
        # check would wave through.
        (r"\in^^0ut{/etc/passwd}", "passwd"),
    ],
)
@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_unsafe_math_is_rendered_as_literal_text(math, text, delimiter):
    body = _export_body(math, delimiter)

    assert text in body
    assert delimiter.replace("$", "\\$") in body
    assert f"{delimiter}{math}{delimiter}" not in body
    assert "^^" not in body
    for command in re.findall(r"\\[A-Za-z]+", math):
        assert not _has_control_word(body, command)


@pytest.mark.parametrize(
    "command",
    [
        "input",
        "include",
        "InputIfFileExists",
        "write",
        "immediate",
        "openout",
        "openin",
        "read",
        "readline",
        "catcode",
        "def",
        "edef",
        "gdef",
        "xdef",
        "let",
        "newcommand",
        "renewcommand",
        "providecommand",
        "DeclareRobustCommand",
        "csname",
        "expandafter",
        "usepackage",
        "RequirePackage",
        "documentclass",
        "special",
        "scantokens",
        "directlua",
        "ShellEscape",
        "makeatletter",
        "begin",
        "end",
        "relax",
    ],
)
@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_one_command_outside_the_allowlist_makes_the_span_literal(
    command, delimiter
):
    """Every other token here is allowed, so only this command decides."""
    math = "\\alpha \\le \\" + command + "{x} \\beta"

    body = _export_body(math, delimiter)

    assert f"{delimiter}{math}{delimiter}" not in body
    assert not _has_control_word(body, "\\" + command)


@pytest.mark.parametrize(
    ("math", "packages"),
    [
        (r"\text{if } x > 0", ("amsmath", "amstext")),
        (r"\mathbb{R}^n", ("amsfonts", "amssymb")),
    ],
)
@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_commands_from_packages_the_export_does_not_load_stay_literal(
    math, packages, delimiter
):
    r"""``\text`` and ``\mathbb`` are undefined unless these packages load.

    The export preamble loads none of them, so both commands stay off the
    allowlist; allowing them would need the preamble to load the package.
    """
    result = LaTeXExporter().export_to_latex(
        f"# Report\n\nThe set {delimiter}{math}{delimiter} here."
    )
    preamble = result.split("\\begin{document}", 1)[0]

    for package in packages:
        assert package not in preamble
    assert f"{delimiter}{math}{delimiter}" not in result


@pytest.mark.parametrize(
    "math",
    [
        "x_2^2 + y^2 = r^2",
        r"\frac{1}{2} + \sqrt{x} = \alpha",
        r"\sum_{i=1}^{n} i = \frac{n(n+1)}{2}",
        r"x\$y",
        pytest.param(
            r"\chi^2 + \eta\nu + \xi\kappa + \zeta\psi + \iota\upsilon"
            r" + \vartheta\varpi\varrho\varsigma",
            id="greek-lower-case",
        ),
        pytest.param(r"\Xi + \Upsilon + \Psi", id="greek-upper-case"),
        pytest.param(
            r"a \ast b \star c \circ d \bullet e \setminus f \oplus g"
            r" \otimes h \wedge i \vee j \dagger k",
            id="binary-operators",
        ),
        pytest.param(
            r"a \le b \ge c \ne d \ll e \gg f \propto g \cong h \simeq i"
            r" \perp j \parallel k \mid l \not= m \ni n \prec o \models p",
            id="relations",
        ),
        pytest.param(
            r"p \Rightarrow q \Leftrightarrow r \iff s \mapsto t"
            r" \longrightarrow u \hookrightarrow v \uparrow w \gets x",
            id="arrows",
        ),
        pytest.param(
            r"\aleph_0 + \hbar + \ell + \Re z + \Im z + \neg p + \angle A"
            r" + \wp + \top + \triangle + f\colon X \to Y",
            id="other-symbols",
        ),
        pytest.param(r"x_1, \ldots, x_n \vdots \ddots", id="dots"),
        pytest.param(
            r"\bigcup_{i=1}^{n} A_i + \bigcap_{j} B_j + \oint_C f"
            r" + \coprod_k + \bigoplus_l + \bigotimes_m",
            id="big-operators",
        ),
        pytest.param(
            r"\exp(x) + \sinh y + \det A + \sup S + \gcd(a, b) + \arg z"
            r" + a \bmod b + x \equiv y \pmod{n}",
            id="named-operators",
        ),
        pytest.param(
            r"\left\{ x \middle| x > 0 \right\} + \big( \langle u, v"
            r" \rangle \big) + \lfloor x \rfloor + \lceil y \rceil + \|v\|"
            r" + \Bigl[ z \Bigr]",
            id="delimiters",
        ),
        pytest.param(
            r"\hat{x} + \bar{y} + \tilde{z} + \vec{v} + \dot{x} + \ddot{x}"
            r" + \widehat{xy} + \overline{AB} + \underbrace{a + b}_{2}",
            id="accents",
        ),
        pytest.param(
            r"\mathcal{O}(n \log n) + \mathsf{T} + \mathtt{x}",
            id="math-alphabets",
        ),
        pytest.param(
            r"\sum\limits_{i} {\scriptstyle x} + \int\nolimits_0^1",
            id="styles-and-limits",
        ),
        pytest.param(
            r"a\,b\:c\;d\!e\ f \quad g \qquad h \enspace i \thinspace j",
            id="spacing",
        ),
        pytest.param(
            r"50\% + \#S + \mathrm{max\_iter} + A \& B",
            id="escaped-characters",
        ),
        pytest.param("f'(x) + g''(x)", id="prime"),
        pytest.param(r"25^\circ", id="degrees"),
        pytest.param("E =\r\nmc^2", id="crlf-line-break"),
    ],
)
@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_supported_math_keeps_rendering(math, delimiter):
    result = LaTeXExporter().export_to_latex(
        f"# Report\n\nThe identity {delimiter}{math}{delimiter} holds."
    )

    assert f"{delimiter}{math}{delimiter}" in result


def test_crlf_display_block_keeps_rendering():
    """Legacy report_content rows store CRLF newlines (see ``_BLOCK_END``)."""
    report = "# Report\r\n\r\nBefore\r\n$$\r\nE = mc^2\r\n$$\r\nafter.\r\n"

    result = LaTeXExporter().export_to_latex(report)

    assert "$$\r\nE = mc^2\r\n$$" in result


@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_math_with_a_lone_cr_stays_literal(delimiter):
    """TeX ends a line at a lone CR, so two of them make a blank line.

    ``_BLOCK_END`` only sees blank lines that end in LF, so such a span can
    reach the check; accepting it would put a paragraph break inside math.
    """
    body = _export_body("a\r\rb", delimiter)

    assert f"{delimiter}a\r\rb{delimiter}" not in body
    assert delimiter.replace("$", "\\$") in body


# ---------------------------------------------------------------------------
# Quarto export. The front matter declares a ``pdf`` format, so a
# ``quarto render`` that builds the PDF hands each math span to Pandoc's
# LaTeX writer, which emits math verbatim. The same allowlist that guards
# the LaTeX export must therefore guard the Quarto export: an unsafe span is
# escaped to literal text, a safe span is kept as math.


def _quarto_span_region(math: str, delimiter: str) -> str:
    """The exported ``.qmd`` body between fixed sentinels around the span."""
    report = f"# Report\n\nBefore {delimiter}{math}{delimiter} after."
    qmd = QuartoExporter().export_to_quarto(report, "Report")
    return qmd.split("Before ", 1)[1].split(" after.", 1)[0]


@pytest.mark.parametrize(
    ("math", "text"),
    [
        (r"\input{/etc/passwd}", "passwd"),
        (r"\write18{touch pwn}", "pwn"),
        (r"\immediate\write18{touch pwn}", "pwn"),
        (r"\global\catcode`\|=0\relax", "catcode"),
        (r"\csname input\endcsname{/etc/passwd}", "passwd"),
        ("^^5cinput{/etc/passwd}", "passwd"),
        # The caret rules the LaTeX test pins apply on this path too.
        (r"\^^69nput{/etc/passwd}", "passwd"),
        (r"\in^^0ut{/etc/passwd}", "passwd"),
    ],
)
@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_unsafe_report_math_is_escaped_in_quarto(math, text, delimiter):
    region = _quarto_span_region(math, delimiter)

    # The span is no longer math, and nothing was dropped.
    assert f"{delimiter}{math}{delimiter}" not in region
    assert text in region
    # Dollars are escaped, so Pandoc never shifts into math on this span.
    assert "\\$" in region
    assert re.search(r"(?<!\\)\$", region) is None
    # Every backslash is escaped, so no control sequence survives for a
    # ``raw_tex``-enabled reader, and no ``^^`` encoding is left.
    assert re.search(r"(?<!\\)\\[A-Za-z]", region) is None
    assert "^^" not in region


@pytest.mark.parametrize(
    "math",
    [
        r"\frac{1}{2} + \sqrt{x} = \alpha",
        r"\sum_{i=1}^{n} i = \frac{n(n+1)}{2}",
        "E = mc^2",
    ],
)
@pytest.mark.parametrize("delimiter", ["$", "$$"])
def test_safe_report_math_stays_math_in_quarto(math, delimiter):
    region = _quarto_span_region(math, delimiter)

    # A safe span is handed to Pandoc as math, unchanged.
    assert f"{delimiter}{math}{delimiter}" in region