Skip to content

Commit

Permalink
Merge pull request #1838 from RubyElders/custom-range-exception-2-2
Browse files Browse the repository at this point in the history
Use custom exception on params too deep error.
  • Loading branch information
rafaelfranca authored Apr 4, 2022
2 parents 547e809 + a2091fa commit 59d4440
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
8 changes: 6 additions & 2 deletions lib/rack/query_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ParameterTypeError < TypeError; end
# sequence.
class InvalidParameterError < ArgumentError; end

# ParamsTooDeepError is the error that is raised when params are recursively
# nested over the specified limit.
class ParamsTooDeepError < RangeError; end

def self.make_default(key_space_limit, param_depth_limit)
new Params, key_space_limit, param_depth_limit
end
Expand Down Expand Up @@ -81,7 +85,7 @@ def parse_nested_query(qs, d = nil)
# the structural types represented by two different parameter names are in
# conflict, a ParameterTypeError is raised.
def normalize_params(params, name, v, depth)
raise RangeError if depth <= 0
raise ParamsTooDeepError if depth <= 0

name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
k = $1 || ''
Expand Down Expand Up @@ -168,7 +172,7 @@ def [](key)

def []=(key, value)
@size += key.size if key && !@params.key?(key)
raise RangeError, 'exceeded available parameter key space' if @size > @limit
raise ParamsTooDeepError, 'exceeded available parameter key space' if @size > @limit
@params[key] = value
end

Expand Down
4 changes: 2 additions & 2 deletions test/spec_multipart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ def multipart_file(name)
params['user_sid'].encoding.must_equal Encoding::UTF_8
end

it "raise RangeError if the key space is exhausted" do
it "raise ParamsTooDeepError if the key space is exhausted" do
env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))

old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
begin
lambda { Rack::Multipart.parse_multipart(env) }.must_raise(RangeError)
lambda { Rack::Multipart.parse_multipart(env) }.must_raise(Rack::QueryParser::ParamsTooDeepError)
ensure
Rack::Utils.key_space_limit = old
end
Expand Down
10 changes: 5 additions & 5 deletions test/spec_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def initialize(*)
old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
begin
req = make_request(env)
lambda { req.GET }.must_raise RangeError
lambda { req.GET }.must_raise Rack::QueryParser::ParamsTooDeepError
ensure
Rack::Utils.key_space_limit = old
end
Expand All @@ -306,7 +306,7 @@ def initialize(*)
begin
exp = { "foo" => { "bar" => { "baz" => { "qux" => "1" } } } }
make_request(nested_query).GET.must_equal exp
lambda { make_request(plain_query).GET }.must_raise RangeError
lambda { make_request(plain_query).GET }.must_raise Rack::QueryParser::ParamsTooDeepError
ensure
Rack::Utils.key_space_limit = old
end
Expand All @@ -315,7 +315,7 @@ def initialize(*)
it "limit the allowed parameter depth when parsing parameters" do
env = Rack::MockRequest.env_for("/?a#{'[a]' * 110}=b")
req = make_request(env)
lambda { req.GET }.must_raise RangeError
lambda { req.GET }.must_raise Rack::QueryParser::ParamsTooDeepError

env = Rack::MockRequest.env_for("/?a#{'[a]' * 90}=b")
req = make_request(env)
Expand All @@ -331,7 +331,7 @@ def initialize(*)

env = Rack::MockRequest.env_for("/?a[a][a][a]=b")
req = make_request(env)
lambda { make_request(env).GET }.must_raise RangeError
lambda { make_request(env).GET }.must_raise Rack::QueryParser::ParamsTooDeepError
ensure
Rack::Utils.param_depth_limit = old
end
Expand Down Expand Up @@ -416,7 +416,7 @@ def initialize(*)
old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
begin
req = make_request(env)
lambda { req.POST }.must_raise RangeError
lambda { req.POST }.must_raise Rack::QueryParser::ParamsTooDeepError
ensure
Rack::Utils.key_space_limit = old
end
Expand Down
2 changes: 1 addition & 1 deletion test/spec_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def assert_nested_query(exp, act)

lambda {
Rack::Utils.parse_nested_query("foo#{"[a]" * len}=bar")
}.must_raise(RangeError)
}.must_raise(Rack::QueryParser::ParamsTooDeepError)

Rack::Utils.parse_nested_query("foo#{"[a]" * (len - 1)}=bar")
end
Expand Down

0 comments on commit 59d4440

Please sign in to comment.