Skip to content

Commit eb8f0dd

Browse files
committed
Revert "AM::MassAssingmentSecurity: improve performance"
It introduces backwards incompatible changes in the API. This reverts commit 7d1379f.
1 parent f961ec4 commit eb8f0dd

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

activemodel/lib/active_model/mass_assignment_security/sanitizer.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ module MassAssignmentSecurity
33
class Sanitizer
44
# Returns all attributes not denied by the authorizer.
55
def sanitize(attributes, authorizer)
6-
attributes.reject do |attr, value|
7-
if authorizer.deny?(attr)
8-
process_removed_attribute(attr)
9-
true
10-
end
11-
end
6+
sanitized_attributes = attributes.reject { |key, value| authorizer.deny?(key) }
7+
debug_protected_attribute_removal(attributes, sanitized_attributes)
8+
sanitized_attributes
129
end
1310

1411
protected
1512

16-
def process_removed_attribute(attr)
17-
raise NotImplementedError, "#process_removed_attribute(attr) suppose to be overwritten"
13+
def debug_protected_attribute_removal(attributes, sanitized_attributes)
14+
removed_keys = attributes.keys - sanitized_attributes.keys
15+
process_removed_attributes(removed_keys) if removed_keys.any?
16+
end
17+
18+
def process_removed_attributes(attrs)
19+
raise NotImplementedError, "#process_removed_attributes(attrs) suppose to be overwritten"
1820
end
1921
end
2022

@@ -32,8 +34,8 @@ def logger?
3234
@target.respond_to?(:logger) && @target.logger
3335
end
3436

35-
def process_removed_attribute(attr)
36-
logger.warn "Can't mass-assign protected attribute: #{attr}" if logger?
37+
def process_removed_attributes(attrs)
38+
logger.warn "Can't mass-assign protected attributes: #{attrs.join(', ')}" if logger?
3739
end
3840
end
3941

@@ -42,19 +44,19 @@ def initialize(target = nil)
4244
super()
4345
end
4446

45-
def process_removed_attribute(attr)
46-
return if insensitive_attributes.include?(attr)
47-
raise ActiveModel::MassAssignmentSecurity::Error.new(attr)
47+
def process_removed_attributes(attrs)
48+
return if (attrs - insensitive_attributes).empty?
49+
raise ActiveModel::MassAssignmentSecurity::Error.new(attrs)
4850
end
4951

5052
def insensitive_attributes
51-
@insensitive_attributes ||= ['id']
53+
['id']
5254
end
5355
end
5456

5557
class Error < StandardError
56-
def initialize(attr)
57-
super("Can't mass-assign protected attribute: #{attr}")
58+
def initialize(attrs)
59+
super("Can't mass-assign protected attributes: #{attrs.join(', ')}")
5860
end
5961
end
6062
end

activemodel/test/cases/mass_assignment_security_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer
66

7-
def process_removed_attribute(attr)
7+
def process_removed_attributes(attrs)
88
raise StandardError
99
end
1010

0 commit comments

Comments
 (0)