Skip to content

Handle incomptible encodings in email headers #1375

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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