Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit dbde26f

Browse files
committed
Merge branch 'hotfix/256'
Close #256 Fixes #255
2 parents 3f11e95 + 8068d9d commit dbde26f

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#256](https://github.com/zendframework/zend-validator/pull/256) fixes hostname validation when omitting the TLD from verification,
26+
ensuring validation of the domain segment considers all URI criteria.
2627

2728
## 2.11.0 - 2018-12-13
2829

src/Hostname.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,7 @@ public function isValid($value)
20532053
}
20542054

20552055
// Match TLD against known list
2056+
$removedTld = false;
20562057
if ($this->getTldCheck()) {
20572058
if (! in_array(strtolower($this->tld), $this->validTlds)
20582059
&& ! in_array($this->tld, $this->validTlds)) {
@@ -2063,6 +2064,7 @@ public function isValid($value)
20632064
// We have already validated that the TLD is fine. We don't want it to go through the below
20642065
// checks as new UTF-8 TLDs will incorrectly fail if there is no IDN regex for it.
20652066
array_pop($domainParts);
2067+
$removedTld = true;
20662068
}
20672069

20682070
/**
@@ -2083,6 +2085,9 @@ public function isValid($value)
20832085
// Check each hostname part
20842086
$check = 0;
20852087
$lastDomainPart = end($domainParts);
2088+
if (! $removedTld) {
2089+
$lastDomainPart = prev($domainParts);
2090+
}
20862091
foreach ($domainParts as $domainPart) {
20872092
// Decode Punycode domain names to IDN
20882093
if (strpos($domainPart, 'xn--') === 0) {

test/HostnameTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ public function testValidatorHandlesUnderscoresInDomainsCorrectly($input, $asser
173173
$this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages()));
174174
}
175175

176+
/**
177+
* Ensure the underscore character tests work as expected when not using tld check
178+
*
179+
* @dataProvider domainsWithUnderscores
180+
* @param string $input
181+
* @param string $assertion
182+
*/
183+
public function testValidatorHandlesUnderscoresInDomainsWithoutTldCheckCorrectly($input, $assertion)
184+
{
185+
$validator = new Hostname([
186+
'useTldCheck' => false,
187+
'allow' => Hostname::ALLOW_DNS,
188+
]);
189+
$this->$assertion($validator->isValid($input), implode("\n", $validator->getMessages()));
190+
}
191+
176192
/**
177193
* Ensures that getMessages() returns expected default value
178194
*

0 commit comments

Comments
 (0)