Skip to content

Commit ed4786b

Browse files
nobushugo
authored andcommitted
Decode UTF-7 more strictly
Reported by svalkanov in <https://hackerone.com/reports/1969040>.
1 parent 203e243 commit ed4786b

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/net/imap/data_encoding.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class IMAP < Protocol
5454
# Net::IMAP does _not_ automatically encode and decode
5555
# mailbox names to and from UTF-7.
5656
def self.decode_utf7(s)
57-
return s.gsub(/&([^-]+)?-/n) {
58-
if $1
59-
($1.tr(",", "/") + "===").unpack1("m").encode(Encoding::UTF_8, Encoding::UTF_16BE)
57+
return s.gsub(/&([A-Za-z0-9+,]+)?-/n) {
58+
if base64 = $1
59+
(base64.tr(",", "/") + "===").unpack1("m").encode(Encoding::UTF_8, Encoding::UTF_16BE)
6060
else
6161
"&"
6262
end

test/net/imap/test_imap_data_encoding.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def test_decode_utf7
2929
s = Net::IMAP.decode_utf7("&,yH,Iv8j-")
3030
utf8 = "\357\274\241\357\274\242\357\274\243".dup.force_encoding("UTF-8")
3131
assert_equal(utf8, s)
32+
33+
assert_linear_performance([1, 10, 100], pre: ->(n) {'&'*(n*1_000)}) do |s|
34+
Net::IMAP.decode_utf7(s)
35+
end
3236
end
3337

3438
def test_encode_date

0 commit comments

Comments
 (0)