Commit 178a2c42 authored by Daniel Martí's avatar Daniel Martí

net/mail: better error in ParseAddress when missing "@domain"

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 583fddf3
......@@ -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()
......
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment