Commit f60216c87aa for php
commit f60216c87aa6a747614d76cd8eacf4afd3f7310e
Author: imattas <ian@mattas.net>
Date: Wed Nov 25 13:21:45 2026 -0500
ext/dom: Guard namespace prefix lookup for non-element nodes
Dom\Node::lookupPrefix() can reach the namespace-prefix helper with a
non-element parent for text nodes inside DTD entity declarations. The
helper reads element-specific namespace and attribute fields before
checking the node type. Return NULL for non-elements and add a
regression test for this case.
Close GH-25762
diff --git a/NEWS b/NEWS
index 30afb9e1238..8fe84a4176d 100644
--- a/NEWS
+++ b/NEWS
@@ -75,6 +75,8 @@ PHP NEWS
(Ian Mattas)
. Fixed bug GH-25750 (adoptNode() accepts DTD declaration nodes, which can
crash at document teardown). (Ian Mattas)
+ . Fixed crash in Dom\Node::lookupPrefix() on a text node inside a DTD entity
+ declaration. (Ian Mattas)
- FFI:
. Fixed bug GH-25558 (struct with an enum field passed by value is described
diff --git a/ext/dom/node.c b/ext/dom/node.c
index e982cd6a13b..9397abc850c 100644
--- a/ext/dom/node.c
+++ b/ext/dom/node.c
@@ -1862,6 +1862,10 @@ PHP_METHOD(Dom_Node, isEqualNode)
/* https://dom.spec.whatwg.org/#locate-a-namespace-prefix */
static const xmlChar *dom_locate_a_namespace_prefix(xmlNodePtr elem, const char *uri)
{
+ if (elem == NULL || elem->type != XML_ELEMENT_NODE) {
+ return NULL;
+ }
+
do {
/* 1. If element’s namespace is namespace and its namespace prefix is non-null, then return its namespace prefix. */
if (elem->ns != NULL && elem->ns->prefix != NULL && xmlStrEqual(elem->ns->href, BAD_CAST uri)) {
diff --git a/ext/dom/tests/modern/spec/Node_lookupPrefix.phpt b/ext/dom/tests/modern/spec/Node_lookupPrefix.phpt
index 913ede69cec..087286d1ee1 100644
--- a/ext/dom/tests/modern/spec/Node_lookupPrefix.phpt
+++ b/ext/dom/tests/modern/spec/Node_lookupPrefix.phpt
@@ -45,6 +45,17 @@
var_dump($x->lookupPrefix("test"));
}
+echo "--- text node inside DTD entity declaration ---\n";
+
+$dom = Dom\XMLDocument::createFromString(<<<XML
+<?xml version="1.0"?>
+<!DOCTYPE r [<!ENTITY ent "E">]>
+<r>&ent;</r>
+XML);
+
+$text = $dom->doctype->childNodes->item(0)->childNodes->item(0);
+var_dump($text->lookupPrefix("urn:p"));
+
?>
--EXPECT--
--- NULL case because invalid node type ---
@@ -62,3 +73,5 @@
string(1) "y"
NULL
string(1) "x"
+--- text node inside DTD entity declaration ---
+NULL