Permalink
Browse files

* Strip nils from collections on JSON and XML posts. [CVE-2013-0155] …

…* dealing with empty hashes. Thanks Damien Mathieu

Conflicts:
	actionpack/CHANGELOG.md
	activerecord/CHANGELOG.md

Conflicts:
	actionpack/CHANGELOG.md
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/predicate_builder.rb
  • Loading branch information...
1 parent fb06fe4 commit 97b3b68d3a8dc3979e74bec3921aeda69735eb6a @tenderlove tenderlove committed Jan 4, 2013
View
@@ -1,3 +1,7 @@
+## Rails 3.0.19
+
+* Strip nils from collections on JSON and XML posts. [CVE-2013-0155]
+
## Rails 3.0.18
## Rails 3.0.17 (Aug 9, 2012)
@@ -258,18 +258,14 @@ def local?
LOCALHOST.any? { |local_ip| local_ip === remote_addr && local_ip === remote_ip }
end
- protected
-
# Remove nils from the params hash
def deep_munge(hash)
- keys = hash.keys.find_all { |k| hash[k] == [nil] }
- keys.each { |k| hash[k] = nil }
-
- 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?
@thomasmckay

thomasmckay Jan 29, 2013

Is this right? That means that the JSON cannot contain empty arrays.

"foo":[]
goes to
"foo":null

@stevenharman

stevenharman Jan 30, 2013

Contributor

This is a breaking change, no? It is breaking our apps, at least.

when Hash
deep_munge(v)
end
@@ -278,6 +274,8 @@ def deep_munge(hash)
hash
end
+ protected
+
def parse_query(qs)
deep_munge(super)
end
@@ -38,13 +38,13 @@ def parse_formatted_parameters(env)
when Proc
strategy.call(request.raw_post)
when :xml_simple, :xml_node
- data = Hash.from_xml(request.body.read) || {}
+ data = request.deep_munge(Hash.from_xml(request.body.read) || {})
request.body.rewind if request.body.respond_to?(:rewind)
data.with_indifferent_access
when :yaml
YAML.load(request.raw_post)
when :json
- data = ActiveSupport::JSON.decode(request.body)
+ data = request.deep_munge ActiveSupport::JSON.decode(request.body)
@vilcsak

vilcsak Jan 9, 2013

Is it okay to assume that JSON.decode returns a hash here? We've got a bug in production where it's returning a string, and request.deep_munge ends up throwing an exception.

@carlosantoniodasilva

carlosantoniodasilva Jan 11, 2013

Owner

It has been fixed in 92fada9 and backported to all 3-x branches.

request.body.rewind if request.body.respond_to?(:rewind)
data = {:_json => data} unless data.is_a?(Hash)
data.with_indifferent_access
@@ -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
begin
@@ -29,6 +29,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>"
View
@@ -1,3 +1,7 @@
+## Rails 3.0.19
+
+* Fix querying with an empty hash *Damien Mathieu* [CVE-2013-0155]
+
## Rails 3.0.18
* CVE-2012-5664 ensure that options are never taken from the first parameter
@@ -11,7 +11,12 @@ def build_from_hash(attributes, default_table, allow_table_name = true)
if allow_table_name && value.is_a?(Hash)
table = Arel::Table.new(column, :engine => @engine)
- build_from_hash(value, table, false)
+
+ if value.empty?
+ '1 = 2'
+ else
+ build_from_hash(value, table, false)
+ end
else
column = column.to_s
@@ -1,9 +1,11 @@
require "cases/helper"
require 'models/post'
+require 'models/comment'
+require 'models/edge'
module ActiveRecord
class WhereTest < ActiveRecord::TestCase
- fixtures :posts
+ fixtures :posts, :edges
def test_where_error
assert_raises(ActiveRecord::StatementInvalid) do
@@ -21,5 +23,17 @@ def test_where_with_table_name
post = Post.first
assert_equal post, Post.where(:posts => { 'id' => post.id }).first
end
+
+ def test_where_with_table_name_and_empty_hash
+ assert_equal 0, Post.where(:posts => {}).count
+ 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

0 comments on commit 97b3b68

Please sign in to comment.