From 93c606a51c0f23b95342a2687b4356b7c70f242c Mon Sep 17 00:00:00 2001 From: Siebe Vanden Eynden Date: Wed, 23 Jan 2019 14:27:36 +0100 Subject: [PATCH 1/3] Fix fault subdomain when not checking TLD issue #255 --- src/Hostname.php | 5 +++++ test/HostnameTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Hostname.php b/src/Hostname.php index a97d787d1..ee968511f 100644 --- a/src/Hostname.php +++ b/src/Hostname.php @@ -2053,6 +2053,7 @@ public function isValid($value) } // Match TLD against known list + $removedTld = false; if ($this->getTldCheck()) { if (! in_array(strtolower($this->tld), $this->validTlds) && ! in_array($this->tld, $this->validTlds)) { @@ -2063,6 +2064,7 @@ public function isValid($value) // We have already validated that the TLD is fine. We don't want it to go through the below // checks as new UTF-8 TLDs will incorrectly fail if there is no IDN regex for it. array_pop($domainParts); + $removedTld = true; } /** @@ -2083,6 +2085,9 @@ public function isValid($value) // Check each hostname part $check = 0; $lastDomainPart = end($domainParts); + if(!$removedTld){ + $lastDomainPart = prev($domainParts); + } foreach ($domainParts as $domainPart) { // Decode Punycode domain names to IDN if (strpos($domainPart, 'xn--') === 0) { diff --git a/test/HostnameTest.php b/test/HostnameTest.php index 562a3a5c2..fa71e1ab2 100644 --- a/test/HostnameTest.php +++ b/test/HostnameTest.php @@ -173,6 +173,22 @@ public function testValidatorHandlesUnderscoresInDomainsCorrectly($input, $asser $this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages())); } + /** + * Ensure the underscore character tests work as expected when not using tld check + * + * @dataProvider domainsWithUnderscores + * @param string $input + * @param string $assertion + */ + public function testValidatorHandlesUnderscoresInDomainsWithoutTldCheckCorrectly($input, $assertion) + { + $validator = new Hostname([ + 'useTldCheck' => false, + 'allow' => Hostname::ALLOW_DNS, + ]); + $this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages())); + } + /** * Ensures that getMessages() returns expected default value * From 2632ae004992710083d660c969f8885a5a9f4281 Mon Sep 17 00:00:00 2001 From: Siebe Vanden Eynden Date: Wed, 23 Jan 2019 14:31:01 +0100 Subject: [PATCH 2/3] Code style fixes --- src/Hostname.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hostname.php b/src/Hostname.php index ee968511f..31a5329e3 100644 --- a/src/Hostname.php +++ b/src/Hostname.php @@ -2085,7 +2085,7 @@ public function isValid($value) // Check each hostname part $check = 0; $lastDomainPart = end($domainParts); - if(!$removedTld){ + if (! $removedTld) { $lastDomainPart = prev($domainParts); } foreach ($domainParts as $domainPart) { From 8068d9dc1048965307aab230299a3b0f71100097 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 29 Jan 2019 16:20:48 -0600 Subject: [PATCH 3/3] Adds CHANGELOG entry for #256 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80dbd3c8b..24dbb6b03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#256](https://github.com/zendframework/zend-validator/pull/256) fixes hostname validation when omitting the TLD from verification, + ensuring validation of the domain segment considers all URI criteria. ## 2.11.0 - 2018-12-13