Skip to content

IPv6 host addr causes URI::InvalidComponentError since uri v1.1.0 #235

Description

@Watson1978

We give the URI.parse result as Net::HTTP.get argument.
If we provide IPv6 address into URI.parse,
Net::HTTP.get causes URI::InvalidComponentError since uri v1.1.0.

Reroduce code

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'uri', '= 1.1.0'
  # gem 'uri', '= 1.0.4'
  gem 'webrick'
end

require 'net/http'

PORT = 24447

Thread.new do
  server = WEBrick::HTTPServer.new(Port: PORT)

  trap 'INT' do
    server.shutdown
  end

  puts "WEBrick server running on http://localhost:#{PORT}"
  server.start
end
sleep 0.2

puts "=== uri gem version ==="
puts URI::VERSION

puts "=== Use IPv4 localhost ==="
localhost = "localhost"

p URI.parse("http://#{localhost}:#{PORT}/api/plugins.flushBuffers")
p Net::HTTP.get(URI.parse("http://#{localhost}:#{PORT}/api/plugins.flushBuffers"))

puts "=== Use IPv6 localhost ==="
localhost = "[::1]"

p URI.parse("http://#{localhost}:#{PORT}/api/plugins.flushBuffers")
p Net::HTTP.get(URI.parse("http://#{localhost}:#{PORT}/api/plugins.flushBuffers"))

Result with uri 1.1.0

It causes URI::InvalidComponentError with uri 1.1.0.

$ ruby -v uri.rb
ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +PRISM [x86_64-linux]
[2025-11-04 20:21:57] INFO  WEBrick 1.9.1
[2025-11-04 20:21:57] INFO  ruby 3.4.7 (2025-10-08) [x86_64-linux]
WEBrick server running on http://localhost:24447
[2025-11-04 20:21:57] INFO  WEBrick::HTTPServer#start: pid=660980 port=24447
=== uri gem version ===
1.1.0
=== Use IPv4 localhost ===
#<URI::HTTP http://localhost:24447/api/plugins.flushBuffers>
[2025-11-04 20:21:57] ERROR '/api/plugins.flushBuffers' not found.
::1 - - [04/Nov/2025:20:21:57 JST] "GET /api/plugins.flushBuffers HTTP/1.1" 404 295
- -> /api/plugins.flushBuffers
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n  <HEAD><TITLE>Not Found</TITLE></HEAD>\n  <BODY>\n    <H1>Not Found</H1>\n    '/api/plugins.flushBuffers' not found.\n    <HR>\n    <ADDRESS>\n     WEBrick/1.9.1 (Ruby/3.4.7/2025-10-08) at\n     localhost:24447\n    </ADDRESS>\n  </BODY>\n</HTML>\n"
=== Use IPv6 localhost ===
#<URI::HTTP http://[::1]:24447/api/plugins.flushBuffers>
[2025-11-04 20:21:57] INFO  going to shutdown ...
[2025-11-04 20:21:57] INFO  WEBrick::HTTPServer#start done.
/home/watson/.rbenv/versions/3.4.7/lib/ruby/gems/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 /home/watson/.rbenv/versions/3.4.7/lib/ruby/gems/3.4.0/gems/uri-1.1.0/lib/uri/generic.rb:653:in 'URI::Generic#host='
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http/generic_request.rb:230:in 'Net::HTTPGenericRequest#update_uri'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:2475:in 'Net::HTTP#begin_transport'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:2404:in 'Net::HTTP#transport_request'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:2378:in 'Net::HTTP#request'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:2249:in 'Net::HTTP#request_get'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:826:in 'block in Net::HTTP.get_response'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:1626:in 'Net::HTTP#start'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:1064:in 'Net::HTTP.start'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:824:in 'Net::HTTP.get_response'
        from /home/watson/.rbenv/versions/3.4.7/lib/ruby/3.4.0/net/http.rb:805:in 'Net::HTTP.get'
        from uri.rb:38:in '<main>'

Expected (with uri 1.0.4)

No exception with uri 1.0.4

$ ruby -v uri.rb
ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +PRISM [x86_64-linux]
[2025-11-04 20:22:48] INFO  WEBrick 1.9.1
[2025-11-04 20:22:48] INFO  ruby 3.4.7 (2025-10-08) [x86_64-linux]
WEBrick server running on http://localhost:24447
[2025-11-04 20:22:48] INFO  WEBrick::HTTPServer#start: pid=661911 port=24447
=== uri gem version ===
1.0.4
=== Use IPv4 localhost ===
#<URI::HTTP http://localhost:24447/api/plugins.flushBuffers>
[2025-11-04 20:22:48] ERROR '/api/plugins.flushBuffers' not found.
::1 - - [04/Nov/2025:20:22:48 JST] "GET /api/plugins.flushBuffers HTTP/1.1" 404 295
- -> /api/plugins.flushBuffers
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n  <HEAD><TITLE>Not Found</TITLE></HEAD>\n  <BODY>\n    <H1>Not Found</H1>\n    '/api/plugins.flushBuffers' not found.\n    <HR>\n    <ADDRESS>\n     WEBrick/1.9.1 (Ruby/3.4.7/2025-10-08) at\n     localhost:24447\n    </ADDRESS>\n  </BODY>\n</HTML>\n"
=== Use IPv6 localhost ===
#<URI::HTTP http://[::1]:24447/api/plugins.flushBuffers>
[2025-11-04 20:22:48] ERROR '/api/plugins.flushBuffers' not found.
::1 - - [04/Nov/2025:20:22:48 JST] "GET /api/plugins.flushBuffers HTTP/1.1" 404 283
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n<HTML>\n  <HEAD><TITLE>Not Found</TITLE></HEAD>\n  <BODY>\n    <H1>Not Found</H1>\n    '/api/plugins.flushBuffers' not found.\n    <HR>\n    <ADDRESS>\n     WEBrick/1.9.1 (Ruby/3.4.7/2025-10-08) at\n     :80\n    </ADDRESS>\n  </BODY>\n</HTML>\n"
- -> /api/plugins.flushBuffers
[2025-11-04 20:22:48] INFO  going to shutdown ...
[2025-11-04 20:22:48] INFO  WEBrick::HTTPServer#start done.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions