Skip to content

Commit b3cecf0

Browse files
Prevent duplicate filters for encrypted attributes
When an Active Record encrypted attribute is declared, a filter for it is automatically added to `config.filter_parameters`. Prior to this commit, the filter would be re-added every time the model was reloaded: ```ruby class Post < ActiveRecord::Base encrypts :title end ``` ```irb irb> Rails.application.config.filter_parameters # => [:passw, ..., :ssn] irb> Post irb> Rails.application.config.filter_parameters # => [:passw, ..., :ssn, "post.title"] irb> reload! irb> Post irb> Rails.application.config.filter_parameters # => [:passw, ..., :ssn, "post.title", "post.title"] ``` This commit ensures filters are only added once so that `config.filter_parameters` does not grow unbounded.
1 parent 2b8b45a commit b3cecf0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

activerecord/lib/active_record/encryption/configurable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def encrypted_attribute_was_declared(klass, name) # :nodoc:
4848
end
4949
end
5050

51-
def install_auto_filtered_parameters_hook(application) # :nodoc:
51+
def install_auto_filtered_parameters_hook(app) # :nodoc:
5252
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, encrypted_attribute_name|
53-
filter_parameter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
54-
unless excluded_from_filter_parameters?(filter_parameter)
55-
application.config.filter_parameters << filter_parameter
53+
filter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
54+
unless excluded_from_filter_parameters?(filter)
55+
app.config.filter_parameters << filter unless app.config.filter_parameters.include?(filter)
5656
klass.filter_attributes += [encrypted_attribute_name]
5757
end
5858
end

railties/test/application/initializers/frameworks_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,27 @@ def self.<(_)
376376
assert_nil ActiveRecord::Scoping::ScopeRegistry.current_scope(Post)
377377
end
378378

379+
test "filters for Active Record encrypted attributes are added to config.filter_parameters only once" do
380+
rails %w(generate model post title:string)
381+
rails %w(db:migrate)
382+
383+
app_file "app/models/post.rb", <<~RUBY
384+
class Post < ActiveRecord::Base
385+
encrypts :title
386+
end
387+
RUBY
388+
389+
require "#{app_path}/config/environment"
390+
391+
assert Post
392+
filter_parameters = Rails.application.config.filter_parameters.dup
393+
394+
reload
395+
396+
assert Post
397+
assert_equal filter_parameters, Rails.application.config.filter_parameters
398+
end
399+
379400
test "ActiveRecord::MessagePack extensions are installed when using ActiveSupport::MessagePack::CacheSerializer" do
380401
rails %w(generate model post title:string)
381402
rails %w(db:migrate)

0 commit comments

Comments
 (0)