Skip to content

Refuse a page that nests too deeply to render instead of overflowing the stack - #19

Open
sharmis-tp wants to merge 1 commit into
cppalliance:masterfrom
sharmis-tp:limit-nesting-v2
Open

sharmis-tp wants to merge 1 commit into
cppalliance:masterfrom
sharmis-tp:limit-nesting-v2

Conversation

@sharmis-tp

Copy link
Copy Markdown
Collaborator

A page with about 2,000 nested elements, only about 10 KB of HTML, makes web/fetch abort the whole process. htmd recurses once per level of nesting and overflows the 2 MiB stack of the thread that renders. A stack overflow is not a panic, so nothing can catch it. It happens with and without raw, so any page a model fetches can trigger it. On master (b2c5749), a release build crashes at a depth of 2,000 and a debug build at 1,000.

To reproduce:

"<div>".repeat(2000) + "text"

served as text/html and fetched with web/fetch.

The fix:

  • A new html_tree.rs parses the HTML with html5ever in 4 KB chunks. Between chunks it stops once more than 512 elements are open, and it checks the finished tree for a depth over 256. So a deep page is refused while it is read, not after it. The parser's own time also grows with the square of the depth.
  • 256 is half of Chromium's 512 levels, and leaves htmd room on the stack even in a debug build.
  • extract_html parses with it first, and the whole-page render parses the absolutized HTML through it again, so both the article path and raw are covered.
  • A page over the limit gets a new soft error, FetchError::TooDeep: "the HTML of {url} nests elements more than 256 levels deep, which is the limit; try a different URL". The model gets it like any other soft failure and can try another URL.

Tests: the /deep route serves 100,000 nested <div>s, and a_page_nested_too_deeply_is_refused_with_and_without_raw checks that both modes return the error instead of crashing. disposition_table also lists TooDeep as soft. fmt, clippy, nextest (184 passed), doc and the release build all pass locally.

If #18 merges first, the deep test's call needs a trailing , None, and I'll rebase.

@coderabbitai

coderabbitai Bot commented Dec 4, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f2ad0b61-3eb7-4366-a07d-0e1e7528b8af
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

1 participant