Skip to content

Commit

Permalink
Merge pull request #46453 from skipkayhil/feat-filter-encrypted-attri…
Browse files Browse the repository at this point in the history
…butes-inspect

Add filtering of encrypted attributes in #inspect
  • Loading branch information
jonathanhefner committed Nov 12, 2022
2 parents a78f341 + 9b7ae2b commit 90cba59
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
10 changes: 10 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
* Add automatic filtering of encrypted attributes on inspect

This feature is enabled by default but can be disabled with

```ruby
config.active_record.encryption.add_to_filter_parameters = false
```

*Hartley McGuire*

* Clear locking column on #dup

This change fixes not to duplicate locking_column like id and timestamps.
Expand Down
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/encryption/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def encrypted_attribute_was_declared(klass, name) # :nodoc:
def install_auto_filtered_parameters_hook(application) # :nodoc:
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, encrypted_attribute_name|
filter_parameter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
application.config.filter_parameters << filter_parameter unless excluded_from_filter_parameters?(filter_parameter)
unless excluded_from_filter_parameters?(filter_parameter)
application.config.filter_parameters << filter_parameter
klass.filter_attributes += [encrypted_attribute_name]
end
end
end

Expand Down
6 changes: 2 additions & 4 deletions activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,8 @@ class Railtie < Rails::Railtie # :nodoc:
end

# Filtered params
ActiveSupport.on_load(:action_controller, run_once: true) do
if ActiveRecord::Encryption.config.add_to_filter_parameters
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(app)
end
if ActiveRecord::Encryption.config.add_to_filter_parameters
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(app)
end
end

Expand Down
38 changes: 38 additions & 0 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,44 @@ class MyLogger < ::Logger
assert_equal [ :password, :credit_card_number ], ActiveRecord::Base.filter_attributes
end

test "encrypted attributes are added to record's filter_attributes by default" do
app_file "app/models/post.rb", <<-RUBY
class Post < ActiveRecord::Base
encrypts :content
end
RUBY

add_to_config <<-RUBY
config.enable_reloading = false
config.eager_load = true
RUBY

app "production"

assert_includes Post.filter_attributes, :content
assert_not_includes ActiveRecord::Base.filter_attributes, :content
end

test "encrypted attributes are not added to record filter_attributes if disabled" do
app_file "app/models/post.rb", <<-RUBY
class Post < ActiveRecord::Base
encrypts :content
end
RUBY

add_to_config <<-RUBY
config.enable_reloading = false
config.eager_load = true
config.active_record.encryption.add_to_filter_parameters = false
RUBY

app "production"

assert_not_includes Post.filter_attributes, :content
assert_not_includes ActiveRecord::Base.filter_attributes, :content
end

test "ActiveStorage.routes_prefix can be configured via config.active_storage.routes_prefix" do
app_file "config/environments/development.rb", <<-RUBY
Rails.application.configure do
Expand Down

0 comments on commit 90cba59

Please sign in to comment.