Skip to content

Commit 67d6183

Browse files
committed
Include the model name when filtering encrypted attributes.
For example, when encrypting `Person#name` it will add `person.name` as a filter parameter, instead of just `name`. This prevents unintended filtering of parameters with a matching name in other models. Closes rails#44330
1 parent c21c413 commit 67d6183

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Use the model name as a prefix when filtering encrypted attributes from logs.
2+
3+
For example, when encrypting `Person#name` it will add `person.name` as a filter
4+
parameter, instead of just `name`. This prevents unintended filtering of parameters
5+
with a matching name in other models.
6+
7+
*Jorge Manrubia*
8+
19
* Fix `config.active_record.destroy_association_async_job` configuration
210

311
`config.active_record.destroy_association_async_job` should allow

activerecord/lib/active_record/encryption/configurable.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ def encrypted_attribute_was_declared(klass, name) # :nodoc:
5252

5353
def install_auto_filtered_parameters_hook(application) # :nodoc:
5454
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, encrypted_attribute_name|
55-
application.config.filter_parameters << encrypted_attribute_name unless ActiveRecord::Encryption.config.excluded_from_filter_parameters.include?(encrypted_attribute_name)
55+
filter_parameter = [("#{klass.model_name.element}" if klass.name), encrypted_attribute_name.to_s].compact.join(".")
56+
application.config.filter_parameters << filter_parameter unless excluded_from_filter_parameters?(filter_parameter)
5657
end
5758
end
59+
60+
private
61+
def excluded_from_filter_parameters?(filter_parameter)
62+
ActiveRecord::Encryption.config.excluded_from_filter_parameters.find { |excluded_filter| excluded_filter.to_s == filter_parameter }
63+
end
5864
end
5965
end
6066
end

activerecord/test/cases/encryption/configurable_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ class ActiveRecord::Encryption::ConfigurableTest < ActiveRecord::EncryptionTestC
4141
assert_equal :isbn, @attribute_name
4242
end
4343

44-
test "install autofiltered params" do
44+
test "installing autofiltered parameters will add the encrypted attribute as a filter parameter using the dot notation" do
45+
application = OpenStruct.new(config: OpenStruct.new(filter_parameters: []))
46+
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(application)
47+
48+
NamedPirate = Class.new(Pirate) do
49+
self.table_name = "pirates"
50+
end
51+
NamedPirate.encrypts :catchphrase
52+
53+
assert_includes application.config.filter_parameters, "named_pirate.catchphrase"
54+
end
55+
56+
test "installing autofiltered parameters will work with unnamed classes" do
4557
application = OpenStruct.new(config: OpenStruct.new(filter_parameters: []))
4658
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(application)
4759

@@ -50,7 +62,7 @@ class ActiveRecord::Encryption::ConfigurableTest < ActiveRecord::EncryptionTestC
5062
encrypts :catchphrase
5163
end
5264

53-
assert_includes application.config.filter_parameters, :catchphrase
65+
assert_includes application.config.filter_parameters, "catchphrase"
5466
end
5567

5668
test "exclude the installation of autofiltered params" do

guides/source/active_record_encryption.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ end
241241

242242
By default, encrypted columns are configured to be [automatically filtered in Rails logs](https://guides.rubyonrails.org/action_controller_overview.html#parameters-filtering). You can disable this behavior by adding the following to your `application.rb`:
243243

244+
When generating the filter parameter, it will use the model name as a prefix. E.g: For `Person#name` the filter parameter will be `person.name`.
245+
244246
```ruby
245247
config.active_record.encryption.add_to_filter_parameters = false
246248
```

0 commit comments

Comments
 (0)