[前][次][番号順一覧][スレッド一覧]

ruby-changes:20174

From: nahi <ko1@a...>
Date: Fri, 24 Jun 2011 20:06:11 +0900 (JST)
Subject: [ruby-changes:20174] nahi:r32222 (trunk): * lib/webrick/httprequest.rb (setup_forwarded_info): Parsing request

nahi	2011-06-24 20:05:59 +0900 (Fri, 24 Jun 2011)

  New Revision: 32222

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32222

  Log:
    * lib/webrick/httprequest.rb (setup_forwarded_info): Parsing request 
      header failed when the request is from 2 or more Apache reverse 
      proxies. It's said that all X-Forwarded-* headers will contain more
      than one (comma-separated) value if the original request already
      contained one of these headers.  Since we could use these values as
      Host header, we choose the initial(first) value. See #4922.
    
    * test/webrick/test_httprequest.rb (test_forwarded): Test it.

  Modified files:
    trunk/ChangeLog
    trunk/lib/webrick/httprequest.rb
    trunk/test/webrick/test_httprequest.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 32221)
+++ ChangeLog	(revision 32222)
@@ -1,3 +1,14 @@
+Fri Jun 24 19:57:30 2011  Hiroshi Nakamura  <nahi@r...>
+
+	* lib/webrick/httprequest.rb (setup_forwarded_info): Parsing request
+	  header failed when the request is from 2 or more Apache reverse
+	  proxies. It's said that all X-Forwarded-* headers will contain more
+	  than one (comma-separated) value if the original request already
+	  contained one of these headers.  Since we could use these values as
+	  Host header, we choose the initial(first) value. See #4922.
+
+	* test/webrick/test_httprequest.rb (test_forwarded): Test it.
+
 Fri Jun 24 17:06:33 2011  Nobuyoshi Nakada  <nobu@r...>
 
 	* process.c (proc_daemon): should not start timer thread
Index: lib/webrick/httprequest.rb
===================================================================
--- lib/webrick/httprequest.rb	(revision 32221)
+++ lib/webrick/httprequest.rb	(revision 32222)
@@ -434,10 +434,18 @@
       ^(::ffff:)?(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\.
     /ixo
 
+    # It's said that all X-Forwarded-* headers will contain more than one
+    # (comma-separated) value if the original request already contained one of
+    # these headers. Since we could use these values as Host header, we choose
+    # the initial(first) value. (apr_table_mergen() adds new value after the
+    # existing value with ", " prefix)
     def setup_forwarded_info
-      @forwarded_server = self["x-forwarded-server"]
+      if @forwarded_server = self["x-forwarded-server"]
+        @forwarded_server = @forwarded_server.split(",", 2).first
+      end
       @forwarded_proto = self["x-forwarded-proto"]
       if host_port = self["x-forwarded-host"]
+        host_port = host_port.split(",", 2).first
         @forwarded_host, tmp = host_port.split(":", 2)
         @forwarded_port = (tmp || (@forwarded_proto == "https" ? 443 : 80)).to_i
       end
Index: test/webrick/test_httprequest.rb
===================================================================
--- test/webrick/test_httprequest.rb	(revision 32221)
+++ test/webrick/test_httprequest.rb	(revision 32222)
@@ -303,6 +303,28 @@
     assert_equal(443, req.port)
     assert_equal("234.234.234.234", req.remote_ip)
     assert(req.ssl?)
+
+    msg = <<-_end_of_message_
+      GET /foo HTTP/1.1
+      Host: localhost:10080
+      Client-IP: 234.234.234.234
+      X-Forwarded-Proto: https
+      X-Forwarded-For: 192.168.1.10
+      X-Forwarded-Host: forward1.example.com:1234, forward2.example.com:5678
+      X-Forwarded-Server: server1.example.com, server2.example.com
+      X-Requested-With: XMLHttpRequest
+      Connection: Keep-Alive
+
+    _end_of_message_
+    msg.gsub!(/^ {6}/, "")
+    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
+    req.parse(StringIO.new(msg))
+    assert_equal("server1.example.com", req.server_name)
+    assert_equal("https://forward1.example.com:1234/foo", req.request_uri.to_s)
+    assert_equal("forward1.example.com", req.host)
+    assert_equal(1234, req.port)
+    assert_equal("234.234.234.234", req.remote_ip)
+    assert(req.ssl?)
   end
 
   def test_continue_sent

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]