`document.write()` works during custom element `connectedCallback` in the parser — incomplete fix for CVE-2026-2775
Firefox did not block document.write() while custom element reaction callbacks fired by the HTML parser were running, so a page could inject markup and script during parsing. An attacker could get script execution, corrupt the parser's open element stack, and produce mutation XSS through namespace switching.
- Fixed in 11 days
- Paid in 26 days
- Disclosed after 11 days
Created attachment 9589090 cve2026_2775_exploit_poc2.html
Summary
document.write() is not blocked during custom element reaction callbacks (connectedCallback, disconnectedCallback) fired by the HTML parser. The throwOnDynamicMarkupInsertionCounter that prevents document.write() during CE construction is only incremented in CreateHTMLElement — it is never incremented in Append(FromParser), where AutoCEReaction invokes queued reactions.
A web page can trigger this by placing a custom element inside misnested formatting tags (e.g. <b>text<div><x-evil>...</x-evil></div>more</b>). The parser runs the Adoption Agency Algorithm, which moves children to a clone and appends it. When the clone is inserted into the document, connectedCallback fires on any custom element children. Inside that callback, document.write() succeeds and the injected content is parsed immediately — including <script> elements that execute, event handlers that fire, and SVG/MathML namespace switches that produce mXSS.
This is an incomplete fix for CVE-2026-2775 (MFSA 2026-13). That patch added parent checks to Append(FromParser), AppendToDocument, and FosterParent, but did not address the underlying issue: CE reactions during parsing can call document.write() because throwOnDynamicMarkupInsertionCounter is not set in the reaction path.
Root Cause
The HTML spec requires that document.write() throws an InvalidStateError when the custom element constructor is running (the "throw-on-dynamic-markup-insertion counter" check). Firefox implements this in CreateHTMLElement at nsHtml5TreeOperation.cpp:557 via AutoSetThrowOnDynamicMarkupInsertionCounter.
However, this counter is NOT incremented when CE reactions (connectedCallback, disconnectedCallback, adoptedCallback, attributeChangedCallback) are invoked. In Append(FromParser) at nsHtml5TreeOperation.cpp:280, an AutoCEReaction is created at line 294 to batch reactions. When this destructs, it calls PopAndInvokeElementQueue(), which fires connectedCallback. At this point:
IsSafeToRunScript()is true (thensHtml5AutoPauseUpdatehas calledEndDocUpdate()which removed the script blocker)throwOnDynamicMarkupInsertionCounteris 0 (onlyCreateHTMLElementincrements it)- The parser's tokenizer accepts new input via
document.write()
So document.write() succeeds, and the injected tokens are processed by the parser while it is in the middle of executing tree operations from the AAA.
Additionally, AppendChildrenToNewParent at line 374 has no AutoCEReaction and no parent check — it was not included in the CVE-2026-2775 fix. CE reactions from children moved by this function go to the backup element queue and fire at the next microtask checkpoint.
Proof of Concept
The attached PoC (cve2026_2775_exploit_poc2.html) runs five tests. All five succeed on Firefox Nightly 152.0a1. Open the file in Firefox and observe the output.
Steps to reproduce:
- Open Firefox Nightly (tested on 152.0a1)
- Serve the attached
cve2026_2775_exploit_poc2.htmlover HTTP (e.g.python3 -m http.server) - Open the page
- Observe the results after ~1 second
Each test creates an iframe, defines a custom element whose connectedCallback calls document.write() with a different payload, then triggers the AAA via document.write('<b>X<div><x-evil>V</x-evil></div>Y</b>') in a script.
Test A — <script> execution:
The connectedCallback calls document.write('<script>window.scriptRan=true</script>'). The injected script executes immediately. window.scriptRan is true.
Test B — Event handler execution:
The callback injects <img src=x onerror="..."> and <svg onload="...">. Both handlers fire. window.imgErrorFired and window.svgLoadFired are both true.
Test C — Namespace injection / mXSS:
The callback injects <svg><foreignObject><div><style></style><img onerror="..."> and a MathML variant. Both produce script execution via namespace confusion. The SVG element is in the http://www.w3.org/2000/svg namespace.
Test D — Parser state corruption:
The callback injects </div></b> to close elements on the parser's open element stack. This corrupts the stack mid-AAA. A subsequently injected <div id="injectedD"> ends up directly under <body>, escaping its expected container entirely.
Test E — Recursive re-entrancy:
The callback injects another misnested custom element via document.write(), triggering AAA again, triggering another connectedCallback, which calls document.write() again. This reaches 3 levels of recursion. The parser re-enters itself unboundedly.
Impact
This is exploitable from any web page with no user interaction beyond visiting the page. No compromised renderer or special privileges are needed.
- Arbitrary script execution during parsing — attacker-controlled
<script>elements injected viadocument.write()execute immediately - Parser state corruption — injected close tags corrupt the parser's open element stack, causing subsequent content to be inserted in wrong locations
- mXSS — SVG and MathML namespace injection during parsing produces script execution patterns that bypass DOMParser-based sanitizers
- Recursive re-entrancy — the parser can be re-entered to arbitrary depth via nested custom elements, each triggering AAA and
connectedCallback
Relationship to CVE-2026-2775
CVE-2026-2775 (MFSA 2026-13) fixed parser re-entrancy via CE callbacks by adding parent checks to Append(FromParser), AppendToDocument, and FosterParent. This variant differs in two ways:
AppendChildrenToNewParent(line 374) was not patched — it has no parent check and noAutoCEReaction- The deeper issue:
document.write()is not blocked during CE reactions. ThethrowOnDynamicMarkupInsertionCounteris only incremented inCreateHTMLElement, not in the reaction invocation path. Even if all parent checks were added,connectedCallbackcan still calldocument.write()and inject content into the parser
The CVE-2026-2775 fix addressed specific insertion points but did not address the root cause: CE reactions fired by the parser have full script access including document.write().
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the throw-on-dynamic-markup-insertion counter is only incremented in CreateHTMLElement and not in the custom element reaction path, so document.write() is allowed during connectedCallback fired by the parser.
Impact
Any web page could run attacker script during parsing with no user interaction, corrupt parser state, and bypass DOMParser-based sanitisers via mXSS; the report notes this was tested only on Firefox Nightly.
What the attacker needed
Victim visits a malicious page; Firefox browser
The fix
The text does not state a fix; it argues the counter should also be set when parser-fired custom element reactions run, not only in CreateHTMLElement.
How it was found
- Open Firefox Nightly and serve the proof-of-concept page over HTTP.
- Open the page and wait about one second for the tests to run.
- Each test creates an iframe, defines a custom element whose connectedCallback calls document.write(), and triggers the adoption agency algorithm with misnested markup.
- Observe that the injected script, event handlers, namespace payloads, stack corruption and recursion all succeed.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A web page is normally built step by step by a parser, and a page can define its own HTML tags with code that runs when the tag appears. This report shows that such code could sneak extra content, including scripts, back into the parser while it was still building the page. That let a malicious page run its own script and confuse the parser about where elements belong.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.