Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Handle unstructured field with UTF-7 encoded-word #650

Open
wants to merge 1 commit into from

2 participants

@johngrimes

Attempting to parse an email with the following header:

References: =?utf-7?B?PDEyMzRAdGVzdC5saW5kc2Fhci5uZXQ+?=

Will cause an error: Encoding::ConverterNotFoundError: code converter not found (UTF-7 to UTF-8).

This can be fixed by checking for UTF-7 within the b_value_decode method and using Net::IMAP to decode the string first.

@johngrimes johngrimes Decode UTF-7 with Net::IMAP before UTF-8 conversion
This prevents an Encoding::ConverterNotFoundError when trying to parse an
unstructured field with a UTF-7 encoded-word.
98b5c05
@tdg5

I'm not sure when it was fixed, but this appears to work on v2.6.3. It still gives a warning, but otherwise seems like this PR could be closed?

::Mail::Encodings.value_decode("=?utf-7?B?PDEyMzRAdGVzdC5saW5kc2Fhci5uZXQ+?=")
# Encoding conversion failed code converter not found (UTF-7 to UTF-8)
"<1234@test.lindsaar.net>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 7, 2014
  1. @johngrimes

    Decode UTF-7 with Net::IMAP before UTF-8 conversion

    johngrimes authored
    This prevents an Encoding::ConverterNotFoundError when trying to parse an
    unstructured field with a UTF-7 encoded-word.
This page is out of date. Refresh to see the latest.
View
8 lib/mail/version_specific/ruby_1_9.rb
@@ -53,7 +53,13 @@ def Ruby19.b_value_decode(str)
if match
charset = match[1]
str = Ruby19.decode_base64(match[2])
- str.force_encoding(pick_encoding(charset))
+ if charset.downcase == 'utf-7'
+ str.force_encoding('utf-8')
+ str = Net::IMAP.decode_utf7(str)
+ str.force_encoding('utf-8')
+ else
+ str.force_encoding(pick_encoding(charset))
+ end
end
decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
View
6 spec/mail/fields/unstructured_field_spec.rb
@@ -103,6 +103,12 @@
@field.decoded.should eq string
end
+ it "should decode a utf-7(B) encoded unstructured field" do
+ string = "=?utf-7?B?PDEyMzRAdGVzdC5saW5kc2Fhci5uZXQ+?="
+ @field = Mail::UnstructuredField.new("References", string)
+ @field.decoded.should eq "<1234@test.lindsaar.net>"
+ end
+
if !'1.9'.respond_to?(:force_encoding)
it "shouldn't get fooled into encoding on 1.8 due to an unrelated Encoding constant" do
begin
Something went wrong with that request. Please try again.