From d501af31e7f6deaaa491a733040c718acd8693a7 Mon Sep 17 00:00:00 2001 From: Stephane Rathgeber Date: Mon, 4 Mar 2024 14:59:16 +0100 Subject: [PATCH 1/2] fix DNS validation --- src/Handlers/AbstractDnsHandler.php | 18 ++---------------- src/Records/DnsUtils.php | 2 +- src/Records/ExtendedTxtRecords.php | 2 +- src/Regex.php | 3 --- 4 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/Handlers/AbstractDnsHandler.php b/src/Handlers/AbstractDnsHandler.php index 2e82b71..834da82 100644 --- a/src/Handlers/AbstractDnsHandler.php +++ b/src/Handlers/AbstractDnsHandler.php @@ -88,26 +88,12 @@ protected function validateHostName(string $hostName): void $hostnameErrorInfo = 'Invalid hostname ' . json_encode($hostName); - if (strlen($hostName) < 3) { + if (!filter_var($hostName, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { throw new DnsHandlerException( - $hostnameErrorInfo . ' length. It must be 3 or more!', - DnsHandlerException::HOSTNAME_LENGTH_TOO_SMALL - ); - } - - if (!preg_match(Regex::DOMAIN_OR_SUBDOMAIN, $hostName)) { - throw new DnsHandlerException( - $hostnameErrorInfo . ' format! (characters "A-Za-z0-9.-", max length 63 chars allowed)', + $hostnameErrorInfo . ' format!', DnsHandlerException::HOSTNAME_FORMAT_INVALID ); } - - if (!preg_match(Regex::HOSTNAME_LENGTH, $hostName)) { - throw new DnsHandlerException( - $hostnameErrorInfo . ' length! (min 3, max 253 characters allowed)', - DnsHandlerException::HOSTNAME_LENGTH_INVALID - ); - } } /** diff --git a/src/Records/DnsUtils.php b/src/Records/DnsUtils.php index 2e7294b..b92f892 100644 --- a/src/Records/DnsUtils.php +++ b/src/Records/DnsUtils.php @@ -13,7 +13,7 @@ public static function isValidDomainOrSubdomain(string $domain): bool if (empty($domain) || strlen($domain) < 4) { return false; } - return preg_match(Regex::DOMAIN_OR_SUBDOMAIN, $domain) === 1; + return (bool) filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); } public static function ipV6Shortener(string $ipv6): string diff --git a/src/Records/ExtendedTxtRecords.php b/src/Records/ExtendedTxtRecords.php index c2975f4..4e1cb0e 100644 --- a/src/Records/ExtendedTxtRecords.php +++ b/src/Records/ExtendedTxtRecords.php @@ -103,7 +103,7 @@ public function isParentHostName($host): bool */ private function isDomainOrSubdomainHostName(string $host): bool { - return preg_match(Regex::DOMAIN_OR_SUBDOMAIN, $host) === 1; + return (bool) filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); } private function isDomainKeyHostName(string $host): bool diff --git a/src/Regex.php b/src/Regex.php index 35c8522..5e45a9f 100644 --- a/src/Regex.php +++ b/src/Regex.php @@ -4,9 +4,6 @@ class Regex { - public const DOMAIN_OR_SUBDOMAIN = '/^(([\w\d\_\-]+){1,63}\.)+(\w+){2,63}$/i'; - public const HOSTNAME_LENGTH = '/^.{3,253}$/'; - public const SPF_VALIDATION = '/^v=spf1 ([a-z0-9:.\/ ~\-_\+]+)/i'; public const DKIM_SELECTOR_VALUE = '/^([\w\_]+)\._domainkey.*/'; From 1a1d63863a14a11fa4d77ccc515e7edbb2f38299 Mon Sep 17 00:00:00 2001 From: Stephane Rathgeber Date: Wed, 2 Oct 2024 11:08:44 +0200 Subject: [PATCH 2/2] remove FILTER_FLAG_HOSTNAME, to allow _dnsauth.test.com --- src/Handlers/AbstractDnsHandler.php | 2 +- src/Records/DnsUtils.php | 2 +- src/Records/ExtendedTxtRecords.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Handlers/AbstractDnsHandler.php b/src/Handlers/AbstractDnsHandler.php index 834da82..8116078 100644 --- a/src/Handlers/AbstractDnsHandler.php +++ b/src/Handlers/AbstractDnsHandler.php @@ -88,7 +88,7 @@ protected function validateHostName(string $hostName): void $hostnameErrorInfo = 'Invalid hostname ' . json_encode($hostName); - if (!filter_var($hostName, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { + if (!filter_var($hostName, FILTER_VALIDATE_DOMAIN)) { throw new DnsHandlerException( $hostnameErrorInfo . ' format!', DnsHandlerException::HOSTNAME_FORMAT_INVALID diff --git a/src/Records/DnsUtils.php b/src/Records/DnsUtils.php index b92f892..dd00840 100644 --- a/src/Records/DnsUtils.php +++ b/src/Records/DnsUtils.php @@ -13,7 +13,7 @@ public static function isValidDomainOrSubdomain(string $domain): bool if (empty($domain) || strlen($domain) < 4) { return false; } - return (bool) filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); + return (bool) filter_var($domain, FILTER_VALIDATE_DOMAIN); } public static function ipV6Shortener(string $ipv6): string diff --git a/src/Records/ExtendedTxtRecords.php b/src/Records/ExtendedTxtRecords.php index 4e1cb0e..769d616 100644 --- a/src/Records/ExtendedTxtRecords.php +++ b/src/Records/ExtendedTxtRecords.php @@ -103,7 +103,7 @@ public function isParentHostName($host): bool */ private function isDomainOrSubdomainHostName(string $host): bool { - return (bool) filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); + return (bool) filter_var($host, FILTER_VALIDATE_DOMAIN); } private function isDomainKeyHostName(string $host): bool