~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""LaTeX body-escaping contracts for the legacy LaTeXExporter.
Report content is synthesized from fetched web pages, so body text can
carry attacker-influenced characters. A compiled ``.tex`` must not turn
those into live control sequences: unescaped ``\\input``/``\\write18``
must not survive into the compiled document as file-read / shell-escape
vectors, and that must still hold once the markdown-to-LaTeX conversions
(headings, emphasis, citations) run.
The control-sequence scenarios (``\\input`` file-read, ``\\write18``
shell-escape, heading injection) are pinned end-to-end by the promoted
contracts in ``tests/web/services/test_export_generation_contracts.py``;
this file pins only the edges those suites do not cover: the remaining
special characters in body and heading text, the markdown-conversion
interplay, and the ``_escape_latex`` helper's exactness.
These contracts pin the escaping only — they do not depend on Quarto or
a TeX installation.
"""
from __future__ import annotations
from local_deep_research.text_optimization.citation_formatter import (
LaTeXExporter,
)
class TestBodyControlSequenceInjection:
"""Backslash commands in body text must be escaped, not live."""
def test_brace_hash_tilde_circumflex_in_body_are_escaped(self):
exporter = LaTeXExporter()
content = (
"# Title\n\nC# and {braces} ~ tilde ^ circumflex path\\to\\file\n"
)
result = exporter.export_to_latex(content)
assert "C\\#" in result
assert "\\{braces\\}" in result
assert "\\textasciitilde{}" in result
assert "\\textasciicircum{}" in result
assert "\\textbackslash{}" in result
assert "path\\textbackslash{}to\\textbackslash{}file" in result
class TestHeadingTextInjection:
"""Heading text must be escaped the same as body text."""
def test_heading_special_characters_are_escaped(self):
exporter = LaTeXExporter()
content = "# C# & F# — 100% {styled}\n"
result = exporter.export_to_latex(content)
assert "\\section{" in result
assert "C\\#" in result
assert "\\{styled\\}" in result
class TestMarkdownConversionSurvivesEscaping:
"""Escaping must not break the downstream markdown conversions."""
def test_emphasis_and_citations_still_convert(self):
exporter = LaTeXExporter()
content = (
"# Title\n\n**bold{x}** and *italic* cite [1].\n\n"
"## Sources\n\n[1] A Source\n"
)
result = exporter.export_to_latex(content)
assert "\\textbf{bold\\{x\\}}" in result
assert "\\textit{italic}" in result
assert "\\cite{1}" in result
class TestEscapeLatexHelper:
"""_escape_latex must produce valid LaTeX for its own replacements."""
def test_single_backslash_is_not_double_escaped(self):
exporter = LaTeXExporter()
# The replacement for \ contains literal braces; a sequential
# pass then escapes those braces again and corrupts the output.
assert exporter._escape_latex("\\") == "\\textbackslash{}"
def test_backslash_among_other_specials(self):
exporter = LaTeXExporter()
assert (
exporter._escape_latex("a\\b&c%d") == "a\\textbackslash{}b\\&c\\%d"
)