Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Model/Api/MediaContentValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* Copyright © MagestyApps. All rights reserved.
* See LICENSE.txt for license details.
*/

namespace MagestyApps\WebImages\Model\Api;

use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\Api\ImageContentValidator;
use Magento\Framework\Api\ImageContentValidatorInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Phrase;

class MediaContentValidator implements ImageContentValidatorInterface
{
public function __construct(
private ImageContentValidator $imageContentValidator,
) {}

public function isValid(ImageContentInterface $imageContent): bool
{
return $this->isSvgContent($imageContent) && $this->isAllowed($imageContent)
? $this->isValidSvg($imageContent)
: $this->imageContentValidator->isValid($imageContent);
}

/**
* Skip image size check in case of svg image, SVGs are not compatible with getimagesizefromstring
* Use it at your own risk! This method does not sanitize the SVG content and malicious code can be pushed!
*
* @throws InputException
* @see ImageContentValidator::isValid
*/
private function isValidSvg(ImageContentInterface $imageContent): bool
{
$imageName = (string)$imageContent->getName();
if ($imageName !== '' && preg_match('/^[^\\/?*:";<>()|{}\\\\]+$/', $imageName) === 1) {
throw new InputException(new Phrase('Provided image name contains forbidden characters.'));
}

return true;
}

private function isSvgContent(ImageContentInterface $imageContent): bool
{
$fileContent = @base64_decode($imageContent->getBase64EncodedData(), true);

return str_starts_with($fileContent, '<svg ') && str_ends_with($fileContent, '</svg>');
}

private function isAllowed(ImageContentInterface $imageContent): bool
{
return in_array($imageContent->getType(), ['svg-xml', 'svg'], true);
}
}
4 changes: 1 addition & 3 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<type name="Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive">
<plugin name="allow-web-images" type="MagestyApps\WebImages\Plugin\Controller\Adminhtml\Wysiwyg\DirectivePlugin" />
</type>

<type name="Magento\Theme\Model\Design\Backend\Logo">
<plugin name="allow-web-images" type="MagestyApps\WebImages\Plugin\Design\Backend\ImagePlugin" />
</type>
Expand All @@ -91,11 +90,9 @@
<type name="Magento\Theme\Model\Design\Backend\Image">
<plugin name="allow-web-images" type="MagestyApps\WebImages\Plugin\Design\Backend\ImagePlugin" />
</type>

<type name="Magento\Swatches\Helper\Media">
<plugin name="allow-web-images" type="MagestyApps\WebImages\Plugin\Swatches\Helper\MediaPlugin" />
</type>

<type name="Magento\MediaGallerySynchronization\Model\CreateAssetFromFile">
<arguments>
<argument name="assetFactory" xsi:type="object">MagestyApps\WebImages\Model\AssetFactory</argument>
Expand All @@ -106,4 +103,5 @@
<argument name="uploaderFactory" xsi:type="object">MagestyApps\WebImages\Model\File\UploaderFactory</argument>
</arguments>
</type>
<preference for="Magento\Framework\Api\ImageContentValidatorInterface" type="MagestyApps\WebImages\Model\Api\MediaContentValidator"/>
</config>