From e2d2e21a570516424a3a935e5ab2a67db0ac53d0 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Fri, 27 Nov 2026 21:37:11 +0000 Subject: [PATCH] Decode URI components with cgi/escape URI.decode_www_form_component and URI.decode_uri_component copy the String with `b` and then run `gsub` with a regexp and a Hash table, which costs a match, a substring and a Hash lookup for every "+" and "%XX". Rack and Rails decode query strings, URL-encoded form fields and cookies through decode_www_form_component. The shared _decode_uri_component keeps the lone-"%" check, the `b` copy and the final force_encoding, and now takes the decoding step as a block. Where cgi/escape implements unescapeURIComponent natively (in C since cgi 0.3.3, in Java since 0.4.1), decode_www_form_component passes CGI.unescape and decode_uri_component passes CGI.unescapeURIComponent, which keeps "+"; both decode in one pass. Both get the BINARY copy, so the result has the same class, bytes and encoding as before. Elsewhere both methods keep the `gsub`: older cgi has no unescapeURIComponent, and on TruffleRuby cgi/escape is written in Ruby, where CGI.unescapeURIComponent keeps the String subclass that String#b returns there. URI.decode_www_form keeps its `gsub!`: it leaves a lone "%" as is, and for the plain keys and values most forms have, CGI.unescape would return a new String where `gsub!` changes nothing. --- lib/uri/common.rb | 91 +++++++++++++++++++++++------------------ test/uri/test_common.rb | 14 +++++++ 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/lib/uri/common.rb b/lib/uri/common.rb index 971a684b..bf2e7148 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -11,6 +11,7 @@ require_relative "rfc2396_parser" require_relative "rfc3986_parser" +require "cgi/escape" module URI # The default parser instance for RFC 2396. @@ -398,37 +399,52 @@ def self.encode_www_form_component(str, enc=nil) _encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_, str, enc) end - # Returns a string decoded from the given \URL-encoded string +str+. - # - # The given string is first encoded as Encoding::ASCII-8BIT (using String#b), - # then decoded (as below), and finally force-encoded to the given encoding +enc+. - # - # The returned string: - # - # - Preserves: - # - # - Characters '*', '.', '-', and '_'. - # - Character in ranges 'a'..'z', 'A'..'Z', - # and '0'..'9'. - # - # Example: - # - # URI.decode_www_form_component('*.-_azAZ09') - # # => "*.-_azAZ09" - # - # - Converts: - # - # - Character '+' to character ' '. - # - Each "percent notation" to an ASCII character. - # - # Example: - # - # URI.decode_www_form_component('Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A') - # # => "Here are some punctuation characters: ,;?:" - # - # Related: URI.decode_uri_component (preserves '+'). - def self.decode_www_form_component(str, enc=Encoding::UTF_8) - _decode_uri_component(/\+|%\h\h/, str, enc) + if CGI.respond_to?(:unescapeURIComponent) && !CGI.method(:unescapeURIComponent).source_location + # Returns a string decoded from the given \URL-encoded string +str+. + # + # The given string is first encoded as Encoding::ASCII-8BIT (using String#b), + # then decoded (as below), and finally force-encoded to the given encoding +enc+. + # + # The returned string: + # + # - Preserves: + # + # - Characters '*', '.', '-', and '_'. + # - Character in ranges 'a'..'z', 'A'..'Z', + # and '0'..'9'. + # + # Example: + # + # URI.decode_www_form_component('*.-_azAZ09') + # # => "*.-_azAZ09" + # + # - Converts: + # + # - Character '+' to character ' '. + # - Each "percent notation" to an ASCII character. + # + # Example: + # + # URI.decode_www_form_component('Here+are+some+punctuation+characters%3A+%2C%3B%3F%3A') + # # => "Here are some punctuation characters: ,;?:" + # + # Related: URI.decode_uri_component (preserves '+'). + def self.decode_www_form_component(str, enc=Encoding::UTF_8) + _decode_uri_component(str, enc) {|bytes| CGI.unescape(bytes, Encoding::ASCII_8BIT) } + end + + # Like URI.decode_www_form_component, except that '+' is preserved. + def self.decode_uri_component(str, enc=Encoding::UTF_8) + _decode_uri_component(str, enc) {|bytes| CGI.unescapeURIComponent(bytes, Encoding::ASCII_8BIT) } + end + else # cgi before 0.3.3, or cgi/escape written in Ruby + def self.decode_www_form_component(str, enc=Encoding::UTF_8) + _decode_uri_component(str, enc) {|bytes| bytes.gsub(/\+|%\h\h/, TBLDECWWWCOMP_) } + end + + def self.decode_uri_component(str, enc=Encoding::UTF_8) + _decode_uri_component(str, enc) {|bytes| bytes.gsub(/%\h\h/, TBLDECWWWCOMP_) } + end end # Like URI.encode_www_form_component, except that ' ' (space) @@ -437,11 +453,6 @@ def self.encode_uri_component(str, enc=nil) _encode_uri_component(/[^*\-.0-9A-Z_a-z]/, TBLENCURICOMP_, str, enc) end - # Like URI.decode_www_form_component, except that '+' is preserved. - def self.decode_uri_component(str, enc=Encoding::UTF_8) - _decode_uri_component(/%\h\h/, str, enc) - end - # Returns a string derived from the given string +str+ with # URI-encoded characters matching +regexp+ according to +table+. def self._encode_uri_component(regexp, table, str, enc) @@ -458,11 +469,11 @@ def self._encode_uri_component(regexp, table, str, enc) end private_class_method :_encode_uri_component - # Returns a string decoding characters matching +regexp+ from the - # given \URL-encoded string +str+. - def self._decode_uri_component(regexp, str, enc) + # Returns the given \URL-encoded string +str+ decoded in encoding +enc+; + # the block decodes its bytes. + def self._decode_uri_component(str, enc) raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/.match?(str) - str.b.gsub(regexp, TBLDECWWWCOMP_).force_encoding(enc) + yield(str.b).force_encoding(enc) end private_class_method :_decode_uri_component diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb index 56926400..0efe3f2a 100644 --- a/test/uri/test_common.rb +++ b/test/uri/test_common.rb @@ -210,10 +210,17 @@ def test_decode_www_form_component URI.decode_www_form_component("%A1%A2", "EUC-JP")) assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"), URI.decode_www_form_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8"))) + assert_equal("\u3042\uFFFD", URI.decode_www_form_component("%e3%81%82%Ef%bF%Bd")) + assert_equal("\xFF".force_encoding("UTF-8"), URI.decode_www_form_component("%FF".b)) + assert_equal("\x82\x33\xE7\x34 ".force_encoding("UTF-8"), + URI.decode_www_form_component("\x82\x33\xE7\x34+".force_encoding("GB18030"))) + assert_equal("", URI.decode_www_form_component("")) + assert_instance_of(String, URI.decode_www_form_component(Class.new(String).new("a"))) assert_raise(ArgumentError){URI.decode_www_form_component("%")} assert_raise(ArgumentError){URI.decode_www_form_component("%a")} assert_raise(ArgumentError){URI.decode_www_form_component("x%a_")} + assert_raise(ArgumentError){URI.decode_www_form_component("%zz")} assert_nothing_raised(ArgumentError){URI.decode_www_form_component("x"*(1024*1024))} end @@ -262,10 +269,17 @@ def test_decode_uri_component URI.decode_uri_component("%A1%A2", "EUC-JP")) assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"), URI.decode_uri_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8"))) + assert_equal("\u3042\uFFFD", URI.decode_uri_component("%e3%81%82%Ef%bF%Bd")) + assert_equal("\xFF".force_encoding("UTF-8"), URI.decode_uri_component("%FF".b)) + assert_equal("\x82\x33\xE7\x34+".force_encoding("UTF-8"), + URI.decode_uri_component("\x82\x33\xE7\x34+".force_encoding("GB18030"))) + assert_equal("", URI.decode_uri_component("")) + assert_instance_of(String, URI.decode_uri_component(Class.new(String).new("a"))) assert_raise(ArgumentError){URI.decode_uri_component("%")} assert_raise(ArgumentError){URI.decode_uri_component("%a")} assert_raise(ArgumentError){URI.decode_uri_component("x%a_")} + assert_raise(ArgumentError){URI.decode_uri_component("%zz")} assert_nothing_raised(ArgumentError){URI.decode_uri_component("x"*(1024*1024))} end