Skip to content

Commit 8cc9aeb

Browse files
authored
Fix #18749: Fix yii\web\ErrorHandler::encodeHtml() to support strings with invalid UTF symbols
1 parent 17742cb commit 8cc9aeb

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

framework/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Yii Framework 2 Change Log
66

77
- Bug #14663: Do not convert int to string if database type of a column is numeric (egorrishe)
88
- Bug #18650: Refactor `framework/assets/yii.activeForm.js` arrow function into traditional function for IE11 compatibility (marcovtwout)
9+
- Bug #18749: Fix `yii\web\ErrorHandler::encodeHtml()` to support strings with invalid UTF symbols (vjik)
910
- Enh #18724: Allow jQuery 3.6 to be installed (marcovtwout)
1011
- Enh #18628: Added strings "software", and "hardware" to `$specials` array in `yii\helpers\BaseInflector` (kjusupov)
1112
- Enh #18653: Added method `yii\helpers\BaseHtml::getInputIdByName()` (WinterSilence)

framework/web/ErrorHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected function convertExceptionToArray($exception)
180180
*/
181181
public function htmlEncode($text)
182182
{
183-
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
183+
return htmlspecialchars($text, ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8');
184184
}
185185

186186
/**

tests/framework/web/ErrorHandlerTest.php

+55
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,61 @@ public function testRenderCallStackItem()
7979

8080
$this->assertContains('<a href="netbeans://open?file=' . $file . '&line=63">', $out);
8181
}
82+
83+
public function dataHtmlEncode()
84+
{
85+
return [
86+
[
87+
"a \t=<>&\"'\x80`\n",
88+
"a \t=&lt;&gt;&amp;\"'�`\n",
89+
],
90+
[
91+
'<b>test</b>',
92+
'&lt;b&gt;test&lt;/b&gt;',
93+
],
94+
[
95+
'"hello"',
96+
'"hello"',
97+
],
98+
[
99+
"'hello world'",
100+
"'hello world'",
101+
],
102+
[
103+
'Chip&amp;Dale',
104+
'Chip&amp;amp;Dale',
105+
],
106+
[
107+
"\t\$x=24;",
108+
"\t\$x=24;",
109+
],
110+
];
111+
}
112+
113+
/**
114+
* @dataProvider dataHtmlEncode
115+
*/
116+
public function testHtmlEncode($text, $expected)
117+
{
118+
$handler = Yii::$app->getErrorHandler();
119+
120+
$this->assertSame($expected, $handler->htmlEncode($text));
121+
}
122+
123+
public function testHtmlEncodeWithUnicodeSequence()
124+
{
125+
if (PHP_VERSION_ID < 70000) {
126+
$this->markTestSkipped('Can not be tested on PHP < 7.0');
127+
return;
128+
}
129+
130+
$handler = Yii::$app->getErrorHandler();
131+
132+
$text = "a \t=<>&\"'\x80\u{20bd}`\u{000a}\u{000c}\u{0000}";
133+
$expected = "a \t=&lt;&gt;&amp;\"'�₽`\n\u{000c}\u{0000}";
134+
135+
$this->assertSame($expected, $handler->htmlEncode($text));
136+
}
82137
}
83138

84139
class ErrorHandler extends \yii\web\ErrorHandler

0 commit comments

Comments
 (0)