Skip to content

Commit 7c6c8e1

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-16535: UAF when using document as a child Fix GH-16533: Segfault when adding attribute to parent that is not an element
2 parents 50acf5e + a026692 commit 7c6c8e1

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

ext/dom/node.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,17 @@ static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNode
864864
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
865865
return false;
866866
}
867+
/* Attributes must be in elements. */
868+
if (child->type == XML_ATTRIBUTE_NODE && parentp->type != XML_ELEMENT_NODE) {
869+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
870+
return false;
871+
}
872+
873+
/* Documents can never be a child. */
874+
if (child->type == XML_DOCUMENT_NODE || child->type == XML_HTML_DOCUMENT_NODE) {
875+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
876+
return false;
877+
}
867878

868879
return true;
869880
}

ext/dom/tests/gh16533.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-16533 (Segfault when adding attribute to parent that is not an element)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument();
9+
try {
10+
$doc->appendChild($doc->createAttribute('foo'));
11+
} catch (DOMException $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
15+
echo $doc->saveXML();
16+
17+
?>
18+
--EXPECT--
19+
Hierarchy Request Error
20+
<?xml version="1.0"?>

ext/dom/tests/gh16535.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-16535 (UAF when using document as a child)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$v2 = new DOMDocument("t");
9+
10+
$v2->loadHTML("t");
11+
$v4 = $v2->createElement('foo');
12+
try {
13+
$v4->appendChild($v2);
14+
} catch (DOMException $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
$v2->loadHTML("oU");
18+
echo $v2->saveXML();
19+
20+
?>
21+
--EXPECT--
22+
Hierarchy Request Error
23+
<?xml version="1.0" standalone="yes"?>
24+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
25+
<html><body><p>oU</p></body></html>

0 commit comments

Comments
 (0)