Skip to content

Commit 1e50290

Browse files
author
ogorkun
committed
MC-34385: Filter fields allowing HTML
1 parent 2c1e036 commit 1e50290

File tree

3 files changed

+33
-64
lines changed

3 files changed

+33
-64
lines changed

app/code/Magento/Cms/Model/Page.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Framework\DataObject\IdentityInterface;
1414
use Magento\Framework\Exception\LocalizedException;
1515
use Magento\Framework\Model\AbstractModel;
16+
use Magento\Framework\Validation\ValidationException;
17+
use Magento\Framework\Validator\HTML\WYSIWYGValidatorInterface;
1618

1719
/**
1820
* Cms Page Model
@@ -64,25 +66,34 @@ class Page extends AbstractModel implements PageInterface, IdentityInterface
6466
*/
6567
private $customLayoutRepository;
6668

69+
/**
70+
* @var WYSIWYGValidatorInterface
71+
*/
72+
private $wysiwygValidator;
73+
6774
/**
6875
* @param \Magento\Framework\Model\Context $context
6976
* @param \Magento\Framework\Registry $registry
7077
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
7178
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
7279
* @param array $data
7380
* @param CustomLayoutRepository|null $customLayoutRepository
81+
* @param WYSIWYGValidatorInterface|null $wysiwygValidator
7482
*/
7583
public function __construct(
7684
\Magento\Framework\Model\Context $context,
7785
\Magento\Framework\Registry $registry,
7886
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
7987
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
8088
array $data = [],
81-
?CustomLayoutRepository $customLayoutRepository = null
89+
?CustomLayoutRepository $customLayoutRepository = null,
90+
?WYSIWYGValidatorInterface $wysiwygValidator = null
8291
) {
8392
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
8493
$this->customLayoutRepository = $customLayoutRepository
8594
?? ObjectManager::getInstance()->get(CustomLayoutRepository::class);
95+
$this->wysiwygValidator = $wysiwygValidator
96+
?? ObjectManager::getInstance()->get(WYSIWYGValidatorInterface::class);
8697
}
8798

8899
/**
@@ -615,6 +626,26 @@ public function beforeSave()
615626
$this->setData('layout_update_selected', $layoutUpdate);
616627
$this->customLayoutRepository->validateLayoutSelectedFor($this);
617628

629+
//Validating Content HTML.
630+
$oldValue = null;
631+
if ($this->getId()) {
632+
if ($this->getOrigData()) {
633+
$oldValue = $this->getOrigData(self::CONTENT);
634+
} elseif (array_key_exists(self::CONTENT, $this->getStoredData())) {
635+
$oldValue = $this->getStoredData()[self::CONTENT];
636+
}
637+
}
638+
if ($this->getContent() && $this->getContent() !== $oldValue) {
639+
try {
640+
$this->wysiwygValidator->validate($this->getContent());
641+
} catch (ValidationException $exception) {
642+
throw new ValidationException(
643+
__('Content HTML contains restricted elements. %1', $exception->getMessage()),
644+
$exception
645+
);
646+
}
647+
}
648+
618649
return parent::beforeSave();
619650
}
620651

app/code/Magento/Cms/Model/PageRepository/Validator/ContentValidator.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

app/code/Magento/Cms/etc/di.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@
233233
<argument name="repository" xsi:type="object">Magento\Cms\Model\PageRepository</argument>
234234
<argument name="validators" xsi:type="array">
235235
<item name="layout_update" xsi:type="object">Magento\Cms\Model\PageRepository\Validator\LayoutUpdateValidator</item>
236-
<item name="content" xsi:type="object">Magento\Cms\Model\PageRepository\Validator\ContentValidator</item>
237236
</argument>
237+
<argument name="hydrator" xsi:type="object">Magento\Framework\EntityManager\AbstractModelHydrator</argument>
238238
</arguments>
239239
</type>
240240
<preference for="Magento\Cms\Model\Page\CustomLayoutManagerInterface" type="Magento\Cms\Model\Page\CustomLayout\CustomLayoutManager" />
@@ -257,9 +257,4 @@
257257
</argument>
258258
</arguments>
259259
</type>
260-
<type name="Magento\Cms\Model\PageRepository\Validator\ContentValidator">
261-
<arguments>
262-
<argument name="hydrator" xsi:type="object">Magento\Framework\EntityManager\AbstractModelHydrator</argument>
263-
</arguments>
264-
</type>
265260
</config>

0 commit comments

Comments
 (0)