Skip to content

Commit ff90260

Browse files
author
ogorkun
committed
MC-34385: Filter fields allowing HTML
1 parent fa2ab78 commit ff90260

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

app/code/Magento/Cms/Command/WysiwygRestrictCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6666
$this->cache->cleanType('config');
6767

6868
$output->writeln('HTML user content validation is now ' .($restrictArg === 'y' ? 'enforced' : 'suggested'));
69+
70+
return 0;
6971
}
7072
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Magento\Framework\EntityManager\HydratorInterface;
2222

2323
/**
24-
* Class BlockRepository
24+
* Default block repo impl.
2525
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2626
*/
2727
class BlockRepository implements BlockRepositoryInterface
@@ -87,6 +87,8 @@ class BlockRepository implements BlockRepositoryInterface
8787
* @param StoreManagerInterface $storeManager
8888
* @param CollectionProcessorInterface $collectionProcessor
8989
* @param HydratorInterface|null $hydrator
90+
*
91+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9092
*/
9193
public function __construct(
9294
ResourceBlock $resource,
@@ -217,7 +219,7 @@ private function getCollectionProcessor()
217219
{
218220
if (!$this->collectionProcessor) {
219221
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
220-
'Magento\Cms\Model\Api\SearchCriteria\BlockCollectionProcessor'
222+
\Magento\Cms\Model\Api\SearchCriteria\BlockCollectionProcessor::class
221223
);
222224
}
223225
return $this->collectionProcessor;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
* @method Page setStoreId(int $storeId)
2424
* @method int getStoreId()
2525
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2627
* @since 100.0.2
2728
*/
2829
class Page extends AbstractModel implements PageInterface, IdentityInterface
2930
{
3031
/**
31-
* No route page id
32+
* Page ID for the 404 page.
3233
*/
3334
const NOROUTE_PAGE_ID = 'no-route';
3435

@@ -605,6 +606,8 @@ private function validateNewIdentifier(): void
605606
/**
606607
* @inheritdoc
607608
* @since 101.0.0
609+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
610+
* @SuppressWarnings(PHPMD.NPathComplexity)
608611
*/
609612
public function beforeSave()
610613
{

lib/internal/Magento/Framework/Test/Unit/Validator/HTML/ConfigurableWYSIWYGValidatorTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ConfigurableWYSIWYGValidatorTest extends TestCase
2020
* Configurations to test.
2121
*
2222
* @return array
23+
*
24+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2325
*/
2426
public function getConfigurations(): array
2527
{
@@ -178,7 +180,9 @@ public function getConfigurations(): array
178180
* @param bool[] $attributeValidityMap
179181
* @param bool[][] $tagValidators
180182
* @return void
183+
*
181184
* @dataProvider getConfigurations
185+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
182186
*/
183187
public function testConfigurations(
184188
array $allowedTags,
@@ -192,7 +196,7 @@ public function testConfigurations(
192196
$attributeValidator = $this->getMockForAbstractClass(AttributeValidatorInterface::class);
193197
$attributeValidator->method('validate')
194198
->willReturnCallback(
195-
function (string $tag, string $attribute, string $content) use ($attributeValidityMap): void {
199+
function (string $tag, string $attribute) use ($attributeValidityMap): void {
196200
if (array_key_exists($attribute, $attributeValidityMap) && !$attributeValidityMap[$attribute]) {
197201
throw new ValidationException(__('Invalid attribute for %1', $tag));
198202
}
@@ -207,7 +211,7 @@ function (string $tag, string $attribute, string $content) use ($attributeValidi
207211
$mock = $this->getMockForAbstractClass(TagValidatorInterface::class);
208212
$mock->method('validate')
209213
->willReturnCallback(
210-
function (string $givenTag, array $attrs, string $value) use($tag, $allowedAttributes): void {
214+
function (string $givenTag, array $attrs) use ($tag, $allowedAttributes): void {
211215
if ($givenTag !== $tag) {
212216
throw new \RuntimeException();
213217
}

lib/internal/Magento/Framework/Validator/HTML/ConfigurableWYSIWYGValidator.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public function validate(string $content): void
8282
$xpath = new \DOMXPath($dom);
8383

8484
$this->validateConfigured($xpath);
85-
$this->callDynamicValidators($xpath);
85+
$this->callAttributeValidators($xpath);
86+
$this->callTagValidators($xpath);
8687
}
8788

8889
/**
@@ -96,7 +97,7 @@ private function validateConfigured(\DOMXPath $xpath): void
9697
{
9798
//Validating tags
9899
$found = $xpath->query(
99-
$query='//*['
100+
'//*['
100101
. implode(
101102
' and ',
102103
array_map(
@@ -117,10 +118,11 @@ function (string $tag): string {
117118
//Validating attributes
118119
if ($this->attributesAllowedByTags) {
119120
foreach ($this->allowedTags as $tag) {
120-
$allowed = $this->allowedAttributes;
121+
$allowed = [$this->allowedAttributes];
121122
if (!empty($this->attributesAllowedByTags[$tag])) {
122-
$allowed = array_unique(array_merge($allowed, $this->attributesAllowedByTags[$tag]));
123+
$allowed[] = $this->attributesAllowedByTags[$tag];
123124
}
125+
$allowed = array_unique(array_merge(...$allowed));
124126
$allowedQuery = '';
125127
if ($allowed) {
126128
$allowedQuery = '['
@@ -167,15 +169,14 @@ function (string $attribute): string {
167169
}
168170

169171
/**
170-
* Cycle dynamic validators.
172+
* Validate allowed HTML attributes' content.
171173
*
172174
* @param \DOMXPath $xpath
173-
* @return void
174175
* @throws ValidationException
176+
* @return void
175177
*/
176-
private function callDynamicValidators(\DOMXPath $xpath): void
178+
private function callAttributeValidators(\DOMXPath $xpath): void
177179
{
178-
//Validating allowed attributes.
179180
if ($this->attributeValidators) {
180181
foreach ($this->attributeValidators as $attr => $validators) {
181182
$found = $xpath->query("//@*[name() = '$attr']");
@@ -186,8 +187,17 @@ private function callDynamicValidators(\DOMXPath $xpath): void
186187
}
187188
}
188189
}
190+
}
189191

190-
//Validating allowed tags
192+
/**
193+
* Validate allowed tags.
194+
*
195+
* @param \DOMXPath $xpath
196+
* @return void
197+
* @throws ValidationException
198+
*/
199+
private function callTagValidators(\DOMXPath $xpath): void
200+
{
191201
if ($this->tagValidators) {
192202
foreach ($this->tagValidators as $tag => $validators) {
193203
$found = $xpath->query("//*[name() = '$tag']");

0 commit comments

Comments
 (0)