Skip to content

Commit

Permalink
Merge pull request #4330 from magento-obsessive-owls/MC-16599
Browse files Browse the repository at this point in the history
[Owls] MC-16599 Use Escaper methods
  • Loading branch information
davemacaulay committed Jun 13, 2019
2 parents 6801e95 + e5ab4b6 commit 163302f
Show file tree
Hide file tree
Showing 55 changed files with 832 additions and 259 deletions.
25 changes: 18 additions & 7 deletions app/code/Magento/AdminNotification/Model/Feed.php
Expand Up @@ -25,6 +25,11 @@ class Feed extends \Magento\Framework\Model\AbstractModel

const XML_LAST_UPDATE_PATH = 'system/adminnotification/last_update';

/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* Feed url
*
Expand Down Expand Up @@ -77,6 +82,7 @@ class Feed extends \Magento\Framework\Model\AbstractModel
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param \Magento\Framework\Escaper|null $escaper
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -90,21 +96,26 @@ public function __construct(
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
\Magento\Framework\Escaper $escaper = null
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->_backendConfig = $backendConfig;
$this->_inboxFactory = $inboxFactory;
$this->curlFactory = $curlFactory;
$this->_backendConfig = $backendConfig;
$this->_inboxFactory = $inboxFactory;
$this->curlFactory = $curlFactory;
$this->_deploymentConfig = $deploymentConfig;
$this->productMetadata = $productMetadata;
$this->urlBuilder = $urlBuilder;
$this->productMetadata = $productMetadata;
$this->urlBuilder = $urlBuilder;
$this->escaper = $escaper ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Escaper::class
);
}

/**
* Init model
*
* @return void
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
*/
protected function _construct()
{
Expand Down Expand Up @@ -252,6 +263,6 @@ public function getFeedXml()
*/
private function escapeString(\SimpleXMLElement $data)
{
return htmlspecialchars((string)$data);
return $this->escaper->escapeHtml((string)$data);
}
}
44 changes: 43 additions & 1 deletion app/code/Magento/Catalog/Block/Adminhtml/Product/Edit.php
Expand Up @@ -12,8 +12,16 @@
*/
namespace Magento\Catalog\Block\Adminhtml\Product;

/**
* Class Edit
*/
class Edit extends \Magento\Backend\Block\Widget
{
/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* @var string
*/
Expand Down Expand Up @@ -47,6 +55,7 @@ class Edit extends \Magento\Backend\Block\Widget
* @param \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory
* @param \Magento\Framework\Registry $registry
* @param \Magento\Catalog\Helper\Product $productHelper
* @param \Magento\Framework\Escaper $escaper
* @param array $data
*/
public function __construct(
Expand All @@ -55,16 +64,20 @@ public function __construct(
\Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Framework\Escaper $escaper,
array $data = []
) {
$this->_productHelper = $productHelper;
$this->_attributeSetFactory = $attributeSetFactory;
$this->_coreRegistry = $registry;
$this->jsonEncoder = $jsonEncoder;
$this->escaper = $escaper;
parent::__construct($context, $data);
}

/**
* Edit Product constructor
*
* @return void
*/
protected function _construct()
Expand Down Expand Up @@ -144,6 +157,8 @@ protected function _prepareLayout()
}

/**
* Retrieve back button html
*
* @return string
*/
public function getBackButtonHtml()
Expand All @@ -152,6 +167,8 @@ public function getBackButtonHtml()
}

/**
* Retrieve cancel button html
*
* @return string
*/
public function getCancelButtonHtml()
Expand All @@ -160,6 +177,8 @@ public function getCancelButtonHtml()
}

/**
* Retrieve save button html
*
* @return string
*/
public function getSaveButtonHtml()
Expand All @@ -168,6 +187,8 @@ public function getSaveButtonHtml()
}

/**
* Retrieve save and edit button html
*
* @return string
*/
public function getSaveAndEditButtonHtml()
Expand All @@ -176,6 +197,8 @@ public function getSaveAndEditButtonHtml()
}

/**
* Retrieve delete button html
*
* @return string
*/
public function getDeleteButtonHtml()
Expand All @@ -194,6 +217,8 @@ public function getSaveSplitButtonHtml()
}

/**
* Retrieve validation url
*
* @return string
*/
public function getValidationUrl()
Expand All @@ -202,6 +227,8 @@ public function getValidationUrl()
}

/**
* Retrieve save url
*
* @return string
*/
public function getSaveUrl()
Expand All @@ -210,6 +237,8 @@ public function getSaveUrl()
}

/**
* Retrieve save and continue url
*
* @return string
*/
public function getSaveAndContinueUrl()
Expand All @@ -221,6 +250,8 @@ public function getSaveAndContinueUrl()
}

/**
* Retrieve product id
*
* @return mixed
*/
public function getProductId()
Expand All @@ -229,6 +260,8 @@ public function getProductId()
}

/**
* Retrieve product set id
*
* @return mixed
*/
public function getProductSetId()
Expand All @@ -241,6 +274,8 @@ public function getProductSetId()
}

/**
* Retrieve duplicate url
*
* @return string
*/
public function getDuplicateUrl()
Expand All @@ -249,6 +284,8 @@ public function getDuplicateUrl()
}

