Skip to content

feat: limit templates to 512 nested tags and 256 logic levels - #470

Merged
JuroOravec merged 1 commit into
mainfrom
feat/nesting-limits
Oct 28, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
feat/nesting-limits

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this does

Fixes #272. This is the last part. The earlier parts (#318, #337, #343, #355, #356, #360, #364, #377, #397, #400, #403, #428) made deep templates compile and render without crashing. This PR sets how deep a template may go.

A template can now nest tags at most 512 levels deep. At most 256 of those levels can be Citry logic: every c- tag except <c-raw>, and each c-if=, c-elif=, c-else, c-for= or c-empty attribute, which adds a level of its own on top of its tag. When a template goes one level past either limit, the parser stops at the tag that goes too deep and raises SyntaxError with the new diagnostic code citry.parse.too-deep. citry check and the language server report the same code. The parser stops there, before running the passes that call themselves once per level, so a template 100,000 levels deep costs one read of its text and nothing that grows with its depth. For 257 nested <c-if> the message ends:

This <c-if> is 257 Citry logic levels deep. Every c- tag except <c-raw>
is one logic level, and so is each c-if=, c-elif=, c-else, c-for= or
c-empty attribute. A template can nest at most 256 logic levels and 512
levels of any kind. To go deeper, move the inner part into its own
component.

This is a breaking change for plain elements nested more than 512 deep, which used to render. The fix is to move the inner part into its own component, whose template starts counting from zero.

How it works

  • crates/citry_template_parser/src/nesting.rs holds the two limits and the one function that says how many levels a tag adds. The parser and the compiler both count with it, so they agree at the limit.
  • The parser counts while it builds its stack of open tags, and checks every start, self-closing and void tag. A template inside an attribute starts from the depth of the tag that holds it.
  • ParseError::TooDeep carries the diagnostic code. When the tag's line is very long, the message quotes only the part around the tag: a one-line template 100,000 levels deep gives a 617-byte message, not a 2.2 MB one.
  • compile_template(), and the Python binding before it copies a tree, reject a tree built by hand that goes past the limits.
  • citry_core._rust.template_parser exports the limits as private constants (_MAX_TAG_NESTING, _MAX_LOGIC_NESTING), so the frame-budget tests, the parser's and formatter's deep tests and the citry_core smoke script read the numbers instead of repeating them.

What else had to change (cross-language check)

  • Parser, compiler, error type: changed, with Rust tests in tests/nesting_limits.rs.
  • src/lang/*.rs: no behavior change, because the limits are checked before code generation, the same way for every host language. One stale comment in python.rs is corrected.
  • PyO3 (citry_core_py): the two private constants are added, and the compile path checks a hand-built tree before it copies it.
  • _rust.pyi: entries for the two constants.
  • Python wrappers (template_parser/parse.py, compile.py): docstrings name the new SyntaxError.
  • Diagnostic catalog: citry.parse.too-deep is added to catalog.json, and the generated Rust and Python catalog files are regenerated.
  • Formatter (citry_template_formatter): the generated catalog gains the new code, and tests/deep_elements.rs nests to the tag limit.
  • Vue compiler (citry_vue_compiler): comments in server_render.rs, stack.rs and tests/deep_nesting.rs now describe the limits as shipped.
  • Tests: Rust (tests/nesting_limits.rs, tests/deep_nesting.rs); citry_core (test_template_parser_nesting_limits.py); citry (test_nesting_limits.py, which renders at both limits, with and without Vue, plus _frame_budget.py, test_render_depth_frames.py and test_vue_hydration_regions.py); and the LSP (test_engine.py, test_frame_budget.py).
  • Scripts: scripts/smoke_citry_core.py checks the limits in a built wheel, and a comment in scripts/vue_render_parity/check.py describes them as shipped.
  • Agent docs: the parser's AGENTS.md and docs/agent/INDEX.md, and docs/design/vue_ssr_selected_tree_plan.md.

Docs

  • docs_site/content/syntax/index.md gets a "Nesting limits" section. It explains how levels are counted, how to go deeper, that a long chain of components passing slot content on is not limited and can raise RecursionError, and that a Vue component's server HTML holds 400 nested elements.
  • docs/design/template_grammar.md describes the counting rules.
  • Changelog entries in CHANGELOG.md, packages/py/citry_core/CHANGELOG.md and packages/protocol/diagnostics/CHANGELOG.md.

Follow-ups

Checklist

  • Tests added or updated for the change, and the checks pass locally (see below)
  • CHANGELOG.md updated
  • Docs updated
  • Cross-language contract stays in step (see above)

Local evidence on macOS arm64, Python 3.14.9, debug build, rebased on 4ec0dd1:

  • cargo fmt --check, cargo clippy -D warnings and cargo test on every first-party crate (1,238 passed). The parser crate was rerun after the last edits (584 passed).
  • pytest, without e2e and qualification tests, run in chunks: citry_core 532 passed, citry 9,641 passed, citry_lsp 922 passed, citry_ui 3,301 passed, pygments_citry 73 passed. The four test_benchmark_django*/djc* modules could not be imported because the optional benchmark group was not fully installed locally.
  • ruff check, ruff format --check, and mypy, as written and with --platform linux.
  • scripts/smoke_citry_core.py against the built extension.

The stack guard test (#428) and the deep Vue hydration tests (#400) are updated in this PR. Their templates counted a <div c-if> or <div c-for> as one level, but each is two (the tag and the wrapper around it), so they went past the limits. They now stop at the limits and read them from citry_core.

🤖 Generated with Claude Code

A template may now nest at most 512 tags of any kind and at most 256 of
Citry's logic levels: control flow, slots, fills, component tags and the
wrappers that c-if=/c-elif=/c-else/c-for=/c-empty shorthands add. One
level more is a citry.parse.too-deep error at the tag that goes too
deep, before the passes that call themselves once per level run, so a
template far past the limit costs one read of its text and nothing that
grows with its depth.

- nesting.rs holds the two constants and the one function that says how
  many levels a tag adds; the parser and the compiler both count with
  it, so they agree at the limit.
- The parser counts while it builds the tag stack and checks every start,
  self-closing and void tag. A template in an attribute continues from
  the depth of the tag that holds it.
- ParseError::TooDeep carries the new diagnostic code. When the tag's line
  is very long, its message shows only the part of the line around the
  tag: a one-line template 100,000 levels deep gives 617 bytes, not 2.2 MB.
- The compiler, and the Python binding before it clones a tree, reject a
  hand-built tree past the limits.
- citry_core exports the limits as private constants, and the frame
  budget tests, the parser's and formatter's deep tests and the smoke
  script read them instead of repeating the numbers.
- The deep templates in the stack guard test (#428) and the Vue hydration
  tests (#400) counted a `<div c-if>` or `<div c-for>` as one level. Each
  is two, the tag and the wrapper around it, so those templates now stop
  at the limits instead of going past them.
- The diagnostic catalog gains citry.parse.too-deep, and the generated
  catalog files are regenerated.
- Python tests render at both limits on both render paths, with and
  without Vue, and check that `citry check` and the language server
  report citry.parse.too-deep only past them. 100,000-level templates
  run in a subprocess.
- The syntax docs, the grammar design doc and the three changelogs
  describe the limits, including that the Vue server HTML holds 400
  nested elements inside a component. The citry_core smoke script
  checks the limits in a built wheel.

Fixes #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

50 nested <c-if> blocks fail with "too many nested parentheses", and about 1,000 crash the process

1 participant