Skip to content

Commit 10da1f7

Browse files
authored
Fix unguarded .content[1].t access in Lua filters (#14587)
Multiple Lua filters crash when content is empty or structurally unexpected — same class of latent bug as #14585 (empty blockquote). Migrate unsafe `.content[1].t` checks to `quarto.utils.match()` where possible, add explicit nil guards elsewhere, and fix a logic bug. - latexdiv.lua: guard empty div content with match("[1]/Para") - parsefiguredivs.lua: fix and→or logic bug in Figure bail-out; migrate nested Image type check to match("[1]/Para/[1]/Image") - pandoc3_figure.lua: migrate linked-image check to match("[1]/Plain/[1]/Link"); add Typst empty-content early return - crossref/theorems.lua: remove 90 lines of dead Div handler — parseblockreftargets (Dec 2023) converts proof divs to custom nodes before crossref_theorems() runs, making it unreachable - Regression tests for each fix via smoke-all qmd files with pre-ast filters that inject empty/malformed AST nodes
1 parent e7f43aa commit 10da1f7

13 files changed

Lines changed: 196 additions & 107 deletions

‎src/resources/filters/crossref/theorems.lua‎

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -37,98 +37,6 @@ function crossref_theorems()
3737
proof.order = add_crossref(label, type, title)
3838
return proof
3939
end,
40-
Div = function(el)
41-
local type = refType(el.attr.identifier)
42-
local theoremType = theorem_types[type]
43-
if theoremType then
44-
internal_error()
45-
else
46-
-- see if this is a proof, remark, or solution
47-
local proof = proof_type(el)
48-
if proof ~= nil then
49-
50-
-- ensure requisite latex is injected
51-
crossref.using_theorems = true
52-
53-
if proof.env ~= "proof" then
54-
el.attr.classes:insert("proof")
55-
end
56-
57-
-- capture then remove name
58-
--
59-
-- we have string_to_quarto_ast_inlines but we don't need it here
60-
-- because this filter happened after shortcode processing, and this
61-
-- is a regular div we're processing
62-
local name = markdownToInlines(el.attr.attributes["name"])
63-
if not name or #name == 0 then
64-
name = resolveHeadingCaption(el)
65-
end
66-
el.attr.attributes["name"] = nil
67-
68-
-- output
69-
if _quarto.format.isLatexOutput() then
70-
local preamble = pandoc.List()
71-
preamble:insert(pandoc.RawInline("latex", "\\begin{" .. proof.env .. "}"))
72-
if name ~= nil then
73-
preamble:insert(pandoc.RawInline("latex", "["))
74-
tappend(preamble, name)
75-
preamble:insert(pandoc.RawInline("latex", "]"))
76-
end
77-
preamble:insert(pandoc.RawInline("latex", "\n"))
78-
-- https://github.com/quarto-dev/quarto-cli/issues/6077
79-
if el.content[1].t == "Para" then
80-
preamble:extend(el.content[1].content)
81-
el.content[1].content = preamble
82-
else
83-
if (el.content[1].t ~= "Para") then
84-
-- required trick to get correct alignement when non Para content first
85-
preamble:insert(pandoc.RawInline('latex', "\\leavevmode"))
86-
end
87-
el.content:insert(1, pandoc.Plain(preamble))
88-
end
89-
local end_env = "\\end{" .. proof.env .. "}"
90-
-- https://github.com/quarto-dev/quarto-cli/issues/6077
91-
if el.content[#el.content].t == "Para" then
92-
el.content[#el.content].content:insert(pandoc.RawInline("latex", "\n" .. end_env))
93-
elseif el.content[#el.content].t == "RawBlock" and el.content[#el.content].format == "latex" then
94-
-- this is required for no empty line between end_env and previous latex block
95-
el.content[#el.content].text = el.content[#el.content].text .. "\n" .. end_env
96-
else
97-
el.content:insert(pandoc.RawBlock("latex", end_env))
98-
end
99-
elseif _quarto.format.isJatsOutput() then
100-
el = jatsTheorem(el, nil, name )
101-
else
102-
local span = pandoc.Span(
103-
{ pandoc.Emph(pandoc.Str(envTitle(proof.env, proof.title)))},
104-
pandoc.Attr("", { "proof-title" })
105-
)
106-
if name ~= nil then
107-
span.content:insert(pandoc.Str(" ("))
108-
tappend(span.content, name)
109-
span.content:insert(pandoc.Str(")"))
110-
end
111-
tappend(span.content, { pandoc.Str(". ")})
112-
113-
-- if the first block is a paragraph, then prepend the title span
114-
if #el.content > 0 and
115-
el.content[1].t == "Para" and
116-
el.content[1].content ~= nil and
117-
#el.content[1].content > 0 then
118-
el.content[1].content:insert(1, span)
119-
else
120-
-- else insert a new paragraph
121-
el.content:insert(1, pandoc.Para{span})
122-
end
123-
end
124-
125-
end
126-
127-
end
128-
129-
return el
130-
131-
end
13240
}
13341

13442
end

‎src/resources/filters/layout/pandoc3_figure.lua‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ function render_pandoc3_figure()
1111
local function html_handle_linked_image(figure)
1212
local div = pandoc.Div({})
1313
div.identifier = "fig-yesiamafigure" -- this is a bad hack to make discoverLinkedFigureDiv work
14-
local link = nil
15-
if figure.content[1].t == "Plain" then
16-
local plain = figure.content[1]
17-
if plain.content[1].t == "Link" then
18-
link = plain.content[1]
19-
end
20-
end
21-
if link == nil then
14+
local link = quarto.utils.match("[1]/Plain/[1]/Link")(figure)
15+
if not link then
2216
return nil
2317
end
2418
div.content:insert(pandoc.Para({link}))
@@ -197,6 +191,9 @@ function render_pandoc3_figure()
197191
end
198192
end
199193
end
194+
if #figure.content == 0 then
195+
return nil
196+
end
200197
return make_typst_figure({
201198
content = figure.content[1],
202199
caption = figure.caption.long[1],

‎src/resources/filters/quarto-post/latexdiv.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function latexDiv()
3434

3535
-- if the first and last div blocks are paragraphs then we can
3636
-- bring the environment begin/end closer to the content
37-
if divEl.content[1].t == "Para" and divEl.content[#divEl.content].t == "Para" then
37+
if quarto.utils.match("[1]/Para")(divEl) and divEl.content[#divEl.content].t == "Para" then
3838
table.insert(divEl.content[1].content, 1, pandoc.RawInline('tex', beginEnv .. "\n"))
3939
table.insert(divEl.content[#divEl.content].content, pandoc.RawInline('tex', "\n" .. endEnv))
4040
else

‎src/resources/filters/quarto-pre/parsefiguredivs.lua‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,9 @@ function parse_floatreftargets()
305305
local identifier = div.identifier
306306
local attr = pandoc.Attr(identifier, div.classes, div.attributes)
307307
assert(content)
308-
if (#content == 1 and content[1].t == "Para" and
309-
content[1].content[1].t == "Image") then
310-
-- if the div contains a single image, then we simply use the image as
311-
-- the content
312-
content = content[1].content[1]
308+
local single_image = quarto.utils.match("[1]/Para/[1]/Image")(content)
309+
if #content == 1 and single_image then
310+
content = single_image
313311

314312
-- don't merge classes because they often have CSS consequences
315313
-- but merge attributes because they're needed to correctly resolve
@@ -486,7 +484,7 @@ function parse_floatreftargets()
486484
if category == nil then
487485
return nil
488486
end
489-
if #fig.content ~= 1 and fig.content[1].t ~= "Plain" then
487+
if #fig.content ~= 1 or fig.content[1].t ~= "Plain" then
490488
-- we don't know how to parse this pandoc 3 figure
491489
-- just return as is
492490
return nil
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Test helper: empties Figure content to trigger unguarded .content[1].t access
2+
function Figure(fig)
3+
if fig.identifier == "fig-emptied" then
4+
fig.content = pandoc.Blocks({})
5+
return fig
6+
end
7+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Empty Figure content guard
3+
format: pdf
4+
keep-tex: true
5+
filters:
6+
- at: pre-ast
7+
path: empty-figure-content-filter.lua
8+
_quarto:
9+
tests:
10+
pdf:
11+
noErrors: default
12+
ensureLatexFileRegexMatches:
13+
-
14+
- '\\begin\{figure\}'
15+
---
16+
17+
## Normal figure
18+
19+
![A caption](img/surus.jpg){#fig-normal}
20+
21+
See @fig-normal.
22+
23+
## Figure with content stripped by filter
24+
25+
![Emptied caption](img/surus.jpg){#fig-emptied}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Test helper: empties non-crossref Figure content to trigger
2+
-- unguarded .content[1].t access in pandoc3_figure.lua
3+
function Figure(fig)
4+
if fig.identifier == "" then
5+
fig.content = pandoc.Blocks({})
6+
return fig
7+
end
8+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Empty Pandoc 3 Figure content guard
3+
format: html
4+
filters:
5+
- at: pre-ast
6+
path: empty-pandoc3-figure-filter.lua
7+
_quarto:
8+
tests:
9+
html:
10+
noErrors: default
11+
---
12+
13+
## Figure without cross-ref identifier
14+
15+
![A plain figure caption](img/surus.jpg)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Test helper: creates a fig-div with a single empty Para (no inlines)
2+
-- and a fig-cap attribute for caption, triggering unguarded
3+
-- content[1].content[1].t access in parsefiguredivs.lua:309
4+
function Div(div)
5+
if div.identifier == "fig-empty-para" then
6+
div.content = pandoc.Blocks({pandoc.Para({})})
7+
div.attributes["fig-cap"] = "External caption"
8+
return div
9+
end
10+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Empty Para inlines guard
3+
format: html
4+
filters:
5+
- at: pre-ast
6+
path: empty-para-inlines-filter.lua
7+
_quarto:
8+
tests:
9+
html:
10+
noErrors: default
11+
---
12+
13+
## Figure div with content replaced by empty Para
14+
15+
::: {#fig-empty-para}
16+
![Normal image](img/surus.jpg)
17+
18+
Normal caption
19+
:::
20+
21+
See @fig-empty-para.

0 commit comments

Comments
 (0)