Skip to content

Commit 494998d

Browse files
committed
Fix GH-21077: Accessing Dom\Node::baseURI can throw TypeError
Prior to this patch there was a common read handler, and it relied on the dom class set in the intern document. However, Dom\Implementation allows creating DTDs unassociated with a document, so we can't rely on an intern document and the check fails. This causes the ZVAL_NULL() path to be taken. To solve this, just split the handler.
1 parent 23f4b93 commit 494998d

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

ext/dom/dom_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ zend_result dom_modern_node_prefix_read(dom_object *obj, zval *retval);
134134
zend_result dom_node_prefix_write(dom_object *obj, zval *newval);
135135
zend_result dom_node_local_name_read(dom_object *obj, zval *retval);
136136
zend_result dom_node_base_uri_read(dom_object *obj, zval *retval);
137+
zend_result dom_modern_node_base_uri_read(dom_object *obj, zval *retval);
137138
zend_result dom_node_text_content_read(dom_object *obj, zval *retval);
138139
zend_result dom_node_text_content_write(dom_object *obj, zval *newval);
139140

ext/dom/node.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,14 +659,25 @@ zend_result dom_node_base_uri_read(dom_object *obj, zval *retval)
659659
ZVAL_STRING(retval, (const char *) baseuri);
660660
xmlFree(baseuri);
661661
} else {
662-
if (php_dom_follow_spec_intern(obj)) {
663-
if (nodep->doc->URL) {
664-
ZVAL_STRING(retval, (const char *) nodep->doc->URL);
665-
} else {
666-
ZVAL_STRING(retval, "about:blank");
667-
}
662+
ZVAL_NULL(retval);
663+
}
664+
665+
return SUCCESS;
666+
}
667+
668+
zend_result dom_modern_node_base_uri_read(dom_object *obj, zval *retval)
669+
{
670+
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
671+
672+
xmlChar *baseuri = xmlNodeGetBase(nodep->doc, nodep);
673+
if (baseuri) {
674+
ZVAL_STRING(retval, (const char *) baseuri);
675+
xmlFree(baseuri);
676+
} else {
677+
if (nodep->doc && nodep->doc->URL) {
678+
ZVAL_STRING(retval, (const char *) nodep->doc->URL);
668679
} else {
669-
ZVAL_NULL(retval);
680+
ZVAL_STRING(retval, "about:blank");
670681
}
671682
}
672683

ext/dom/php_dom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ PHP_MINIT_FUNCTION(dom)
892892
zend_hash_init(&dom_modern_node_prop_handlers, 0, NULL, NULL, true);
893893
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "nodeType", dom_node_node_type_read, NULL);
894894
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "nodeName", dom_node_node_name_read, NULL);
895-
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "baseURI", dom_node_base_uri_read, NULL);
895+
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "baseURI", dom_modern_node_base_uri_read, NULL);
896896
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "isConnected", dom_node_is_connected_read, NULL);
897897
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "ownerDocument", dom_node_owner_document_read, NULL);
898898
DOM_REGISTER_PROP_HANDLER(&dom_modern_node_prop_handlers, "parentNode", dom_node_parent_node_read, NULL);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GH-21077 (Accessing Dom\Node::baseURI can throw TypeError)
3+
--EXTENSIONS--
4+
dom
5+
--CREDITS--
6+
mbeccati
7+
--FILE--
8+
<?php
9+
10+
$implementation = new Dom\Implementation();
11+
$node = $implementation->createDocumentType('html', 'publicId', 'systemId');
12+
13+
var_dump($node->baseURI);
14+
15+
$dom = Dom\XMLDocument::createEmpty();
16+
$dom->append($node = $dom->importNode($node));
17+
18+
var_dump($dom->saveXML());
19+
20+
var_dump($node->baseURI);
21+
22+
?>
23+
--EXPECT--
24+
string(11) "about:blank"
25+
string(84) "<?xml version="1.0" encoding="UTF-8"?>
26+
<!DOCTYPE html PUBLIC "publicId" "systemId">
27+
"
28+
string(11) "about:blank"

ext/dom/tests/modern/spec/Document_implementation_createDocumentType.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ object(Dom\DocumentType)#3 (19) {
4343
["nodeName"]=>
4444
string(5) "qname"
4545
["baseURI"]=>
46-
NULL
46+
string(11) "about:blank"
4747
["isConnected"]=>
4848
bool(false)
4949
["parentNode"]=>
@@ -86,7 +86,7 @@ object(Dom\DocumentType)#2 (19) {
8686
["nodeName"]=>
8787
string(5) "qname"
8888
["baseURI"]=>
89-
NULL
89+
string(11) "about:blank"
9090
["isConnected"]=>
9191
bool(false)
9292
["parentNode"]=>
@@ -129,7 +129,7 @@ object(Dom\DocumentType)#1 (19) {
129129
["nodeName"]=>
130130
string(5) "qname"
131131
["baseURI"]=>
132-
NULL
132+
string(11) "about:blank"
133133
["isConnected"]=>
134134
bool(false)
135135
["parentNode"]=>
@@ -172,7 +172,7 @@ object(Dom\DocumentType)#4 (19) {
172172
["nodeName"]=>
173173
string(5) "qname"
174174
["baseURI"]=>
175-
NULL
175+
string(11) "about:blank"
176176
["isConnected"]=>
177177
bool(false)
178178
["parentNode"]=>

ext/dom/tests/modern/xml/DTDNamedNodeMap.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ object(Dom\Notation)#4 (13) {
148148
["nodeName"]=>
149149
string(3) "GIF"
150150
["baseURI"]=>
151-
NULL
151+
string(11) "about:blank"
152152
["isConnected"]=>
153153
bool(false)
154154
["parentNode"]=>

0 commit comments

Comments
 (0)