8784: Fix test for latest firefox.
[arvados.git] / services / api / config / initializers / fix_www_decode.rb
1 module URI
2   if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2')
3     # Rack uses the standard library method URI.decode_www_form_component to
4     # process parameters.  This method first validates the string with a
5     # regular expression, and then decodes it using another regular expression.
6     # Ruby 2.1 and earlier has a bug is in the validation; the regular
7     # expression that is used generates many backtracking points, which results
8     # in exponential memory growth when matching large strings.  The fix is to
9     # monkey-patch the version of the method from Ruby 2.2 which checks that
10     # the string is not invalid instead of checking it is valid.
11     def self.decode_www_form_component(str, enc=Encoding::UTF_8)
12       raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/ =~ str
13       str.b.gsub(/\+|%\h\h/, TBLDECWWWCOMP_).force_encoding(enc)
14     end
15   end
16 end