[php-src] PHP-8.3: Fix references not handled correctly in C14N
| From: | Niels Dossche | Date: | Tue, 30 Apr 2024 20:53:42 +0000 |
| Subject: | [php-src] PHP-8.3: Fix references not handled correctly in C14N | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-128279@lists.php.net to get a copy of this message | ||
Author: Niels Dossche (nielsdos)
Date: 2024-04-30T22:30:28+02:00
Commit: https://github.com/php/php-src/commit/30a0b0359ed8338c0e3acd1682de3cf96429e898
Raw diff: https://github.com/php/php-src/commit/30a0b0359ed8338c0e3acd1682de3cf96429e898.diff
Fix references not handled correctly in C14N
Closes GH-14090.
Changed paths:
A ext/dom/tests/DOMNode_C14N_references.phpt
M NEWS
M ext/dom/node.c
Diff:
diff --git a/NEWS b/NEWS
index 580361462504..3a95dec83513 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@ PHP NEWS
- DOM:
. Fix crashes when entity declaration is removed while still having entity
references. (nielsdos)
+ . Fix references not handled correctly in C14N. (nielsdos)
09 May 2024, PHP 8.2.19
diff --git a/ext/dom/node.c b/ext/dom/node.c
index 973505c5b01a..82f40860cfd3 100644
--- a/ext/dom/node.c
+++ b/ext/dom/node.c
@@ -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");
@@ -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));
diff --git a/ext/dom/tests/DOMNode_C14N_references.phpt b/ext/dom/tests/DOMNode_C14N_references.phpt
new file mode 100644
index 000000000000..514e22be6365
--- /dev/null
+++ b/ext/dom/tests/DOMNode_C14N_references.phpt
@@ -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>