Skip to content

Commit 9b7ae2b

Browse files
committed
Add filtering of encrypted attributes in #inspect
Previously, encrypted attributes could be added to an application's filter_parameters which would filter the attribute values from logs. This commit makes the add_to_filter_parameters additionally add encrypted attributes to records' filter_attributes, which allows them to be filtered when models are inspected (such as in the console).
1 parent e5f3d69 commit 9b7ae2b

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

activerecord/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Add automatic filtering of encrypted attributes on inspect
2+
3+
This feature is enabled by default but can be disabled with
4+
5+
```ruby
6+
config.active_record.encryption.add_to_filter_parameters = false
7+
```
8+
9+
*Hartley McGuire*
10+
111
* Clear locking column on #dup
212

313
This change fixes not to duplicate locking_column like id and timestamps.

activerecord/lib/active_record/encryption/configurable.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def encrypted_attribute_was_declared(klass, name) # :nodoc:
5151
def install_auto_filtered_parameters_hook(application) # :nodoc:
5252
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, encrypted_attribute_name|
5353
filter_parameter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
54-
application.config.filter_parameters << filter_parameter unless excluded_from_filter_parameters?(filter_parameter)
54+
unless excluded_from_filter_parameters?(filter_parameter)
55+
application.config.filter_parameters << filter_parameter
56+
klass.filter_attributes += [encrypted_attribute_name]
57+
end
5558
end
5659
end
5760

activerecord/lib/active_record/railtie.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,8 @@ class Railtie < Rails::Railtie # :nodoc:
377377
end
378378

379379
# Filtered params
380-
ActiveSupport.on_load(:action_controller, run_once: true) do
381-
if ActiveRecord::Encryption.config.add_to_filter_parameters
382-
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(app)
383-
end
380+
if ActiveRecord::Encryption.config.add_to_filter_parameters
381+
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(app)
384382
end
385383
end
386384

railties/test/application/configuration_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,6 +3399,44 @@ class MyLogger < ::Logger
33993399
assert_equal [ :password, :credit_card_number ], ActiveRecord::Base.filter_attributes
34003400
end
34013401

3402+
test "encrypted attributes are added to record's filter_attributes by default" do
3403+
app_file "app/models/post.rb", <<-RUBY
3404+
class Post < ActiveRecord::Base
3405+
encrypts :content
3406+
end
3407+
RUBY
3408+
3409+
add_to_config <<-RUBY
3410+
config.enable_reloading = false
3411+
config.eager_load = true
3412+
RUBY
3413+
3414+
app "production"
3415+
3416+
assert_includes Post.filter_attributes, :content
3417+
assert_not_includes ActiveRecord::Base.filter_attributes, :content
3418+
end
3419+
3420+
test "encrypted attributes are not added to record filter_attributes if disabled" do
3421+
app_file "app/models/post.rb", <<-RUBY
3422+
class Post < ActiveRecord::Base
3423+
encrypts :content
3424+
end
3425+
RUBY
3426+
3427+
add_to_config <<-RUBY
3428+
config.enable_reloading = false
3429+
config.eager_load = true
3430+
3431+
config.active_record.encryption.add_to_filter_parameters = false
3432+
RUBY
3433+
3434+
app "production"
3435+
3436+
assert_not_includes Post.filter_attributes, :content
3437+
assert_not_includes ActiveRecord::Base.filter_attributes, :content
3438+
end
3439+
34023440
test "ActiveStorage.routes_prefix can be configured via config.active_storage.routes_prefix" do
34033441
app_file "config/environments/development.rb", <<-RUBY
34043442
Rails.application.configure do

0 commit comments

Comments
 (0)