Skip to content

Commit

Permalink
Merge pull request #713 from rafaelfranca/raise-on-invalid-charset
Browse files Browse the repository at this point in the history
Raise specific exception if the parameters are invalid
  • Loading branch information
tenderlove committed Jul 18, 2014
2 parents 2683d27 + 586cfba commit cc8279f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/rack/utils.rb
Expand Up @@ -26,6 +26,11 @@ module Utils
# parameters (parsed by parse_nested_query) contain conflicting types.
class ParameterTypeError < TypeError; end

# InvalidParameterError is the error that is raised when incoming structural
# parameters (parsed by parse_nested_query) contain invalid format or byte
# sequence.
class InvalidParameterError < TypeError; end

# URI escapes. (CGI style space to +)
def escape(s)
URI.encode_www_form_component(s)
Expand Down Expand Up @@ -106,6 +111,8 @@ def parse_nested_query(qs, d = nil)
end

return params.to_params_hash
rescue ArgumentError => e

This comment has been minimized.

Copy link
@raggi

raggi Jul 18, 2014

Member

This is a major API change!!!!

:'(

Why didn't you make InvalidParamterError < ArgumentError ??

This comment has been minimized.

Copy link
@raggi

raggi Jul 18, 2014

Member

Fixed in 975ccac

raise InvalidParameterError, e.message
end
module_function :parse_nested_query

Expand Down
12 changes: 12 additions & 0 deletions test/spec_request.rb
Expand Up @@ -177,6 +177,18 @@
req.params.should.equal req.GET.merge(req.POST)
end

should "raise if input params has invalid %-encoding" do
mr = Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'POST',
:input => "a%=1"
)
req = Rack::Request.new mr

lambda { req.POST }.
should.raise(Rack::Utils::InvalidParameterError).
message.should.equal "invalid %-encoding (a%)"
end

should "raise if rack.input is missing" do
req = Rack::Request.new({})
lambda { req.POST }.should.raise(RuntimeError)
Expand Down
6 changes: 6 additions & 0 deletions test/spec_utils.rb
Expand Up @@ -223,6 +223,12 @@ def kcodeu
lambda { Rack::Utils.parse_nested_query("x[y]=1&x[y][][w]=2") }.
should.raise(Rack::Utils::ParameterTypeError).
message.should.equal "expected Array (got String) for param `y'"

if RUBY_VERSION.to_f > 1.9
lambda { Rack::Utils.parse_nested_query("foo%81E=1") }.
should.raise(Rack::Utils::InvalidParameterError).
message.should.equal "invalid byte sequence in UTF-8"
end
end

should "build query strings correctly" do
Expand Down

0 comments on commit cc8279f

Please sign in to comment.