mirror of https://github.com/rails/rails
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:
parent
451f63b42e
commit
b93a918337
|
@ -44,8 +44,13 @@ module ActiveModel
|
||||||
|
|
||||||
class StrictSanitizer < Sanitizer
|
class StrictSanitizer < Sanitizer
|
||||||
def process_removed_attributes(attrs)
|
def process_removed_attributes(attrs)
|
||||||
|
return if (attrs - insensitive_attributes).empty?
|
||||||
raise ActiveModel::MassAssignmentSecurity::Error, "Can't mass-assign protected attributes: #{attrs.join(', ')}"
|
raise ActiveModel::MassAssignmentSecurity::Error, "Can't mass-assign protected attributes: #{attrs.join(', ')}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def insensitive_attributes
|
||||||
|
['id']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Error < StandardError
|
class Error < StandardError
|
||||||
|
|
|
@ -7,7 +7,7 @@ class SanitizerTest < ActiveModel::TestCase
|
||||||
|
|
||||||
class Authorizer < ActiveModel::MassAssignmentSecurity::PermissionSet
|
class Authorizer < ActiveModel::MassAssignmentSecurity::PermissionSet
|
||||||
def deny?(key)
|
def deny?(key)
|
||||||
key.in?(['admin'])
|
['admin', 'id'].include?(key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,4 +40,12 @@ class SanitizerTest < ActiveModel::TestCase
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -34,6 +34,11 @@
|
||||||
# like if you have constraints or database-specific column types
|
# like if you have constraints or database-specific column types
|
||||||
# config.active_record.schema_format = :sql
|
# 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
|
# Print deprecation notices to the stderr
|
||||||
config.active_support.deprecation = :stderr
|
config.active_support.deprecation = :stderr
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue