Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove memoization to accept key_provider overridden by with_encryption_context #51019

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ def encryptor
end

def encryption_options
@encryption_options ||= { key_provider: key_provider, cipher_options: { deterministic: deterministic? } }.compact
{ key_provider: key_provider, cipher_options: { deterministic: deterministic? } }.compact
end

def decryption_options
@decryption_options ||= { key_provider: key_provider }.compact
{ key_provider: key_provider }.compact
end

def clean_text_scheme
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/encryption/scheme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def fixed?
end

def key_provider
@key_provider ||= @key_provider_param || build_key_provider || default_key_provider
@key_provider_param || build_key_provider || default_key_provider
end

def merge(other_scheme)
Expand Down
45 changes: 45 additions & 0 deletions activerecord/test/cases/encryption/encryptable_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,51 @@ class ActiveRecord::Encryption::EncryptableRecordTest < ActiveRecord::Encryption
assert_invalid_key_cant_read_attribute(post, :body)
end

test "swapping key_providers via with_encryption_context" do
key_provider1 = ActiveRecord::Encryption::DerivedSecretKeyProvider.new(SecureRandom.base64(32))
key_provider2 = ActiveRecord::Encryption::DerivedSecretKeyProvider.new(SecureRandom.base64(32))

post1 = post2 = nil

ActiveRecord::Encryption.with_encryption_context key_provider: key_provider1 do
post1 = EncryptedPost.create!(title: "post1!", body: "first post!")
end

ActiveRecord::Encryption.with_encryption_context key_provider: key_provider2 do
post2 = EncryptedPost.create!(title: "post2!", body: "second post!")
end

post1.reload
assert_raises ActiveRecord::Encryption::Errors::Decryption do
post1.title
end

post2.reload
assert_raises ActiveRecord::Encryption::Errors::Decryption do
post2.title
end

ActiveRecord::Encryption.with_encryption_context key_provider: key_provider1 do
post1.reload
assert_equal "post1!", post1.title

post2.reload
assert_raises ActiveRecord::Encryption::Errors::Decryption do
post2.title
end
end

ActiveRecord::Encryption.with_encryption_context key_provider: key_provider2 do
post2.reload
assert_equal "post2!", post2.title

post1.reload
assert_raises ActiveRecord::Encryption::Errors::Decryption do
post1.title
end
end
end

test "ignores nil values" do
assert_nil EncryptedBook.create!(name: nil).name
end
Expand Down