Skip to content

Commit e39e8ba

Browse files
committed
Allow underscores in cnames
1 parent 05fa0aa commit e39e8ba

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

src/Structure/Dns/AbstractDnsRecord.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getObject(): \stdClass
6666
return $obj;
6767
}
6868

69-
protected function validateHostname(string $hostname, bool $allowOrigin = true): string
69+
protected function validateHostname(string $hostname, bool $allowOrigin = true, bool $lax = false): string
7070
{
7171
if ($allowOrigin && in_array($hostname, ['', '@', '*'])) {
7272
return $hostname;
@@ -79,9 +79,12 @@ protected function validateHostname(string $hostname, bool $allowOrigin = true):
7979

8080
// remove special characters from labels, as we consider them valid, then send through filter_var
8181
$filtered = preg_replace('(^\*\._|^\*\.|^_|\._|\#)', '', $ascii);
82+
if ($lax) {
83+
$filtered = str_replace('_', '', $filtered);
84+
}
8285
$filtered = filter_var($filtered, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
8386

84-
// did the check pass and did we removed leading underscores? if so, use original value;
87+
// did the check pass and did we remove leading underscores? if so, use original value;
8588
if (false !== $filtered && $filtered !== $hostname) {
8689
$filtered = $hostname;
8790
}

src/Structure/Dns/DnsCNAMERecord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getContent(): string
2020
private function setContent(string $content): void
2121
{
2222
try {
23-
$filtered = $this->validateHostname($content);
23+
$filtered = $this->validateHostname($content, lax: true);
2424
$this->content = $filtered;
2525
} catch (\Exception $exception) {
2626
throw new \InvalidArgumentException('Invalid value for content: "'.$content.'"');

tests/Structure/Dns/DnsTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testDNSCNAMERecord()
162162
$this->assertEquals($r->getContent(), $o->content);
163163
}
164164

165-
/** @dataProvider dataHostnameValues */
165+
/** @dataProvider dataHostnameValuesCname */
166166
public function testDNSCNAMERecordContentValidation($address, $isValid)
167167
{
168168
if (!$isValid) {
@@ -450,6 +450,25 @@ public static function dataHostnameValues()
450450
];
451451
}
452452

453+
public static function dataHostnameValuesCname()
454+
{
455+
return [
456+
['', true],
457+
['@', true],
458+
['example', true],
459+
['example.com', true],
460+
['example.com ', false],
461+
[' example.com', false],
462+
['127.0.0.1', true],
463+
['2001:0db8:85a3:0000:0000:8a2e:0370:7334', false],
464+
['yellow-banana.example.com', true],
465+
['brown_banana.example.com', true],
466+
['_banana.example.com', true],
467+
['__banana.example.com', true],
468+
['-banana.example.com', false],
469+
];
470+
}
471+
453472
public static function dataHostnameValuesNoOrigin()
454473
{
455474
return [

0 commit comments

Comments
 (0)