Skip to content

Fix - Customer address form allows random code in the name fields #38331 #38345

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

Merged
merged 6 commits into from
May 28, 2024
Merged
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
61 changes: 61 additions & 0 deletions app/code/Magento/Customer/Model/Validator/City.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Model\Validator;

use Magento\Customer\Model\Customer;
use Magento\Framework\Validator\AbstractValidator;

/**
* Customer city fields validator.
*/
class City extends AbstractValidator
{
/**
* Allowed characters:
*
* \p{L}: Unicode letters.
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
* ': Apostrophe mark.
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
*/
private const PATTERN_CITY = '/(?:[\p{L}\p{M}\s\-\']{1,100})/u';

/**
* Validate city fields.
*
* @param Customer $customer
* @return bool
*/
public function isValid($customer)
{
if (!$this->isValidCity($customer->getCity())) {
parent::_addMessages([[
'city' => "Invalid City. Please use A-Z, a-z, 0-9, -, ', spaces"
]]);
}

return count($this->_messages) == 0;
}

/**
* Check if city field is valid.
*
* @param string|null $cityValue
* @return bool
*/
private function isValidCity($cityValue)
{
if ($cityValue != null) {
if (preg_match(self::PATTERN_CITY, $cityValue, $matches)) {
return $matches[0] == $cityValue;
}
}

return true;
}
}
68 changes: 68 additions & 0 deletions app/code/Magento/Customer/Model/Validator/Street.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Model\Validator;

use Magento\Customer\Model\Customer;
use Magento\Framework\Validator\AbstractValidator;

/**
* Customer street fields validator.
*/
class Street extends AbstractValidator
{
/**
* Allowed characters:
*
* \p{L}: Unicode letters.
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
* ,: Comma.
* -: Hyphen.
* .: Period.
* `'’: Single quotes, both regular and right single quotation marks.
* &: Ampersand.
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
* \d: Digits (0-9).
*/
private const PATTERN_STREET = "/(?:[\p{L}\p{M}\"[],-.'’`&\s\d]){1,255}+/u";

/**
* Validate street fields.
*
* @param Customer $customer
* @return bool
*/
public function isValid($customer)
{
foreach ($customer->getStreet() as $street) {
if (!$this->isValidStreet($street)) {
parent::_addMessages([[
'street' => "Invalid Street Address. Please use A-Z, a-z, 0-9, , - . ' ’ ` & spaces"
]]);
}
}

return count($this->_messages) == 0;
}

/**
* Check if street field is valid.
*
* @param string|null $streetValue
* @return bool
*/
private function isValidStreet($streetValue)
{
if ($streetValue != null) {
if (preg_match(self::PATTERN_STREET, $streetValue, $matches)) {
return $matches[0] == $streetValue;
}
}

return true;
}
}
61 changes: 61 additions & 0 deletions app/code/Magento/Customer/Model/Validator/Telephone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Model\Validator;

use Magento\Customer\Model\Customer;
use Magento\Framework\Validator\AbstractValidator;

/**
* Customer telephone fields validator.
*/
class Telephone extends AbstractValidator
{
/**
* Allowed char:
*
* \() :Matches open and close parentheses
* \+: Matches the plus sign.
* \-: Matches the hyphen.
* \d: Digits (0-9).
*/
private const PATTERN_TELEPHONE = '/(?:[\d\s\+\-\()]{1,20})/u';

/**
* Validate telephone fields.
*
* @param Customer $customer
* @return bool
*/
public function isValid($customer)
{
if (!$this->isValidTelephone($customer->getTelephone())) {
parent::_addMessages([[
'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ) and space."
]]);
}

return count($this->_messages) == 0;
}

/**
* Check if telephone field is valid.
*
* @param string|null $telephoneValue
* @return bool
*/
private function isValidTelephone($telephoneValue)
{
if ($telephoneValue != null) {
if (preg_match(self::PATTERN_TELEPHONE, (string) $telephoneValue, $matches)) {
return $matches[0] == $telephoneValue;
}
}

return true;
}
}
85 changes: 85 additions & 0 deletions app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Test\Unit\Model\Validator;

use Magento\Customer\Model\Validator\City;
use Magento\Customer\Model\Customer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Customer city validator tests
*/
class CityTest extends TestCase
{
/**
* @var City
*/
private City $nameValidator;

/**
* @var Customer|MockObject
*/
private MockObject $customerMock;

/**
* @return void
*/
protected function setUp(): void
{
$this->nameValidator = new City;
$this->customerMock = $this
->getMockBuilder(Customer::class)
->disableOriginalConstructor()
->addMethods(['getCity'])
->getMock();
}

/**
* Test for allowed apostrophe and other punctuation characters in customer names
*
* @param string $city
* @param string $message
* @return void
* @dataProvider expectedPunctuationInNamesDataProvider
*/
public function testValidateCorrectPunctuationInNames(
string $city,
string $message
) {
$this->customerMock->expects($this->once())->method('getCity')->willReturn($city);

$isValid = $this->nameValidator->isValid($this->customerMock);
$this->assertTrue($isValid, $message);
}

/**
* @return array
*/
public function expectedPunctuationInNamesDataProvider(): array
{
return [
[
'city' => 'Москва',
'message' => 'Unicode letters must be allowed in city'
],
[
'city' => 'Мо́сква',
'message' => 'Unicode marks must be allowed in city'
],
[
'city' => ' Moscow \'',
'message' => 'Apostrophe characters must be allowed in city'
],
[
'city' => ' Moscow Moscow',
'message' => 'Whitespace characters must be allowed in city'
]
];
}
}
Loading