Commit 6f6dabb0f98 for php
commit 6f6dabb0f98a9a828cb228c8d410a21a7031c897
Author: lazerg <lazerg2@gmail.com>
Date: Sun Dec 6 17:38:58 2026 +0500
ext/dom: UAF after adopting a DocumentFragment into another document
php_dom_adopt_node() reconciles the namespaces of the adopted node with
php_dom_libxml_reconcile_modern(), but that function returns early for
anything that is not an element. For a DocumentFragment the children kept
ns pointers into the old document's namespace mapper, which are freed with
that document.
This reconciles each child of the fragment instead, the same way
dom_clone_node() already does for cloned fragments.
libxml_fixup_name_and_content_outer() had the same gap on libxml < 2.13:
the children's names stayed in the old document's dictionary, so it now
recurses into fragments too.
Fix GH-26111
Close GH-26115
diff --git a/NEWS b/NEWS
index 642cbdb591e..380b4eb6a74 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ PHP NEWS
- DOM:
. Fixed bug GH-26109 (Setting innerHTML on an element of a cloned
Dom\HTMLDocument creates attributes without a name). (lazerg)
+ . Fixed bug GH-26111 (Dom\Document::adoptNode() of a DocumentFragment leaves
+ its children on the old document's namespaces). (lazerg, joemax-o360)
- Enchant:
. Fixed enchant_broker_set_dict_path() and enchant_broker_get_dict_path()
diff --git a/ext/dom/document.c b/ext/dom/document.c
index 3d327f3f4f4..403a6e2f8ae 100644
--- a/ext/dom/document.c
+++ b/ext/dom/document.c
@@ -1151,7 +1151,7 @@ static void libxml_fixup_name_and_content_outer(xmlDocPtr src_doc, xmlDocPtr dst
}
}
- if (node->type == XML_ELEMENT_NODE || node->type == XML_ATTRIBUTE_NODE) {
+ if (node->type == XML_ELEMENT_NODE || node->type == XML_ATTRIBUTE_NODE || node->type == XML_DOCUMENT_FRAG_NODE) {
for (xmlNodePtr child = node->children; child != NULL; child = child->next) {
libxml_fixup_name_and_content_outer(src_doc, dst_doc, child);
}
@@ -1173,7 +1173,13 @@ bool php_dom_adopt_node(xmlNodePtr nodep, dom_object *dom_object_new_document, x
xmlUnlinkNode(nodep);
xmlSetTreeDoc(nodep, new_document);
php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(dom_object_new_document);
- php_dom_libxml_reconcile_modern(ns_mapper, nodep);
+ if (nodep->type == XML_DOCUMENT_FRAG_NODE) {
+ for (xmlNodePtr child = nodep->children; child != NULL; child = child->next) {
+ php_dom_libxml_reconcile_modern(ns_mapper, child);
+ }
+ } else {
+ php_dom_libxml_reconcile_modern(ns_mapper, nodep);
+ }
} else {
int ret = xmlDOMWrapAdoptNode(NULL, old_doc, nodep, new_document, NULL, /* options, unused */ 0);
if (UNEXPECTED(ret != 0)) {
diff --git a/ext/dom/tests/modern/spec/gh26111.phpt b/ext/dom/tests/modern/spec/gh26111.phpt
new file mode 100644
index 00000000000..4d295774f95
--- /dev/null
+++ b/ext/dom/tests/modern/spec/gh26111.phpt
@@ -0,0 +1,26 @@
+--TEST--
+GH-26111 (Dom\Document::adoptNode() of a DocumentFragment leaves its children on the old document's namespaces)
+--CREDITS--
+joemax-o360
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+$html = Dom\HTMLDocument::createFromString('<p>x</p>', LIBXML_NOERROR);
+$xml = Dom\XMLDocument::createFromString('<r/>');
+$fragment = $html->createDocumentFragment();
+$fragment->append($html->documentElement);
+$xml->adoptNode($fragment);
+unset($html);
+
+echo $fragment->firstChild->namespaceURI, PHP_EOL;
+$xml->documentElement->append($fragment);
+unset($fragment);
+echo $xml->saveXml(), PHP_EOL;
+
+?>
+--EXPECT--
+http://www.w3.org/1999/xhtml
+<?xml version="1.0" encoding="UTF-8"?>
+<r><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><p>x</p></body></html></r>