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.

fix(quarto): stop a fenced cell in report prose becoming executable (#6471)

Lukasae0d4d275702
diff --git a/changelog.d/6471.security.md b/changelog.d/6471.security.md
new file mode 100644
index 0000000000..8d0105936b
--- /dev/null
+++ b/changelog.d/6471.security.md
@@ -0,0 +1,13 @@
+Quarto export no longer promotes a fenced cell in report prose to an
+executable cell. A ```` ```{python} ```` fence is inert text in a markdown
+report and a cell that `quarto render` runs in a `.qmd`, and report bodies
+carry web-influenced content, so the braces are now dropped and the code is
+exported as the display fence it was (#6470).
+
+The rule follows Quarto's own splitter rather than CommonMark, because that is
+what decides which fences run: a cell may be indented arbitrarily while a
+display fence may not be indented at all, whitespace is allowed between the
+fence and the brace, cell options are unrestricted text (backticks and `}`
+included), a block closes only on an exact tick-count match, and `~~~` is not
+a fence to Quarto. A plain ```` ```python ```` fence, Pandoc's `{.python}`
+attribute form, and a cell tag quoted inside an open fence are all left alone.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index e3f9a71042..7118b2b560 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -990,6 +990,107 @@ class CitationFormatter:
return "source"
+# What counts as an executable cell is decided by Quarto's own splitter, so
+# this mirrors that grammar rather than CommonMark's. From quarto-cli
+# ``src/core/lib/break-quarto-md.ts``:
+#
+# startCodeCellRegEx ^\s*(```+)\s*\{([=A-Za-z][=A-Za-z0-9._]*)( *[ ,].*)?\}\s*$
+# startCodeRegEx ^```
+# endCodeRegEx ^\s*(```+)\s*$ closing on an EXACT tick match
+#
+# Every difference from CommonMark is one a fence can be written to exploit: a
+# cell may be indented arbitrarily while a display fence may not be indented at
+# all, whitespace is allowed between the fence and the brace, cell options are
+# unrestricted text (backticks and ``}`` included), the closer must match the
+# opener's tick count exactly, and ``~~~`` is not a fence to Quarto at all.
+#
+# Pandoc's attribute form carries a leading marker — ``{.python}`` (class),
+# ``{#id}`` — and is excluded by the ``[=A-Za-z]`` first character, which is
+# also why ``{=html}`` raw blocks are in scope: Quarto reads them as cells.
+_QUARTO_CELL_FENCE = re.compile(
+ r"^(?P<indent>\s*)(?P<fence>`{3,})\s*"
+ r"\{(?P<lang>[=A-Za-z][=A-Za-z0-9._]*)(?P<rest> *[ ,].*)?\}\s*$"
+)
+_QUARTO_DISPLAY_FENCE = re.compile(r"^`{3,}")
+_QUARTO_FENCE_CLOSE = re.compile(r"^\s*(?P<fence>`{3,})\s*$")
+
+
+def _quarto_tick_count(line: str) -> int:
+ """Backticks in the first space-separated token, as Quarto counts them."""
+ return line.split(" ")[0].count("`")
+
+
+def _defuse_quarto_cells_once(body: str) -> tuple[str, bool]:
+ """One pass of the cell demotion. Returns the body and whether it changed."""
+ out: list[str] = []
+ changed = False
+ open_ticks = 0
+
+ for line in body.split("\n"):
+ if open_ticks:
+ # Only an exact tick match closes: a longer fence inside a shorter
+ # block is content to Quarto, and treating it as a closer would
+ # hand the rest of the block back to plain text.
+ closing = _QUARTO_FENCE_CLOSE.match(line)
+ if closing and len(closing.group("fence")) == open_ticks:
+ open_ticks = 0
+ out.append(line)
+ continue
+
+ cell = _QUARTO_CELL_FENCE.match(line)
+ if cell:
+ open_ticks = len(cell.group("fence"))
+ out.append(
+ f"{cell.group('indent')}{cell.group('fence')}{cell.group('lang')}"
+ )
+ changed = True
+ continue
+
+ if _QUARTO_DISPLAY_FENCE.match(line):
+ # Column 0 only. An indented ``` is prose to Quarto, so treating it
+ # as an open fence would shield a cell further down.
+ open_ticks = _quarto_tick_count(line)
+ out.append(line)
+
+ return "\n".join(out), changed
+
+
+def _defuse_quarto_cells(body: str) -> str:
+ """Turn executable Quarto cells in report prose into display fences.
+
+ Markdown to ``.qmd`` is not format-preserving: a ```` ```{python} ````
+ fence is inert text in a markdown report and an EXECUTABLE cell in a
+ Quarto document, which ``quarto render`` — the only reason to ask for this
+ format — runs. Report bodies carry web-influenced content, so the fence
+ that arrives need not have been written by the user reading it.
+
+ Dropping the braces keeps the code visible and stops it being a cell. A
+ plain ```` ```python ```` fence is display-only already and is untouched,
+ and so is anything inside an open fence: a cell tag quoted inside a code
+ block is content, not syntax.
+
+ The pass repeats to a fixed point. Demoting an *indented* cell leaves a
+ line Quarto does not read as a fence at all, which returns the lines after
+ it to plain text — where a tag that was content a moment ago becomes a cell
+ in its own right. Each pass only ever removes braces, so the set of
+ cell-shaped lines shrinks and the loop settles; it is bounded by that count
+ so an unforeseen shape cannot spin.
+
+ Front matter is deliberately not modelled. Quarto reads a ``---`` block as
+ raw YAML, and a mid-document ``---`` surrounded by blank lines as a
+ horizontal rule, so tracking it here would only ever *stop* a defusion.
+ """
+ passes = (
+ sum(1 for line in body.split("\n") if _QUARTO_CELL_FENCE.match(line))
+ + 1
+ )
+ for _ in range(passes):
+ body, changed = _defuse_quarto_cells_once(body)
+ if not changed:
+ break
+ return body
+
+
class QuartoExporter:
"""Export markdown documents to Quarto (.qmd) format."""
@@ -1075,7 +1176,13 @@ csl: apa.csl
+ "\n```\n:::\n"
)
- return yaml_header + processed_content + bibliography_note
+ # The callout and its bibtex fence below are the exporter's own
+ # markup, so only the report body is defused.
+ return (
+ yaml_header
+ + _defuse_quarto_cells(processed_content)
+ + bibliography_note
+ )
def _generate_bibliography(self, content: str) -> str:
"""Generate BibTeX bibliography from sources."""
diff --git a/tests/web/services/test_export_generation_contracts.py b/tests/web/services/test_export_generation_contracts.py
index 4a7edaa575..49d967c9cd 100644
--- a/tests/web/services/test_export_generation_contracts.py
+++ b/tests/web/services/test_export_generation_contracts.py
@@ -370,15 +370,6 @@ class TestLaTeXBibliographyIsAlreadyHardened:
class TestQuartoDocumentPromotion:
- @pytest.mark.xfail(
- strict=True,
- reason=(
- "LIVE DEFECT: the Quarto body is copied verbatim, so a "
- "```{python} fence in LLM prose becomes an EXECUTABLE cell "
- "that `quarto render` runs. Markdown->qmd is not "
- "format-preserving here: it promotes inert text to code. "
- ),
- )
def test_a_fenced_cell_in_prose_must_not_become_an_executable_cell(self):
"""``.qmd`` is not an inert rendering of the markdown.
@@ -396,6 +387,196 @@ class TestQuartoDocumentPromotion:
assert "print(open(" in body # arrival: the text is still there
assert "```{python}" not in body # but not as a runnable cell
+ def test_a_defused_cell_keeps_its_language(self):
+ """Defusing is a demotion, not a deletion: the cell becomes the
+ display fence it would have been in the markdown it came from, so the
+ reader still gets highlighted Python. A bare ```` ``` ```` would be
+ just as inert and would lose that, which no security assertion here
+ can see."""
+ report = f"## Method\n\n{QUARTO_EXECUTABLE_CELL}\n\nDiscussion.\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "```python\n" in body
+
+ def test_a_display_fence_survives_untouched(self):
+ """Accept control on the defuser, and the reason it is not a blanket
+ strip: a plain fence was never executable, so a rule that neutralised
+ it would cost every code block in every report to close nothing."""
+ report = (
+ "## Method\n\n```python\nprint(open('/etc/passwd').read())\n```\n"
+ )
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "```python\n" in body
+ assert "```{" not in body
+
+ def test_a_pandoc_attribute_fence_is_not_a_cell(self):
+ """``{.python}`` is Pandoc's attribute syntax — a class on a display
+ block — and Quarto does not run it. The two forms differ by one
+ character, so the rule is graded on that character rather than on a
+ language name."""
+ report = "## Method\n\n```{.python}\nprint(1)\n```\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "```{.python}" in body
+
+ def test_a_cell_tag_inside_an_open_fence_stays_content(self):
+ """A report that QUOTES the executable syntax — documentation, a
+ review of this very defect — is showing text, not opening a cell. A
+ line-by-line rewrite that ignored fence state would corrupt it."""
+ report = "## Method\n\n````\n```{python}\nprint(1)\n```\n````\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "```{python}" in body
+
+ def test_the_exporters_own_callout_and_bibtex_fence_survive(self):
+ """The defuser runs on the report body only. The bibliography note the
+ exporter appends is its own markup, and a `::: {.callout-note}` div is
+ not a fence at all."""
+ report = (
+ "Body.\n\n## Sources\n\n[1] A paper\n"
+ " URL: https://example.com/paper\n"
+ )
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "::: {.callout-note}" in body
+ assert "```bibtex" in body
+
+
+class TestQuartoCellGrammarFollowsQuarto:
+ """The defuser is graded against Quarto's splitter, not CommonMark.
+
+ ``quarto render`` decides what runs with the regexes in
+ ``src/core/lib/break-quarto-md.ts``:
+
+ startCodeCellRegEx ^\\s*(```+)\\s*\\{([=A-Za-z][=A-Za-z0-9._]*)( *[ ,].*)?\\}\\s*$
+ startCodeRegEx ^```
+ endCodeRegEx ^\\s*(```+)\\s*$ closing on an EXACT tick match
+
+ Each case below is a fence that those regexes read as an executable cell
+ while a CommonMark reading does not — a defuser written to the wrong
+ grammar passes its own tests and still ships the cell.
+ """
+
+ def test_a_space_between_the_fence_and_the_brace_still_opens_a_cell(self):
+ """``\\s*`` sits between the fence and the brace in Quarto's regex."""
+ report = (
+ "## M\n\n``` {python}\nprint(open('/etc/passwd').read())\n```\n"
+ )
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(open(" in body
+ assert "{python}" not in body
+
+ def test_an_option_value_may_contain_a_backtick(self):
+ """Quarto's option group is ``.*``. Excluding backticks from it lets a
+ cell carry one and slip past."""
+ report = '## M\n\n```{python, comment="`"}\nprint(1)\n```\n'
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "{python" not in body
+
+ def test_an_option_value_may_contain_a_closing_brace(self):
+ """``fig.cap="a}b"`` is an ordinary caption. Quarto's ``.*`` is greedy
+ up to the last brace, so the cell opens."""
+ report = '## M\n\n```{r, fig.cap="a}b"}\nsessionInfo()\n```\n'
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "sessionInfo()" in body
+ assert "{r," not in body
+
+ def test_a_cell_may_be_indented_past_the_commonmark_limit(self):
+ """CommonMark stops a fence at three spaces; Quarto's cell open is
+ ``^\\s*``, so four spaces — or a tab — is still a cell."""
+ for indent in (" ", "\t"):
+ report = f"## M\n\n{indent}```{{python}}\n{indent}print(1)\n{indent}```\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "{python}" not in body
+
+ def test_an_indented_closer_ends_the_block_for_quarto(self):
+ """``endCodeRegEx`` is ``^\\s*``: an indented ``` closes a block opened
+ at column 0. A defuser that misses the close holds state and hands the
+ next cell through as if it were content."""
+ report = (
+ "## M\n\n```python\nx = 1\n ```\n```{python}\nprint(1)\n```\n"
+ )
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "{python}" not in body
+
+ def test_an_indented_display_fence_never_opens_a_block(self):
+ """The mirror image: ``startCodeRegEx`` is ``^```` with no leading
+ whitespace, so an indented display fence is prose and does not shield
+ what follows it."""
+ report = "## M\n\n ```python\nx = 1\n```{python}\nprint(1)\n```\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "{python}" not in body
+
+ def test_a_tilde_fence_is_not_a_fence_to_quarto(self):
+ """Quarto's splitter has no tilde branch. A ``~~~`` block is prose, so
+ a cell inside one runs — while a CommonMark reader treats the same
+ lines as an opaque code block."""
+ report = "## M\n\n~~~\n```{python}\nprint(1)\n```\n~~~\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "{python}" not in body
+
+ def test_a_longer_fence_inside_a_block_does_not_close_it(self):
+ """Quarto closes on an EXACT tick match. Closing on ``>=`` ends the
+ block early, and everything after it is then read in the wrong state —
+ here that returns a later cell to plain text."""
+ report = (
+ "## M\n\n```{python}\n````\nprint(1)\n```\n"
+ "```{python}\nprint(2)\n```\n"
+ )
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "print(2)" in body
+ assert "{python}" not in body
+
+ def test_defusing_an_indented_cell_cannot_expose_a_new_one(self):
+ """Why the pass repeats. Demoting an indented cell leaves a line Quarto
+ does not read as a fence at all, which returns the lines after it to
+ plain text — where a tag that was content a moment ago is a cell of its
+ own. One pass closes the first and opens the second."""
+ report = "## M\n\n ```{python}\n```{python}\nprint(1)\n ```\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "print(1)" in body
+ assert "{python}" not in body
+
+ def test_a_pandoc_attribute_fence_is_still_left_alone(self):
+ """Accept control for the whole class: the first character of the tag
+ is what separates a cell from Pandoc's attribute syntax, and widening
+ the grammar must not start rewriting display blocks."""
+ report = "## M\n\n```{.python}\nprint(1)\n```\n"
+
+ _front, body, _names = _quarto_parts(report, title="Report")
+
+ assert "```{.python}" in body
+
@pytest.mark.xfail(
strict=True,
reason=(