Skip to content

Commit

Permalink
Fix the bogus '=' at the end of some quoted-printable messages.
Browse files Browse the repository at this point in the history
Trailing CRLF is significant in Quoted-Printable transfer encoding.
Stripping trailing whitespace corrupts the encoding, resulting in
odd '=' chars showing up at the end of decoded emails. Fixed by only
stripping leading whitespace.

Closes mikel#440
  • Loading branch information
jeremy committed Jan 25, 2013
1 parent e9f1307 commit e6238bc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/mail/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self.maximum_amount=(value)
def initialize(header_text = nil, charset = nil)
@errors = []
@charset = charset
self.raw_source = header_text.to_crlf
self.raw_source = header_text.to_crlf.lstrip
split_header if header_text
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mail/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def initialize(*args, &block)
if args.flatten.first.respond_to?(:each_pair)
init_with_hash(args.flatten.first)
else
init_with_string(args.flatten[0].to_s.strip)
init_with_string(args.flatten[0].to_s)
end

if block_given?
Expand Down Expand Up @@ -1875,7 +1875,7 @@ def text?
# Additionally, I allow for the case where someone might have put whitespace
# on the "gap line"
def parse_message
header_part, body_part = raw_source.split(/#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2)
header_part, body_part = raw_source.lstrip.split(/#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2)
self.header = header_part
self.body = body_part
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mail/example_emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
mail.from.should eq ["[email protected]"]
mail.subject.should eq "Re: TEST テストテスト"
mail.message_id.should eq '[email protected]'
mail.body.should eq "Hello"
mail.body.decoded.should eq "Hello\n"
end
end

Expand Down
12 changes: 8 additions & 4 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def basic_email

it "should set a raw source instance variable to equal the passed in message" do
mail = Mail::Message.new(basic_email)
mail.raw_source.should eq basic_email.strip
mail.raw_source.should eq basic_email
end

it "should set the raw source instance variable to '' if no message is passed in" do
Expand All @@ -259,7 +259,7 @@ def basic_email

it "should give the body class the body to parse" do
body = Mail::Body.new("email message")
Mail::Body.should_receive(:new).with("email message").and_return(body)
Mail::Body.should_receive(:new).with("email message\r\n").and_return(body)
mail = Mail::Message.new(basic_email)
mail.body #body calculates now lazy so need to ask for it
end
Expand Down Expand Up @@ -290,8 +290,8 @@ def basic_email

it "should allow for whitespace at the start of the email" do
mail = Mail.new("\r\n\r\nFrom: mikel\r\n\r\nThis is the body")
mail.from.should eq ['mikel']
mail.body.to_s.should eq 'This is the body'
mail.from.should eq ['mikel']
end

it "should read in an email message with the word 'From' in it multiple times and parse it" do
Expand All @@ -303,7 +303,7 @@ def basic_email
it "should parse non-UTF8 sources" do
mail = Mail::Message.new(File.read(fixture('emails', 'multi_charset', 'japanese_shiftjis.eml')))
mail.to.should eq ["[email protected]"]
mail.decoded.should eq "すみません。"
mail.decoded.should eq "すみません。\n\n"
end
end

Expand Down Expand Up @@ -1254,6 +1254,10 @@ def basic_email
mail.body.encoded.should eq "VGhlIGJvZHk=\r\n"
end

it 'should not strip the raw mail source in case the trailing \r\n is meaningful' do
Mail.new("Content-Transfer-Encoding: quoted-printable;\r\n\r\nfoo=\r\nbar=\r\nbaz=\r\n").decoded.should eq 'foobarbaz'
end

end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/mail/round_tripping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
parsed_mail.mime_type.should eq 'multipart/alternative'
parsed_mail.boundary.should eq mail.boundary
parsed_mail.parts.length.should eq 2
parsed_mail.parts[0].body.to_s.should eq "This is Text"
parsed_mail.parts[1].body.to_s.should eq "<b>This is HTML</b>"
parsed_mail.parts[0].body.to_s.should eq "This is Text\n\n"
parsed_mail.parts[1].body.to_s.should eq "<b>This is HTML</b>\n\n"
end

it "should round trip an email" do
Expand Down

0 comments on commit e6238bc

Please sign in to comment.