Navigation Menu

Skip to content

Commit

Permalink
Fix decoding nested quotes around non-US-ASCII addresses
Browse files Browse the repository at this point in the history
Previous implementation matched up to the first double-quote char,
possibly an escaped nested quote. Fix to only match up to the first
unescaped quote.

Closes #923
  • Loading branch information
averell23 authored and jeremy committed May 15, 2017
1 parent 2c5eecc commit aaf601d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Expand Up @@ -26,6 +26,7 @@ Bugs:
* #539 - Fix that whitespace-only continued headers would be incorrectly parsed as the break between headers and body. (ConradIrwin)
* #605 - Fix Mail::Address#name for nil addresses (peterkovacs)
* #876 - Strip valid RFC-1342 separator characters between non-matching encoded-words (Caleb W. Corliss)
* #923 – Fix decoding nested quotes around non-US-ASCII addresses. (averell23)
* #978 - Fix for invalid chars being left in a string for invalid b_value from encoding. (kjg)
* #996 - Fix that multipart/mixed emails with a delivery-status part could be interpreted as bounces. (kjg)
* #998 - Fix header parameter parsing (such as attachment names) for values encoded with a blank charset or language code. (kjg)
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/encodings.rb
Expand Up @@ -180,7 +180,7 @@ def Encodings.encode_non_usascii(address, charset)
# With KCODE=u we can't use regexps on other encodings. Go ASCII.
with_ascii_kcode do
# Encode all strings embedded inside of quotes
address = address.gsub(/("[^"]*")/) { |s| Encodings.b_value_encode(unquote(s), charset) }
address = address.gsub(/("[^"]*[^\/]")/) { |s| Encodings.b_value_encode(unquote(s), charset) }

# Then loop through all remaining items and encode as needed
tokens = address.split(/\s/)
Expand Down
26 changes: 16 additions & 10 deletions spec/mail/fields/from_field_spec.rb
Expand Up @@ -2,19 +2,19 @@
# frozen_string_literal: true
require 'spec_helper'

#
#
# from = "From:" mailbox-list CRLF

describe Mail::FromField do

describe "initialization" do

it "should initialize" do
expect { Mail::FromField.new("From: Mikel") }.not_to raise_error
end

it "should mix in the CommonAddress module" do
expect(Mail::FromField.included_modules).to include(Mail::CommonAddress)
expect(Mail::FromField.included_modules).to include(Mail::CommonAddress)
end

it "should accept a string with the field name" do
Expand All @@ -30,7 +30,7 @@
end

end

# Actual testing of CommonAddress methods oFromurs in the address field spec file

describe "instance methods" do
Expand All @@ -51,29 +51,29 @@
expect(t.addresses[1]).to eq 'mikel@me.com'
expect(t.addresses[2]).to eq 'bob@you.com'
end

it "should return the formatted line on to_s" do
t = Mail::FromField.new('sam@me.com, my_group: mikel@me.com, bob@you.com;')
expect(t.value).to eq 'sam@me.com, my_group: mikel@me.com, bob@you.com;'
end

it "should return the encoded line" do
t = Mail::FromField.new('sam@me.com, my_group: mikel@me.com, bob@you.com;')
expect(t.encoded).to eq "From: sam@me.com, \r\n\smy_group: mikel@me.com, \r\n\sbob@you.com;\r\n"
end

it "should return the encoded line" do
t = Mail::FromField.new("bob@me.com")
expect(t.encoded).to eq "From: bob@me.com\r\n"
end

it "should return the decoded line" do
t = Mail::FromField.new('sam@me.com, my_group: mikel@me.com, bob@you.com;')
expect(t.decoded).to eq "sam@me.com, my_group: mikel@me.com, bob@you.com;"
end

end

it "should handle non ascii" do
t = Mail::FromField.new('"Foo áëô îü" <extended@example.net>')
expect(t.decoded).to eq '"Foo áëô îü" <extended@example.net>'
Expand All @@ -85,4 +85,10 @@
expect(t.decoded).to eq '"Foo áëô îü" <extended@example.net>'
expect(t.encoded).to eq "From: =?UTF-8?B?Rm9vIMOhw6vDtCDDrsO8?= <extended@example.net>\r\n"
end

it "should work with combinations of quotes and non ascii" do
t = Mail::FromField.new('" „Floć Žama“ Rain Shelter \"Floć Žama-Rain Shelter\"" <no-reply@example.com>')
expect(t.decoded).to eq '"„Floć Žama“ Rain Shelter \"Floć Žama-Rain Shelter\"" <no-reply@example.com>'
expect(t.encoded).to eq "From: =?UTF-8?B?4oCeRmxvxIcgxb1hbWHigJwgUmFpbiBTaGVsdGVyICJGbG/EhyDFvWFtYS1S?= =?UTF-8?B?YWluIFNoZWx0ZXIi?= <no-reply@example.com>\r\n"
end
end

0 comments on commit aaf601d

Please sign in to comment.