[ruby/uri] Do not allow empty host names, as they are not allowed by RFC 3986

Pointed out by John Hawthorn.

Fixes [Bug #20686]

https://github.com/ruby/uri/commit/c0cfa04a66
This commit is contained in:
Jeremy Evans 2024-08-19 16:42:40 -07:00 committed by git
parent 1add45e2a6
commit 22b81b5bf5
3 changed files with 20 additions and 2 deletions

View File

@ -61,6 +61,18 @@ module URI
super(tmp)
end
# Do not allow empty host names, as they are not allowed by RFC 3986.
def check_host(v)
ret = super
if ret && v.empty?
raise InvalidComponentError,
"bad component(expected host component): #{v}"
end
ret
end
#
# == Description
#

View File

@ -846,8 +846,10 @@ class URI::TestGeneric < Test::Unit::TestCase
assert_equal("http://[::1]/bar", u.to_s)
u.hostname = "::1"
assert_equal("http://[::1]/bar", u.to_s)
u.hostname = ""
assert_equal("http:///bar", u.to_s)
u = URI("file://foo/bar")
u.hostname = ''
assert_equal("file:///bar", u.to_s)
end
def test_build

View File

@ -19,6 +19,10 @@ class URI::TestHTTP < Test::Unit::TestCase
assert_kind_of(URI::HTTP, u)
end
def test_build_empty_host
assert_raise(URI::InvalidComponentError) { URI::HTTP.build(host: '') }
end
def test_parse
u = URI.parse('http://a')
assert_kind_of(URI::HTTP, u)