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

[Fix #48922] Honour encrypted attribute context in encrypted_attribute? #48923

Merged
merged 1 commit into from Jan 5, 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
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Fix `encrypted_attribute?` to take into account context properties passed to `encrypts`.

*Maxime Réty*

* The object returned by `explain` now responds to `pluck`, `first`,
`last`, `average`, `count`, `maximum`, `minimum`, and `sum`. Those
new methods run `EXPLAIN` on the corresponding queries:
Expand Down
Expand Up @@ -144,7 +144,13 @@ def validate_column_size(attribute_name)

# Returns whether a given attribute is encrypted or not.
def encrypted_attribute?(attribute_name)
ActiveRecord::Encryption.encryptor.encrypted? read_attribute_before_type_cast(attribute_name)
name = attribute_name.to_s
name = self.class.attribute_aliases[name] || name

return false unless self.class.encrypted_attributes&.include? name.to_sym

type = type_for_attribute(name)
type.encrypted? read_attribute_before_type_cast(name)
end

# Returns the ciphertext for +attribute_name+.
Expand Down
Expand Up @@ -44,6 +44,10 @@ def serialize(value)
end
end

def encrypted?(value)
with_context { encryptor.encrypted? value }
end

def changed_in_place?(raw_old_value, new_value)
old_value = raw_old_value.nil? ? nil : deserialize(raw_old_value)
old_value != new_value
Expand Down
Expand Up @@ -9,6 +9,7 @@ class ActiveRecord::Encryption::EncryptionSchemesTest < ActiveRecord::Encryption

author = create_author_with_name_encrypted_with_previous_scheme
assert_equal "dhh", author.reload.name
assert author.encrypted_attribute? :name
end

test "when defining previous encryption schemes, you still get Decryption errors when using invalid clear values" do
Expand All @@ -24,6 +25,7 @@ class ActiveRecord::Encryption::EncryptionSchemesTest < ActiveRecord::Encryption
test "use a custom encryptor" do
author = EncryptedAuthor1.create name: "1"
assert_equal "1", author.name
assert author.encrypted_attribute? :name
end

test "support previous contexts" do
Expand All @@ -32,10 +34,12 @@ class ActiveRecord::Encryption::EncryptionSchemesTest < ActiveRecord::Encryption
author = EncryptedAuthor2.create name: "2"
assert_equal "2", author.name
assert_equal author, EncryptedAuthor2.find_by_name("2")
assert author.encrypted_attribute? :name

Author.find(author.id).update! name: "1"
assert_equal "1", author.reload.name
assert_equal author, EncryptedAuthor2.find_by_name("1")
assert_not author.encrypted_attribute? :name
end

test "use global previous schemes to decrypt data encrypted with previous schemes" do
Expand Down Expand Up @@ -191,7 +195,9 @@ def decrypt(encrypted_text, key_provider: nil, cipher_options: {})
end

def encrypted?(text)
text == encrypted_text
decrypt(text)
rescue ActiveRecord::Encryption::Errors::Decryption
false
end
end

Expand Down