Skip to content

Commit 5c749ad

Browse files
committed
Implement request #64137 (XSLTProcessor::setParameter() should allow both quotes to be used)
This reimplements the parameter handling. Instead of quoting the strings manually, adding them to an array, and passing that as input; use the libxslt API to pass data verbatim to the processor. This also simplifies the code a lot. Closes GH-12331.
1 parent f550c08 commit 5c749ad

8 files changed

+239
-77
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ Standard:
2626
(timwolla)
2727
. Fix GH-12252 (round(): Validate the rounding mode). (timwolla)
2828

29+
XSL:
30+
. Implement request #64137 (XSLTProcessor::setParameter() should allow both
31+
quotes to be used). (nielsdos)
32+
2933
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

UPGRADING

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ PHP 8.4 UPGRADE NOTES
3939
for invalid modes. Previously invalid modes would have been interpreted as
4040
PHP_ROUND_HALF_UP.
4141

42+
- XSL:
43+
. XSLTProcessor::setParameter() will now throw a ValueError when its arguments
44+
contain null bytes. This never actually worked correctly in the first place,
45+
which is why it throws an exception nowadays.
46+
4247
========================================
4348
2. New Features
4449
========================================
@@ -51,6 +56,10 @@ PHP 8.4 UPGRADE NOTES
5156
. Added constant DOMNode::DOCUMENT_POSITION_CONTAINED_BY.
5257
. Added constant DOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC.
5358

59+
- XSL:
60+
. It is now possible to use parameters that contain both single and double
61+
quotes.
62+
5463
========================================
5564
3. Changes in SAPI modules
5665
========================================

ext/xsl/tests/bug48221.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ $proc->importStylesheet($xsl);
99
$proc->setParameter('', '', '"\'');
1010
$proc->transformToXml($dom);
1111
?>
12-
--EXPECTF--
13-
Warning: XSLTProcessor::transformToXml(): Cannot create XPath expression (string contains both quote and double-quotes) in %s on line %d
12+
Done
13+
--EXPECT--
14+
Done
1415
--CREDITS--
1516
Christian Weiske, cweiske@php.net
1617
PHP Testfest Berlin 2009-05-09

