Repository navigation
security: cap total node count in convertChildren (Parse/ParseFragment) - #277
Merged
Merged
Conversation
Continuing round 172's own sweep into the one other place a script routinely hands this engine a whole subtree at once: innerHTML/ outerHTML/insertAdjacentHTML, which all route through dom.ParseFragment (or dom.Parse for the top-level page), sharing one core: convertChildren. golang.org/x/net/html's own parser refuses markup nested past 512 levels, but that guard is about depth -- a WIDE document (millions of sibling elements, never nesting past a handful of levels) sails straight through it untouched. Confirmed live, two ways: `root.innerHTML = "<div>x</div>".repeat(3000000)` -- one statement, no loop -- allocated ~11.5GB of real memory in 15 seconds; a 25-iteration `root.innerHTML = root.outerHTML + root.outerHTML` loop (the same doubling shape as round 172's cloneNode case, through serialize-then-reparse instead of direct cloning) reached ~6GB. convertChildren now carries a shared node-count budget (maxParsedNodes=200000, matching js.maxCloneNodes's own value and reasoning) threaded through its own recursion and the <template> content-fragment branch. Past zero, remaining siblings/descendants are simply not converted rather than crashing or exhausting memory. Measured effect: the flat repeat(3000000) case drops from ~11.5GB/15.4s to ~1.1GB/1.8s; the outerHTML-doubling loop drops from ~6GB/8.3s to ~1.4GB/3.5s. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Continuing round 172's own sweep into the one other place a script routinely hands this engine a whole subtree at once:
innerHTML/outerHTML/insertAdjacentHTML, which all route throughdom.ParseFragment(ordom.Parsefor the top-level page), sharing one core:convertChildren.golang.org/x/net/html's own parser refuses markup nested past 512 levels, but that guard is about depth — a WIDE document (millions of sibling elements, never nesting past a handful of levels) sails straight through it untouched.Confirmed live, two ways:
root.innerHTML = "<div>x</div>".repeat(3000000)— one statement, no loop — allocated ~11.5GB of real memory in 15 seconds.root.innerHTML = root.outerHTML + root.outerHTMLloop (the same doubling shape as round 172'scloneNodecase, through serialize-then-reparse instead of direct cloning) reached ~6GB.convertChildrennow carries a shared node-count budget (maxParsedNodes=200000, matchingjs.maxCloneNodes's own value and reasoning) threaded through its own recursion and the<template>content-fragment branch. Past zero, remaining siblings/descendants are simply not converted rather than crashing or exhausting memory.Measured effect: the flat
repeat(3000000)case drops from ~11.5GB/15.4s to ~1.1GB/1.8s; theouterHTML-doubling loop drops from ~6GB/8.3s to ~1.4GB/3.5s.Test plan
go build ./...go test ./...(all packages pass)TestParseFragmentCapsTotalNodeCount,TestParseCapsTotalNodeCountconvertChildrenat 100% coverage;dompackage 98.4%→98.5% (confirmed viagit stash, no regression)🤖 Generated with Claude Code