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

Commit f4c26cf

Browse files
bacinskyweierophinney
authored andcommitted
Fixing Hostname validator subdomain underscore allowed
1 parent 8fa16a6 commit f4c26cf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Hostname.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ public function isValid($value)
20902090

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

21192120
// Check each domain part
21202121
$checked = false;
2121-
foreach ($regexChars as $regexKey => $regexChar) {
2122+
$isSubDomain = $domainPart != $lastDomainPart;
2123+
$partRegexChars = $isSubDomain ? ['/^[a-z0-9_\x2d]{1,63}$/i'] + $regexChars : $regexChars;
2124+
foreach ($partRegexChars as $regexKey => $regexChar) {
21222125
$status = preg_match($regexChar, $domainPart);
21232126
if ($status > 0) {
21242127
$length = 63;

test/HostnameTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ public function testDashes()
119119
}
120120
}
121121

122+
/**
123+
* Ensure the underscore character tests work as expected
124+
*
125+
*/
126+
public function testUnderscores()
127+
{
128+
$valuesExpected = [
129+
[Hostname::ALLOW_DNS, true, [
130+
'_subdomain.domain.com', 'subdomain_.domain.com', 'sub_domain.domain.com', 'sub__domain.domain.com'
131+
]],
132+
[Hostname::ALLOW_DNS, false, ['_domain.com', 'domain_.com', 'do_main.com']]
133+
];
134+
foreach ($valuesExpected as $element) {
135+
$validator = new Hostname($element[0]);
136+
foreach ($element[2] as $input) {
137+
$this->assertEquals(
138+
$element[1],
139+
$validator->isValid($input),
140+
implode("\n", $validator->getMessages()) . $input
141+
);
142+
}
143+
}
144+
}
145+
122146
/**
123147
* Ensures that getMessages() returns expected default value
124148
*

0 commit comments

Comments
 (0)