/**
* Retrieve product header
*
* @deprecated 101.1.0
* @return string
*/
Expand All @@ -263,6 +300,8 @@ public function getHeader()
}

/**
* Get product attribute set name
*
* @return string
*/
public function getAttributeSetName()
Expand All @@ -275,11 +314,14 @@ public function getAttributeSetName()
}

/**
* Retrieve id of selected tab
*
* @return string
*/
public function getSelectedTabId()
{
return addslashes(htmlspecialchars($this->getRequest()->getParam('tab')));
// phpcs:ignore Magento2.Functions.DiscouragedFunction
return addslashes($this->escaper->escapeHtml($this->getRequest()->getParam('tab')));
}

/**
Expand Down
Expand Up @@ -13,7 +13,7 @@
<p><?= $block->escapeHtml($error) ?></p>
<?php endif ?>
<p><?= $block->escapeHtml(
_('Click <a href="%1">here</a> to continue shopping.', $block->escapeUrl($block->getContinueShoppingUrl())),
__('Click <a href="%1">here</a> to continue shopping.', $block->escapeUrl($block->getContinueShoppingUrl())),
['a']
) ?>
</p>
Expand Up @@ -10,6 +10,11 @@
*/
class Action extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* Core registry
*
Expand All @@ -21,17 +26,24 @@ class Action extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Abstract
* @param \Magento\Backend\Block\Context $context
* @param \Magento\Framework\Registry $registry
* @param array $data
* @param \Magento\Framework\Escaper|null $escaper
*/
public function __construct(
\Magento\Backend\Block\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
array $data = [],
\Magento\Framework\Escaper $escaper = null
) {
$this->_coreRegistry = $registry;
$this->escaper = $escaper ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Escaper::class
);
parent::__construct($context, $data);
}

/**
* Render actions
*
* @param \Magento\Framework\DataObject $row
* @return string
*/
Expand All @@ -57,15 +69,20 @@ public function render(\Magento\Framework\DataObject $row)
}

/**
* Retrieve escaped value
*
* @param string $value
* @return string
*/
protected function _getEscapedValue($value)
{
return addcslashes(htmlspecialchars($value), '\\\'');
// phpcs:ignore Magento2.Functions.DiscouragedFunction
return addcslashes($this->escaper->escapeHtml($value), '\\\'');
}

/**
* Actions to html
*
* @param array $actions
* @return string
*/
Expand Down
Expand Up @@ -18,6 +18,6 @@ class Description extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Abs
*/
public function render(\Magento\Framework\DataObject $row)
{
return nl2br(htmlspecialchars($row->getData($this->getColumn()->getIndex())));
return nl2br($this->escapeHtml($row->getData($this->getColumn()->getIndex())));
}
}
16 changes: 13 additions & 3 deletions app/code/Magento/Customer/Model/Address/Validator/Country.php
Expand Up @@ -16,6 +16,11 @@
*/
class Country implements ValidatorInterface
{
/**
* @var \Magento\Framework\Escaper
*/
private $escaper;

/**
* @var Data
*/
Expand All @@ -29,13 +34,18 @@ class Country implements ValidatorInterface
/**
* @param Data $directoryData
* @param AllowedCountries $allowedCountriesReader
* @param \Magento\Framework\Escaper|null $escaper
*/
public function __construct(
Data $directoryData,
AllowedCountries $allowedCountriesReader
AllowedCountries $allowedCountriesReader,
\Magento\Framework\Escaper $escaper = null
) {
$this->directoryData = $directoryData;
$this->allowedCountriesReader = $allowedCountriesReader;
$this->escaper = $escaper ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Escaper::class
);
}

/**
Expand Down Expand Up @@ -67,7 +77,7 @@ private function validateCountry(AbstractAddress $address)
//Checking if such country exists.
$errors[] = __(
'Invalid value of "%value" provided for the %fieldName field.',
['fieldName' => 'countryId', 'value' => htmlspecialchars($countryId)]
['fieldName' => 'countryId', 'value' => $this->escaper->escapeHtml($countryId)]
);
}

Expand Down Expand Up @@ -104,7 +114,7 @@ private function validateRegion(AbstractAddress $address)
//If a region is selected then checking if it exists.
$errors[] = __(
'Invalid value of "%value" provided for the %fieldName field.',
['fieldName' => 'regionId', 'value' => htmlspecialchars($regionId)]
['fieldName' => 'regionId', 'value' => $this->escaper->escapeHtml($regionId)]
);
}

Expand Down
Expand Up @@ -35,11 +35,17 @@ protected function setUp()
\Magento\Directory\Model\AllowedCountries::class,
['getAllowedCountries']
);

$escaper = $this->objectManager->getObject(
\Magento\Framework\Escaper::class
);

$this->model = $this->objectManager->getObject(
\Magento\Customer\Model\Address\Validator\Country::class,
[
'directoryData' => $this->directoryDataMock,
'allowedCountriesReader' => $this->allowedCountriesReaderMock,
'escaper' => $escaper
]
);
}
Expand Down

0 comments on commit 163302f

Please sign in to comment.