Skip to content

Conversation

@taketo1113
Copy link

@taketo1113 taketo1113 commented Nov 5, 2025

Background

When an IPv6 literal URI is passed as uri_or_path to Net::HTTPGenericRequest, an issue in IPv6 handling causes the HTTP request header Host to become incorrect.
For example, it becomes ::1:8000 instead of [::1]:8000.

This incorrect Host header leads to the following problems:

Details

This Pull Request fixes the handling of IPv6 literal hosts in Net::HTTPGenericRequest when a URI with an IPv6 literal is passed.
(This Pull Request revises and improves upon Pull Request #156.)

With this change, the Host header in HTTP requests generated from IPv6 literal URIs will now have the correct format.
Since this fix only resolves cases that previously caused HTTP 400 Bad Request errors, I think it does not introduce any compatibility issues.

  • Before: Host: 2001:db8::1:8000
  • After: Host: [2001:db8::1]:8000

The behavior for non-IPv6 literal URIs remains unchanged.

Expected behavior

  • The Host header (req['Host']) returns an correct value
  • No error occurs in Net::HTTPGenericRequest#update_uri with http://[::1]
require 'net/http'

# http://[2001:db8::1]:8000
req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]:8000"))
req['Host']
#=> "[2001:db8::1]:8000"

req.update_uri("test", 8001, false)
req.uri.host
#=> "[2001:db8::1]"

# http://[::1]:8000
req = Net::HTTP::Get.new(URI.parse("http://[::1]:8000"))
req['Host']
#=> "[::1]:8000"

req.update_uri("example.com", 8001, false)
req.uri.host
#=> "[::1]"

Actual behavior

  • The Host header (req['Host']) returns an incorrect value
  • Net::HTTPGenericRequest#update_uri raises URI::InvalidComponentError with http://[::1] when using uri v1.0.4 - 1.1.0
require 'net/http'

# http://[2001:db8::1]:8000
req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]:8000"))
req['Host']
#=> "2001:db8::1:8000"

req.update_uri("example.com", 8001, false)
req.uri.host
#=> "2001"

# http://[::1]:8000
req = Net::HTTP::Get.new(URI.parse("http://[::1]:8000"))
req['Host']
#=> "::1:8000"

req.update_uri("example.com", 8001, false)
/Users/taketo/path/ruby/3.4.0/gems/uri-1.1.0/lib/uri/http.rb:69:in 'URI::HTTP#check_host': bad component(expected host component):  (URI::InvalidComponentError)
	from /Users/taketo/path/ruby/3.4.0/gems/uri-1.1.0/lib/uri/generic.rb:653:in 'URI::Generic#host='
	from /Users/taketo/path/ruby/3.4.0/gems/net-http-0.7.0/lib/net/http/generic_request.rb:255:in 'Net::HTTPGenericRequest#update_uri'
...

System configuration

  • ruby: 3.4.7
  • net-http: 0.7.0
  • uri: 1.1.0

Additional Information

The CI errors in test (2.7 / windows-latest) and test (2.6 / windows-latest) are unrelated to this fix and will be resolved once #236 is merged.
(It resolved by power_assert v3.0.1.)

Co-authored-by: 0x1eef <0x1eef@users.noreply.github.com>
@taketo1113
Copy link
Author

Additional Information

The CI errors in test (2.7 / windows-latest) and test (2.6 / windows-latest) are unrelated to this fix and will be resolved once #236 is merged.

Since the cause of the CI errors has been resolved in power_assert v3.0.1, rerunning the CI should make the tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IPv6 host addr causes URI::InvalidComponentError since uri v1.1.0 Incorrect Host header for IPv6 addresses

1 participant