Skip to content

Fix references not handled correctly in C14N #14090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ static void dom_canonicalization(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{
zval *tmp;
char *xquery;

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

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

ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), prefix, tmpns) {
ZVAL_DEREF(tmpns);
if (Z_TYPE_P(tmpns) == IS_STRING) {
if (prefix) {
xmlXPathRegisterNs(ctxp, (xmlChar *) ZSTR_VAL(prefix), (xmlChar *) Z_STRVAL_P(tmpns));
Expand Down
41 changes: 41 additions & 0 deletions ext/dom/tests/DOMNode_C14N_references.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Test: Canonicalization - C14N() with references
--EXTENSIONS--
dom
--FILE--
<?php
// Adapted from canonicalization.phpt

$xml = <<<EOXML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<foo xmlns="http://www.example.com/ns/foo"
xmlns:fubar="http://www.example.com/ns/fubar" xmlns:test="urn::test"><contain>
<bar><test1 /></bar>
<bar><test2 /></bar>
<fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3 /></fubar:bar>
<fubar:bar><test4 /></fubar:bar>
</contain>
</foo>
EOXML;

$dom = new DOMDocument();
$dom->loadXML($xml);
$doc = $dom->documentElement->firstChild;

$xpath = [
'query' => '(//a:contain | //a:bar | .//namespace::*)',
'namespaces' => ['a' => 'http://www.example.com/ns/foo'],
];
$prefixes = ['test'];

foreach ($xpath['namespaces'] as $k => &$v);
unset($v);
foreach ($xpath as $k => &$v);
unset($v);
foreach ($prefixes as $k => &$v);
unset($v);

echo $doc->C14N(true, false, $xpath, $prefixes);
?>
--EXPECT--
<contain xmlns="http://www.example.com/ns/foo"><bar></bar><bar></bar></contain>
Loading