Commit e7538df7 authored by Hiroshi Ioka's avatar Hiroshi Ioka Committed by Brad Fitzpatrick

net/mail: throw error when multiple addresses are given to ParseAddress

Fixes #14610

Change-Id: I3e57dd60b531c1495ea3bc37ef707a1e4e644baa
Reviewed-on: https://go-review.googlesource.com/20180Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a62ae9f6
......@@ -138,7 +138,7 @@ type Address struct {
// Parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
func ParseAddress(address string) (*Address, error) {
return (&addrParser{s: address}).parseAddress()
return (&addrParser{s: address}).parseSingleAddress()
}
// ParseAddressList parses the given string as a list of addresses.
......@@ -155,7 +155,7 @@ type AddressParser struct {
// Parse parses a single RFC 5322 address of the
// form "Gogh Fir <gf@example.com>" or "foo@example.com".
func (p *AddressParser) Parse(address string) (*Address, error) {
return (&addrParser{s: address, dec: p.WordDecoder}).parseAddress()
return (&addrParser{s: address, dec: p.WordDecoder}).parseSingleAddress()
}
// ParseList parses the given string as a list of comma-separated addresses
......@@ -168,7 +168,6 @@ func (p *AddressParser) ParseList(list string) ([]*Address, error) {
// If the address's name contains non-ASCII characters
// the name will be rendered according to RFC 2047.
func (a *Address) String() string {
// Format address local@domain
at := strings.LastIndex(a.Address, "@")
var local, domain string
......@@ -269,6 +268,18 @@ func (p *addrParser) parseAddressList() ([]*Address, error) {
return list, nil
}
func (p *addrParser) parseSingleAddress() (*Address, error) {
addr, err := p.parseAddress()
if err != nil {
return nil, err
}
p.skipSpace()
if !p.empty() {
return nil, fmt.Errorf("mail: expected single address, got %q", p.s)
}
return addr, nil
}
// parseAddress parses a single RFC 5322 address at the start of p.
func (p *addrParser) parseAddress() (addr *Address, err error) {
debug.Printf("parseAddress: %q", p.s)
......
......@@ -120,18 +120,20 @@ func TestDateParsing(t *testing.T) {
}
func TestAddressParsingError(t *testing.T) {
const txt = "=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <unknown@gmail.com>"
_, err := ParseAddress(txt)
if err == nil || !strings.Contains(err.Error(), "charset not supported") {
t.Errorf(`mail.ParseAddress(%q) err: %q, want ".*charset not supported.*"`, txt, err)
mustErrTestCases := [...]struct {
text string
wantErrText string
}{
0: {"=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <unknown@gmail.com>", "charset not supported"},
1: {"µ <micro@example.net>", "unencoded non-ASCII text in address"},
2: {"a@gmail.com b@gmail.com", "expected single address"},
}
}
func TestAddressParsingErrorUnquotedNonASCII(t *testing.T) {
const txt = "µ <micro@example.net>"
_, err := ParseAddress(txt)
if err == nil || !strings.Contains(err.Error(), "unencoded non-ASCII text in address") {
t.Errorf(`mail.ParseAddress(%q) err: %q, want ".*unencoded non-ASCII text in address.*"`, txt, err)
for i, tc := range mustErrTestCases {
_, err := ParseAddress(tc.text)
if err == nil || !strings.Contains(err.Error(), tc.wantErrText) {
t.Errorf(`mail.ParseAddress(%q) #%d want %q, got %v`, tc.text, i, tc.wantErrText, err)
}
}
}
......
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