fix(web): refuse a bare IP address as the domain of a typed address

a@127.0.0.1 has dots and digit labels, so it passed the domain check
and named the daemon's own host without brackets. No top-level domain
is all digits: a domain whose last label is all digits is refused.
This commit is contained in:
tgiachi 2026-11-10 15:16:48 +01:00
commit a63807e71b
2 changed files with 9 additions and 2 deletions

View file

@ -338,8 +338,9 @@ func plainAddress(s string) (string, bool) {
}
// publicDomain accepts a domain name that can be on the Internet: dotted labels of letters, digits
// and hyphens, and not one of the names reserved for local use. A domain literal ([127.0.0.1]), a
// bare host (localhost) or a .local name would make the daemon mail itself or its network.
// and hyphens, not an IP address, and not one of the names reserved for local use. A domain literal
// ([127.0.0.1]), a bare host (localhost) or a .local name would make the daemon mail itself or its
// network.
func publicDomain(d string) bool {
d = strings.ToLower(d)
if !strings.Contains(d, ".") {
@ -352,6 +353,10 @@ func publicDomain(d string) bool {
return false
}
}
// No top-level domain is all digits: such a name is an IP address (a@127.0.0.1).
if strings.Trim(d[strings.LastIndexByte(d, '.')+1:], "0123456789") == "" {
return false
}
for _, local := range []string{".localhost", ".local", ".internal", ".invalid"} {
if strings.HasSuffix(d, local) {
return false

View file

@ -545,6 +545,7 @@ func TestSubscribeRejectsBadAddressesAndEscapesWhatWasTyped(t *testing.T) {
{"underscore in the domain", "a@x_y.com"},
{"symbol in the domain", "a@x!y.com"},
{"plus in the domain", "a@x+y.com"},
{"bare IP address", "a@127.0.0.1"},
} {
rec := subscribe(t, f, "dev@example.org", fmt.Sprintf("192.0.2.%d", i+1), email(c.in)) // one IP each: the limit is per IP
if rec.Code != 400 {
@ -882,6 +883,7 @@ func TestPublicDomain(t *testing.T) {
"example.com": true, "members.test": true, "a-b.example": true, "X1.Example.ORG": true,
"x..com": false, ".x.com": false, "x.com.": false, "x": false, "localhost": false, "[127.0.0.1]": false,
"a.localhost": false, "a.local": false, "a.internal": false, "a.invalid": false, "x_y.com": false,
"127.0.0.1": false, "10.1.2.3": false, "x.123": false, "123.example": true, "x.c0m": true,
} {
if got := publicDomain(d); got != want {
t.Errorf("publicDomain(%q) = %v, want %v", d, got, want)