MassAssignmentProtection: consider 'id' insensetive in StrictSanitizer

In order to use StrictSanitizer in test mode
Consider :id as not sensetive attribute that can be filtered from
mass assignement without exception.
This commit is contained in:
Bogdan Gusiev 2011-07-28 11:56:08 +03:00
parent 451f63b42e
commit b93a918337
3 changed files with 19 additions and 1 deletions

View File

@ -44,8 +44,13 @@ module ActiveModel
class StrictSanitizer < Sanitizer
def process_removed_attributes(attrs)
return if (attrs - insensitive_attributes).empty?
raise ActiveModel::MassAssignmentSecurity::Error, "Can't mass-assign protected attributes: #{attrs.join(', ')}"
end
def insensitive_attributes
['id']
end
end
class Error < StandardError

View File

@ -7,7 +7,7 @@ class SanitizerTest < ActiveModel::TestCase
class Authorizer < ActiveModel::MassAssignmentSecurity::PermissionSet
def deny?(key)
key.in?(['admin'])
['admin', 'id'].include?(key)
end
end
@ -40,4 +40,12 @@ class SanitizerTest < ActiveModel::TestCase
end
end
test "mass assignment insensitive attributes" do
original_attributes = {'id' => 1, 'first_name' => 'allowed'}
assert_nothing_raised do
@strict_sanitizer.sanitize(original_attributes, @authorizer)
end
end
end

View File

@ -34,6 +34,11 @@
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
<%- unless options.skip_active_record? -%>
# Raise exception on mass assignment protection for ActiveRecord models
config.active_record.mass_assignment_sanitizer = :strict
<%- end -%>
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
end