]> Cypherpunks repositories - gostls13.git/commitdiff
net/mail: throw error when multiple addresses are given to ParseAddress
authorHiroshi Ioka <hirochachacha@gmail.com>
Thu, 3 Mar 2016 17:54:51 +0000 (02:54 +0900)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 31 Mar 2016 10:09:55 +0000 (10:09 +0000)
Fixes #14610

Change-Id: I3e57dd60b531c1495ea3bc37ef707a1e4e644baa
Reviewed-on: https://go-review.googlesource.com/20180
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/mail/message.go
src/net/mail/message_test.go

index 923630c49ce02143309c00365afcb04d2b401ac7..12342b368ffdc77c5c9f24743194325de6d63ae2 100644 (file)
@@ -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)
index 4e718e2636727454ca901ef71aee450954988284..cf86ace68f95d7d9d5eb403b0d601b7d3a3c16f8 100644 (file)
@@ -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)
+               }
        }
 }