Monorepo for Tangled

appview: expand domain of acceptable email addresses

Use address resolution steps from RFC5321 section 5.1 when validating
email addresses.

ref: https://datatracker.ietf.org/doc/html/rfc5321#section-5.1

Signed-off-by: tjh <x@tjh.dev>

authored by tjh.dev and committed by tangled.org ff7ddde5 fa04d2f6

+27 -4
+27 -4
appview/email/email.go
··· 49 49 parts := strings.Split(addr.Address, "@") 50 50 domain := parts[1] 51 51 52 - mx, err := net.LookupMX(domain) 53 - if err != nil || len(mx) == 0 { 54 - return false 52 + canonical := coalesceToCanonicalName(domain) 53 + mx, err := net.LookupMX(canonical) 54 + 55 + // Don't check err here; mx will only contain valid mx records, and we should 56 + // only fallback to an implicit mx if there are no mx records defined (whether 57 + // they are valid or not). 58 + if len(mx) != 0 { 59 + return true 60 + } 61 + 62 + if err != nil { 63 + // If the domain resolves to an address, assume it's an implicit mx. 64 + address, _ := net.LookupIP(canonical) 65 + if len(address) != 0 { 66 + return true 67 + } 55 68 } 56 69 57 - return true 70 + return false 71 + } 72 + 73 + func coalesceToCanonicalName(domain string) string { 74 + canonical, err := net.LookupCNAME(domain) 75 + if err != nil { 76 + // net.LookupCNAME() returns an error if there is no cname record *and* no 77 + // a/aaaa records, but there may still be mx records. 78 + return domain 79 + } 80 + return canonical 58 81 }