Skip to content

Commit b570d0c

Browse files
committed
fix handling of RFC2047 words containing invalid bytes after decoding
Yet another bad case somehow not covered by all the tests. If the encoding is recognised, and the word is successfully decoded, but the resulting string contains an invalid byte sequence for its declared encoding, Sup would crash with ArgumentError. Only use the decoded word if the end result has a valid encoding.
1 parent 4e23e13 commit b570d0c

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

lib/sup/rfc2047.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ def Rfc2047.decode_to(target, from)
6262
end
6363

6464
begin
65-
text.force_encoding(charset).encode(target)
65+
text.force_encoding charset
66+
text.encode! target
6667
rescue ArgumentError, EncodingError
67-
word
68+
next word
6869
end
70+
next word unless text.valid_encoding?
71+
text
6972
end
7073
end
7174
end

test/fixtures/rfc2047-header-encoding.eml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From: test@example.invalid
1+
From: =?utf-8?q?YouTube-tj=E4nst?= <service@youtube.com>
22
To: test@example.invalid
33
Date: Sun, 19 Jul 2020 17:03:56 +1000
44
Subject:

test/test_message.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def test_rfc2047_header_encoding
253253
"bad: =?UTF16?q?badcharsetname?==?US-ASCII?b?/w?=" +
254254
"=?UTF-7?Q?=41=6D=65=72=69=63=61=E2=80=99=73?=",
255255
sup_message.subj)
256+
assert_equal "=?utf-8?q?YouTube-tj=E4nst?=", sup_message.from.name
256257
end
257258

258259
def test_nonascii_header

0 commit comments

Comments
 (0)