Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit ba7fc81

Browse files
authored
Merge pull request #3559 from magento-tango/PR-2012
[tango] MAGETWO-97040: Magento Framework Escaper - Critical log with special symbols
2 parents c4fb1ee + ad4fd4f commit ba7fc81

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/internal/Magento/Framework/Escaper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function ($errorNumber, $errorString) {
9191
throw new \Exception($errorString, $errorNumber);
9292
}
9393
);
94+
$data = $this->prepareUnescapedCharacters($data);
9495
$string = mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8');
9596
try {
9697
$domDocument->loadHTML(
@@ -119,6 +120,19 @@ function ($errorNumber, $errorString) {
119120
return $result;
120121
}
121122

123+
/**
124+
* Used to replace characters, that mb_convert_encoding will not process
125+
*
126+
* @param string $data
127+
* @return string|null
128+
*/
129+
private function prepareUnescapedCharacters(string $data): ?string
130+
{
131+
$patterns = ['/\&/u'];
132+
$replacements = ['&'];
133+
return \preg_replace($patterns, $replacements, $data);
134+
}
135+
122136
/**
123137
* Remove not allowed tags
124138
*

lib/internal/Magento/Framework/Test/Unit/EscaperTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public function testEscapeJsEscapesOwaspRecommendedRanges()
6969
// Exceptions to escaping ranges
7070
$immune = [',', '.', '_'];
7171
for ($chr = 0; $chr < 0xFF; $chr++) {
72-
if ($chr >= 0x30 && $chr <= 0x39
73-
|| $chr >= 0x41 && $chr <= 0x5A
74-
|| $chr >= 0x61 && $chr <= 0x7A
72+
if (($chr >= 0x30 && $chr <= 0x39)
73+
|| ($chr >= 0x41 && $chr <= 0x5A)
74+
|| ($chr >= 0x61 && $chr <= 0x7A)
7575
) {
7676
$literal = $this->codepointToUtf8($chr);
7777
$this->assertEquals($literal, $this->escaper->escapeJs($literal));
@@ -171,6 +171,11 @@ public function escapeHtmlDataProvider()
171171
'data' => '&<>"\'&amp;&lt;&gt;&quot;&#039;&#9;',
172172
'expected' => '&amp;&lt;&gt;&quot;&#039;&amp;&lt;&gt;&quot;&#039;&#9;'
173173
],
174+
'text with special characters and allowed tag' => [
175+
'data' => '&<br/>"\'&amp;&lt;&gt;&quot;&#039;&#9;',
176+
'expected' => '&amp;<br>&quot;&#039;&amp;&lt;&gt;&quot;&#039;&#9;',
177+
'allowedTags' => ['br'],
178+
],
174179
'text with multiple allowed tags, includes self closing tag' => [
175180
'data' => '<span>some text in tags<br /></span>',
176181
'expected' => '<span>some text in tags<br></span>',

0 commit comments

Comments
 (0)