summaryrefslogtreecommitdiff
diff options
authorJeremy Evans <[email protected]>2024-08-19 16:42:40 -0700
committergit <[email protected]>2025-07-12 07:07:39 +0000
commit22b81b5bf56d7c5053008697d9e6b2a9c4eb79f4 (patch)
tree89f2a57a285ae92a3a8fbe2460c4709366b0d2ec
parent1add45e2a6e2ab62458f04eddf24898f61e7c01d (diff)
[ruby/uri] Do not allow empty host names, as they are not allowed by RFC 3986HEADmaster
Pointed out by John Hawthorn. Fixes [Bug #20686] https://github.com/ruby/uri/commit/c0cfa04a66
-rw-r--r--lib/uri/http.rb12
-rw-r--r--test/uri/test_generic.rb6
-rw-r--r--test/uri/test_http.rb4
3 files changed, 20 insertions, 2 deletions
diff --git a/lib/uri/http.rb b/lib/uri/http.rb
index 900b132c8c..3c41cd4e93 100644
--- a/lib/uri/http.rb
+++ b/lib/uri/http.rb
@@ -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
#
diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb
index 0d29dd42d3..c725116e96 100644
--- a/test/uri/test_generic.rb
+++ b/test/uri/test_generic.rb
@@ -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
diff --git a/test/uri/test_http.rb b/test/uri/test_http.rb
index dc076ce541..8816d20175 100644
--- a/test/uri/test_http.rb
+++ b/test/uri/test_http.rb
@@ -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)
close