Skip to content

Commit

Permalink
Fix #18749: Fix yii\web\ErrorHandler::encodeHtml() to support strin…
Browse files Browse the repository at this point in the history
…gs with invalid UTF symbols
  • Loading branch information
vjik authored Jul 6, 2021
1 parent 17742cb commit 8cc9aeb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Yii Framework 2 Change Log

- Bug #14663: Do not convert int to string if database type of a column is numeric (egorrishe)
- Bug #18650: Refactor `framework/assets/yii.activeForm.js` arrow function into traditional function for IE11 compatibility (marcovtwout)
- Bug #18749: Fix `yii\web\ErrorHandler::encodeHtml()` to support strings with invalid UTF symbols (vjik)
- Enh #18724: Allow jQuery 3.6 to be installed (marcovtwout)
- Enh #18628: Added strings "software", and "hardware" to `$specials` array in `yii\helpers\BaseInflector` (kjusupov)
- Enh #18653: Added method `yii\helpers\BaseHtml::getInputIdByName()` (WinterSilence)
Expand Down
2 changes: 1 addition & 1 deletion framework/web/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected function convertExceptionToArray($exception)
*/
public function htmlEncode($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
return htmlspecialchars($text, ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8');
}

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/framework/web/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,61 @@ public function testRenderCallStackItem()

$this->assertContains('<a href="netbeans://open?file=' . $file . '&line=63">', $out);
}

public function dataHtmlEncode()
{
return [
[
"a \t=<>&\"'\x80`\n",
"a \t=&lt;&gt;&amp;\"'�`\n",
],
[
'<b>test</b>',
'&lt;b&gt;test&lt;/b&gt;',
],
[
'"hello"',
'"hello"',
],
[
"'hello world'",
"'hello world'",
],
[
'Chip&amp;Dale',
'Chip&amp;amp;Dale',
],
[
"\t\$x=24;",
"\t\$x=24;",
],
];
}

/**
* @dataProvider dataHtmlEncode
*/
public function testHtmlEncode($text, $expected)
{
$handler = Yii::$app->getErrorHandler();

$this->assertSame($expected, $handler->htmlEncode($text));
}

public function testHtmlEncodeWithUnicodeSequence()
{
if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('Can not be tested on PHP < 7.0');
return;
}

$handler = Yii::$app->getErrorHandler();

$text = "a \t=<>&\"'\x80\u{20bd}`\u{000a}\u{000c}\u{0000}";
$expected = "a \t=&lt;&gt;&amp;\"'�₽`\n\u{000c}\u{0000}";

$this->assertSame($expected, $handler->htmlEncode($text));
}
}

class ErrorHandler extends \yii\web\ErrorHandler
Expand Down

0 comments on commit 8cc9aeb

Please # to comment.