Skip to content

Commit 5ccb145

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: ext/dom: fix UAF when setting an attribute colliding by local name.
2 parents 786e423 + 6e74914 commit 5ccb145

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

ext/dom/element.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ static void dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAMETERS,
720720
nsp = attrp->ns;
721721
if (use_ns && nsp != NULL) {
722722
existattrp = xmlHasNsProp(nodep, attrp->name, nsp->href);
723+
} else if (nsp == NULL) {
724+
existattrp = xmlHasNsProp(nodep, attrp->name, NULL);
723725
} else {
724726
existattrp = xmlHasProp(nodep, attrp->name);
725727
}

ext/dom/node.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_obj
970970
xmlAttrPtr lastattr;
971971

972972
if (child->ns == NULL)
973-
lastattr = xmlHasProp(refp->parent, child->name);
973+
lastattr = xmlHasNsProp(refp->parent, child->name, NULL);
974974
else
975975
lastattr = xmlHasNsProp(refp->parent, child->name, child->ns->href);
976976
if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
@@ -1017,7 +1017,7 @@ static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_obj
10171017
xmlAttrPtr lastattr;
10181018

10191019
if (child->ns == NULL)
1020-
lastattr = xmlHasProp(parentp, child->name);
1020+
lastattr = xmlHasNsProp(parentp, child->name, NULL);
10211021
else
10221022
lastattr = xmlHasNsProp(parentp, child->name, child->ns->href);
10231023
if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {
@@ -1379,7 +1379,7 @@ static void dom_node_append_child_legacy(zval *return_value, dom_object *intern,
13791379
xmlAttrPtr lastattr;
13801380

13811381
if (child->ns == NULL)
1382-
lastattr = xmlHasProp(nodep, child->name);
1382+
lastattr = xmlHasNsProp(nodep, child->name, NULL);
13831383
else
13841384
lastattr = xmlHasNsProp(nodep, child->name, child->ns->href);
13851385
if (lastattr != NULL && lastattr->type != XML_ATTRIBUTE_DECL) {

ext/dom/tests/gh22447.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-22447 (UAF at dom_objects_free_storage when setAttributeNode collides with a namespaced attribute of the same local name)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$dom = Dom\HTMLDocument::createEmpty();
8+
9+
$attribute1 = $dom->createAttribute("my-attribute");
10+
$container = $dom->appendChild($dom->createElement("container"));
11+
$attribute2 = $dom->createAttribute("my-attribute");
12+
$attribute4 = $dom->createAttributeNS("urn:a", "my-attribute");
13+
14+
$container->setAttributeNode($attribute1);
15+
$container->setAttributeNode($attribute4);
16+
17+
var_dump($container->setAttributeNode($attribute2) === $attribute1);
18+
var_dump($container->setAttributeNode($attribute1) === $attribute2);
19+
20+
echo $dom->saveXml($container), PHP_EOL;
21+
?>
22+
--EXPECT--
23+
bool(true)
24+
bool(true)
25+
<container xmlns="http://www.w3.org/1999/xhtml" xmlns:ns1="urn:a" ns1:my-attribute="" my-attribute=""></container>

0 commit comments

Comments
 (0)