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