Skip to content

Commit ed21ebd

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix GH-16595: Another UAF in DOM -> cloneNode Fix GH-16593: Assertion failure in DOM->replaceChild
2 parents 7e5ed47 + 9d8983c commit ed21ebd

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ PHP NEWS
4141
. Fixed bug GH-16533 (Segfault when adding attribute to parent that is not
4242
an element). (nielsdos)
4343
. Fixed bug GH-16535 (UAF when using document as a child). (nielsdos)
44+
. Fixed bug GH-16593 (Assertion failure in DOM->replaceChild). (nielsdos)
45+
. Fixed bug GH-16595 (Another UAF in DOM -> cloneNode). (nielsdos)
4446

4547
- EXIF:
4648
. Fixed bug GH-16409 (Segfault in exif_thumbnail when not dealing with a

ext/dom/node.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNode
990990
PHP_METHOD(DOMNode, insertBefore)
991991
{
992992
zval *id, *node, *ref = NULL;
993-
xmlNodePtr child, new_child, parentp, refp;
993+
xmlNodePtr child, new_child, parentp, refp = NULL;
994994
dom_object *intern, *childobj, *refpobj;
995995
int ret, stricterror;
996996

@@ -1015,19 +1015,21 @@ PHP_METHOD(DOMNode, insertBefore)
10151015
RETURN_FALSE;
10161016
}
10171017

1018-
if (child->doc == NULL && parentp->doc != NULL) {
1019-
dom_set_document_ref_pointers(child, intern->document);
1020-
}
1021-
1022-
php_libxml_invalidate_node_list_cache(intern->document);
1023-
10241018
if (ref != NULL) {
10251019
DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj);
10261020
if (refp->parent != parentp) {
10271021
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
10281022
RETURN_FALSE;
10291023
}
1024+
}
1025+
1026+
if (child->doc == NULL && parentp->doc != NULL) {
1027+
dom_set_document_ref_pointers(child, intern->document);
1028+
}
10301029

1030+
php_libxml_invalidate_node_list_cache(intern->document);
1031+
1032+
if (ref != NULL) {
10311033
if (child->parent != NULL) {
10321034
xmlUnlinkNode(child);
10331035
}
@@ -1173,6 +1175,13 @@ PHP_METHOD(DOMNode, replaceChild)
11731175
RETURN_FALSE;
11741176
}
11751177

1178+
/* This is already disallowed by libxml, but we should check it here to avoid
1179+
* breaking assumptions and assertions. */
1180+
if ((oldchild->type == XML_ATTRIBUTE_NODE) != (newchild->type == XML_ATTRIBUTE_NODE)) {
1181+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1182+
RETURN_FALSE;
1183+
}
1184+
11761185
if (oldchild->parent != nodep) {
11771186
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
11781187
RETURN_FALSE;

ext/dom/tests/gh16593.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-16593 (Assertion failure in DOM->replaceChild)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument;
9+
$root = $doc->appendChild($doc->createElement('root'));
10+
$child = $root->appendChild($doc->createElement('child'));
11+
try {
12+
$root->replaceChild($doc->createAttribute('foo'), $child);
13+
} catch (DOMException $e) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
echo $doc->saveXML();
17+
18+
?>
19+
--EXPECT--
20+
Hierarchy Request Error
21+
<?xml version="1.0"?>
22+
<root><child/></root>

0 commit comments

Comments
 (0)