[#43186] [Ruby 1.9-Bug#4388][Open] open-uriで環境変数http_proxyを使うときに認証付きのProxyが使えません — あつし よしだ <redmine@...>

Bug #4388: open-uri=E3=81=A7=E7=92=B0=E5=A2=83=E5=A4=89=E6=95=B0http_prox=

11 messages 2011/02/10
[#43192] [Ruby 1.9-Bug#4388] open-uriで環境変数http_proxyを使うときに認証付きのProxyが使えません — あつし よしだ <redmine@...> 2011/02/11

チケット #4388 が更新されました。 (by あつし よしだ)

[#43193] Re: [Ruby 1.9-Bug#4388] open-uriで環境変数http_proxyを使うときに認証付きのProxyが使えません — Tanaka Akira <akr@...> 2011/02/11

2011年2月11日12:59 あつし よしだ <redmine@ruby-lang.org>:

[#43203] [Ruby 1.9-Bug#4397][Open] test-mkmf fails due to compilation errors — Shyouhei Urabe <redmine@...>

Bug #4397: test-mkmf fails due to compilation errors

10 messages 2011/02/14

[#43272] [Ruby 1.9 - Bug #4443] [Open] odd evaluation order in a multiple assignment — Yusuke Endoh <mame@...>

13 messages 2011/02/24

[#43274] [Ruby 1.9 - Bug #4445] [Open] ext/openssl の verify_callback が rb_protect で保護されていない — Ippei Obayashi <ohai@...>

13 messages 2011/02/24

[#43276] iseq_compile_each()でのマジックナンバ — きたざわけんいち <peisunstar@...>

きたざわです。

15 messages 2011/02/27
[#43303] Re: iseq_compile_each()でのマジックナンバ — nagachika <nagachika00@...> 2011/03/04

近永と申します。

[#43304] Re: iseq_compile_each()でのマジックナンバ — Yusuke ENDOH <mame@...> 2011/03/04

遠藤です。

[ruby-dev:43196] Re: [Ruby 1.9-Bug#4388] open-uriで環境変数http_proxyを使うときに認証付きのProxyが使えません

From: "Shota Fukumori (sora_h)" <sorah@...>
Date: 2011-02-11 11:40:39 UTC
List: ruby-dev #43196
#ruby-ja @ircnetでも出ていましたが、warningを出力しながらも使用するというのはどうでしょうか。
patchも書きました

2011/2/11 Tanaka Akira <akr@fsij.org>:
> とくにおかしいとは思いません。
> http_proxy は URI ですが、query や fragment など他にも無視するところは
> たくさんあります。


-- 
Shota Fukumori a.k.a. @sora_h - http://codnote.net/

Attachments (1)

open-uri_with_http_proxy2.patch (3.12 KB, text/x-diff)
diff --git a/lib/open-uri.rb b/lib/open-uri.rb
index c8393fa..807283a 100644
--- a/lib/open-uri.rb
+++ b/lib/open-uri.rb
@@ -183,7 +183,15 @@ module OpenURI
     end
     case opt_proxy
     when true
-      find_proxy = lambda {|u| pxy = u.find_proxy; pxy ? [pxy, nil, nil] : nil}
+      find_proxy = lambda {|u|
+        if pxy = u.find_proxy
+          (proxy_user, proxy_pass) = pxy.userinfo.to_s.split(":")
+          warn "#{caller(1)[0}}: warning: Including password or username has security problem." if proxy_user || proxy_pass
+          [pxy, proxy_user, proxy_pass]
+        else
+          nil
+        end
+      }
     when nil, false
       find_proxy = lambda {|u| nil}
     when String
@@ -736,17 +744,17 @@ module URI
         proxy_uri = ENV[name] || ENV[name.upcase]
       end
 
-      if proxy_uri && self.hostname
-        require 'socket'
-        begin
-          addr = IPSocket.getaddress(self.hostname)
-          proxy_uri = nil if /\A127\.|\A::1\z/ =~ addr
-        rescue SocketError
-        end
-      end
-
       if proxy_uri
         proxy_uri = URI.parse(proxy_uri)
+        if self.hostname
+          require 'socket'
+          begin
+            addr = IPSocket.getaddress(self.hostname)
+            return nil if /\A127\.|\A::1\z/ =~ addr && proxy_uri.userinfo.nil?
+          rescue SocketError
+          end
+        end
+
         name = 'no_proxy'
         if no_proxy = ENV[name] || ENV[name.upcase]
           no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
diff --git a/test/open-uri/test_open-uri.rb b/test/open-uri/test_open-uri.rb
index 14f08ff..621649e 100644
--- a/test/open-uri/test_open-uri.rb
+++ b/test/open-uri/test_open-uri.rb
@@ -271,6 +271,42 @@ class TestOpenURI < Test::Unit::TestCase
     }
   end
 
+  def test_proxy_http_basic_authentication_from_env
+    with_http {|srv, dr, url|
+      log = ''
+      proxy_user = 'user'
+      proxy_pass = 'pass'
+      proxy = WEBrick::HTTPProxyServer.new({
+        :ServerType => Thread,
+        :Logger => WEBrick::Log.new(NullLog),
+        :AccessLog => [[NullLog, ""]],
+        :ProxyAuthProc => lambda {|req, res|
+          log << req.request_line
+          auth = ["#{proxy_user}:#{proxy_pass}"].pack('m').chomp
+          if req["Proxy-Authorization"] != "Basic #{auth}"
+            raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
+          end
+        },
+        :BindAddress => '127.0.0.1',
+        :Port => 0})
+      _, proxy_port, _, proxy_host = proxy.listeners[0].addr
+      proxy_url = "http://#{proxy_user}:#{proxy_pass}@#{proxy_host}:#{proxy_port}"
+      ::ENV['http_proxy'] = proxy_url
+      begin
+        th = proxy.start
+        open("#{dr}/proxy", "w") {|f| f << "proxy" }
+        open("#{url}/proxy") {|f|
+          assert_equal("200", f.status[0])
+          assert_equal("proxy", f.read)
+        }
+        assert_match(/#{Regexp.quote url}/, log); log.clear
+        assert_equal("", log); log.clear
+      ensure
+        proxy.shutdown
+      end
+    }
+  end
+
   def test_redirect
     with_http {|srv, dr, url|
       srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }

In This Thread