8f07d4
commit 95ba9053e20ad8d113af37b3f1f4cbfff1f6a8f1
8f07d4
Author: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8f07d4
Date:   Wed Jul 7 10:38:10 2021 +0000
8f07d4
8f07d4
    Fix StartTLS stripping vulnerability
8f07d4
    
8f07d4
    Reported by Alexandr Savca in https://hackerone.com/reports/1178562
8f07d4
    
8f07d4
    Co-authored-by: Shugo Maeda <shugo@ruby-lang.org>
8f07d4
    
8f07d4
    
8f07d4
    git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67950 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8f07d4
8f07d4
diff --git a/lib/net/imap.rb b/lib/net/imap.rb
8f07d4
index 1c7e89ba14..91df89b79e 100644
8f07d4
--- a/lib/net/imap.rb
8f07d4
+++ b/lib/net/imap.rb
8f07d4
@@ -1213,12 +1213,14 @@ def get_tagged_response(tag, cmd)
8f07d4
       end
8f07d4
       resp = @tagged_responses.delete(tag)
8f07d4
       case resp.name
8f07d4
+      when /\A(?:OK)\z/ni
8f07d4
+        return resp
8f07d4
       when /\A(?:NO)\z/ni
8f07d4
         raise NoResponseError, resp
8f07d4
       when /\A(?:BAD)\z/ni
8f07d4
         raise BadResponseError, resp
8f07d4
       else
8f07d4
-        return resp
8f07d4
+        raise UnknownResponseError, resp
8f07d4
       end
8f07d4
     end
8f07d4
 
8f07d4
@@ -3714,6 +3716,10 @@ class BadResponseError < ResponseError
8f07d4
     class ByeResponseError < ResponseError
8f07d4
     end
8f07d4
 
8f07d4
+    # Error raised upon an unknown response from the server.
8f07d4
+    class UnknownResponseError < ResponseError
8f07d4
+    end
8f07d4
+
8f07d4
     RESPONSE_ERRORS = Hash.new(ResponseError)
8f07d4
     RESPONSE_ERRORS["NO"] = NoResponseError
8f07d4
     RESPONSE_ERRORS["BAD"] = BadResponseError
8f07d4
diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb
8f07d4
index 936f4e0f42..81928cb8fe 100644
8f07d4
--- a/test/net/imap/test_imap.rb
8f07d4
+++ b/test/net/imap/test_imap.rb
8f07d4
@@ -127,6 +127,24 @@ def test_starttls
8f07d4
         imap.disconnect
8f07d4
       end
8f07d4
     end
8f07d4
+
8f07d4
+    def test_starttls_stripping
8f07d4
+      starttls_stripping_test do |port|
8f07d4
+        imap = Net::IMAP.new("localhost", :port => port)
8f07d4
+        assert_raise(Net::IMAP::UnknownResponseError) do
8f07d4
+          imap.starttls(:ca_file => CA_FILE)
8f07d4
+        end
8f07d4
+        imap
8f07d4
+      end
8f07d4
+    end
8f07d4
+  end
8f07d4
+
8f07d4
+  def start_server
8f07d4
+    th = Thread.new do
8f07d4
+      yield
8f07d4
+    end
8f07d4
+    @threads << th
8f07d4
+    sleep 0.1 until th.stop?
8f07d4
   end
8f07d4
 
8f07d4
   def test_unexpected_eof
8f07d4
@@ -760,6 +760,27 @@ def starttls_test
8f07d4
     end
8f07d4
   end
8f07d4
 
8f07d4
+  def starttls_stripping_test
8f07d4
+    server = create_tcp_server
8f07d4
+    port = server.addr[1]
8f07d4
+    start_server do
8f07d4
+      sock = server.accept
8f07d4
+      begin
8f07d4
+        sock.print("* OK test server\r\n")
8f07d4
+        sock.gets
8f07d4
+        sock.print("RUBY0001 BUG unhandled command\r\n")
8f07d4
+      ensure
8f07d4
+        sock.close
8f07d4
+        server.close
8f07d4
+      end
8f07d4
+    end
8f07d4
+    begin
8f07d4
+      imap = yield(port)
8f07d4
+    ensure
8f07d4
+      imap.disconnect if imap && !imap.disconnected?
8f07d4
+    end
8f07d4
+  end
8f07d4
+
8f07d4
   def create_tcp_server
8f07d4
     return TCPServer.new(server_addr, 0)
8f07d4
   end