Bugfix: domain autoconfig regular expression is too large

This commit is contained in:
the-djmaze 2024-02-05 21:29:25 +01:00
parent 3ec93d11ea
commit cbcab5b981
2 changed files with 20 additions and 15 deletions

View file

@ -14,16 +14,19 @@ abstract class Autoconfig
$autoconfig = static::resolve($domain, $emailaddress); $autoconfig = static::resolve($domain, $emailaddress);
// Else try MX // Else try MX
if (!$autoconfig) { if (!$autoconfig) {
$regex = '/([^\.]+\.(?:' // regular expression is too large
. \str_replace('.', '\\.', \implode('|', static::publicsuffixes())) $suffixes = static::publicsuffixes();
. '|[^\.]+'
. '))$/';
$hostnames = []; $hostnames = [];
foreach (\SnappyMail\DNS::MX($domain) as $hostname) { foreach (\SnappyMail\DNS::MX($domain) as $hostname) {
$mxbasedomain = [];
// Extract only the second-level domain from the MX hostname // Extract only the second-level domain from the MX hostname
if (\preg_match($regex, $hostname, $mxbasedomain)) { $mxbasedomain = \explode('.', $hostname);
$mxfulldomain = $mxbasedomain = $mxbasedomain[1]; $i = -2;
while (\in_array(\implode('.', \array_slice($mxbasedomain, $i)), $suffixes)) {
--$i;
}
$mxbasedomain = \implode('.', \array_slice($mxbasedomain, $i));
if ($mxbasedomain) {
$mxfulldomain = $mxbasedomain;
if (\substr_count($hostname, '.') > \substr_count($mxbasedomain, '.')) { if (\substr_count($hostname, '.') > \substr_count($mxbasedomain, '.')) {
// Remove the first component from the MX hostname // Remove the first component from the MX hostname
$mxfulldomain = \explode('.', $hostname, 2)[1]; $mxfulldomain = \explode('.', $hostname, 2)[1];
@ -32,12 +35,14 @@ abstract class Autoconfig
} }
} }
foreach ($hostnames as $mxfulldomain => $mxbasedomain) { foreach ($hostnames as $mxfulldomain => $mxbasedomain) {
$autoconfig = static::resolve($mxfulldomain, $emailaddress); if ($domain != $mxfulldomain) {
if (!$autoconfig && $mxfulldomain != $mxbasedomain) { $autoconfig = static::resolve($mxfulldomain, $emailaddress);
$autoconfig = static::resolve($mxbasedomain, $emailaddress); if (!$autoconfig && $mxfulldomain != $mxbasedomain && $domain != $mxbasedomain) {
} $autoconfig = static::resolve($mxbasedomain, $emailaddress);
if ($autoconfig) { }
break; if ($autoconfig) {
break;
}
} }
} }
} }
@ -96,7 +101,7 @@ abstract class Autoconfig
if ($data) { if ($data) {
$list = \array_filter( $list = \array_filter(
\explode("\n", $data), \explode("\n", $data),
fn($text) => \strlen($text) && '/' !== $text[0] fn($text) => \strlen($text) && '/' !== $text[0] && '*' !== $text[0] && \substr_count($text, '.')
); );
} }
// Don't lookup for 24 hours // Don't lookup for 24 hours

View file

@ -59,7 +59,7 @@ abstract class DNS
} }
} }
if (!$mxhosts) { if (!$mxhosts) {
\getmxrr($hostname, $mxhosts); \getmxrr($domain, $mxhosts);
} }
\ksort($mxhosts); \ksort($mxhosts);
return \array_values($mxhosts); return \array_values($mxhosts);