]> Cypherpunks repositories - gostls13.git/commitdiff
net/mail: use base64 encoding when needed in Address.String()
authorAlexandre Cesaro <alexandre.cesaro@gmail.com>
Tue, 20 Oct 2015 15:30:21 +0000 (17:30 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 2 Dec 2015 17:37:36 +0000 (17:37 +0000)
When the name of an Address contains non-ASCII characters,
Address.String() used mime.QEncoding to encode the name.

However certain characters are forbidden when an encoded-word is
in a phrase context (see RFC 2047 section 5.3) and these
characters are not encoded by mime.QEncoding.

In this case we now use mime.BEncoding (base64 encoding) so that
forbidden characters are also encoded.

Fixes #11292

Change-Id: I52db98b41ece439295e97d7e94c8190426f499c2
Reviewed-on: https://go-review.googlesource.com/16012
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 01290bc65be853d9979098e5231e57a8c1ba044c..571c95ddc98f21e48fccdabd5bed78bbaeaccc06 100644 (file)
@@ -234,6 +234,12 @@ func (a *Address) String() string {
                return b.String()
        }
 
+       // Text in an encoded-word in a display-name must not contain certain
+       // characters like quotes or parentheses (see RFC 2047 section 5.3).
+       // When this is the case encode the name using base64 encoding.
+       if strings.ContainsAny(a.Name, "\"#$%&'(),.:;<>@[]^`{|}~") {
+               return mime.BEncoding.Encode("utf-8", a.Name) + " " + s
+       }
        return mime.QEncoding.Encode("utf-8", a.Name) + " " + s
 }
 
index e05af6c6ba30f9d8ca1fb981251b4aaba33c36f3..9fd7923c3481120d0a698e841a07349812462b4f 100644 (file)
@@ -499,6 +499,10 @@ func TestAddressFormatting(t *testing.T) {
                        &Address{Name: "Rob", Address: "@"},
                        `"Rob" <@>`,
                },
+               {
+                       &Address{Name: "Böb, Jacöb", Address: "bob@example.com"},
+                       `=?utf-8?b?QsO2YiwgSmFjw7Zi?= <bob@example.com>`,
+               },
        }
        for _, test := range tests {
                s := test.addr.String()
@@ -594,3 +598,32 @@ func TestAddressParsingAndFormatting(t *testing.T) {
        }
 
 }
+
+func TestAddressFormattingAndParsing(t *testing.T) {
+       tests := []*Address{
+               &Address{Name: "@lïce", Address: "alice@example.com"},
+               &Address{Name: "Böb O'Connor", Address: "bob@example.com"},
+               &Address{Name: "???", Address: "bob@example.com"},
+               &Address{Name: "Böb ???", Address: "bob@example.com"},
+               &Address{Name: "Böb (Jacöb)", Address: "bob@example.com"},
+               &Address{Name: "à#$%&'(),.:;<>@[]^`{|}~'", Address: "bob@example.com"},
+               // https://golang.org/issue/11292
+               &Address{Name: "\"\\\x1f,\"", Address: "0@0"},
+               // https://golang.org/issue/12782
+               &Address{Name: "naé, mée", Address: "test.mail@gmail.com"},
+       }
+
+       for _, test := range tests {
+               parsed, err := ParseAddress(test.String())
+               if err != nil {
+                       t.Errorf("ParseAddr(%q) error: %v", test.String(), err)
+                       continue
+               }
+               if parsed.Name != test.Name {
+                       t.Errorf("Parsed name = %q; want %q", parsed.Name, test.Name)
+               }
+               if parsed.Address != test.Address {
+                       t.Errorf("Parsed address = %q; want %q", parsed.Address, test.Address)
+               }
+       }
+}