From a63807e71bcc0a25137214b14c2cdb508bbef470 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 15:16:48 +0100 Subject: [PATCH] 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. --- internal/web/web.go | 9 +++++++-- internal/web/web_test.go | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/web/web.go b/internal/web/web.go index 35cf630..ae06a79 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -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 diff --git a/internal/web/web_test.go b/internal/web/web_test.go index 915f9d3..f48340e 100644 --- a/internal/web/web_test.go +++ b/internal/web/web_test.go @@ -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)