Skip to content

Commit 7697c19

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 c3ff3fe commit 7697c19

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
@@ -348,33 +348,46 @@ func (hT *HTMLTest) checkMailto(ref *htmldoc.Reference) {
348348
})
349349
return
350350
}
351+
352+
// split off domain, check mx, fallback to A or AAAA if that fais
351353
domain := strings.Split(ref.URL.Opaque, "@")[1]
352354
_, err := net.LookupMX(domain)
355+
var dnserr *net.DNSError
356+
var ok bool
353357
if err != nil {
354-
if dnserr, ok := err.(*net.DNSError); ok {
358+
if dnserr, ok = err.(*net.DNSError); ok {
355359
switch dnserr.Err {
356360
case "no such host":
357-
hT.issueStore.AddIssue(issues.Issue{
358-
Level: issues.LevelError,
359-
Message: "domain contains no valid MX records",
360-
Reference: ref,
361-
})
362-
break
363-
default:
364-
hT.issueStore.AddIssue(issues.Issue{
365-
Level: issues.LevelError,
366-
Message: "unable to perform LookupMX, unkown error",
367-
Reference: ref,
368-
})
361+
// current no MX records, but we should try again with A record
362+
_, err := net.LookupHost(domain)
363+
if dnserr, ok = err.(*net.DNSError); ok {
364+
break
365+
} else {
366+
hT.issueStore.AddIssue(issues.Issue{
367+
Level: issues.LevelWarning,
368+
Message: "unable to perform LookupHost, unknown error",
369+
Reference: ref,
370+
})
371+
return
372+
}
369373
}
370374
} else {
371375
hT.issueStore.AddIssue(issues.Issue{
372376
Level: issues.LevelWarning,
373-
Message: "unable to perform LookupMX, unkown error",
377+
Message: "unable to perform LookupMX, unknown error",
374378
Reference: ref,
375379
})
380+
return
376381
}
377-
return
382+
}
383+
384+
// check if we finally have a dnserr
385+
if dnserr != nil {
386+
hT.issueStore.AddIssue(issues.Issue{
387+
Level: issues.LevelError,
388+
Message: "email cannot be routed to domain, no MX/A/AAAA records",
389+
Reference: ref,
390+
})
378391
}
379392
}
380393

htmltest/check-link_test.go

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

379-
func TestMailtoInvalidMx(t *testing.T) {
379+
func TestMailtoInvalidNoRecords(t *testing.T) {
380380
// fails for invalid mailto links
381-
hT := tTestFile("fixtures/links/invalid_mailto_mx.html")
381+
hT := tTestFile("fixtures/links/invalid_mailto_norecords.html")
382382
tExpectIssueCount(t, hT, 1)
383-
tExpectIssue(t, hT, "domain contains no valid MX records", 1)
383+
tExpectIssue(t, hT, "email cannot be routed to domain, no MX/A/AAAA records", 1)
384+
}
385+
386+
func TestMailtoInvalidAFallback(t *testing.T) {
387+
// fails for invalid mailto links
388+
hT := tTestFile("fixtures/links/invalid_mailto_fallback.html")
389+
tExpectIssueCount(t, hT, 0)
384390
}
385391

386392
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)