Skip to content

Commit e832b5e

Browse files
committed
[DOM] Fix getNamedItemNS() with empty URI not matching null namespace
Normalize an empty URI to NULL in spec-following mode so xmlHasNsProp() matches null-namespace attributes, and skip XML_ATTRIBUTE_DECL results which cannot be wrapped as nodes. Closes GH-23498
1 parent f09a81d commit e832b5e

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
return value of php_cli_server_client_send_through()). (Lazizbek Ergashev)
1414

1515
- DOM:
16+
. Fixed NamedNodeMap::getNamedItemNS() with an empty URI not matching
17+
the null namespace in spec-following mode. (Ilia Alshanetsky)
1618
. Fixed a use-after-free when cloning a DOMNameSpaceNode after
1719
DOMDocument::xinclude(). (iliaal)
1820
. Fixed bug GH-23331 (UAF when node_list_unlink() skips attribute children

‎ext/dom/namednodemap.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ PHP_METHOD(DOMNamedNodeMap, getNamedItemNS)
213213
objmap = (dom_nnodemap_object *)intern->ptr;
214214

215215
if (objmap != NULL) {
216+
if (urilen == 0 && objmap->baseobj != NULL && objmap->nodetype != XML_NOTATION_NODE && objmap->nodetype != XML_ENTITY_NODE && php_dom_follow_spec_intern(objmap->baseobj)) {
217+
uri = NULL;
218+
}
216219
if ((objmap->nodetype == XML_NOTATION_NODE) ||
217220
objmap->nodetype == XML_ENTITY_NODE) {
218221
if (objmap->ht) {
@@ -229,6 +232,9 @@ PHP_METHOD(DOMNamedNodeMap, getNamedItemNS)
229232
nodep = dom_object_get_node(objmap->baseobj);
230233
if (nodep) {
231234
itemnode = (xmlNodePtr)xmlHasNsProp(nodep, BAD_CAST named, BAD_CAST uri);
235+
if (itemnode != NULL && itemnode->type == XML_ATTRIBUTE_DECL) {
236+
itemnode = NULL;
237+
}
232238
}
233239
}
234240
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
getNamedItemNS() with an empty URI must look up the null namespace
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$d = new DOMDocument();
8+
$d->loadXML('<root xmlns:q="urn:q" bar="no-ns" q:bar="ns"/>');
9+
$a = $d->documentElement->attributes->getNamedItemNS('', 'bar');
10+
var_dump($a === null ? null : $a->nodeValue);
11+
$b = $d->documentElement->attributes->getNamedItemNS('urn:q', 'bar');
12+
var_dump($b === null ? null : $b->nodeValue);
13+
$d2 = Dom\XMLDocument::createFromString('<root xmlns:q="urn:q" bar="no-ns" q:bar="ns"/>');
14+
$a2 = $d2->documentElement->attributes->getNamedItemNS('', 'bar');
15+
var_dump($a2 === null ? null : $a2->nodeValue);
16+
var_dump($d2->documentElement->hasAttributeNS('', 'bar'));
17+
$c = $d2->documentElement->attributes->getNamedItemNS('urn:q', 'bar');
18+
var_dump($c === null ? null : $c->nodeValue);
19+
?>
20+
--EXPECT--
21+
NULL
22+
string(2) "ns"
23+
string(5) "no-ns"
24+
bool(true)
25+
string(2) "ns"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
getNamedItemNS() with empty URI must not throw on DTD default attributes
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$xml = <<<XML
8+
<?xml version="1.0"?>
9+
<!DOCTYPE root [
10+
<!ELEMENT root EMPTY>
11+
<!ATTLIST root defaulted CDATA "from-dtd">
12+
]>
13+
<root real="present"/>
14+
XML;
15+
16+
$el = Dom\XMLDocument::createFromString($xml)->documentElement;
17+
$defaulted = $el->attributes->getNamedItemNS('', 'defaulted');
18+
var_dump($defaulted === null ? null : $defaulted->nodeValue);
19+
$real = $el->attributes->getNamedItemNS('', 'real');
20+
var_dump($real === null ? null : $real->nodeValue);
21+
?>
22+
--EXPECT--
23+
NULL
24+
string(7) "present"

0 commit comments

Comments
 (0)