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

Fix fault subdomain when not checking TLD issue #255 #256

Merged
merged 2 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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