Encoding changes when PHRASE_UNSAFE in Mail::Utilities#quote_phrase #621

Closed
bmorton opened this Issue Sep 11, 2013 · 3 comments

Comments

Projects
None yet
4 participants

bmorton commented Sep 11, 2013

When I do this:
address = Mail::Address.new(email)
address.display_name = name
address.format

If name is UTF-8, it gets force encoded to ASCII-8BIT and then doesn't get force encoded back to its original encoding because of:
https://github.com/mikel/mail/blob/master/lib/mail/utilities.rb#L24

Do we need to pass the ASCII-8BIT string to dquote and then return the new string in its original encoding or can we pass the originally encoded string to dquote so that the string that's passed in gets set back to its proper encoding?

I'll throw together a quick pull request for this, but comments on the question above would be appreciated.

Contributor

carsonreinke commented Sep 16, 2013

I just checked this, I think you want to call encoded on address first before calling format.

$> address = Mail::Address.new('hi@example.com')
 => #<Mail::Address:70191549114900 Address: |hi@example.com| > 
$> address.display_name = ""
 => "" 
$> address.encoded
 => "=?UTF-8?B?4piD?= <hi@example.com>" 

kylev commented Nov 3, 2013

It's actually worse than all that. Under some conditions, Mail::Address may mutate the display name string and destroy its encoding.

>> name = "\u{101}\u{10d}\u{113}"
=> "āčē"
>> name.encoding
=> #<Encoding:UTF-8>
>> e = Mail::Address.new('ace@example.com')
=> #<Mail::Address:70108045388440 Address: |ace@example.com| >
>> e.display_name = name
=> "āčē"
>> e.format
=> ""āčē" <ace@example.com>"
>> name.encoding
=> #<Encoding:ASCII-8BIT>
>> name
=> "\xC4\x81\xC4\x8D\xC4\x93"

The UTF-8 encoding has been destroyed. If this was the name of a new user on an as-yet unsaved database record, preparing an email to send may wreck the database record.

Such is the bane of a language with mutable strings and the possibility for side-effects.

At the very least we should consider force_encoding to be a !-method and clone the string before using it. str = str.clone within the Ruby 1.9 block of quote_phrase would solve at least my case.

ryanwood referenced this issue in Shopify/liquid Apr 4, 2014

Closed

UTF-8 Support Again #290

Collaborator

jeremy commented May 17, 2017

Working on current master:

>> name = "\u{101}\u{10d}\u{113}"
=> "āčē"
>> name.encoding
=> #<Encoding:UTF-8>
>> e = Mail::Address.new('ace@example.com')
=> #<Mail::Address:70260901962420 Address: |ace@example.com| >
>> e.display_name = name
=> "āčē"
>> e.format
=> "\"āčē\" <ace@example.com>"
>> name.encoding
=> #<Encoding:UTF-8>
>> name
=> "āčē"

jeremy closed this May 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment