Project

General

Profile

Actions

Feature #10503

open

introduce InvalidPercentEncoding error for failed URI parsing

Added by jackdanger (Jack Danger) about 10 years ago. Updated almost 7 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:66239]

Description

Currently an ArgumentError is raised if a URI has an invalid percent encoding. This makes it difficult to rescue safely. Here I'm adding a patch to introduce an InvalidPercentEncoding error that would help applications rescue and handle this specific problem.

Index: lib/uri/common.rb
===================================================================
--- lib/uri/common.rb	(revision 48392)
+++ lib/uri/common.rb	(working copy)
@@ -152,6 +152,10 @@
   # URI is valid, bad usage is not.
   #
   class BadURIError < Error; end
+  #
+  # The "%"-encoding of a URI part is invalid
+  #
+  class InvalidPercentEncoding < ArgumentError; end
 
   #
   # == Synopsis
@@ -379,7 +383,7 @@
   #
   # See URI.encode_www_form_component, URI.decode_www_form
   def self.decode_www_form_component(str, enc=Encoding::UTF_8)
-    raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/ =~ str
+    raise InvalidPercentEncoding, "invalid %-encoding (#{str})" if /%(?!\h\h)/ =~ str
     str.b.gsub(/\+|%\h\h/, TBLDECWWWCOMP_).force_encoding(enc)
   end
 
Index: test/uri/test_common.rb
===================================================================
--- test/uri/test_common.rb	(revision 48392)
+++ test/uri/test_common.rb	(working copy)
@@ -99,10 +99,10 @@
     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_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_nothing_raised(ArgumentError){URI.decode_www_form_component("x"*(1024*1024))}
+    assert_raise(InvalidPercentEncoding){URI.decode_www_form_component("%")}
+    assert_raise(InvalidPercentEncoding){URI.decode_www_form_component("%a")}
+    assert_raise(InvalidPercentEncoding){URI.decode_www_form_component("x%a_")}
+    assert_nothing_raised(InvalidPercentEncoding){URI.decode_www_form_component("x"*(1024*1024))}
   end
 
   def test_encode_www_form

Files

raise_invalid_percent_encoding_error.diff (1.8 KB) raise_invalid_percent_encoding_error.diff jackdanger (Jack Danger), 11/13/2014 12:06 AM
Actions #1

Updated by naruse (Yui NARUSE) almost 7 years ago

  • Target version deleted (2.2.0)
Actions

Also available in: Atom PDF

Like0
Like0