HTML parser should not insert nodes the DOM would reject - #69748
Merged
webkit-commit-queue merged 1 commit intoAug 19, 2026
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
annevk
force-pushed
the
eng/HTML-parser-should-drop-nodes-that-cannot-be-inserted-into-a-document
branch
from
August 18, 2026 15:52
521994e to
4ab005f
Compare
This comment was marked as outdated.
This comment was marked as outdated.
annevk
marked this pull request as ready for review
August 18, 2026 15:55
rniwa
approved these changes
Aug 18, 2026
rniwa
reviewed
Aug 18, 2026
| task.parent = templateElement->fragmentForInsertion(); | ||
| task.nextChild = nullptr; | ||
| } else { | ||
| if (task.nextChild && task.nextChild->parentNode() != task.parent) [[unlikely]] |
Member
There was a problem hiding this comment.
Don't we want to use containsIncludingHostElements here as well?
Contributor
Author
There was a problem hiding this comment.
No, nextChild needs to be a direct child. And insert() only deals with freshly created elements, if that's what you were worried about. (It has ASSERT(!task.child->parentNode());)
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
annevk
force-pushed
the
eng/HTML-parser-should-drop-nodes-that-cannot-be-inserted-into-a-document
branch
from
August 19, 2026 06:31
4ab005f to
72bdea2
Compare
Collaborator
|
EWS run on current version of this PR (hash 72bdea2) Details |
https://bugs.webkit.org/show_bug.cgi?id=319780 rdar://183256491 Reviewed by Ryosuke Niwa. The parser inserts nodes with low-level operations that skip pre-insertion validity checks, so script that moves a node on the stack of open elements during parsing could make us build a tree the DOM does not allow. Use Document::canAcceptChild() when inserting into a Document, which drops Text node children, a second element child, and a second or misordered DocumentType child on the floor. This is reachable by foster parenting into a table that script has made another Document's document element, and by document.open() leaving children in the Document before the DOCTYPE token or the "html" start tag. The adoption agency algorithm checked for cycles with Node::contains(), which only considers the node's own tree, so it missed a parent script had moved into a shadow root (crashing) or into a template element's content (silently creating an invalid tree). Move the nextChild check out of executeInsertAlreadyParsedChildTask() and into insert(), where nextChild is used. Script can run before a queued insertion takes place: in the case from bug 169222, parserRemoveChild() destroys an iframe element's frame and its unload handler removes nextChild from the parent, which is covered by fast/parser/scriptexec-during-parserInsertBefore.html. Nothing about that is specific to the adoption agency, though no input is known to reach the check through a plain insertion. Because the template element branch runs first and clears nextChild, a stale nextChild no longer prevents an insertion into a template element's contents. That is what appropriate place for inserting a node calls for, as it redirects into the contents after the last child, discarding the reference node. Spec changes: whatwg/html#12708 whatwg/html#12709 Tests: imported/w3c/web-platform-tests/html/syntax/parsing/foster-parenting-into-document-with-element-child.html imported/w3c/web-platform-tests/html/syntax/parsing/insert-into-nonempty-document.html imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_url.html?file=scripted_foster01 imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write.html?file=scripted_foster01 imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write_single.html?file=scripted_foster01 imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-document-target.html imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-shadow-cycle.html imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-template-cycle.html Canonical link: https://commits.webkit.org/319453@main
webkit-commit-queue
force-pushed
the
eng/HTML-parser-should-drop-nodes-that-cannot-be-inserted-into-a-document
branch
from
August 19, 2026 13:04
72bdea2 to
e8b1d6b
Compare
Collaborator
|
Committed 319453@main (e8b1d6b): https://commits.webkit.org/319453@main Reviewed commits have been landed. Closing PR #69748 and removing active labels. |
annevk
deleted the
eng/HTML-parser-should-drop-nodes-that-cannot-be-inserted-into-a-document
branch
August 19, 2026 13:57
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.
🛠 mac-apple
e8b1d6b
72bdea2
🧪 win-tests