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

Commit

Permalink
Fixing Hostname validator subdomain underscore allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
bacinsky authored and weierophinney committed Dec 13, 2018
1 parent 8fa16a6 commit f4c26cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ public function isValid($value)

// Check each hostname part
$check = 0;
$lastDomainPart = end($domainParts);
foreach ($domainParts as $domainPart) {
// Decode Punycode domain names to IDN
if (strpos($domainPart, 'xn--') === 0) {
Expand Down Expand Up @@ -2118,7 +2119,9 @@ public function isValid($value)

// Check each domain part
$checked = false;
foreach ($regexChars as $regexKey => $regexChar) {
$isSubDomain = $domainPart != $lastDomainPart;
$partRegexChars = $isSubDomain ? ['/^[a-z0-9_\x2d]{1,63}$/i'] + $regexChars : $regexChars;
foreach ($partRegexChars as $regexKey => $regexChar) {
$status = preg_match($regexChar, $domainPart);
if ($status > 0) {
$length = 63;
Expand Down
24 changes: 24 additions & 0 deletions test/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ public function testDashes()
}
}

/**
* Ensure the underscore character tests work as expected
*
*/
public function testUnderscores()
{
$valuesExpected = [
[Hostname::ALLOW_DNS, true, [
'_subdomain.domain.com', 'subdomain_.domain.com', 'sub_domain.domain.com', 'sub__domain.domain.com'
]],
[Hostname::ALLOW_DNS, false, ['_domain.com', 'domain_.com', 'do_main.com']]
];
foreach ($valuesExpected as $element) {
$validator = new Hostname($element[0]);
foreach ($element[2] as $input) {
$this->assertEquals(
$element[1],
$validator->isValid($input),
implode("\n", $validator->getMessages()) . $input
);
}
}
}

/**
* Ensures that getMessages() returns expected default value
*
Expand Down

0 comments on commit f4c26cf

Please sign in to comment.