Skip to content

Commit 2814290

Browse files
committed
Fix GH-20444: Dom\XMLDocument::C14N() seems broken compared to DOMDocument::C14N()
1 parent ca084ac commit 2814290

File tree

4 files changed

+187
-12
lines changed

4 files changed

+187
-12
lines changed

ext/dom/node.c

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,82 @@ PHP_METHOD(DOMNode, lookupNamespaceURI)
20812081
}
20822082
/* }}} end dom_node_lookup_namespace_uri */
20832083

2084+
static void dom_relink_ns_decls_element(HashTable *links, xmlNodePtr node)
2085+
{
2086+
if (node->type == XML_ELEMENT_NODE) {
2087+
for (xmlAttrPtr attr = node->properties; attr; attr = attr->next) {
2088+
if (php_dom_ns_is_fast((const xmlNode *) attr, php_dom_ns_is_xmlns_magic_token)) {
2089+
xmlNsPtr ns = xmlMalloc(sizeof(*ns));
2090+
if (!ns) {
2091+
continue;
2092+
}
2093+
2094+
bool should_free;
2095+
xmlChar *attr_value = php_libxml_attr_value(attr, &should_free);
2096+
2097+
memset(ns, 0, sizeof(*ns));
2098+
ns->type = XML_NAMESPACE_DECL;
2099+
ns->href = should_free ? attr_value : xmlStrdup(attr_value);
2100+
ns->prefix = attr->ns->prefix ? xmlStrdup(attr->name) : NULL;
2101+
ns->next = node->nsDef;
2102+
node->nsDef = ns;
2103+
2104+
ns->_private = attr;
2105+
if (attr->prev) {
2106+
attr->prev = attr->next;
2107+
} else {
2108+
node->properties = attr->next;
2109+
}
2110+
if (attr->next) {
2111+
attr->next->prev = attr->prev;
2112+
}
2113+
2114+
zval *zv = zend_hash_index_lookup(links, (zend_ulong) node);
2115+
if (Z_ISNULL_P(zv)) {
2116+
ZVAL_LONG(zv, 1);
2117+
} else {
2118+
Z_LVAL_P(zv)++;
2119+
}
2120+
}
2121+
}
2122+
}
2123+
}
2124+
2125+
static void dom_relink_ns_decls(HashTable *links, xmlNodePtr root)
2126+
{
2127+
dom_relink_ns_decls_element(links, root);
2128+
2129+
xmlNodePtr base = root;
2130+
xmlNodePtr node = base->children;
2131+
while (node != NULL) {
2132+
dom_relink_ns_decls_element(links, node);
2133+
node = php_dom_next_in_tree_order(node, base);
2134+
}
2135+
}
2136+
2137+
static void dom_unlink_ns_decls(HashTable *links)
2138+
{
2139+
ZEND_HASH_MAP_FOREACH_NUM_KEY_VAL(links, zend_ulong h, zval *counter) {
2140+
xmlNodePtr node = (xmlNodePtr) h;
2141+
while (Z_LVAL_P(counter)-- > 0) {
2142+
xmlNsPtr ns = node->nsDef;
2143+
node->nsDef = node->nsDef->next;
2144+
2145+
xmlAttrPtr attr = ns->_private;
2146+
if (attr->prev) {
2147+
attr->prev->next = attr;
2148+
} else {
2149+
node->properties = attr;
2150+
}
2151+
if (attr->next) {
2152+
attr->next->prev = attr;
2153+
}
2154+
2155+
xmlFreeNs(ns);
2156+
}
2157+
} ZEND_HASH_FOREACH_END();
2158+
}
2159+
20842160
static int dom_canonicalize_node_parent_lookup_cb(void *user_data, xmlNodePtr node, xmlNodePtr parent)
20852161
{
20862162
xmlNodePtr root = user_data;
@@ -2136,7 +2212,23 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
21362212

21372213
docp = nodep->doc;
21382214

2139-
if (! docp) {
2215+
HashTable links;
2216+
bool modern = php_dom_follow_spec_node(nodep);
2217+
if (modern) {
2218+
xmlNodePtr root = nodep;
2219+
while (root->parent) {
2220+
root = root->parent;
2221+
}
2222+
2223+
if (UNEXPECTED(root->type != XML_DOCUMENT_NODE && root->type != XML_HTML_DOCUMENT_NODE)) {
2224+
php_dom_throw_error_with_message(HIERARCHY_REQUEST_ERR, "Canonicalization can only happen on nodes attached to a document.", /* strict */ true);
2225+
RETURN_THROWS();
2226+
}
2227+
2228+
zend_hash_init(&links, 0, NULL, NULL, false);
2229+
dom_relink_ns_decls(&links, xmlDocGetRootElement(docp));
2230+
} else if (!docp) {
2231+
/* Note: not triggerable with modern DOM */
21402232
zend_throw_error(NULL, "Node must be associated with a document");
21412233
RETURN_THROWS();
21422234
}
@@ -2158,12 +2250,12 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
21582250
if (!tmp) {
21592251
/* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
21602252
zend_argument_value_error(3 + mode, "must have a \"query\" key");
2161-
RETURN_THROWS();
2253+
goto clean_links;
21622254
}
21632255
if (Z_TYPE_P(tmp) != IS_STRING) {
21642256
/* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
21652257
zend_argument_type_error(3 + mode, "\"query\" option must be a string, %s given", zend_zval_value_name(tmp));
2166-
RETURN_THROWS();
2258+
goto clean_links;
21672259
}
21682260
xquery = Z_STRVAL_P(tmp);
21692261

@@ -2195,7 +2287,7 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
21952287
}
21962288
xmlXPathFreeContext(ctxp);
21972289
zend_throw_error(NULL, "XPath query did not return a nodeset");
2198-
RETURN_THROWS();
2290+
goto clean_links;
21992291
}
22002292
}
22012293

@@ -2264,6 +2356,12 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
22642356
RETURN_LONG(bytes);
22652357
}
22662358
}
2359+
2360+
clean_links:
2361+
if (modern) {
2362+
dom_unlink_ns_decls(&links);
2363+
zend_hash_destroy(&links);
2364+
}
22672365
}
22682366
/* }}} */
22692367

ext/dom/tests/canonicalization.phpt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,46 @@ $dom = new DOMDocument();
2121
$dom->loadXML($xml);
2222
$doc = $dom->documentElement->firstChild;
2323

24+
$newDom = Dom\XMLDocument::createFromString($xml);
25+
$newDoc = $newDom->documentElement->firstChild;
26+
$counter = 0;
27+
28+
function check($doc, $newDoc, ...$args) {
29+
global $counter;
30+
$counter++;
31+
echo $doc->C14N(...$args)."\n\n";
32+
if ($doc->C14N(...$args) !== $newDoc->C14N(...$args)) {
33+
var_dump($doc->C14N(...$args), $newDoc->C14N(...$args));
34+
throw new Error("mismatch: $counter");
35+
}
36+
}
37+
2438
/* inclusive/without comments first child element of doc element is context. */
25-
echo $doc->C14N()."\n\n";
39+
check($doc, $newDoc);
2640

2741
/* exclusive/without comments first child element of doc element is context. */
28-
echo $doc->c14N(TRUE)."\n\n";
42+
check($doc, $newDoc, TRUE);
2943

3044
/* inclusive/with comments first child element of doc element is context. */
31-
echo $doc->C14N(FALSE, TRUE)."\n\n";
45+
check($doc, $newDoc, FALSE, TRUE);
3246

3347
/* exclusive/with comments first child element of doc element is context. */
34-
echo $doc->C14N(TRUE, TRUE)."\n\n";
48+
check($doc, $newDoc, TRUE, TRUE);
3549

3650
/* exclusive/without comments using xpath query. */
37-
echo $doc->c14N(TRUE, FALSE, array('query'=>'(//. | //@* | //namespace::*)'))."\n\n";
51+
check($doc, $newDoc, TRUE, FALSE, array('query'=>'(//. | //@* | //namespace::*)'))."\n\n";
3852

3953
/* exclusive/without comments first child element of doc element is context.
4054
using xpath query with registered namespace.
4155
test namespace prefix is also included. */
42-
echo $doc->c14N(TRUE, FALSE,
56+
check($doc, $newDoc, TRUE, FALSE,
4357
array('query'=>'(//a:contain | //a:bar | .//namespace::*)',
4458
'namespaces'=>array('a'=>'http://www.example.com/ns/foo')),
45-
array('test'))."\n\n";
59+
array('test'));
4660

4761
/* exclusive/without comments first child element of doc element is context.
4862
test namespace prefix is also included */
49-
echo $doc->C14N(TRUE, FALSE, NULL, array('test'));
63+
check($doc, $newDoc, TRUE, FALSE, NULL, array('test'));
5064
?>
5165
--EXPECT--
5266
<contain xmlns="http://www.example.com/ns/foo" xmlns:fubar="http://www.example.com/ns/fubar" xmlns:test="urn::test">
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Canonicalize unattached node should fail
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$d = \Dom\XMLDocument::createFromString('<root><child/></root>');
9+
$child = $d->documentElement->firstChild;
10+
$child->remove();
11+
12+
try {
13+
$child->C14N();
14+
} catch (Dom\DOMException $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
18+
?>
19+
--EXPECT--
20+
Canonicalization can only happen on nodes attached to a document.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
GH-20444 (Dom\XMLDocument::C14N() seems broken compared to DOMDocument::C14N())
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$xml = <<<EOF
9+
<?xml version="1.0" encoding="UTF-8"?>
10+
<test:root xmlns:test="http://example.com/dummy/ns">
11+
<test:a xmlns:test="http://example.com/dummy/ns"/>
12+
<test:b test:kind="123">abc</test:b>
13+
</test:root>
14+
EOF;
15+
16+
$d = \Dom\XMLDocument::createFromString($xml);
17+
var_dump($d->C14N(true));
18+
19+
$xml = <<<EOF
20+
<?xml version="1.0" encoding="UTF-8"?>
21+
<ns1:root xmlns:ns1="http://example.com/dummy/ns">
22+
<ns1:a/>
23+
<ns1:b>
24+
<ns1:c>123</ns1:c>
25+
</ns1:b>
26+
</ns1:root>
27+
EOF;
28+
29+
$d = \Dom\XMLDocument::createFromString($xml);
30+
var_dump($d->C14N());
31+
32+
?>
33+
--EXPECT--
34+
string(128) "<test:root xmlns:test="http://example.com/dummy/ns">
35+
<test:a></test:a>
36+
<test:b test:kind="123">abc</test:b>
37+
</test:root>"
38+
string(134) "<ns1:root xmlns:ns1="http://example.com/dummy/ns">
39+
<ns1:a></ns1:a>
40+
<ns1:b>
41+
<ns1:c>123</ns1:c>
42+
</ns1:b>
43+
</ns1:root>"

0 commit comments

Comments
 (0)