Skip to content

Commit 5a69a2c

Browse files
committed
MC-15311: [Sec] PageBuilder XSS Injection Possible Through Block on HTML Code Content Type For CSS Classes Attribute & in TinyMCE
Safely decode outer html on html content types
1 parent d560bae commit 5a69a2c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

app/code/Magento/PageBuilder/Plugin/Filter/TemplatePlugin.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,27 @@ private function generateDecodedHtmlPlaceholderMappingInDocument(\DOMDocument $d
180180
continue;
181181
}
182182

183+
// clone html code content type to save reference to its attributes/outerHTML, which we are not going to
184+
// decode
185+
$clonedHtmlContentTypeNode = clone $htmlContentTypeNode;
186+
187+
// clear inner contents of cloned node for replacement later with $decodedInnerHtml using sprintf;
188+
// we want to retain html content type node and avoid doing any manipulation on it
189+
$clonedHtmlContentTypeNode->nodeValue = '%s';
190+
191+
// remove potentially harmful attributes on html content type node itself
192+
while ($htmlContentTypeNode->attributes->length) {
193+
$htmlContentTypeNode->removeAttribute($htmlContentTypeNode->attributes->item(0)->name);
194+
}
195+
196+
// decode outerHTML safely
183197
$preDecodedOuterHtml = $document->saveHTML($htmlContentTypeNode);
184-
$decodedOuterHtml = html_entity_decode($preDecodedOuterHtml);
198+
199+
// clear empty <div> wrapper around outerHTML to replace with $clonedHtmlContentTypeNode
200+
$decodedInnerHtml = preg_replace('#^<[^>]*>|</[^>]*>$#', '', html_entity_decode($preDecodedOuterHtml));
201+
202+
// Use $clonedHtmlContentTypeNode's placeholder to inject decoded inner html
203+
$decodedOuterHtml = sprintf($document->saveHTML($clonedHtmlContentTypeNode), $decodedInnerHtml);
185204

186205
// generate unique node name element to replace with decoded html contents at end of processing;
187206
// goal is to create a document as few times as possible to prevent inadvertent parsing of contents as html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div data-content-type="html" class="&gt;'&gt;&quot;&gt;&lt;img src=x onerror=alert(0)&gt;" data-decoded="true">
2+
Nothing to see here
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div data-content-type="html" class="&gt;'&gt;&quot;&gt;&lt;img src=x onerror=alert(0)&gt;">
2+
Nothing to see here
3+
</div>

0 commit comments

Comments
 (0)