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.)

@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.

@HoneyryderChuck
Copy link

At this point you could just use https://docs.ruby-lang.org/en/master/URI/HTTP.html#method-i-authority ?

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

At this point you could just use https://docs.ruby-lang.org/en/master/URI/HTTP.html#method-i-authority ?

@HoneyryderChuck Thank you for the comment.

The URI::HTTP#authority method was added in Ruby 3.0, but since the net-http gem supports Ruby 2.6 and later, I don't think we can use it.

spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")

@taketo1113
Copy link
Author

@sorah Thank you for reviewing.

I've fixed the parts you pointed out in your comments.

Only the following caused errors with older versions of Ruby or the uri gem, so I kept the original implementation:
#237 (comment)

@sorah
Copy link
Member

sorah commented Nov 10, 2025

The URI::HTTP#authority method was added in Ruby 3.0, but since the net-http gem supports Ruby 2.6 and later, I don't think we can use it.

I agree raising minimum version to 3.0 as the present minimum version is too outdated. It's also worth to specify version to > 0.10.0.

@taketo1113
Copy link
Author

I agree raising minimum version to 3.0 as the present minimum version is too outdated. It's also worth to specify version to > 0.10.0.

Should this Pull Request handle raising the minimum Ruby version?

Personally, since this Pull Request is a bug fix, I think it's better not to include breaking changes such as raising the minimum Ruby version.
Also, since the system Ruby on macOS 26 (released in September 2025) is still 2.6.10, I'd appreciate it if older Ruby versions could continue to be supported. (That said, Ruby 4.0 is coming soon, though…)


As for the CI errors, I believe they will pass if you rerun the workflow.
I tried running it several times in my forked repository, and all tests passed successfully.

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

3 participants