Skip to content

Commit

Permalink
* Strip nils from collections on JSON and XML posts. [CVE-2013-0155] …
Browse files Browse the repository at this point in the history
…* 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
  • Loading branch information
tenderlove committed Jan 8, 2013
1 parent c31cc96 commit 8e577fe
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
7 changes: 4 additions & 3 deletions actionpack/lib/action_dispatch/http/request.rb
Expand Up @@ -276,15 +276,14 @@ def local?
LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip
end end


protected

# Remove nils from the params hash # Remove nils from the params hash
def deep_munge(hash) def deep_munge(hash)
hash.each_value do |v| hash.each do |k, v|
case v case v
when Array when Array
v.grep(Hash) { |x| deep_munge(x) } v.grep(Hash) { |x| deep_munge(x) }
v.compact! v.compact!
hash[k] = nil if v.empty?
when Hash when Hash
deep_munge(v) deep_munge(v)
end end
Expand All @@ -293,6 +292,8 @@ def deep_munge(hash)
hash hash
end end


protected

def parse_query(qs) def parse_query(qs)
deep_munge(super) deep_munge(super)
end end
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_dispatch/middleware/params_parser.rb
Expand Up @@ -47,12 +47,12 @@ def parse_formatted_parameters(env)
when Proc when Proc
strategy.call(request.raw_post) strategy.call(request.raw_post)
when :xml_simple, :xml_node 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 data.with_indifferent_access
when :yaml when :yaml
YAML.load(request.raw_post) YAML.load(request.raw_post)
when :json 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 = {:_json => data} unless data.is_a?(Hash)
data.with_indifferent_access data.with_indifferent_access
else else
Expand Down
15 changes: 15 additions & 0 deletions actionpack/test/dispatch/request/json_params_parsing_test.rb
Expand Up @@ -30,6 +30,21 @@ def teardown
) )
end 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 test "logs error if parsing unsuccessful" do
with_test_routing do with_test_routing do
output = StringIO.new output = StringIO.new
Expand Down
17 changes: 17 additions & 0 deletions actionpack/test/dispatch/request/xml_params_parsing_test.rb
Expand Up @@ -30,6 +30,23 @@ def call(env)
assert_equal "<ok>bar</ok>", resp.body assert_equal "<ok>bar</ok>", resp.body
end 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 test "parses hash params" do
with_test_routing do with_test_routing do
xml = "<person><name>David</name></person>" xml = "<person><name>David</name></person>"
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/relation/where_test.rb
Expand Up @@ -90,6 +90,12 @@ def test_where_with_blank_conditions
[[], {}, nil, ""].each do |blank| [[], {}, nil, ""].each do |blank|
assert_equal 4, Edge.where(blank).order("sink_id").to_a.size assert_equal 4, Edge.where(blank).order("sink_id").to_a.size
end 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 end
end end

0 comments on commit 8e577fe

Please sign in to comment.