From: Daniel Martí Date: Tue, 5 Mar 2019 22:31:37 +0000 (+0000) Subject: net/mail: better error in ParseAddress when missing "@domain" X-Git-Tag: go1.13beta1~1193 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=178a2c42254166cffed1b25fb1d3c7a5727cada6;p=gostls13.git net/mail: better error in ParseAddress when missing "@domain" If the input was "John Doe", we're definitely missing "", as "John Doe@domain" isn't a valid email address. However, if the input was "john.doe", it's possible that the user meant "john.doe@domain", and not just "john.doe ". Make it clear in the error that either could be the source of the problem. Fixes #27064. Change-Id: I1b8f1342775d711823dffc3db974898ee62d3a34 Reviewed-on: https://go-review.googlesource.com/c/go/+/165517 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 554377aa1d..e0907806ca 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -342,6 +342,21 @@ func (p *addrParser) parseAddress(handleGroup bool) ([]*Address, error) { } // angle-addr = "<" addr-spec ">" if !p.consume('<') { + atext := true + for _, r := range displayName { + if !isAtext(r, true, false) { + atext = false + break + } + } + if atext { + // The input is like "foo.bar"; it's possible the input + // meant to be "foo.bar@domain", or "foo.bar <...>". + return nil, errors.New("mail: missing '@' or angle-addr") + } + // The input is like "Full Name", which couldn't possibly be a + // valid email address if followed by "@domain"; the input + // likely meant to be "Full Name <...>". return nil, errors.New("mail: no angle-addr") } spec, err = p.consumeAddrSpec() diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go index 14ac9192a4..2950bc4de9 100644 --- a/src/net/mail/message_test.go +++ b/src/net/mail/message_test.go @@ -144,6 +144,9 @@ func TestAddressParsingError(t *testing.T) { 12: {"root group: embed group: null@example.com;", "no angle-addr"}, 13: {"group not closed: null@example.com", "expected comma"}, 14: {"group: first@example.com, second@example.com;", "group with multiple addresses"}, + 15: {"john.doe", "missing '@' or angle-addr"}, + 16: {"john.doe@", "no angle-addr"}, + 17: {"John Doe@foo.bar", "no angle-addr"}, } for i, tc := range mustErrTestCases {