1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/webrick] Support literal IPv6 addresses in X-Forwarded-Host

https://github.com/ruby/webrick/commit/6b6990ec81
This commit is contained in:
Jeremy Evans 2019-08-26 22:22:00 -07:00 committed by Hiroshi SHIBATA
parent 37c266aa57
commit c28d50a753
2 changed files with 50 additions and 1 deletions

View file

@ -611,7 +611,12 @@ module WEBrick
end
if host_port = self["x-forwarded-host"]
host_port = host_port.split(",", 2).first
@forwarded_host, tmp = host_port.split(":", 2)
if host_port =~ /\A(\[[0-9a-fA-F:]+\])(?::(\d+))?\z/
@forwarded_host = $1
tmp = $2
else
@forwarded_host, tmp = host_port.split(":", 2)
end
@forwarded_port = (tmp || (@forwarded_proto == "https" ? 443 : 80)).to_i
end
if addrs = self["x-forwarded-for"]

View file

@ -348,6 +348,50 @@ GET /
assert_equal(1234, 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: [fd20:8b1e:b255:8154:250:56ff:fea8:4d84], 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://[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]/foo", req.request_uri.to_s)
assert_equal("[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]", req.host)
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: [fd20:8b1e:b255:8154:250:56ff:fea8:4d84]: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://[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]:1234/foo", req.request_uri.to_s)
assert_equal("[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]", req.host)
assert_equal(1234, req.port)
assert_equal("234.234.234.234", req.remote_ip)
assert(req.ssl?)
end
def test_continue_sent