MechanizeでSSLの証明書確認を無暗にオフにすると警告がでた話
Ruby(2.0.0-p195)でMechanize(gemでインストールした2.7.3)を使ってがりがり書いてた。
ら、案の定SSLで詰まった。証明書確認できてないからエラー吐いた様子。
F:\hoge>ruby mechanize-warning.rb
C:/***/Ruby-200-p195/lib/ruby/2.0.0/net/http.rb:918:in `connect': SSL
_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify fail
ed (OpenSSL::SSL::SSLError)
from C:/***/Ruby-200-p195/lib/ruby/2.0.0/net/http.rb:918:in `
block in connect'
以下略。
RubyのMechanizeがSSLエラーになる。 - それマグで!を参考に、
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
を追加。open-sslはmechanizeですでにreuireされている模様。
すると、
mechanize-warning.rb:4: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER !!!SECURITY WARNING!!! The SSL HTTP connection to: hoge.jp:443 !!!MAY NOT BE VERIFIED!!! On your platform your OpenSSL implementation is broken. There is no difference between the values of VERIFY_NONE and VERIFY_PEER. This means that attempting to verify the security of SSL connections may not work. This exposes you to man-in-the-middle exploits, snooping on the contents of your connection and other dangers to the security of your data. To disable this warning define the following constant at top-level in your application: I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nil
という警告が。
指定通りにI_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG = nilをすると警告なしに。
また、代わりに
agent = Mechanize.new agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
を指定しても警告なしに。
恐らくOpenSSLモジュール(だっけか)側の話だと思われるが、片方警告出してもう片方は警告なしというのは気持ち悪いなぁ。