CVE-2026-9769: Uncontrolled recursion DoS in JustHTML() via deeply nested HTML
Summary justhtml through 1.9.1 allows denial of service via deeply nested HTML. During parsing, JustHTML.init() always reaches TreeBuilder.finish(), which unconditionally calls populateselectedcontent(). That function recursively traverses the DOM via findelements() / findelement() without a depth bound, allowing attacker-controlled deeply nested input to trigger an unhandled RecursionError on CPython.
Published Mar 17, 2026 · Updated Aug 25, 2026
Affected packages
justhtml
- Ecosystem
- PyPI
- Affected
- <= 1.9.1
- Fixed in
- 1.10.0
- Check
pip show justhtml
Advisory details
From the GitHub Advisory Database (CC BY 4.0).
Summary
justhtml through 1.9.1 allows denial of service via deeply nested HTML. During parsing, JustHTML.__init__() always reaches TreeBuilder.finish(), which unconditionally calls _populate_selectedcontent(). That function recursively traverses the DOM via _find_elements() / _find_element() without a depth bound, allowing attacker-controlled deeply nested input to trigger an unhandled RecursionError on CPython. Depending on the host application's exception handling, this can abort parsing, fail requests, or terminate a worker/process.
Details
TreeBuilder.finish() (`treebuilder.py#L476`) unconditionally calls _populate_selectedcontent(self.document) at line 494. _populate_selectedcontent() (`treebuilder.py#L1243`) calls _find_elements() (`treebuilder.py#L1280`) to recursively search the DOM tree for <select> elements:
def _find_elements(self, node: Any, name: str, result: list[Any]) -> None:
"""Recursively find all elements with given name."""
if node.name == name:
result.append(node)
if node.has_child_nodes():
for child in node.children:
self._find_elements(child, name, result) # recursive callWhen the DOM tree depth exceeds CPython's default recursion limit (1000), this raises an unhandled RecursionError. The full call path is:
JustHTML(html) → tokenizer.run() → tree_builder.finish() → _populate_selectedcontent(document) → _find_elements(root, "select", selects) (recursive)
Deeply nested DOM trees can be produced by nesting ` tags ~1000 levels deep. On CPython with the default recursion limit, approximately 11 KB of ` nesting is sufficient to trigger the error. The exact depth threshold is environment-dependent (CPython version, recursion limit setting, call stack depth at invocation).
Additional recursive functions are affected on already-parsed deep trees:
Node.clone_node(deep=True)(`node.py#L523`) — called during sanitization_node_to_html()(`serialize.py#L580`) — used byto_html(pretty=True)_to_markdown_walk()(`node.py#L817`) — used byto_markdown()
Note: the library already uses iterative traversal in several comparable functions (e.g., _node_to_html_compact at `serialize.py#L197`, _to_text_collect at `node.py#L161`, _is_blocky_element at `serialize.py#L405`, apply_to_children at `transforms.py#L1642`), demonstrating the correct pattern.
PoC
from justhtml import JustHTML
html = "" * 1000 + "x" + "" * 1000
doc = JustHTML(html) # raises RecursionErrorTest environment: CPython 3.14.3, macOS ARM64 (Apple Silicon), justhtml 1.9.1, default recursion limit (1000)
| Input | Size | Result | | `` × 500 | 5,501 bytes | OK | | `` × 800 | 8,801 bytes | OK | | `` × 1000 | 11,001 bytes | RecursionError |
The error occurs with both sanitize=True (default) and sanitize=False.
Impact
An attacker who can supply HTML for parsing can trigger an unhandled RecursionError during JustHTML() construction. The error is triggered during construction and is not avoided by justhtml configuration alone; mitigating it requires host-application exception handling or input constraints. Depending on the host application's exception handling, this can abort parsing, fail requests, or terminate a worker/process.
Suggested Fix
Convert the recursive tree traversal functions to iterative implementations using an explicit stack. Example for _find_elements:
def _find_elements(self, node: Any, name: str, result: list[Any]) -> None:
stack = [node]
while stack:
current = stack.pop()
if current.name == name:
result.append(current)
if current.has_child_nodes():
stack.extend(reversed(current.children))The same conversion should be applied to _find_element, clone_node(deep=True), _node_to_html(), and _to_markdown_walk().
Credited to kq5y, SebTardif.
References
- https://github.com/EmilStenstrom/justhtml/security/advisories/GHSA-v7cf-c9rm-wm3j
- https://github.com/EmilStenstrom/justhtml/releases/tag/v1.10.0
- https://nvd.nist.gov/vuln/detail/CVE-2026-9769
- https://www.vulncheck.com/advisories/justhtml-before-denial-of-service-via-deeply-nested-html
- https://github.com/advisories/GHSA-v7cf-c9rm-wm3j
How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. No analysis has been written for this entry yet. Methodology
More PyPI vulnerabilities
All PyPI entriesGHSA-jf6w-2mvx-633j
justhtml: to_markdown() code-span blank-line breakout enables XSS
medium6.1Jun 25, 2026GHSA-r8cj-3554-33mr
justhtml introduces denial-of-service hardening
lowMay 8, 2026GHSA-vrx2-77f2-ww34
justhtml has sanitization bypass in custom policies and programmatic DOM
medium6.0Apr 22, 2026GHSA-4p64-v8f5-r2gx
Multiple security fixes in justhtml
low1.3Apr 14, 2026GHSA-c9vm-hv86-f23r
justhtml includes multiple security fixes
mediumApr 10, 2026GHSA-r758-8hxw-4845
justhtml: Mutation XSS with custom foreign-namespace sanitization policies
low2.1Apr 8, 2026
