From b2590c82dbaec71ba975375675776479056c8864 Mon Sep 17 00:00:00 2001 From: Noam Rosenthal Date: Mon, 23 Nov 2026 13:25:07 +0000 Subject: [PATCH] Guard both node and parser documents against dynamic markup insertion Increment and decrement the throw-on-dynamic-markup-insertion counter in prepare content patching around its node removal loop (addressing item 3 of #13036), and also guard the parser's Document whenever it differs from the node document in both prepare content patching and create an element for a token. When script moves a patch's insertion target or an open element into another document mid-parse, guarding only the node document leaves the parser's Document open to re-entrant document.write() or document.open() calls during tree construction, while guarding only the parser's Document leaves streaming fragment parsers unguarded on the target's node document. Fixes #13317. --- source | 61 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/source b/source index 3d6d29d1f53..581e7f180b6 100644 --- a/source +++ b/source @@ -70445,8 +70445,8 @@ interface HTMLTemplateElement : HTMLElement {

To prepare content patching given an Element or - DocumentFragment scope and a template element - template:

+ DocumentFragment scope, a template element + template, and a Document parserDocument:

  1. Assert: template's for @@ -70489,6 +70489,21 @@ interface HTMLTemplateElement : HTMLElement {

+
  • Let scopeDocument be scope's node document.

  • + +
  • Increment scopeDocument's throw-on-dynamic-markup-insertion + counter.

  • + +
  • +

    If parserDocument is not scopeDocument, then increment + parserDocument's throw-on-dynamic-markup-insertion counter.

    + +

    parserDocument can differ from scopeDocument when script + moves an insertion target into another document mid-parse, or in the fragment + case where parserDocument is the inert Document with which the + fragment parser is associated.

    +
  • +
  • For each node of removedNodes:

    @@ -70505,6 +70520,12 @@ interface HTMLTemplateElement : HTMLElement {
  • +
  • If parserDocument is not scopeDocument, then decrement + parserDocument's throw-on-dynamic-markup-insertion counter.

  • + +
  • Decrement scopeDocument's throw-on-dynamic-markup-insertion + counter.

  • +
  • Return true.

  • @@ -126543,12 +126564,14 @@ document.body.appendChild(frame)

    Document objects have a throw-on-dynamic-markup-insertion counter, - which is used in conjunction with the create an element for the token algorithm to - prevent custom element constructors from being - able to use document.open(), create an element for the token and + prepare content patching algorithms to prevent scripts (such as custom element constructors and pagehide event handlers) from being able to use document.open(), document.close(), and document.write() when they are invoked by the parser. - Initially, the counter must be set to zero.

    + data-x="dom-document-write">document.write() when they are invoked synchronously by tree + construction. Initially, the counter must be set to zero.

    @@ -126571,7 +126594,8 @@ document.body.appendChild(frame) Document is an XML document.

    Throws an "InvalidStateError" DOMException if the - parser is currently executing a custom element constructor.

    + parser is currently executing a custom element constructor or removing nodes for a + template element's for attribute.

    window = document.open(url, name, features)
    @@ -126750,7 +126774,8 @@ document.body.appendChild(frame) Document is an XML document.

    Throws an "InvalidStateError" DOMException if the - parser is currently executing a custom element constructor.

    + parser is currently executing a custom element constructor or removing nodes for a + template element's for attribute.

    @@ -126818,7 +126843,8 @@ document.body.appendChild(frame) invoked on XML documents.

    Throws an "InvalidStateError" DOMException if the - parser is currently executing a custom element constructor.

    + parser is currently executing a custom element constructor or removing nodes for a + template element's for attribute.

    @@ -126937,7 +126963,8 @@ document.body.appendChild(frame) invoked on XML documents.

    Throws an "InvalidStateError" DOMException if the - parser is currently executing a custom element constructor.

    + parser is currently executing a custom element constructor or removing nodes for a + template element's for attribute.

    @@ -147757,6 +147784,8 @@ dictionary StorageEventInit : EventInit {
  • Let willExecuteScript be true if definition is non-null and the parser's fragment context element is null; otherwise false.

  • +
  • Let parserDocument be the parser's Document.

  • +
  • If willExecuteScript is true:

    @@ -147764,6 +147793,9 @@ dictionary StorageEventInit : EventInit {
  • Increment document's throw-on-dynamic-markup-insertion counter.

  • +
  • If parserDocument is not document, then increment + parserDocument's throw-on-dynamic-markup-insertion counter.

  • +
  • If the JavaScript execution context stack is empty, then perform a microtask checkpoint.

  • @@ -147815,6 +147847,9 @@ dictionary StorageEventInit : EventInit {
  • Invoke custom element reactions in queue.

  • +
  • If parserDocument is not document, then decrement + parserDocument's throw-on-dynamic-markup-insertion counter.

  • +
  • Decrement document's throw-on-dynamic-markup-insertion counter.

  • @@ -149086,8 +149121,8 @@ document.body.appendChild(text);
  • Let success be the result of preparing content patching given scope and - template.

  • + patching">preparing content patching given scope, template, + and the parser's Document.

  • If success is false, then insert an element at the adjusted insertion location with template,