-
-
Notifications
You must be signed in to change notification settings - Fork 227
Fix connection timeout issue when try combine MX + A records for specfic DNS when A record not exists #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.x
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| /** | ||
| * Reserved Top Level DNS Names (https://tools.ietf.org/html/rfc2606#section-2), | ||
| * mDNS and private DNS Namespaces (https://tools.ietf.org/html/rfc6762#appendix-G) | ||
| * | ||
| * | ||
| * @var string[] | ||
| */ | ||
| public const RESERVED_DNS_TOP_LEVEL_NAMES = [ | ||
|
|
@@ -145,14 +145,20 @@ | |
| */ | ||
| private function validateDnsRecords($host): bool | ||
| { | ||
| $dnsRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_A + DNS_MX); | ||
| $dnsRecords = []; | ||
|
|
||
| if ($dnsRecordsResult->withError()) { | ||
| $this->error = new InvalidEmail(new UnableToGetDNSRecord(), ''); | ||
| return false; | ||
| $mxRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_MX); | ||
|
|
||
| if (! $mxRecordsResult->withError()) { | ||
| $dnsRecords = $mxRecordsResult->getRecords(); | ||
| } | ||
|
|
||
| $dnsRecords = $dnsRecordsResult->getRecords(); | ||
| // Combined check for A+MX can fail with connection timed out, even in the presence of valid MX record | ||
| $aRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_A); | ||
|
|
||
| if (! $aRecordsResult->withError()) { | ||
| $dnsRecords = array_merge($dnsRecords, $aRecordsResult->getRecords()); | ||
| } | ||
|
|
||
| // Combined check for A+MX+AAAA can fail with SERVFAIL, even in the presence of valid A/MX records | ||
| $aaaaRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_AAAA); | ||
|
|
@@ -163,13 +169,26 @@ | |
|
|
||
| // No MX, A or AAAA DNS records | ||
| if ($dnsRecords === []) { | ||
| if ($mxRecordsResult->withError() | ||
| && $aRecordsResult->withError() | ||
| && $aaaaRecordsResult->withError() | ||
| ) { | ||
| $this->error = new InvalidEmail(new UnableToGetDNSRecord(), ''); | ||
| return false; | ||
| } | ||
|
|
||
| $this->error = new InvalidEmail(new ReasonNoDNSRecord(), ''); | ||
| return false; | ||
| } | ||
|
|
||
| return $this->validateMxRecords($dnsRecords); | ||
| } | ||
|
|
||
| private function validateMxRecords($dnsRecords): bool | ||
| { | ||
| // For each DNS record | ||
| foreach ($dnsRecords as $dnsRecord) { | ||
| if (!$this->validateMXRecord($dnsRecord)) { | ||
|
Check failure on line 191 in src/Validation/DNSCheckValidation.php
|
||
| // No MX records (fallback to A or AAAA records) | ||
| if (empty($this->mxRecords)) { | ||
| $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); | ||
|
|
@@ -177,6 +196,7 @@ | |
| return false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this expected as soon as one record doesn't pass validateMXRecord it marks the domain as failed? Didn't found any issue with it but thought about if we should do instead of 3 lookup check after every lookup if we already found a valid dns record and early return true. But that would mean this line would not work as expected that it early return false. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. personally I would expect something like this: // For each DNS record
foreach ($dnsRecords as $dnsRecord) {
if ($this->validateMXRecord($dnsRecord)) {
return true; // do not check all domains
}
//if (empty($this->mxRecords)) {
// $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
// }
}
return false;and then do something like todo A and AAAA check only when MX failed: $mxRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_MX);
if (! $mxRecordsResult->withError()
+ && $this->validateMxRecords($mxRecordsResult->getRecords())) {
- $dnsRecords = $mxRecordsResult->getRecords();
+ return true;
}
// Combined check for A+MX can fail with connection timed out, even in the presence of valid MX record
$aRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_A);
if (! $aRecordsResult->withError()
+ && $this->validateMxRecords($aRecordsResult->getRecords())) {
- $dnsRecords = array_merge($dnsRecords, $aRecordsResult->getRecords());
+ return true;
}
// Combined check for A+MX+AAAA can fail with SERVFAIL, even in the presence of valid A/MX records
$aaaaRecordsResult = $this->dnsGetRecord->getRecords($host, DNS_AAAA);
if (! $aaaaRecordsResult->withError()
+ && $this->validateMxRecords($aaaaRecordsResult->getRecords())) {
- $dnsRecords = array_merge($dnsRecords, $aaaaRecordsResult->getRecords());
+ return true;
}
+ if ($mxRecordsResult->withError()
+ && $aRecordsResult->withError()
+ && $aaaaRecordsResult->withError()
+ ) {
+ $this->error = new InvalidEmail(new UnableToGetDNSRecord(), '');
+ return false;
+ }
+ if ([] === $mxRecordsResult->getRecords()
+ && [] === $aRecordsResult->getRecords()
+ && [] === $aaaaRecordsResult->getRecords()
+ ) {
+ $this->error = new InvalidEmail(new ReasonNoDNSRecord(), '');
+ return false;
+ }
+ $this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
return false;but I'm not deep into this Lib or DNS records and maybe the early There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The general strategy of the lib is fail fast . Now for this case, given that there are network roundtrips was to "confirm fase" if that make sense. Hence not checking all records, with one we do have a valid MX. Happy to change if you believe will improve accuracy. With a more in depth validation like your proposal, I agree to the general idea you are suggesting on this comment: we go from more accurate to less accurate and we short-circuit when we find a match. Implementation wise, I'd go for something like if (! $mxRecordsResult->withError()
&& $this->validateMxRecords($mxRecordsResult->getRecords())) {
$dnsRecords = $mxRecordsResult->getRecords();
return true;
}
// We know there are no MX records already
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord();
//If you feel like so, you can create new warnings for each missing DNS record
//...
//do the logic for error setting here
$this->setError($mxRecordsResult, $aRecordsResult, $aaaaRecordsResult) |
||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsure when we should return
UnableToGetDNSRecordif one DNS lookup failed or when all failed?