Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backported rails 2.3 fix for CVE-2012-2695 #6722

Merged
merged 2 commits into from
Jan 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions activerecord/lib/active_record/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2337,26 +2337,28 @@ def expand_hash_conditions_for_aggregates(attrs)
# And for value objects on a composed_of relationship: # And for value objects on a composed_of relationship:
# { :address => Address.new("123 abc st.", "chicago") } # { :address => Address.new("123 abc st.", "chicago") }
# # => "address_street='123 abc st.' and address_city='chicago'" # # => "address_street='123 abc st.' and address_city='chicago'"
def sanitize_sql_hash_for_conditions(attrs, default_table_name = quoted_table_name) def sanitize_sql_hash_for_conditions(attrs, default_table_name = quoted_table_name, top_level = true)
attrs = expand_hash_conditions_for_aggregates(attrs) attrs = expand_hash_conditions_for_aggregates(attrs)


conditions = attrs.map do |attr, value| conditions = attrs.map do |attr, value|
table_name = default_table_name table_name = default_table_name


unless value.is_a?(Hash) if not value.is_a?(Hash)
attr = attr.to_s attr = attr.to_s


# Extract table name from qualified attribute names. # Extract table name from qualified attribute names.
if attr.include?('.') if attr.include?('.') and top_level
attr_table_name, attr = attr.split('.', 2) attr_table_name, attr = attr.split('.', 2)
attr_table_name = connection.quote_table_name(attr_table_name) attr_table_name = connection.quote_table_name(attr_table_name)
else else
attr_table_name = table_name attr_table_name = table_name
end end


attribute_condition("#{attr_table_name}.#{connection.quote_column_name(attr)}", value) attribute_condition("#{attr_table_name}.#{connection.quote_column_name(attr)}", value)
elsif top_level
sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s), false)
else else
sanitize_sql_hash_for_conditions(value, connection.quote_table_name(attr.to_s)) raise ActiveRecord::StatementInvalid
end end
end.join(' AND ') end.join(' AND ')


Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/finder_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -375,6 +375,22 @@ def test_hash_condition_find_malformed
} }
end end


def test_hash_condition_find_with_improper_nested_hashes
assert_raise(ActiveRecord::StatementInvalid) {
Company.find(:first, :conditions => { :name => { :companies => { :id => 1 }}})
}
end

def test_hash_condition_find_with_dot_in_nested_column_name
assert_raise(ActiveRecord::StatementInvalid) {
Company.find(:first, :conditions => { :name => { "companies.id" => 1 }})
}
end

def test_hash_condition_find_with_dot_in_column_name_okay
assert Company.find(:first, :conditions => { "companies.id" => 1 })
end

def test_hash_condition_find_with_escaped_characters def test_hash_condition_find_with_escaped_characters
Company.create("name" => "Ain't noth'n like' \#stuff") Company.create("name" => "Ain't noth'n like' \#stuff")
assert Company.find(:first, :conditions => { :name => "Ain't noth'n like' \#stuff" }) assert Company.find(:first, :conditions => { :name => "Ain't noth'n like' \#stuff" })
Expand Down