Permalink
Browse files

Merge pull request #615 from vspy/master

correct handling for incomplete hash/array parameters
  • Loading branch information...
2 parents b7e0bde + 27642c2 commit 23dfded165667e9e8eb34438c7661d34f631478a @raggi raggi committed Dec 28, 2013
Showing with 21 additions and 1 deletion.
  1. +2 −1 lib/rack/request.rb
  2. +2 −0 lib/rack/utils.rb
  3. +7 −0 test/spec_request.rb
  4. +10 −0 test/spec_utils.rb
View
@@ -192,8 +192,9 @@ def GET
if @env["rack.request.query_string"] == query_string
@env["rack.request.query_hash"]
else
+ p = parse_query(query_string)
@env["rack.request.query_string"] = query_string
- @env["rack.request.query_hash"] = parse_query(query_string)
+ @env["rack.request.query_hash"] = p
end
end
View
@@ -109,6 +109,8 @@ def normalize_params(params, name, v = nil)
if after == ""
params[k] = v
+ elsif after == "["
+ params[name] = v
elsif after == "[]"
params[k] ||= []
raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
View
@@ -1115,6 +1115,13 @@ def params
req2.params.should.equal "foo" => "bar"
end
+ should "raise TypeError every time if request parameters are broken" do
+ broken_query = Rack::MockRequest.env_for("/?foo[]=0&foo[bar]=1")
+ req = Rack::Request.new(broken_query)
+ lambda{req.GET}.should.raise(TypeError)
+ lambda{req.params}.should.raise(TypeError)
+ end
+
(0x20...0x7E).collect { |a|
b = a.chr
c = CGI.escape(b)
View
@@ -157,6 +157,16 @@ def kcodeu
should.equal "foo" => [""]
Rack::Utils.parse_nested_query("foo[]=bar").
should.equal "foo" => ["bar"]
+ Rack::Utils.parse_nested_query("foo[]=bar&foo").
+ should.equal "foo" => nil
+ Rack::Utils.parse_nested_query("foo[]=bar&foo[").
+ should.equal "foo" => ["bar"], "foo[" => nil
+ Rack::Utils.parse_nested_query("foo[]=bar&foo[=baz").
+ should.equal "foo" => ["bar"], "foo[" => "baz"
+ Rack::Utils.parse_nested_query("foo[]=bar&foo[]").
+ should.equal "foo" => ["bar", nil]
+ Rack::Utils.parse_nested_query("foo[]=bar&foo[]=").
+ should.equal "foo" => ["bar", ""]
Rack::Utils.parse_nested_query("foo[]=1&foo[]=2").
should.equal "foo" => ["1", "2"]

0 comments on commit 23dfded

Please sign in to comment.