Hack for using OpenURI with SSL

Ruby posted almost 3 years ago by christian

This problem occurs with OpenURI and Ruby 1.8:

   1  /usr/lib/ruby/1.8/net/http.rb:586:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: ce
   2  rtificate verify failed (OpenSSL::SSL::SSLError)
   3          from /usr/lib/ruby/1.8/net/http.rb:586:in `connect'
   4          from /usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
   5          from /usr/lib/ruby/1.8/net/http.rb:542:in `start'
   6          from /usr/lib/ruby/1.8/open-uri.rb:242:in `open_http'

With Ruby 1.9 you have an option to fix it like this:

   1  open(uri,:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)

For Ruby 1.8 you could do this:

   1  require 'open-uri'
   2  require 'openssl'
   3  
   4  url = 'https://www....'
   5  proxy = 'http://...'
   6  
   7  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
   8  
   9  puts open(url, :proxy => proxy).read

There are other options like patches for OpenURI, but nothing beats a quick and dirty hack if you’re just testing.

Code found here

Tagged openuri, ssl, http, hack