Fix DOMElement->prefix with empty string creates bogus prefix

Closes GH-12770.
This commit is contained in:
Niels Dossche 2023-11-24 15:14:24 +01:00
parent cc5a1bac6c
commit acbdfd24ec
2 changed files with 57 additions and 0 deletions

View file

@ -674,8 +674,14 @@ zend_result dom_node_prefix_write(dom_object *obj, zval *newval)
prefix_str = Z_STR_P(newval); prefix_str = Z_STR_P(newval);
prefix = ZSTR_VAL(prefix_str); prefix = ZSTR_VAL(prefix_str);
if (*prefix == '\0') {
/* The empty string namespace prefix does not exist.
* We should fall back to the default namespace in this case. */
prefix = NULL;
}
if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) { if (nsnode && nodep->ns != NULL && !xmlStrEqual(nodep->ns->prefix, (xmlChar *)prefix)) {
strURI = (char *) nodep->ns->href; strURI = (char *) nodep->ns->href;
/* Validate namespace naming constraints */
if (strURI == NULL || if (strURI == NULL ||
(zend_string_equals_literal(prefix_str, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) || (zend_string_equals_literal(prefix_str, "xml") && strcmp(strURI, (char *) XML_XML_NAMESPACE)) ||
(nodep->type == XML_ATTRIBUTE_NODE && zend_string_equals_literal(prefix_str, "xmlns") && (nodep->type == XML_ATTRIBUTE_NODE && zend_string_equals_literal(prefix_str, "xmlns") &&

View file

@ -0,0 +1,51 @@
--TEST--
DOMElement->prefix with empty string creates bogus prefix
--EXTENSIONS--
dom
--FILE--
<?php
$dom = new DOMDocument;
$dom->loadXML("<hello:container xmlns:conflict=\"urn:foo\" xmlns:hello=\"http://www.w3.org/1999/xhtml\"/>");
$container = $dom->documentElement;
echo "--- Changing the prefix to an empty string ---\n";
$container->prefix = "";
echo $dom->saveXML();
echo "--- Changing the prefix to an empty C-style string ---\n";
$container->prefix = "\0foobar";
echo $dom->saveXML();
echo "--- Changing the prefix to \"hello\" ---\n";
$container->prefix = "hello";
echo $dom->saveXML();
echo "--- Changing the prefix to that of a conflicting namespace (\"conflict\") ---\n";
try {
$container->prefix = "conflict";
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
echo $dom->saveXML();
?>
--EXPECT--
--- Changing the prefix to an empty string ---
<?xml version="1.0"?>
<container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>
--- Changing the prefix to an empty C-style string ---
<?xml version="1.0"?>
<container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>
--- Changing the prefix to "hello" ---
<?xml version="1.0"?>
<hello:container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>
--- Changing the prefix to that of a conflicting namespace ("conflict") ---
Namespace Error
<?xml version="1.0"?>
<hello:container xmlns:conflict="urn:foo" xmlns:hello="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"/>