提交 1054ebd6 编写于 作者: J Josh Kalderimis

AM mass assignment security attr_accessible and attr_protected now allow for...

AM mass assignment security attr_accessible and attr_protected now allow for scopes using :as => scope eg.
    
    attr_accessible :name
    attr_accessible :name, :admin, :as => :admin
上级 af1b4892
...@@ -24,10 +24,7 @@ module MassAssignmentSecurity ...@@ -24,10 +24,7 @@ module MassAssignmentSecurity
# include ActiveModel::MassAssignmentSecurity # include ActiveModel::MassAssignmentSecurity
# #
# attr_accessible :first_name, :last_name # attr_accessible :first_name, :last_name
# # attr_accessible :first_name, :last_name, :plan_id, :as => :admin
# def self.admin_accessible_attributes
# accessible_attributes + [ :plan_id ]
# end
# #
# def update # def update
# ... # ...
...@@ -37,19 +34,18 @@ module MassAssignmentSecurity ...@@ -37,19 +34,18 @@ module MassAssignmentSecurity
# #
# protected # protected
# #
# def account_params # def scope
# sanitize_for_mass_assignment(params[:account]) # scope = admin ? :admin : :default
# end # sanitize_for_mass_assignment(params[:account], scope)
#
# def mass_assignment_authorizer
# admin ? admin_accessible_attributes : super
# end # end
# #
# end # end
# #
module ClassMethods module ClassMethods
# Attributes named in this macro are protected from mass-assignment # Attributes named in this macro are protected from mass-assignment
# whenever attributes are sanitized before assignment. # whenever attributes are sanitized before assignment. A scope for the
# attributes is optional, if no scope is provided then :default is used.
# A scope can be defined by using the :as option.
# #
# Mass-assignment to these attributes will simply be ignored, to assign # Mass-assignment to these attributes will simply be ignored, to assign
# to them you can use direct writer methods. This is meant to protect # to them you can use direct writer methods. This is meant to protect
...@@ -60,36 +56,58 @@ module ClassMethods ...@@ -60,36 +56,58 @@ module ClassMethods
# include ActiveModel::MassAssignmentSecurity # include ActiveModel::MassAssignmentSecurity
# #
# attr_accessor :name, :credit_rating # attr_accessor :name, :credit_rating
# attr_protected :credit_rating
# #
# def attributes=(values) # attr_protected :credit_rating, :last_login
# sanitize_for_mass_assignment(values).each do |k, v| # attr_protected :last_login, :as => :admin
#
# def assign_attributes(values, options = {})
# sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
# send("#{k}=", v) # send("#{k}=", v)
# end # end
# end # end
# end # end
# #
# When using a :default scope :
#
# customer = Customer.new # customer = Customer.new
# customer.attributes = { "name" => "David", "credit_rating" => "Excellent" } # customer.assign_attributes({ "name" => "David", "credit_rating" => "Excellent", :last_login => 1.day.ago }, :as => :default)
# customer.name # => "David" # customer.name # => "David"
# customer.credit_rating # => nil # customer.credit_rating # => nil
# customer.last_login # => nil
# #
# customer.credit_rating = "Average" # customer.credit_rating = "Average"
# customer.credit_rating # => "Average" # customer.credit_rating # => "Average"
# #
# And using the :admin scope :
#
# customer = Customer.new
# customer.assign_attributes({ "name" => "David", "credit_rating" => "Excellent", :last_login => 1.day.ago }, :as => :admin)
# customer.name # => "David"
# customer.credit_rating # => "Excellent"
# customer.last_login # => nil
#
# To start from an all-closed default and enable attributes as needed, # To start from an all-closed default and enable attributes as needed,
# have a look at +attr_accessible+. # have a look at +attr_accessible+.
# #
# Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_protected+ # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_protected+
# to sanitize attributes won't provide sufficient protection. # to sanitize attributes won't provide sufficient protection.
def attr_protected(*names) def attr_protected(*args)
self._protected_attributes = self.protected_attributes + names options = args.extract_options!
scope = options[:as] || :default
self._protected_attributes = protected_attributes_configs.dup
self._protected_attributes[scope] = self.protected_attributes(scope) + args
self._active_authorizer = self._protected_attributes self._active_authorizer = self._protected_attributes
end end
# Specifies a white list of model attributes that can be set via # Specifies a white list of model attributes that can be set via
# mass-assignment. # mass-assignment.
# #
# Like +attr_protected+, a scope for the attributes is optional,
# if no scope is provided then :default is used. A scope can be defined by
# using the :as option.
#
# This is the opposite of the +attr_protected+ macro: Mass-assignment # This is the opposite of the +attr_protected+ macro: Mass-assignment
# will only set attributes in this list, to assign to the rest of # will only set attributes in this list, to assign to the rest of
# attributes you can use direct writer methods. This is meant to protect # attributes you can use direct writer methods. This is meant to protect
...@@ -102,57 +120,90 @@ def attr_protected(*names) ...@@ -102,57 +120,90 @@ def attr_protected(*names)
# include ActiveModel::MassAssignmentSecurity # include ActiveModel::MassAssignmentSecurity
# #
# attr_accessor :name, :credit_rating # attr_accessor :name, :credit_rating
#
# attr_accessible :name # attr_accessible :name
# attr_accessible :name, :credit_rating, :as => :admin
# #
# def attributes=(values) # def assign_attributes(values, options = {})
# sanitize_for_mass_assignment(values).each do |k, v| # sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
# send("#{k}=", v) # send("#{k}=", v)
# end # end
# end # end
# end # end
# #
# When using a :default scope :
#
# customer = Customer.new # customer = Customer.new
# customer.attributes = { :name => "David", :credit_rating => "Excellent" } # customer.assign_attributes({ "name" => "David", "credit_rating" => "Excellent", :last_login => 1.day.ago }, :as => :default)
# customer.name # => "David" # customer.name # => "David"
# customer.credit_rating # => nil # customer.credit_rating # => nil
# #
# customer.credit_rating = "Average" # customer.credit_rating = "Average"
# customer.credit_rating # => "Average" # customer.credit_rating # => "Average"
# #
# And using the :admin scope :
#
# customer = Customer.new
# customer.assign_attributes({ "name" => "David", "credit_rating" => "Excellent", :last_login => 1.day.ago }, :as => :admin)
# customer.name # => "David"
# customer.credit_rating # => "Excellent"
#
# Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_accessible+ # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_accessible+
# to sanitize attributes won't provide sufficient protection. # to sanitize attributes won't provide sufficient protection.
def attr_accessible(*names) def attr_accessible(*args)
self._accessible_attributes = self.accessible_attributes + names options = args.extract_options!
scope = options[:as] || :default
self._accessible_attributes = accessible_attributes_configs.dup
self._accessible_attributes[scope] = self.accessible_attributes(scope) + args
self._active_authorizer = self._accessible_attributes self._active_authorizer = self._accessible_attributes
end end
def protected_attributes def protected_attributes(scope = :default)
self._protected_attributes ||= BlackList.new(attributes_protected_by_default).tap do |w| protected_attributes_configs[scope]
w.logger = self.logger if self.respond_to?(:logger)
end
end end
def accessible_attributes def accessible_attributes(scope = :default)
self._accessible_attributes ||= WhiteList.new.tap { |w| w.logger = self.logger if self.respond_to?(:logger) } accessible_attributes_configs[scope]
end end
def active_authorizer def active_authorizers
self._active_authorizer ||= protected_attributes self._active_authorizer ||= protected_attributes_configs
end end
alias active_authorizer active_authorizers
def attributes_protected_by_default def attributes_protected_by_default
[] []
end end
private
def protected_attributes_configs
self._protected_attributes ||= begin
default_black_list = BlackList.new(attributes_protected_by_default).tap do |w|
w.logger = self.logger if self.respond_to?(:logger)
end
Hash.new(default_black_list)
end
end
def accessible_attributes_configs
self._accessible_attributes ||= begin
default_white_list = WhiteList.new.tap { |w| w.logger = self.logger if self.respond_to?(:logger) }
Hash.new(default_white_list)
end
end
end end
protected protected
def sanitize_for_mass_assignment(attributes) def sanitize_for_mass_assignment(attributes, scope = :default)
mass_assignment_authorizer.sanitize(attributes) mass_assignment_authorizer(scope).sanitize(attributes)
end end
def mass_assignment_authorizer def mass_assignment_authorizer(scope = :default)
self.class.active_authorizer self.class.active_authorizer[scope]
end end
end end
end end
...@@ -10,10 +10,27 @@ def test_attribute_protection ...@@ -10,10 +10,27 @@ def test_attribute_protection
assert_equal expected, sanitized assert_equal expected, sanitized
end end
def test_only_moderator_scope_attribute_accessible
user = SpecialUser.new
expected = { "name" => "John Smith", "email" => "john@smith.com" }
sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true), :moderator)
assert_equal expected, sanitized
sanitized = user.sanitize_for_mass_assignment({ "name" => "John Smith", "email" => "john@smith.com", "admin" => true })
assert_equal({}, sanitized)
end
def test_attributes_accessible def test_attributes_accessible
user = Person.new user = Person.new
expected = { "name" => "John Smith", "email" => "john@smith.com" } expected = { "name" => "John Smith", "email" => "john@smith.com" }
sanitized = user.sanitize_for_mass_assignment(expected.merge("super_powers" => true)) sanitized = user.sanitize_for_mass_assignment(expected.merge("admin" => true))
assert_equal expected, sanitized
end
def test_admin_scoped_attributes_accessible
user = Person.new
expected = { "name" => "John Smith", "email" => "john@smith.com", "admin" => true }
sanitized = user.sanitize_for_mass_assignment(expected.merge("super_powers" => true), :admin)
assert_equal expected, sanitized assert_equal expected, sanitized
end end
...@@ -26,20 +43,30 @@ def test_attributes_protected_by_default ...@@ -26,20 +43,30 @@ def test_attributes_protected_by_default
def test_mass_assignment_protection_inheritance def test_mass_assignment_protection_inheritance
assert_blank LoosePerson.accessible_attributes assert_blank LoosePerson.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator']), LoosePerson.protected_attributes assert_equal Set.new(['credit_rating', 'administrator']), LoosePerson.protected_attributes
assert_blank LoosePerson.accessible_attributes
assert_equal Set.new(['credit_rating']), LoosePerson.protected_attributes(:admin)
assert_blank LooseDescendant.accessible_attributes assert_blank LooseDescendant.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number']), LooseDescendant.protected_attributes assert_equal Set.new(['credit_rating', 'administrator', 'phone_number']), LooseDescendant.protected_attributes
assert_blank LooseDescendantSecond.accessible_attributes assert_blank LooseDescendantSecond.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number', 'name']), LooseDescendantSecond.protected_attributes, assert_equal Set.new(['credit_rating', 'administrator', 'phone_number', 'name']), LooseDescendantSecond.protected_attributes,
'Running attr_protected twice in one class should merge the protections' 'Running attr_protected twice in one class should merge the protections'
assert_blank TightPerson.protected_attributes - TightPerson.attributes_protected_by_default assert_blank TightPerson.protected_attributes - TightPerson.attributes_protected_by_default
assert_equal Set.new([ 'name', 'address' ]), TightPerson.accessible_attributes assert_equal Set.new(['name', 'address']), TightPerson.accessible_attributes
assert_blank TightPerson.protected_attributes(:admin) - TightPerson.attributes_protected_by_default
assert_equal Set.new(['name', 'address', 'admin']), TightPerson.accessible_attributes(:admin)
assert_blank TightDescendant.protected_attributes - TightDescendant.attributes_protected_by_default assert_blank TightDescendant.protected_attributes - TightDescendant.attributes_protected_by_default
assert_equal Set.new([ 'name', 'address', 'phone_number' ]), TightDescendant.accessible_attributes assert_equal Set.new(['name', 'address', 'phone_number']), TightDescendant.accessible_attributes
assert_blank TightDescendant.protected_attributes(:admin) - TightDescendant.attributes_protected_by_default
assert_equal Set.new(['name', 'address', 'admin', 'super_powers']), TightDescendant.accessible_attributes(:admin)
end end
def test_mass_assignment_multiparameter_protector def test_mass_assignment_multiparameter_protector
......
...@@ -45,13 +45,14 @@ class SecurePasswordTest < ActiveModel::TestCase ...@@ -45,13 +45,14 @@ class SecurePasswordTest < ActiveModel::TestCase
end end
test "visitor#password_digest should be protected against mass assignment" do test "visitor#password_digest should be protected against mass assignment" do
assert Visitor.active_authorizer.kind_of?(ActiveModel::MassAssignmentSecurity::BlackList) assert Visitor.active_authorizers[:default].kind_of?(ActiveModel::MassAssignmentSecurity::BlackList)
assert Visitor.active_authorizer.include?(:password_digest) assert Visitor.active_authorizers[:default].include?(:password_digest)
end end
test "Administrator's mass_assignment_authorizer should be WhiteList" do test "Administrator's mass_assignment_authorizer should be WhiteList" do
assert Administrator.active_authorizer.kind_of?(ActiveModel::MassAssignmentSecurity::WhiteList) active_authorizer = Administrator.active_authorizers[:default]
assert !Administrator.active_authorizer.include?(:password_digest) assert active_authorizer.kind_of?(ActiveModel::MassAssignmentSecurity::WhiteList)
assert Administrator.active_authorizer.include?(:name) assert !active_authorizer.include?(:password_digest)
assert active_authorizer.include?(:name)
end end
end end
...@@ -5,9 +5,17 @@ class User ...@@ -5,9 +5,17 @@ class User
public :sanitize_for_mass_assignment public :sanitize_for_mass_assignment
end end
class SpecialUser
include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :email, :as => :moderator
public :sanitize_for_mass_assignment
end
class Person class Person
include ActiveModel::MassAssignmentSecurity include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :email attr_accessible :name, :email
attr_accessible :name, :email, :admin, :as => :admin
public :sanitize_for_mass_assignment public :sanitize_for_mass_assignment
end end
...@@ -32,6 +40,7 @@ class Task ...@@ -32,6 +40,7 @@ class Task
class LoosePerson class LoosePerson
include ActiveModel::MassAssignmentSecurity include ActiveModel::MassAssignmentSecurity
attr_protected :credit_rating, :administrator attr_protected :credit_rating, :administrator
attr_protected :credit_rating, :as => :admin
end end
class LooseDescendant < LoosePerson class LooseDescendant < LoosePerson
...@@ -46,6 +55,7 @@ class LooseDescendantSecond< LoosePerson ...@@ -46,6 +55,7 @@ class LooseDescendantSecond< LoosePerson
class TightPerson class TightPerson
include ActiveModel::MassAssignmentSecurity include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :address attr_accessible :name, :address
attr_accessible :name, :address, :admin, :as => :admin
def self.attributes_protected_by_default def self.attributes_protected_by_default
["mobile_number"] ["mobile_number"]
...@@ -54,4 +64,5 @@ def self.attributes_protected_by_default ...@@ -54,4 +64,5 @@ def self.attributes_protected_by_default
class TightDescendant < TightPerson class TightDescendant < TightPerson
attr_accessible :phone_number attr_accessible :phone_number
attr_accessible :super_powers, :as => :admin
end end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册