Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing a Mail::Message from a string ignores the string's encoding #1585

Open
ghost opened this issue Sep 15, 2023 · 0 comments
Open

Parsing a Mail::Message from a string ignores the string's encoding #1585

ghost opened this issue Sep 15, 2023 · 0 comments

Comments

@ghost
Copy link

ghost commented Sep 15, 2023

The following snippet attempts to parse a mail message:

require 'mail'
require 'base64'

msgtxt = "RnJvbTogIkxldJJzIEtldG8gQ2Fwc3VsZXMiIDxhQGIuY28+DQpTdWJqZWN0\nOiBtZWgNCg0KQm9keQ0K\n"
msg = Mail.new(Base64.decode64(msgtxt))
msg.errors.each{|err| puts "Error: #{err}" }

The message contains a sequence of bytes in the From: line that if treated as UTF-8 are malformed. (The message is encoded in base64 to preserve this, but I found the original example in a piece of spam email.)

I can retrieve it by encoding it in ASCII-8BIT (e.g. by setting the the input stream's encoding). However when I pass it to Mail.new, it fails because Mail::Message defaults to UTF-8 and parsing will attempt to convert the value, which doesn't work because of the malformed text.

This looks like a bug to me. I would have expected it to use the string's encoding as the default charset.

Adding the following seems to fix this:

diff --git a/lib/mail/message.rb b/lib/mail/message.rb
index 5c7d40ab..1d9a1e2f 100644
--- a/lib/mail/message.rb
+++ b/lib/mail/message.rb
@@ -2117,6 +2117,7 @@ module Mail
     end
 
     def init_with_string(string)
+      @charset = string.encoding.to_s if @defaulted_charset
       self.raw_source = string
       set_envelope_header
       parse_message

(I also can work around this with Mail::Message.default_charset = 'ASCII-8BIT', but it still seems like something that could be improved.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants