Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a encryption option to support previous data encrypted non-determ…
…inistically with a SHA1 hash digest (#48530) * Make sure active record encryption configuration happens after initializers have run Co-authored-by: Cadu Ribeiro <mail@cadu.dev> * Add a new option to support previous data encrypted non-deterministically with a hash digest of SHA1 There is currently a problem with Active Record encryption for users updating from 7.0 to 7.1 Before #44873, data encrypted with non-deterministic encryption was always using SHA-1. The reason is that `ActiveSupport::KeyGenerator.hash_digest_class` is set in an after_initialize block in the railtie config, but encryption config was running before that, so it was effectively using the previous default SHA1. That means that existing users are using SHA256 for non deterministic encryption, and SHA1 for deterministic encryption. This adds a new option `use_sha1_digest_for_non_deterministic_data` that users can enable to support for SHA1 and SHA256 when decrypting existing data. * Set a default value of true for `support_sha1_for_non_deterministic_encryption` and proper initializer values. We want to enable the flag existing versions (< 7.1), and we want it to be false moving by default moving forward. * Make sure the system to auto-filter params supports different initialization orders This reworks the system to auto-filter params so that it works when encrypted attributes are declared before the encryption configuration logic runs. Co-authored-by: Cadu Ribeiro <mail@cadu.dev> --------- Co-authored-by: Cadu Ribeiro <mail@cadu.dev>
- Loading branch information
1 parent
672376c
commit c8d26bb
Showing
18 changed files
with
233 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
activerecord/lib/active_record/encryption/auto_filtered_parameters.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module Encryption | ||
class AutoFilteredParameters | ||
def initialize(app) | ||
@app = app | ||
@attributes_by_class = Concurrent::Map.new | ||
@collecting = true | ||
|
||
install_collecting_hook | ||
end | ||
|
||
def enable | ||
apply_collected_attributes | ||
@collecting = false | ||
end | ||
|
||
private | ||
attr_reader :app | ||
|
||
def install_collecting_hook | ||
ActiveRecord::Encryption.on_encrypted_attribute_declared do |klass, attribute| | ||
attribute_was_declared(klass, attribute) | ||
end | ||
end | ||
|
||
def attribute_was_declared(klass, attribute) | ||
if collecting? | ||
collect_for_later(klass, attribute) | ||
else | ||
apply_filter(klass, attribute) | ||
end | ||
end | ||
|
||
def apply_collected_attributes | ||
@attributes_by_class.each do |klass, attributes| | ||
attributes.each do |attribute| | ||
apply_filter(klass, attribute) | ||
end | ||
end | ||
end | ||
|
||
def collecting? | ||
@collecting | ||
end | ||
|
||
def collect_for_later(klass, attribute) | ||
@attributes_by_class[klass] ||= Concurrent::Array.new | ||
@attributes_by_class[klass] << attribute | ||
end | ||
|
||
def apply_filter(klass, attribute) | ||
filter = [("#{klass.model_name.element}" if klass.name), attribute.to_s].compact.join(".") | ||
unless excluded_from_filter_parameters?(filter) | ||
app.config.filter_parameters << filter unless app.config.filter_parameters.include?(filter) | ||
klass.filter_attributes += [ attribute ] | ||
end | ||
end | ||
|
||
def excluded_from_filter_parameters?(filter_parameter) | ||
ActiveRecord::Encryption.config.excluded_from_filter_parameters.find { |excluded_filter| excluded_filter.to_s == filter_parameter } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.