Repository navigation
Description
What happens
In a component that uses Vue, Citry writes the page's HTML on the server
with its own renderer in Rust (crates/citry_vue_compiler/src/server_render.rs),
so the browser shows content before Vue starts. That renderer stops at a
fixed depth. When a template nests elements deeper than that, it gives up on
the part that holds them: the nearest element becomes a shell, an element
marked data-allow-mismatch="children" with its deepest content left out,
and Vue builds that content in the browser when it starts.
The page still works once Vue runs, but the server HTML is incomplete, and
nothing tells the author. Templates may nest 512 tags (the nesting limit
from #272), so the renderer gives up well inside what Citry accepts.
Measured by bisection on main cada47d9f, one component with
js = "$component({});" and a template of nested <div> tags with
{{ n }} innermost, rendered with serialize():
Nested <div> tags |
Server HTML |
|---|---|
| 129 | complete, {{ n }} included |
| 130 | the innermost content is missing and an element is marked data-allow-mismatch="children" |
Why
The renderer recurses once per level and has no stack guard, so it caps the
depth to stay inside a small thread's stack:
MAX_DEPTH = 256(server_render.rs:2296) is checked in
Writer::occurrence(:2512, once per component),Writer::node
(:2546, once per node), andWriter::element(:2700). Past it, the
part fails withnesting-depthand becomes a shell.StaticWriter::node
(:3324), which writes a shell's contents, stops at the same depth and
leaves the rest out.- One element appears to cost two levels of that count, probably one for
the element and one for the fragment that holds its children
(write_noderecurses intoNode::Fragmentchildren at:2592). That
fits the cut at 129 and 130 elements, about half of 256, but the exact
count per element was not traced. MAX_READ_DEPTH = 200(:424) is checked inReader::node(:429) and
Reader::expr(:787), which read the render code Vue's compiler
produces into the renderer's own tree. Deeper render code, such as a long
chain of interpolations orv-else-ifbranches, is left unread and built
in the browser.
The crate does not depend on stacker. #272's PR C1 added stack guards to
the template parser, compiler, and formatter, but not to this crate.
The fix
- Add stack guards. Wrap each recursion entry in the same guard the
parser uses (stacker::maybe_grow, the pattern in
crates/citry_template_parser/src/stack.rs:39,with_enough_stack).
Addstacker = { workspace = true }to the crate'sCargo.toml; the
version is already pinned in the rootCargo.toml. The sites:Reader::nodeandReader::expr(reading render code);Writer::occurrence,Writer::node, andWriter::element(writing
HTML);StaticWriter::node(writing a shell's contents);eval(:1131), which recurses once per level of an expression the
reader built. Its depth is bounded byMAX_READ_DEPTH, but it runs on
the same thread, so it should be guarded too.
Check also whether dropping a deepNodetree recurses (the derived
Drop), as C1's test 7 did for the template tree.
- Raise the caps to match the template limits.
MAX_DEPTH: enough for 512 nested elements, inside components and
control flow, with a margin: about 1,100 levels. Name the reason in its
comment (twice the 512-tag limit, plus the levels components and
fragments add).MAX_READ_DEPTH: enough for the render code of a template at both
limits. Measure how deep Vue's render code nests for 512 elements and
for 256 logic levels (v-if/v-forchains), and set the cap above
the larger with a margin.- Keep both caps as a backstop against input that does not come from a
Citry template; past them the current shell behavior stays.
- Error modes stay as they are. Past a cap, the part still becomes a
shell (or the page mounts in the browser when there is no element to
hold the shell). A malformed program still fails the way it does today.
Tests
- Rust, small thread. In the crate's tests, on a thread created with
std::thread::Builder::new().stack_size(256 * 1024)and again with
128 * 1024: read and write a program for 512 nested elements, and for
256 nestedv-iflevels, then drop them on the same thread. Put these
in their own test binary, astests/deep_nesting.rsdoes in the parser,
so a stack overflow aborts only that binary. - Python, server HTML. A component with
jsand 512 nested<div>
tags renders with{{ n }}in the server HTML and no
data-allow-mismatch="children"marker. The same at 256 nested<c-if>
levels, and withc-if=andc-for=on the elements. Use the parser's
private_MAX_TAG_NESTINGand_MAX_LOGIC_NESTINGonce 50 nested<c-if>blocks fail with "too many nested parentheses", and about 1,000 crash the process #272's PR C2
lands; until then, the literals 512 and 256. - Bisection guard. One test just past the new
MAX_DEPTH(built by
hand in Rust, since a template cannot reach it) still produces a shell,
so the backstop keeps working.
Docs
#272's PR C2 adds a "How deep templates can nest" section to
docs_site/content/syntax/index.md. If this fix lands before C2, that
section needs no Vue caveat. If C2 lands first, it states the 129-element
server limit for components that use Vue, and this fix removes that
sentence.
Related: #272 (deep nesting).
Activity