Skip to content

Commit c3ff3fe

Browse files
committed
Add basic checking of mx records for mailto links
Helps ensure that the domain is registered and at least partially configured to recieve email.
1 parent a145d42 commit c3ff3fe

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

htmltest/check-link.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"path"
1212
"strings"
13+
"net"
1314
)
1415

1516
func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) {
@@ -347,6 +348,34 @@ func (hT *HTMLTest) checkMailto(ref *htmldoc.Reference) {
347348
})
348349
return
349350
}
351+
domain := strings.Split(ref.URL.Opaque, "@")[1]
352+
_, err := net.LookupMX(domain)
353+
if err != nil {
354+
if dnserr, ok := err.(*net.DNSError); ok {
355+
switch dnserr.Err {
356+
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+
})
369+
}
370+
} else {
371+
hT.issueStore.AddIssue(issues.Issue{
372+
Level: issues.LevelWarning,
373+
Message: "unable to perform LookupMX, unkown error",
374+
Reference: ref,
375+
})
376+
}
377+
return
378+
}
350379
}
351380

352381
func (hT *HTMLTest) checkTel(ref *htmldoc.Reference) {

htmltest/check-link_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,20 @@ func TestMailtoBlank(t *testing.T) {
369369
tExpectIssue(t, hT, "mailto is empty", 1)
370370
}
371371

372-
func TestMailtoInvalid(t *testing.T) {
372+
func TestMailtoInvalidFormat(t *testing.T) {
373373
// fails for invalid mailto links
374374
hT := tTestFile("fixtures/links/invalid_mailto_link.html")
375375
tExpectIssueCount(t, hT, 1)
376376
tExpectIssue(t, hT, "contains an invalid email address", 1)
377377
}
378378

379+
func TestMailtoInvalidMx(t *testing.T) {
380+
// fails for invalid mailto links
381+
hT := tTestFile("fixtures/links/invalid_mailto_mx.html")
382+
tExpectIssueCount(t, hT, 1)
383+
tExpectIssue(t, hT, "domain contains no valid MX records", 1)
384+
}
385+
379386
func TestMailtoIgnore(t *testing.T) {
380387
// ignores mailto links when told to
381388
hT := tTestFileOpts("fixtures/links/blank_mailto_link.html",
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)