[php-src] master: Merge branch 'PHP-8.6'
| From: | Ilia Alshanetsky | Date: | Mon, 23 Nov 2026 21:15:13 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144088@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-11-23T16:12:26-05:00
Commit: https://github.com/php/php-src/commit/8904d87f04c31b6ada6d8fc38884a2c3b9d45bc1
Raw diff: https://github.com/php/php-src/commit/8904d87f04c31b6ada6d8fc38884a2c3b9d45bc1.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
ext/dom: Fix use-after-free in getElementById() after removing an ID attribute
Changed paths:
A ext/dom/tests/getElementById_removed_id_attribute.phpt
M ext/libxml/libxml.c
Diff:
diff --git a/ext/dom/tests/getElementById_removed_id_attribute.phpt
b/ext/dom/tests/getElementById_removed_id_attribute.phpt
new file mode 100644
index 000000000000..56930a0b06e9
--- /dev/null
+++ b/ext/dom/tests/getElementById_removed_id_attribute.phpt
@@ -0,0 +1,56 @@
+--TEST--
+getElementById() after a removed ID attribute is freed
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+$doc = new DOMDocument();
+$doc->loadXML('<root><a xml:id="x"/><b
xml:id="y"/></root>');
+$a = $doc->documentElement->firstChild;
+$attr = $a->getAttributeNode('xml:id');
+$a->removeAttributeNode($attr);
+unset($attr);
+echo "removed id: ";
+var_dump($doc->getElementById('x'));
+echo "remaining id: ", $doc->getElementById('y')->nodeName,
"\n";
+
+$doc = new DOMDocument();
+$doc->loadXML('<!DOCTYPE root [<!ATTLIST e id ID #IMPLIED>]><root><e
id="x"/><e id="y"/></root>');
+$e = $doc->documentElement->firstChild;
+$attr = $e->getAttributeNode('id');
+$e->removeAttributeNode($attr);
+unset($attr);
+echo "removed DTD id: ";
+var_dump($doc->getElementById('x'));
+echo "remaining DTD id: ", $doc->getElementById('y')->nodeName,
"\n";
+
+$doc = Dom\XMLDocument::createFromString('<root><a xml:id="x"/><b
xml:id="y"/></root>');
+$a = $doc->documentElement->firstChild;
+$attr = $a->getAttributeNode('xml:id');
+$a->removeAttributeNode($attr);
+unset($attr);
+echo "removed Dom\\XMLDocument id: ";
+var_dump($doc->getElementById('x'));
+echo "remaining Dom\\XMLDocument id: ",
$doc->getElementById('y')->nodeName, "\n";
+
+$doc = new DOMDocument();
+$doc->loadXML('<root><a xml:id="x"/><b
xml:id="y"/></root>');
+$a = $doc->documentElement->firstChild;
+$attr = $a->getAttributeNode('xml:id');
+$a->removeAttribute('xml:id');
+unset($attr);
+echo "removeAttribute() id: ";
+var_dump($doc->getElementById('x'));
+echo "remaining removeAttribute() id: ",
$doc->getElementById('y')->nodeName, "\n";
+
+?>
+--EXPECT--
+removed id: NULL
+remaining id: b
+removed DTD id: NULL
+remaining DTD id: e
+removed Dom\XMLDocument id: NULL
+remaining Dom\XMLDocument id: b
+removeAttribute() id: NULL
+remaining removeAttribute() id: b
diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c
index d09ec299ff3c..f9ce4c5310e8 100644
--- a/ext/libxml/libxml.c
+++ b/ext/libxml/libxml.c
@@ -1448,6 +1448,9 @@ PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node)
break;
default:
if (node->parent == NULL || node->type == XML_NAMESPACE_DECL) {
+ if (node->type == XML_ATTRIBUTE_NODE && node->doc != NULL &&
((xmlAttrPtr) node)->atype == XML_ATTRIBUTE_ID) {
+ xmlRemoveID(node->doc, (xmlAttrPtr) node);
+ }
php_libxml_node_free_list((xmlNodePtr) node->children);
if (node->type == XML_ELEMENT_NODE) {
php_libxml_node_free_list((xmlNodePtr) node->properties);