Skip to content

Commit 30a0b03

Browse files
committed
Fix references not handled correctly in C14N
Closes GH-14090.
1 parent e878b9f commit 30a0b03

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PHP NEWS
55
- DOM:
66
. Fix crashes when entity declaration is removed while still having entity
77
references. (nielsdos)
8+
. Fix references not handled correctly in C14N. (nielsdos)
89

910
09 May 2024, PHP 8.2.19
1011

ext/dom/node.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
16141614
zval *tmp;
16151615
char *xquery;
16161616

1617-
tmp = zend_hash_str_find(ht, "query", sizeof("query")-1);
1617+
tmp = zend_hash_str_find_deref(ht, "query", sizeof("query")-1);
16181618
if (!tmp) {
16191619
/* if mode == 0 then $xpath arg is 3, if mode == 1 then $xpath is 4 */
16201620
zend_argument_value_error(3 + mode, "must have a \"query\" key");
@@ -1630,12 +1630,13 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
16301630
ctxp = xmlXPathNewContext(docp);
16311631
ctxp->node = nodep;
16321632

1633-
tmp = zend_hash_str_find(ht, "namespaces", sizeof("namespaces")-1);
1633+
tmp = zend_hash_str_find_deref(ht, "namespaces", sizeof("namespaces")-1);
16341634
if (tmp && Z_TYPE_P(tmp) == IS_ARRAY && !HT_IS_PACKED(Z_ARRVAL_P(tmp))) {
16351635
zval *tmpns;
16361636
zend_string *prefix;
16371637

16381638
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), prefix, tmpns) {
1639+
ZVAL_DEREF(tmpns);
16391640
if (Z_TYPE_P(tmpns) == IS_STRING) {
16401641
if (prefix) {
16411642
xmlXPathRegisterNs(ctxp, (xmlChar *) ZSTR_VAL(prefix), (xmlChar *) Z_STRVAL_P(tmpns));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Test: Canonicalization - C14N() with references
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
// Adapted from canonicalization.phpt
8+
9+
$xml = <<<EOXML
10+
<?xml version="1.0" encoding="ISO-8859-1" ?>
11+
<foo xmlns="http://www.example.com/ns/foo"
12+
xmlns:fubar="http://www.example.com/ns/fubar" xmlns:test="urn::test"><contain>
13+
<bar><test1 /></bar>
14+
<bar><test2 /></bar>
15+
<fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3 /></fubar:bar>
16+
<fubar:bar><test4 /></fubar:bar>
17+
</contain>
18+
</foo>
19+
EOXML;
20+
21+
$dom = new DOMDocument();
22+
$dom->loadXML($xml);
23+
$doc = $dom->documentElement->firstChild;
24+
25+
$xpath = [
26+
'query' => '(//a:contain | //a:bar | .//namespace::*)',
27+
'namespaces' => ['a' => 'http://www.example.com/ns/foo'],
28+
];
29+
$prefixes = ['test'];
30+
31+
foreach ($xpath['namespaces'] as $k => &$v);
32+
unset($v);
33+
foreach ($xpath as $k => &$v);
34+
unset($v);
35+
foreach ($prefixes as $k => &$v);
36+
unset($v);
37+
38+
echo $doc->C14N(true, false, $xpath, $prefixes);
39+
?>
40+
--EXPECT--
41+
<contain xmlns="http://www.example.com/ns/foo"><bar></bar><bar></bar></contain>

0 commit comments

Comments
 (0)