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

Commit

Permalink
Merge pull request #256 from SiebeVE/master
Browse files Browse the repository at this point in the history
Fix fault subdomain when not checking TLD issue #255
  • Loading branch information
weierophinney committed Jan 29, 2019
2 parents 3f11e95 + 2632ae0 commit ac5a895
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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 ac5a895

Please sign in to comment.