net-http icon indicating copy to clipboard operation
net-http copied to clipboard

Incorrect Host header for IPv6 addresses

Open AEtherC0r3 opened this issue 3 years ago • 1 comments

When specifying a URI with an IPv6 address as the host, the Host header of the request contains the IPv6 address but it is not surrounded by brackets

e.g. With a locally running apache webserver, the following snippet results in an HTTP 400 Bad Request error because of the malformed host header (Host: ::1 instead of Host: [::1])

require 'net/http'
uri = URI.parse('http://[::1]')
Net::HTTP.get_response(uri)

AEtherC0r3 avatar Sep 29 '22 12:09 AEtherC0r3

I tried to debug this issue a little bit. When URI is not used, the addr_port method is normally called. The addr_port method handles wrapping an IPv6 address in brackets. When a URI is used, we end up here, where req['host'] is set and that prevents addr_port from being called. I don't know what the best fix would be, but having a single source of truth for handling the conversion of the 'host' header would probably help.

Edit: How we end up with ::1 rather than [::1] is due to using #hostname (vs #host).

0x1eef avatar Sep 18 '23 04:09 0x1eef