ext/xsl/tests/bug64137.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Request #64137 (XSLTProcessor::setParameter() should allow both quotes to be used)
3+
--EXTENSIONS--
4+
xsl
5+
--FILE--
6+
<?php
7+
8+
function test(string $input) {
9+
$xml = new DOMDocument;
10+
$xml->loadXML('<X/>');
11+
12+
$xsl = new DOMDocument;
13+
$xsl->loadXML('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="text"/><xsl:param name="foo"/><xsl:template match="/"><xsl:value-of select="$foo"/></xsl:template></xsl:stylesheet>');
14+
15+
$xslt = new XSLTProcessor;
16+
$xslt->importStylesheet($xsl);
17+
$xslt->setParameter('', 'foo', $input);
18+
19+
echo $xslt->transformToXml($xml), "\n";
20+
}
21+
22+
test("");
23+
test("a'");
24+
test("a\"");
25+
test("fo'o\"ba'r\"ba'z");
26+
test("\"\"\"fo'o\"ba'r\"ba'z\"\"\"");
27+
test("'''\"\"\"fo'o\"ba'r\"ba'z\"\"\"'''");
28+
29+
?>
30+
--EXPECT--
31+
a'
32+
a"
33+
fo'o"ba'r"ba'z
34+
"""fo'o"ba'r"ba'z"""
35+
'''"""fo'o"ba'r"ba'z"""'''
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
--TEST--
2+
setParameter exceptions test
3+
--EXTENSIONS--
4+
xsl
5+
--FILE--
6+
<?php
7+
8+
function test(array $options) {
9+
$xml = new DOMDocument;
10+
$xml->loadXML('<X/>');
11+
12+
$xsl = new DOMDocument;
13+
$xsl->loadXML('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="text"/><xsl:param name="foo"/><xsl:template match="/"><xsl:value-of select="$foo"/></xsl:template></xsl:stylesheet>');
14+
15+
$xslt = new XSLTProcessor;
16+
$xslt->importStylesheet($xsl);
17+
$xslt->setParameter('', $options);
18+
19+
echo $xslt->transformToXml($xml), "\n";
20+
}
21+
22+
echo "--- Invalid key ---\n";
23+
24+
try {
25+
test([
26+
12345 => "foo"
27+
]);
28+
} catch (TypeError $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
echo "--- Valid key and value, but special cases ---\n";
33+
34+
test([
35+
"foo" => null,
36+
]);
37+
38+
test([
39+
"foo" => true,
40+
]);
41+
42+
echo "--- Exception from __toString should abort execution ---\n";
43+
44+
class MyStringable {
45+
public function __toString(): string {
46+
throw new Exception("exception!");
47+
}
48+
}
49+
50+
try {
51+
test([
52+
"foo" => new MyStringable,
53+
]);
54+
} catch (Throwable $e) {
55+
echo $e->getMessage(), "\n";
56+
}
57+
58+
echo "--- Exception from warning should abort execution ---\n";
59+
60+
set_error_handler(function($errno, $errstr) {
61+
throw new Exception($errstr);
62+
}, E_WARNING);
63+
64+
try {
65+
test([
66+
"foo" => [],
67+
"foo2" => [],
68+
]);
69+
} catch (Throwable $e) {
70+
echo $e->getMessage(), "\n";
71+
}
72+
73+
set_error_handler(null, E_WARNING);
74+
75+
echo "--- Warning may continue execution ---\n";
76+
77+
try {
78+
test([
79+
"foo" => [],
80+
"foo2" => [],
81+
]);
82+
} catch (Throwable $e) {
83+
echo $e->getMessage(), "\n";
84+
}
85+
86+
?>
87+
--EXPECTF--
88+
--- Invalid key ---
89+
XSLTProcessor::setParameter(): Argument #2 ($name) must contain only string keys
90+
--- Valid key and value, but special cases ---
91+
92+
1
93+
--- Exception from __toString should abort execution ---
94+
exception!
95+
--- Exception from warning should abort execution ---
96+
Array to string conversion
97+
--- Warning may continue execution ---
98+
99+
Warning: Array to string conversion in %s on line %d
100+
101+
Warning: Array to string conversion in %s on line %d
102+
Array
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
setParameter() with null bytes
3+
--EXTENSIONS--
4+
xsl
5+
--FILE--
6+
<?php
7+
8+
$xslt = new XSLTProcessor();
9+
10+
try {
11+
$xslt->setParameter("", "foo\0", "bar");
12+
} catch (ValueError $e) {
13+
echo $e->getMessage(), "\n";
14+
}
15+
16+
try {
17+
$xslt->setParameter("", "foo", "bar\0");
18+
} catch (ValueError $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
22+
try {
23+
$xslt->setParameter("", [
24+
"foo\0" => "bar",
25+
]);
26+
} catch (ValueError $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
30+
try {
31+
$xslt->setParameter("", [
32+
"foo" => "bar\0",
33+
]);
34+
} catch (ValueError $e) {
35+
echo $e->getMessage(), "\n";
36+
}
37+
38+
?>
39+
--EXPECT--
40+
XSLTProcessor::setParameter(): Argument #2 ($name) must not contain any null bytes
41+
XSLTProcessor::setParameter(): Argument #3 ($value) must not contain any null bytes
42+
XSLTProcessor::setParameter(): Argument #3 ($value) must not contain keys with any null bytes
43+
XSLTProcessor::setParameter(): Argument #3 ($value) must not contain values with any null bytes

ext/xsl/tests/xsltprocessor_setparameter-errorquote.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ $proc->importStylesheet($xsl);
1111
$proc->setParameter('', '', '"\'');
1212
$proc->transformToXml($dom);
1313
?>
14-
--EXPECTF--
15-
Warning: XSLTProcessor::transformToXml(): Cannot create XPath expression (string contains both quote and double-quotes) in %s on line %d
14+
Done
15+
--EXPECT--
16+
Done
1617
--CREDITS--
1718
Christian Weiske, cweiske@php.net
1819
PHP Testfest Berlin 2009-05-09

0 commit comments

Comments
 (0)