]> Cypherpunks repositories - gostls13.git/commitdiff
net/mail: better error in ParseAddress when missing "@domain"
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 5 Mar 2019 22:31:37 +0000 (22:31 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 6 Mar 2019 07:56:20 +0000 (07:56 +0000)
If the input was "John Doe", we're definitely missing "<email>", 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 <email>". 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í <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/mail/message.go
src/net/mail/message_test.go

index 554377aa1da989a06eaee5070db4a79bfaf50971..e0907806ca6e337465753e38cb33d4c7f4ca36c5 100644 (file)
@@ -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()
index 14ac9192a4af0569e3ff3b5ce3c1a2e248dc9bd2..2950bc4de92b82a13f0708b84a69e7f539770286 100644 (file)
@@ -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 {