Skip to content

Commit 8349811

Browse files
committed
Add fallback to A/AAAA records if no MX records are found.
As per https://tools.ietf.org/html/rfc5321#section-5 this should fallback to the hostname for that domain.
1 parent c545c7c commit 8349811

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

htmltest/check-link.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,33 +350,46 @@ func (hT *HTMLTest) checkMailto(ref *htmldoc.Reference) {
350350
})
351351
return
352352
}
353+
354+
// split off domain, check mx, fallback to A or AAAA if that fais
353355
domain := strings.Split(ref.URL.Opaque, "@")[1]
354356
_, err := net.LookupMX(domain)
357+
var dnserr *net.DNSError
358+
var ok bool
355359
if err != nil {
356-
if dnserr, ok := err.(*net.DNSError); ok {
360+
if dnserr, ok = err.(*net.DNSError); ok {
357361
switch dnserr.Err {
358362
case "no such host":
359-
hT.issueStore.AddIssue(issues.Issue{
360-
Level: issues.LevelError,
361-
Message: "domain contains no valid MX records",
362-
Reference: ref,
363-
})
364-
break
365-
default:
366-
hT.issueStore.AddIssue(issues.Issue{
367-
Level: issues.LevelError,
368-
Message: "unable to perform LookupMX, unkown error",
369-
Reference: ref,
370-
})
363+
// current no MX records, but we should try again with A record
364+
_, err := net.LookupHost(domain)
365+
if dnserr, ok = err.(*net.DNSError); ok {
366+
break
367+
} else {
368+
hT.issueStore.AddIssue(issues.Issue{
369+
Level: issues.LevelWarning,
370+
Message: "unable to perform LookupHost, unknown error",
371+
Reference: ref,
372+
})
373+
return
374+
}
371375
}
372376
} else {
373377
hT.issueStore.AddIssue(issues.Issue{
374378
Level: issues.LevelWarning,
375-
Message: "unable to perform LookupMX, unkown error",
379+
Message: "unable to perform LookupMX, unknown error",
376380
Reference: ref,
377381
})
382+
return
378383
}
379-
return
384+
}
385+
386+
// check if we finally have a dnserr
387+
if dnserr != nil {
388+
hT.issueStore.AddIssue(issues.Issue{
389+
Level: issues.LevelError,
390+
Message: "email cannot be routed to domain, no MX/A/AAAA records",
391+
Reference: ref,
392+
})
380393
}
381394
}
382395

htmltest/check-link_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,17 @@ func TestMailtoInvalidFormat(t *testing.T) {
390390
tExpectIssue(t, hT, "contains an invalid email address", 1)
391391
}
392392

393-
func TestMailtoInvalidMx(t *testing.T) {
393+
func TestMailtoInvalidNoRecords(t *testing.T) {
394394
// fails for invalid mailto links
395-
hT := tTestFile("fixtures/links/invalid_mailto_mx.html")
395+
hT := tTestFile("fixtures/links/invalid_mailto_norecords.html")
396396
tExpectIssueCount(t, hT, 1)
397-
tExpectIssue(t, hT, "domain contains no valid MX records", 1)
397+
tExpectIssue(t, hT, "email cannot be routed to domain, no MX/A/AAAA records", 1)
398+
}
399+
400+
func TestMailtoInvalidAFallback(t *testing.T) {
401+
// fails for invalid mailto links
402+
hT := tTestFile("fixtures/links/invalid_mailto_fallback.html")
403+
tExpectIssueCount(t, hT, 0)
398404
}
399405

400406
func TestMailtoIgnore(t *testing.T) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
3+
<body>
4+
5+
<a href="mailto:[email protected]">Meow me</a>
6+
7+
</body>
8+
9+
</html>

htmltest/fixtures/links/invalid_mailto_mx.html

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
3+
<body>
4+
5+
<a href="mailto:[email protected]">Meow me</a>
6+
7+
</body>
8+
9+
</html>

0 commit comments

Comments
 (0)