in dns.ParseDomain, don't allow ipv4 addresses (ipv6 addresses were already rejected)

we are expecting a DNS domain name there.
also highlighted a wrong test in the smtp server.
This commit is contained in:
Mechiel Lukkien
2025-02-23 11:33:31 +01:00
parent 797c1cf9f0
commit 151729af08
2 changed files with 15 additions and 1 deletions

View File

@ -20,6 +20,7 @@ var (
errTrailingDot = errors.New("dns name has trailing dot")
errUnderscore = errors.New("domain name with underscore")
errIDNA = errors.New("idna")
errIPNotName = errors.New("ip address while name required")
)
// Domain is a domain name, with one or more labels, with at least an ASCII
@ -96,6 +97,12 @@ func ParseDomain(s string) (Domain, error) {
return Domain{}, errTrailingDot
}
// IPv4 addresses would be accepted by idna lookups. TLDs cannot be all numerical,
// so IP addresses are not valid DNS names.
if net.ParseIP(s) != nil {
return Domain{}, errIPNotName
}
ascii, err := idna.Lookup.ToASCII(s)
if err != nil {
return Domain{}, fmt.Errorf("%w: to ascii: %v", errIDNA, err)