Skip to content

Commit

Permalink
Decode UTF-7 more strictly
Browse files Browse the repository at this point in the history
Reported by svalkanov in <https://hackerone.com/reports/1969040>.
  • Loading branch information
nobu authored and shugo committed Jun 9, 2023
1 parent 203e243 commit ed4786b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/net/imap/data_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class IMAP < Protocol
# Net::IMAP does _not_ automatically encode and decode
# mailbox names to and from UTF-7.
def self.decode_utf7(s)
return s.gsub(/&([^-]+)?-/n) {
if $1
($1.tr(",", "/") + "===").unpack1("m").encode(Encoding::UTF_8, Encoding::UTF_16BE)
return s.gsub(/&([A-Za-z0-9+,]+)?-/n) {
if base64 = $1
(base64.tr(",", "/") + "===").unpack1("m").encode(Encoding::UTF_8, Encoding::UTF_16BE)
else
"&"
end
Expand Down
4 changes: 4 additions & 0 deletions test/net/imap/test_imap_data_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def test_decode_utf7
s = Net::IMAP.decode_utf7("&,yH,Iv8j-")
utf8 = "\357\274\241\357\274\242\357\274\243".dup.force_encoding("UTF-8")
assert_equal(utf8, s)

assert_linear_performance([1, 10, 100], pre: ->(n) {'&'*(n*1_000)}) do |s|
Net::IMAP.decode_utf7(s)
end
end

def test_encode_date
Expand Down

0 comments on commit ed4786b

Please sign in to comment.