Skip to content

Commit

Permalink
Handle incomptible encodings in email headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bobjflong committed Jan 20, 2020
1 parent e054847 commit bf7add7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/mail/encodings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ def Encodings.value_decode(str)
when *Q_VALUES then q_value_decode(string)
end
end
end.join("")
end

if lines.each_slice(2).all? { |x, y| Encoding.compatible?(x, y) }
lines.join("")
else
lines.map do |line|
line.encode("UTF-8", invalid: :replace, undef: :replace, replace: "�")
end.join("")
end
end

# Takes an encoded string of the format =?<encoding>?[QB]?<string>?=
Expand Down
7 changes: 7 additions & 0 deletions spec/mail/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@
expect(m.subject).to eq "Hello #{replace} World"
end

it "should handle incompatible encodings by force encoding to UTF-8" do
m = Mail.new
subject = "\xEC =?utf-8?Q?=F0=9F=98=80?=".dup.force_encoding("ASCII-8BIT")
m['Subject'] = Mail::SubjectField.new(subject)
expect(m.subject).to eq "� 😀"
end

it "should replace characters of unknown and invalid encoding" do
m = Mail.new
m['Subject'] = Mail::SubjectField.new("Hello=?UNKNOWN?B?4g==?=")
Expand Down

0 comments on commit bf7add7

Please sign in to comment.