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.

test(fuzz): termination and amplification contracts for the export pipeline (#6458)

LearningCircuit0f41bcfc3cb3
diff --git a/tests/fuzz/test_fuzz_export_pipeline.py b/tests/fuzz/test_fuzz_export_pipeline.py
new file mode 100644
index 0000000000..63ef598334
--- /dev/null
+++ b/tests/fuzz/test_fuzz_export_pipeline.py
@@ -0,0 +1,110 @@
+"""Fuzz and property contracts for the export pipeline.
+
+Report content is synthesized from fetched web pages, so every parser
+and regex in ``citation_formatter`` runs over attacker-influenced text.
+A catastrophic-backtracking regression in any of them turns an export
+request into a server-side denial of service. These contracts pin:
+
+* **Termination** — every export entry point completes on adversarial
+ input (bounded by ``@pytest.mark.timeout``; the global pytest-timeout
+ is the backstop).
+* **Bounded amplification** — escaping inflates input by a constant
+ factor, never multiplicatively.
+* **Bounded amplification** — escaping inflates input by a constant
+ factor, never multiplicatively.
+
+Targeted payloads are sized to finish in well under a second on linear
+code; a quadratic or exponential regression blows the per-test timeout
+long before the global one.
+"""
+
+from __future__ import annotations
+
+import pytest
+from hypothesis import given, settings, strategies as st
+
+from local_deep_research.text_optimization.citation_formatter import (
+ LaTeXExporter,
+ QuartoExporter,
+ RISExporter,
+)
+
+# Every character class the exporters treat specially: math delimiters,
+# LaTeX specials, citation brackets (ASCII and lenticular), list markers,
+# emphasis, and the RIS field vocabulary.
+_META_ALPHABET = "$\\{}[]#%&~^【】,0123456789 \n\t-URL:" + "ab"
+
+_meta_text = st.text(alphabet=_META_ALPHABET, min_size=0, max_size=400)
+
+
+class TestExportEntryPointsTerminate:
+ """Adversarial inputs must not hang any exporter."""
+
+ @pytest.mark.timeout(30)
+ @pytest.mark.parametrize(
+ ("label", "payload"),
+ [
+ # Merged-citation opener: worst case for the group-aware
+ # \d+(?:,\s*\d+)* patterns.
+ ("merged_citation_flood", "[1" + ",1" * 25000 + "]"),
+ # Lenticular (Unicode) citation brackets.
+ ("lenticular_flood", "【1,2】" * 20000),
+ # Every dollar unpaired: scanner worst case for lone-$ handling.
+ ("lone_dollar_flood", "$" * 100000),
+ # Maximum pairing density.
+ ("alternating_pairs", "$a" * 50000),
+ # Single unterminated inline math opener.
+ ("one_opener_then_text", "$" + "a" * 100000),
+ # Escaper amplification stress (single-char → \textbackslash{}).
+ ("backslash_flood", "\\" * 100000),
+ ("brace_flood", "{" * 50000 + "}" * 50000),
+ ("hash_flood", "#" * 100000),
+ ("tilde_circumflex_flood", ("~^" * 50000)),
+ # RIS: many complete source entries with URLs.
+ (
+ "ris_source_flood",
+ "## Sources\n\n"
+ + ("[1] " + "a" * 120 + "\nURL: https://x.example/b\n\n")
+ * 1500,
+ ),
+ # RIS: unterminated URL lines at section end.
+ (
+ "ris_partial_url_flood",
+ "## Sources\n\n" + ("[1] t\nURL:" * 20000),
+ ),
+ # One enormous heading line.
+ ("heading_flood", "# " + "x" * 100000),
+ ],
+ )
+ def test_exporters_complete_on_adversarial_input(self, label, payload):
+ latex = LaTeXExporter().export_to_latex(payload)
+ quarto = QuartoExporter().export_to_quarto(payload)
+ ris = RISExporter().export_to_ris(payload)
+
+ assert isinstance(latex, str)
+ assert isinstance(quarto, str)
+ assert isinstance(ris, str)
+
+ @pytest.mark.timeout(60)
+ @settings(max_examples=50, deadline=None)
+ @given(_meta_text)
+ def test_exporters_complete_on_arbitrary_meta_text(self, text):
+ LaTeXExporter().export_to_latex(text)
+ RISExporter().export_to_ris(text)
+
+
+class TestEscapingAmplificationIsBounded:
+ """Escaping may inflate by a constant factor, never worse."""
+
+ @pytest.mark.timeout(30)
+ def test_backslash_flood_stays_under_constant_factor(self):
+ # Longest replacement is r"\textbackslash{}" (15 chars) for one
+ # input char; allow a 20x margin over the worst single-char case.
+ payload = "\\" * 20000
+
+ latex = LaTeXExporter().export_to_latex(payload)
+ body = latex.replace(
+ LaTeXExporter()._create_latex_header(), ""
+ ).replace(LaTeXExporter()._create_latex_footer(), "")
+
+ assert len(body) <= 20 * len(payload)