Commit 5835cf23255 for php
commit 5835cf23255fa28e57a9ac53662aa4314b80cc45
Author: imattas <ian@mattas.net>
Date: Wed Nov 25 13:19:45 2026 -0500
ext/dom: Reject DTD declaration nodes in adoptNode()
Element, attribute-list, and entity declarations remain owned by their
DTD, but adoptNode() unlinked them and moved them anyway, which can crash
when the source document is freed. Reject them with NotSupportedError, as
adoptNode() already does for entity and notation nodes. This also applies
to DOMDocument::adoptNode(), which returned false for them.
Fixes GH-25750
Closes GH-25758
diff --git a/NEWS b/NEWS
index d982a1008b1..8e49c3bf062 100644
--- a/NEWS
+++ b/NEWS
@@ -69,6 +69,8 @@ PHP NEWS
freed. (Ilia Alshanetsky)
. Fixed bug GH-25752 (Reading Dom\Notation publicId or systemId can crash).
(Ian Mattas)
+ . Fixed bug GH-25750 (adoptNode() accepts DTD declaration nodes, which can
+ crash at document teardown). (Ian Mattas)
- FFI:
. Fixed bug GH-25558 (struct with an enum field passed by value is described
diff --git a/ext/dom/document.c b/ext/dom/document.c
index c7dd8a0c7a0..3d327f3f4f4 100644
--- a/ext/dom/document.c
+++ b/ext/dom/document.c
@@ -1213,6 +1213,9 @@ static void dom_document_adopt_node(INTERNAL_FUNCTION_PARAMETERS, bool modern)
|| nodep->type == XML_DOCUMENT_TYPE_NODE
|| nodep->type == XML_DTD_NODE
|| nodep->type == XML_ENTITY_NODE
+ || nodep->type == XML_ELEMENT_DECL
+ || nodep->type == XML_ATTRIBUTE_DECL
+ || nodep->type == XML_ENTITY_DECL
|| nodep->type == XML_NOTATION_NODE)) {
php_dom_throw_error(NOT_SUPPORTED_ERR, dom_get_strict_error(dom_object_nodep->document));
RETURN_FALSE;
diff --git a/ext/dom/tests/modern/spec/XMLDocument_adoptNode_dtd_declarations.phpt b/ext/dom/tests/modern/spec/XMLDocument_adoptNode_dtd_declarations.phpt
new file mode 100644
index 00000000000..3ffa3dff700
--- /dev/null
+++ b/ext/dom/tests/modern/spec/XMLDocument_adoptNode_dtd_declarations.phpt
@@ -0,0 +1,27 @@
+--TEST--
+Dom\Document::adoptNode() rejects DTD declarations
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+$xml = '<!DOCTYPE root [<!ELEMENT root EMPTY><!ENTITY ent "value">]><root/>';
+$source = Dom\XMLDocument::createFromString($xml);
+$target = Dom\XMLDocument::createFromString('<target/>');
+
+$nodes = [
+ $source->doctype->childNodes->item(0),
+ $source->doctype->entities->item(0),
+];
+
+foreach ($nodes as $node) {
+ try {
+ $target->adoptNode($node);
+ echo "adopted\n";
+ } catch (DOMException $e) {
+ echo $e::class, ": ", $e->getCode(), "\n";
+ }
+}
+?>
+--EXPECT--
+DOMException: 9
+DOMException: 9