fix(web): refuse local addresses on the subscribe form
root@localhost, a@b and a@[127.0.0.1] passed mail.ParseAddress, so the public form could make the daemon mail itself or its own network. The domain of a typed address must now contain a dot, not be a domain literal, be made of non-empty labels of letters, digits and hyphens, and not be localhost or end in .localhost, .local, .internal or .invalid. .test and .example stay accepted (the end-to-end tests use them).
This commit is contained in:
parent
44b3f9d98a
commit
96a1243c2f
2 changed files with 47 additions and 2 deletions
|
|
@ -319,7 +319,7 @@ func printable(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// plainAddress accepts one bare ASCII address of at most 254 characters: no display name, no angle
|
// plainAddress accepts one bare ASCII address of at most 254 characters: no display name, no angle
|
||||||
// brackets, no list, no quoting, no control characters.
|
// brackets, no list, no quoting, no control characters, and a public domain (see publicDomain).
|
||||||
func plainAddress(s string) (string, bool) {
|
func plainAddress(s string) (string, bool) {
|
||||||
s = strings.TrimSpace(s)
|
s = strings.TrimSpace(s)
|
||||||
if s == "" || len(s) > 254 {
|
if s == "" || len(s) > 254 {
|
||||||
|
|
@ -331,12 +331,35 @@ func plainAddress(s string) (string, bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
a, err := mail.ParseAddress(s)
|
a, err := mail.ParseAddress(s)
|
||||||
if err != nil || a.Name != "" || a.Address != s {
|
if err != nil || a.Name != "" || a.Address != s || !publicDomain(s[strings.LastIndexByte(s, '@')+1:]) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
return s, true
|
return s, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func publicDomain(d string) bool {
|
||||||
|
d = strings.ToLower(d)
|
||||||
|
if !strings.Contains(d, ".") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, label := range strings.Split(d, ".") {
|
||||||
|
if label == "" || strings.ContainsFunc(label, func(r rune) bool {
|
||||||
|
return !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-')
|
||||||
|
}) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, local := range []string{".localhost", ".local", ".internal", ".invalid"} {
|
||||||
|
if strings.HasSuffix(d, local) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (s *server) unsubscribePage(w http.ResponseWriter, r *http.Request) {
|
func (s *server) unsubscribePage(w http.ResponseWriter, r *http.Request) {
|
||||||
l, err := s.router.CheckUnsubscribe(r.PathValue("token"))
|
l, err := s.router.CheckUnsubscribe(r.PathValue("token"))
|
||||||
switch {
|
switch {
|
||||||
|
|
|
||||||
|
|
@ -532,6 +532,19 @@ func TestSubscribeRejectsBadAddressesAndEscapesWhatWasTyped(t *testing.T) {
|
||||||
{"line break", "a@x.com\r\nBcc: v@x.com"},
|
{"line break", "a@x.com\r\nBcc: v@x.com"},
|
||||||
{"non-ASCII", "cärol@x.com"},
|
{"non-ASCII", "cärol@x.com"},
|
||||||
{"too long", long},
|
{"too long", long},
|
||||||
|
{"no dot in the domain", "a@b"},
|
||||||
|
{"localhost", "root@localhost"},
|
||||||
|
{"localhost in capitals", "root@LOCALHOST"},
|
||||||
|
{"under localhost", "a@app.localhost"},
|
||||||
|
{"mDNS", "a@printer.local"},
|
||||||
|
{"internal", "a@svc.internal"},
|
||||||
|
{"invalid", "a@x.invalid"},
|
||||||
|
{"domain literal", "a@[127.0.0.1]"},
|
||||||
|
{"IPv6 domain literal", "a@[IPv6:::1]"},
|
||||||
|
{"trailing dot", "a@x.com."},
|
||||||
|
{"underscore in the domain", "a@x_y.com"},
|
||||||
|
{"symbol in the domain", "a@x!y.com"},
|
||||||
|
{"plus in the domain", "a@x+y.com"},
|
||||||
} {
|
} {
|
||||||
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
|
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 {
|
if rec.Code != 400 {
|
||||||
|
|
@ -555,6 +568,15 @@ func TestSubscribeRejectsBadAddressesAndEscapesWhatWasTyped(t *testing.T) {
|
||||||
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.101", email("a"+ok)), "valid email address")
|
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.101", email("a"+ok)), "valid email address")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSubscribeAcceptsOrdinaryDomains(t *testing.T) {
|
||||||
|
f := newFixture(t)
|
||||||
|
for i, addr := range []string{"a@members.test", "a@example.com", "a@mail.example", "a@my-host.Example.ORG", "a@x1.co", "a@localhost.example.com"} {
|
||||||
|
rec := subscribe(t, f, "dev@example.org", fmt.Sprintf("192.0.2.%d", i+1), email(addr))
|
||||||
|
wantStatus(t, rec, 200)
|
||||||
|
wantBody(t, rec, neutralText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubscribeBodyIsLimitedTo4KiB(t *testing.T) {
|
func TestSubscribeBodyIsLimitedTo4KiB(t *testing.T) {
|
||||||
f := newFixture(t)
|
f := newFixture(t)
|
||||||
body := func(n int) string {
|
body := func(n int) string {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue