From 5bb5ce9ff035bd4dd9bdcce7f926ab961d2c0749 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Thu, 27 Aug 2026 23:35:55 +0300 Subject: [PATCH 1/2] Use converted strings in query and fragment setters --- lib/uri/generic.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index fce8a8a..0110703 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -855,8 +855,7 @@ def query=(v) return @query = nil unless v raise InvalidURIError, "query conflicts with opaque" if @opaque - x = v.to_str - v = x.dup if x.equal? v + v = v.to_str.dup v.encode!(Encoding::UTF_8) rescue nil v.delete!("\t\r\n") v.force_encoding(Encoding::ASCII_8BIT) @@ -944,8 +943,7 @@ def opaque=(v) def fragment=(v) return @fragment = nil unless v - x = v.to_str - v = x.dup if x.equal? v + v = v.to_str.dup v.encode!(Encoding::UTF_8) rescue nil v.delete!("\t\r\n") v.force_encoding(Encoding::ASCII_8BIT) From 4f787ec5c8088f749822e62b008428df0ba388f5 Mon Sep 17 00:00:00 2001 From: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> Date: Fri, 28 Aug 2026 15:04:00 +0300 Subject: [PATCH 2/2] test: cover URI regression --- test/uri/test_generic.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index 9006125..6eeaf5f 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -16,6 +16,20 @@ def uri_to_ary(uri) uri.class.component.collect {|c| uri.send(c)} end + + def test_query_and_fragment_use_to_str_result + converted = "a b".freeze + value = Object.new + value.define_singleton_method(:to_str) { converted } + uri = URI("https://example.test/path") + + uri.query = value + uri.fragment = value + + assert_equal("https://example.test/path?a%20b#a%20b", uri.to_s) + assert_equal("a b", converted) + end + def test_to_s exp = 'http://example.com/'.freeze str = URI(exp).to_s