* Strip nils from collections on JSON and XML posts. [CVE-2013-0155] * dealing with empty hashes. Thanks Damien Mathieu

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/action_dispatch/http/request.rb
	actionpack/lib/action_dispatch/middleware/params_parser.rb
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/predicate_builder.rb
	activerecord/test/cases/relation/where_test.rb
This commit is contained in:
Aaron Patterson 2013-01-04 12:02:22 -08:00
parent efbb041fb0
commit d99e8c9e16
5 changed files with 44 additions and 5 deletions

@ -276,15 +276,14 @@ def local?
LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip
end
protected
# Remove nils from the params hash
def deep_munge(hash)
hash.each_value do |v|
hash.each do |k, v|
case v
when Array
v.grep(Hash) { |x| deep_munge(x) }
v.compact!
hash[k] = nil if v.empty?
when Hash
deep_munge(v)
end
@ -293,6 +292,8 @@ def deep_munge(hash)
hash
end
protected
def parse_query(qs)
deep_munge(super)
end

@ -47,12 +47,12 @@ def parse_formatted_parameters(env)
when Proc
strategy.call(request.raw_post)
when :xml_simple, :xml_node
data = Hash.from_xml(request.raw_post) || {}
data = request.deep_munge(Hash.from_xml(request.body.read) || {})
data.with_indifferent_access
when :yaml
YAML.load(request.raw_post)
when :json
data = ActiveSupport::JSON.decode(request.raw_post)
data = request.deep_munge ActiveSupport::JSON.decode(request.body)
data = {:_json => data} unless data.is_a?(Hash)
data.with_indifferent_access
else

@ -30,6 +30,21 @@ def teardown
)
end
test "nils are stripped from collections" do
assert_parses(
{"person" => nil},
"{\"person\":[null]}", { 'CONTENT_TYPE' => 'application/json' }
)
assert_parses(
{"person" => ['foo']},
"{\"person\":[\"foo\",null]}", { 'CONTENT_TYPE' => 'application/json' }
)
assert_parses(
{"person" => nil},
"{\"person\":[null, null]}", { 'CONTENT_TYPE' => 'application/json' }
)
end
test "logs error if parsing unsuccessful" do
with_test_routing do
output = StringIO.new

@ -30,6 +30,23 @@ def call(env)
assert_equal "<ok>bar</ok>", resp.body
end
def assert_parses(expected, xml)
with_test_routing do
post "/parse", xml, default_headers
assert_response :ok
assert_equal(expected, TestController.last_request_parameters)
end
end
test "nils are stripped from collections" do
assert_parses(
{"hash" => { "person" => nil} },
"<hash><person type=\"array\"><person nil=\"true\"/></person></hash>")
assert_parses(
{"hash" => { "person" => ['foo']} },
"<hash><person type=\"array\"><person>foo</person><person nil=\"true\"/></person>\n</hash>")
end
test "parses hash params" do
with_test_routing do
xml = "<person><name>David</name></person>"

@ -90,6 +90,12 @@ def test_where_with_blank_conditions
[[], {}, nil, ""].each do |blank|
assert_equal 4, Edge.where(blank).order("sink_id").to_a.size
end
def test_where_with_table_name_and_empty_array
assert_equal 0, Post.where(:id => []).count
end
def test_where_with_empty_hash_and_no_foreign_key
assert_equal 0, Edge.where(:sink => {}).count
end
end
end