From 58cead1df19f43a0d2bd56d82e75edcaccc507cf Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 25 Oct 2024 18:59:45 +0200 Subject: [PATCH] Fix GH-16593: Assertion failure in DOM->replaceChild This is already forbidden by libxml, but this condition isn't properly checked; so the return value and lack of error makes it seem like it worked while it actually didn't. Furthermore, this can break assumptions and assertions later on. --- ext/dom/node.c | 7 +++++++ ext/dom/tests/gh16593.phpt | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 ext/dom/tests/gh16593.phpt diff --git a/ext/dom/node.c b/ext/dom/node.c index a8e3b035d14c1..188806564c69d 100644 --- a/ext/dom/node.c +++ b/ext/dom/node.c @@ -1093,6 +1093,13 @@ PHP_METHOD(DOMNode, replaceChild) RETURN_FALSE; } + /* This is already disallowed by libxml, but we should check it here to avoid + * breaking assumptions and assertions. */ + if ((oldchild->type == XML_ATTRIBUTE_NODE) != (newchild->type == XML_ATTRIBUTE_NODE)) { + php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror); + RETURN_FALSE; + } + if (oldchild->parent != nodep) { php_dom_throw_error(NOT_FOUND_ERR, stricterror); RETURN_FALSE; diff --git a/ext/dom/tests/gh16593.phpt b/ext/dom/tests/gh16593.phpt new file mode 100644 index 0000000000000..416d794a4f0d9 --- /dev/null +++ b/ext/dom/tests/gh16593.phpt @@ -0,0 +1,22 @@ +--TEST-- +GH-16593 (Assertion failure in DOM->replaceChild) +--EXTENSIONS-- +dom +--FILE-- +appendChild($doc->createElement('root')); +$child = $root->appendChild($doc->createElement('child')); +try { + $root->replaceChild($doc->createAttribute('foo'), $child); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $doc->saveXML(); + +?> +--EXPECT-- +Hierarchy Request Error + +