Skip to content

#39353 Added sanitization of address fields for the presence of template variables in the customer address #39673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: 2.4-develop
Choose a base branch
from
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
22 changes: 20 additions & 2 deletions app/code/Magento/Customer/Model/Address/AbstractAddress.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2013 Adobe
* All Rights Reserved.
*/

namespace Magento\Customer\Model\Address;
Expand Down Expand Up @@ -141,6 +141,9 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
/** @var CompositeValidator */
private $compositeValidator;

/** @var CompositeSanitizer */
private $compositeSanitizer;

/**
* @var array
*/
Expand All @@ -166,6 +169,7 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
* @param CompositeValidator $compositeValidator
* @param CountryModelsCache|null $countryModelsCache
* @param RegionModelsCache|null $regionModelsCache
* @param CompositeSanitizer|null $compositeSanitizer
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -189,6 +193,7 @@ public function __construct(
?CompositeValidator $compositeValidator = null,
?CountryModelsCache $countryModelsCache = null,
?RegionModelsCache $regionModelsCache = null,
?CompositeSanitizer $compositeSanitizer = null
) {
$this->_directoryData = $directoryData;
$data = $this->_implodeArrayField($data);
Expand All @@ -206,6 +211,8 @@ public function __construct(
->get(CountryModelsCache::class);
$this->regionModelsCache = $regionModelsCache ?: ObjectManager::getInstance()
->get(RegionModelsCache::class);
$this->compositeSanitizer = $compositeSanitizer ?: ObjectManager::getInstance()
->get(CompositeSanitizer::class);
parent::__construct(
$context,
$registry,
Expand Down Expand Up @@ -680,6 +687,17 @@ public function validate()
return $errors;
}

/**
* Sanitize address attribute values.
*
* @return $this
*/
public function sanitize(): self
{
$this->compositeSanitizer->sanitize($this);
return $this;
}

/**
* Create region instance
*
Expand Down
40 changes: 40 additions & 0 deletions app/code/Magento/Customer/Model/Address/CompositeSanitizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright 2025 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Customer\Model\Address;

/**
* Address composite validator.
*/
class CompositeSanitizer implements SanitizerInterface
{
/**
* @var SanitizerInterface[]
*/
private array $sanitizers;

/**
* @param array $sanitizers
*/
public function __construct(
array $sanitizers = []
) {
$this->sanitizers = $sanitizers;
}

/**
* @inheritdoc
*/
public function sanitize(AbstractAddress $address): AbstractAddress
{
foreach ($this->sanitizers as $sanitizer) {
$sanitizer->sanitize($address);
}

return $address;
}
}
76 changes: 76 additions & 0 deletions app/code/Magento/Customer/Model/Address/Sanitizer/TemplateVars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright 2025 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Customer\Model\Address\Sanitizer;

use Magento\Customer\Model\Address\SanitizerInterface;
use Magento\Customer\Model\Address\AbstractAddress;

class TemplateVars implements SanitizerInterface
{
/**
* Template vars patter
* @var string
*/
private string $templateVarsPattern = '/\{\{\s*([^{}]+)\s*\}\}/';

/**
* List of attributes that can be entered via user input or API
* @var array|string[]
*/
private array $attributesToSanitize = [
'firstname',
'lastname',
'middlename',
'city',
'company',
'country_id',
'fax',
'postcode',
'prefix',
'region',
'region_id',
'street',
'suffix',
'telephone',
'vat_id'
];

/**
* Sanitize string for template vars in address attributes.
*
* @param AbstractAddress $address
* @return AbstractAddress
*/
public function sanitize(AbstractAddress $address): AbstractAddress
{
foreach ($this->attributesToSanitize as $attributeCode) {
$attributeValue = $address->getData($attributeCode);
preg_match_all($this->templateVarsPattern, (string)$attributeValue, $matches);
if (!empty($matches[0])) {
$sanitizedAttributeValue = $this->sanitizeTemplateVars($attributeValue);
$address->setData($attributeCode, $sanitizedAttributeValue);
}
}

return $address;
}

/**
* Sanitize string for template vars in address attributes.
*
* @param string $value
* @return string
*/
private function sanitizeTemplateVars(string $value): string
{
if (!empty($value)) {
return str_replace(['{', '}'], ['&#123;', '&#125;'], $value);
}
return $value;
}
}
20 changes: 20 additions & 0 deletions app/code/Magento/Customer/Model/Address/SanitizerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright 2025 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Customer\Model\Address;

interface SanitizerInterface
{
/**
* Sanitize address instance.
*
* @param AbstractAddress $address
* @return AbstractAddress
* @since 102.0.0
*/
public function sanitize(AbstractAddress $address): AbstractAddress;
}
4 changes: 2 additions & 2 deletions app/code/Magento/Customer/Model/Address/Validator/General.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2018 Adobe
* All Rights Reserved.
*/
namespace Magento\Customer\Model\Address\Validator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
namespace Magento\Customer\Model\ResourceModel;

Expand Down Expand Up @@ -126,6 +126,7 @@ public function save(\Magento\Customer\Api\Data\AddressInterface $address)
$addressModel->setStoreId($customerModel->getStoreId());
}

$addressModel = $addressModel->sanitize();
$errors = $addressModel->validate();
if ($errors !== true) {
$inputException = new InputException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand Down Expand Up @@ -142,6 +142,7 @@ protected function setUp(): void
'updateData',
'setCustomer',
'getCountryModel',
'sanitize',
'validate',
'save',
'getDataModel',
Expand Down Expand Up @@ -210,6 +211,9 @@ public function testSave()
$this->address->expects($this->once())
->method('setCustomer')
->with($this->customer);
$this->address->expects($this->once())
->method('sanitize')
->willReturn($this->address);
$this->address->expects($this->once())
->method('validate')
->willReturn(true);
Expand Down Expand Up @@ -280,6 +284,9 @@ public function testSaveWithConfigCustomerAccountShareScopeWebsite()
->willReturn(true);
$this->address->expects($this->once())
->method('setStoreId');
$this->address->expects($this->once())
->method('sanitize')
->willReturn($this->address);
$this->address->expects($this->once())
->method('validate')
->willReturn(true);
Expand Down Expand Up @@ -350,6 +357,9 @@ public function testSaveWithConfigCustomerAccountShareScopeGlobal()
->willReturn(false);
$this->address->expects($this->never())
->method('setStoreId');
$this->address->expects($this->once())
->method('sanitize')
->willReturn($this->address);
$this->address->expects($this->once())
->method('validate')
->willReturn(true);
Expand Down Expand Up @@ -404,6 +414,9 @@ public function testSaveWithException()
$this->address->expects($this->once())
->method('updateData')
->with($customerAddress);
$this->address->expects($this->once())
->method('sanitize')
->willReturn($this->address);
$this->address->expects($this->once())
->method('validate')
->willReturn($errors);
Expand Down Expand Up @@ -446,6 +459,9 @@ public function testSaveWithInvalidRegion()
$this->address->expects($this->never())
->method('getRegionId')
->willReturn(null);
$this->address->expects($this->once())
->method('sanitize')
->willReturn($this->address);
$this->address->expects($this->once())
->method('validate')
->willReturn($errors);
Expand Down Expand Up @@ -487,6 +503,9 @@ public function testSaveWithInvalidRegionId()
$this->address->expects($this->never())
->method('getRegion')
->willReturn('');
$this->address->expects($this->once())
->method('sanitize')
->willReturn($this->address);
$this->address->expects($this->once())
->method('validate')
->willReturn($errors);
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/Customer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,13 @@
</argument>
</arguments>
</type>
<type name="Magento\Customer\Model\Address\CompositeSanitizer">
<arguments>
<argument name="sanitizers" xsi:type="array">
<item name="templateVars" xsi:type="object">Magento\Customer\Model\Address\Sanitizer\TemplateVars</item>
</argument>
</arguments>
</type>
<type name="Magento\Customer\Model\ResourceModel\Group\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">customer_group</argument>
Expand Down