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

Commit

Permalink
Merge branch 'hotfix/256' into develop
Browse files Browse the repository at this point in the history
Forward port #256
  • Loading branch information
weierophinney committed Jan 29, 2019
2 parents a2d53b5 + 8068d9d commit 9b6ee2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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

Expand Down
5 changes: 5 additions & 0 deletions src/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}

/**
Expand All @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions test/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 9b6ee2b

Please sign in to comment.