From 414a3463a8086d684e8b498c5f49a43fc84b543b Mon Sep 17 00:00:00 2001 From: Deki Akbar Date: Wed, 10 Jan 2024 19:31:31 +0700 Subject: [PATCH 1/4] Fix - Customer address form allows random code in the name fields #38331 --- app/code/Magento/Customer/etc/validation.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/code/Magento/Customer/etc/validation.xml b/app/code/Magento/Customer/etc/validation.xml index bac6e54afa7b5..114733e66d04d 100644 --- a/app/code/Magento/Customer/etc/validation.xml +++ b/app/code/Magento/Customer/etc/validation.xml @@ -46,11 +46,17 @@ + + + + + + From 6a458bd7b17a32b79f1c99cc35fb08aea5eb0b61 Mon Sep 17 00:00:00 2001 From: Deki Akbar Date: Thu, 18 Jan 2024 06:33:56 +0700 Subject: [PATCH 2/4] add validation to Street, city, and phone number fields --- .../Magento/Customer/Model/Validator/City.php | 66 +++++++++++++++++++ .../Customer/Model/Validator/Street.php | 66 +++++++++++++++++++ .../Customer/Model/Validator/Telephone.php | 60 +++++++++++++++++ app/code/Magento/Customer/etc/validation.xml | 18 +++++ 4 files changed, 210 insertions(+) create mode 100644 app/code/Magento/Customer/Model/Validator/City.php create mode 100644 app/code/Magento/Customer/Model/Validator/Street.php create mode 100644 app/code/Magento/Customer/Model/Validator/Telephone.php diff --git a/app/code/Magento/Customer/Model/Validator/City.php b/app/code/Magento/Customer/Model/Validator/City.php new file mode 100644 index 0000000000000..d90e511387450 --- /dev/null +++ b/app/code/Magento/Customer/Model/Validator/City.php @@ -0,0 +1,66 @@ +isValidCity($customer->getCity())) { + parent::_addMessages([[ + 'city' => 'City is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits' + ]]); + } + + 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; + } +} diff --git a/app/code/Magento/Customer/Model/Validator/Street.php b/app/code/Magento/Customer/Model/Validator/Street.php new file mode 100644 index 0000000000000..4dfb42ff0d06a --- /dev/null +++ b/app/code/Magento/Customer/Model/Validator/Street.php @@ -0,0 +1,66 @@ +getStreet() as $street) { + if (!$this->isValidStreet($street)) { + parent::_addMessages([['street' => 'Street is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits']]); + } + } + + 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; + } +} diff --git a/app/code/Magento/Customer/Model/Validator/Telephone.php b/app/code/Magento/Customer/Model/Validator/Telephone.php new file mode 100644 index 0000000000000..fcf419f2174d5 --- /dev/null +++ b/app/code/Magento/Customer/Model/Validator/Telephone.php @@ -0,0 +1,60 @@ +isValidTelephone($customer->getTelephone())) { + parent::_addMessages([['telephone' => 'Telephone is not valid! Allowed chars: Matches open and close parentheses, Matches any whitespace character, Matches the plus sign, Matches the hyphen, Digits (0-9)']]); + } + + 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, $telephoneValue, $matches)) { + return $matches[0] == $telephoneValue; + } + } + + return true; + } +} diff --git a/app/code/Magento/Customer/etc/validation.xml b/app/code/Magento/Customer/etc/validation.xml index 114733e66d04d..7fd6cfeb79472 100644 --- a/app/code/Magento/Customer/etc/validation.xml +++ b/app/code/Magento/Customer/etc/validation.xml @@ -51,12 +51,30 @@ + + + + + + + + + + + + + + + + + + From 580dcf12587e7ade2516f39eff790a01e0035037 Mon Sep 17 00:00:00 2001 From: Deki Akbar Date: Thu, 18 Jan 2024 19:25:44 +0700 Subject: [PATCH 3/4] update city, street, phone number validation and unit test --- .../Magento/Customer/Model/Validator/City.php | 11 +- .../Customer/Model/Validator/Street.php | 10 +- .../Customer/Model/Validator/Telephone.php | 11 +- .../Test/Unit/Model/Validator/CityTest.php | 85 +++++++++++ .../Test/Unit/Model/Validator/StreetTest.php | 133 ++++++++++++++++++ .../Unit/Model/Validator/TelephoneTest.php | 85 +++++++++++ 6 files changed, 318 insertions(+), 17 deletions(-) create mode 100644 app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php create mode 100644 app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php create mode 100644 app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php diff --git a/app/code/Magento/Customer/Model/Validator/City.php b/app/code/Magento/Customer/Model/Validator/City.php index d90e511387450..0b53551dfd88f 100644 --- a/app/code/Magento/Customer/Model/Validator/City.php +++ b/app/code/Magento/Customer/Model/Validator/City.php @@ -20,15 +20,10 @@ class City extends AbstractValidator * * \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. + * ': Apostrophe mark. * \s: Whitespace characters (spaces, tabs, newlines, etc.). - * \d: Digits (0-9). */ - private const PATTERN_CITY = '/(?:[\p{L}\p{M}\,\-\.\'’`&\s\d]){1,255}+/u'; + private const PATTERN_CITY = '/(?:[\p{L}\p{M}\s\-\']{1,100})/u'; /** * Validate city fields. @@ -40,7 +35,7 @@ public function isValid($customer) { if (!$this->isValidCity($customer->getCity())) { parent::_addMessages([[ - 'city' => 'City is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits' + 'city' => "Invalid City. Please use A-Z, a-z, 0-9, -, ', spaces" ]]); } diff --git a/app/code/Magento/Customer/Model/Validator/Street.php b/app/code/Magento/Customer/Model/Validator/Street.php index 4dfb42ff0d06a..9b047911f2d8b 100644 --- a/app/code/Magento/Customer/Model/Validator/Street.php +++ b/app/code/Magento/Customer/Model/Validator/Street.php @@ -21,14 +21,14 @@ class Street extends AbstractValidator * \p{L}: Unicode letters. * \p{M}: Unicode marks (diacritic marks, accents, etc.). * ,: Comma. - * \-: Hyphen. - * \.: Period. + * -: 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'; + private const PATTERN_STREET = "/(?:[\p{L}\p{M},-.'’`&\s\d]){1,255}+/u"; /** * Validate street fields. @@ -40,7 +40,9 @@ public function isValid($customer) { foreach ($customer->getStreet() as $street) { if (!$this->isValidStreet($street)) { - parent::_addMessages([['street' => 'Street is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits']]); + parent::_addMessages([[ + 'street' => "Invalid Street Address. Please use A-Z, a-z, 0-9, , - . ' ’ ` & spaces" + ]]); } } diff --git a/app/code/Magento/Customer/Model/Validator/Telephone.php b/app/code/Magento/Customer/Model/Validator/Telephone.php index fcf419f2174d5..52f9db2287bd2 100644 --- a/app/code/Magento/Customer/Model/Validator/Telephone.php +++ b/app/code/Magento/Customer/Model/Validator/Telephone.php @@ -18,14 +18,13 @@ class Telephone extends AbstractValidator /** * Allowed char: * - * \(\) :Matches open and close parentheses - * \s: Matches any whitespace character. + * \() :Matches open and close parentheses * \+: Matches the plus sign. * \-: Matches the hyphen. * \d: Digits (0-9). */ - private const PATTERN_TELEPHONE = '/(?:[\(\)\+\-\s\d]){1,255}+/u'; - + private const PATTERN_TELEPHONE = '/(?:[\d\+\-\()]{1,20})/u'; + /** * Validate telephone fields. * @@ -35,7 +34,9 @@ class Telephone extends AbstractValidator public function isValid($customer) { if (!$this->isValidTelephone($customer->getTelephone())) { - parent::_addMessages([['telephone' => 'Telephone is not valid! Allowed chars: Matches open and close parentheses, Matches any whitespace character, Matches the plus sign, Matches the hyphen, Digits (0-9)']]); + parent::_addMessages([[ + 'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ). space not allowed." + ]]); } return count($this->_messages) == 0; diff --git a/app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php b/app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php new file mode 100644 index 0000000000000..9c15427154fea --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php @@ -0,0 +1,85 @@ +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' + ] + ]; + } +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php b/app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php new file mode 100644 index 0000000000000..6d40bec460b3e --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php @@ -0,0 +1,133 @@ +nameValidator = new Street; + $this->customerMock = $this + ->getMockBuilder(Customer::class) + ->disableOriginalConstructor() + ->addMethods(['getStreet']) + ->getMock(); + } + + /** + * Test for allowed apostrophe and other punctuation characters in customer names + * + * @param array $street + * @param string $message + * @return void + * @dataProvider expectedPunctuationInNamesDataProvider + */ + public function testValidateCorrectPunctuationInNames( + array $street, + string $message + ) { + $this->customerMock->expects($this->once())->method('getStreet')->willReturn($street); + + $isValid = $this->nameValidator->isValid($this->customerMock); + $this->assertTrue($isValid, $message); + } + + /** + * @return array + */ + public function expectedPunctuationInNamesDataProvider(): array + { + return [ + [ + 'street' => [ + "123 Rue de l'Étoile", + "Ville d'Ölives, Çôte d'Azur", + "Çôte d'Azur" + ], + 'message' => 'Unicode marks and Unicode letters must be allowed in street' + ], + [ + 'street' => [ + '876 Elm Way, Redwood Lodge', + '456 Pine Street, Serenity Cottage', + '321 Birch Boulevard, Willow Retreat' + ], + 'message' => 'Comma must be allowed in street' + ], + [ + 'street' => [ + '321 Birch Boulevard-Retreat', + '234 Spruce Place-Residence', + '456 Pine Street-Haven' + ], + 'message' => 'Hyphen must be allowed in street' + ], + [ + 'street' => [ + '1234 Elm St.', + 'Main. Street', + '1234 Elm St' + ], + 'message' => 'Period must be allowed in street' + ], + [ + 'street' => [ + 'O\'Connell Street', + 'O`Connell Street', + '321 Birch Boulevard ’Willow Retreat’' + ], + 'message' => 'quotes must be allowed in street' + ], + [ + 'street' => [ + '123 Main Street & Elm Avenue', + '456 Pine Street & Maple Avenue', + '789 Oak Lane & Cedar Road' + ], + 'message' => 'Ampersand must be allowed in street' + ], + [ + 'street' => [ + 'Oak Lane Space', + 'Birch Boulevard Space', + 'Spruce Place' + ], + 'message' => 'Whitespace must be allowed in street' + ], + [ + 'street' => [ + '234 Spruce Place', + '321 Birch Boulevard', + '876 Elm Way' + ], + 'message' => 'Digits must be allowed in street' + ] + ]; + } +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php b/app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php new file mode 100644 index 0000000000000..47a9d6da18831 --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php @@ -0,0 +1,85 @@ +nameValidator = new Telephone; + $this->customerMock = $this + ->getMockBuilder(Customer::class) + ->disableOriginalConstructor() + ->addMethods(['getTelephone']) + ->getMock(); + } + + /** + * Test for allowed apostrophe and other punctuation characters in customer names + * + * @param string $telephone + * @param string $message + * @return void + * @dataProvider expectedPunctuationInNamesDataProvider + */ + public function testValidateCorrectPunctuationInNames( + string $telephone, + string $message + ) { + $this->customerMock->expects($this->once())->method('getTelephone')->willReturn($telephone); + + $isValid = $this->nameValidator->isValid($this->customerMock); + $this->assertTrue($isValid, $message); + } + + /** + * @return array + */ + public function expectedPunctuationInNamesDataProvider(): array + { + return [ + [ + 'telephone' => '(1)99887766', + 'message' => 'parentheses must be allowed in telephone' + ], + [ + 'telephone' => '+6255554444', + 'message' => 'plus sign be allowed in telephone' + ], + [ + 'telephone' => '555-555-555', + 'message' => 'hyphen must be allowed in telephone' + ], + [ + 'telephone' => '123456789', + 'message' => 'Digits (numbers) must be allowed in telephone' + ] + ]; + } +} From 164d81857a54427a18f3112cfe0812f6c21f35b8 Mon Sep 17 00:00:00 2001 From: Deki Akbar Date: Fri, 19 Jan 2024 01:57:26 +0700 Subject: [PATCH 4/4] allow [] in street, allow white space in phone number --- app/code/Magento/Customer/Model/Validator/Street.php | 2 +- app/code/Magento/Customer/Model/Validator/Telephone.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Customer/Model/Validator/Street.php b/app/code/Magento/Customer/Model/Validator/Street.php index 9b047911f2d8b..7de57d0ed32ef 100644 --- a/app/code/Magento/Customer/Model/Validator/Street.php +++ b/app/code/Magento/Customer/Model/Validator/Street.php @@ -28,7 +28,7 @@ class Street extends AbstractValidator * \s: Whitespace characters (spaces, tabs, newlines, etc.). * \d: Digits (0-9). */ - private const PATTERN_STREET = "/(?:[\p{L}\p{M},-.'’`&\s\d]){1,255}+/u"; + private const PATTERN_STREET = "/(?:[\p{L}\p{M}\"[],-.'’`&\s\d]){1,255}+/u"; /** * Validate street fields. diff --git a/app/code/Magento/Customer/Model/Validator/Telephone.php b/app/code/Magento/Customer/Model/Validator/Telephone.php index 52f9db2287bd2..0c85cb51f7e3d 100644 --- a/app/code/Magento/Customer/Model/Validator/Telephone.php +++ b/app/code/Magento/Customer/Model/Validator/Telephone.php @@ -23,7 +23,7 @@ class Telephone extends AbstractValidator * \-: Matches the hyphen. * \d: Digits (0-9). */ - private const PATTERN_TELEPHONE = '/(?:[\d\+\-\()]{1,20})/u'; + private const PATTERN_TELEPHONE = '/(?:[\d\s\+\-\()]{1,20})/u'; /** * Validate telephone fields. @@ -35,7 +35,7 @@ public function isValid($customer) { if (!$this->isValidTelephone($customer->getTelephone())) { parent::_addMessages([[ - 'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ). space not allowed." + 'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ) and space." ]]); } @@ -51,7 +51,7 @@ public function isValid($customer) private function isValidTelephone($telephoneValue) { if ($telephoneValue != null) { - if (preg_match(self::PATTERN_TELEPHONE, $telephoneValue, $matches)) { + if (preg_match(self::PATTERN_TELEPHONE, (string) $telephoneValue, $matches)) { return $matches[0] == $telephoneValue; } }