rails: Upgrade to Rails 7.1 alpha breaks ActiveRecord attribute encryption (deterministic)
Steps to reproduce
In Rails 7.0:
create_table "my_models", force: :cascade do |t|
t.string "my_attribute"
end
class MyModel < ApplicationRecord
encrypts :my_attribute, deterministic: true
end
MyModel.create!(my_attribute: "my value")
model = MyModel.last
model.my_attribute == "my value" # true
Upgrade to Rails 7.1-alpha and run:
model = MyModel.last # raises exception
ActiveRecord encryption configuration has not changed in the meanwhile. I don’t know with what version of Rails the ActiveRecord encryption configuration has been created. Only deterministically encrypted attributes seem to be affected by this. We also have non-deterministically encrypted attributes and those still work after upgrading to Rails 7.1-alpha.
Expected behavior
Deterministically encrypted attribute should be decrypted properly.
Actual behavior
An error is raised while attribute is being decrypted:
/Users/someuser/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/bundler/gems/rails-403e4e5b5639/activerecord/lib/active_record/encryption/encryptor.rb:58:in `rescue in decrypt': ActiveRecord::Encryption::Errors::Decryption (ActiveRecord::Encryption::Errors::Decryption)
/Users/someuser/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/bundler/gems/rails-403e4e5b5639/activerecord/lib/active_record/encryption/cipher/aes256_gcm.rb:79:in `rescue in decrypt': ActiveRecord::Encryption::Errors::Decryption (ActiveRecord::Encryption::Errors::Decryption)
/Users/someuser/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/bundler/gems/rails-403e4e5b5639/activerecord/lib/active_record/encryption/cipher/aes256_gcm.rb:75:in `final': OpenSSL::Cipher::CipherError
System configuration
Rails version: 7.1.0-alpha
Ruby version: 3.2.1
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 55 (21 by maintainers)
Commits related to this issue
- Fix Active Record encryption not picking up encryption settings with eager-loading This deals with a problem where, when eager-loading is enabled, Active Record fails to pick up settings affecting en... — committed to basecamp/rails by jorgemanrubia 3 years ago
- Fix Active Record encryption not picking up encryption settings with eager-loading (#48577) This deals with a problem where, when eager-loading is enabled, Active Record fails to pick up settings aff... — committed to rails/rails by jorgemanrubia 3 years ago
- Fix queries for deterministically encrypted attributed for data migrated from 7.0. #48530 introduced a problem for the system that extends AR queries involving deterministically encrypted attributes.... — committed to basecamp/rails by jorgemanrubia 3 years ago
- Fix queries for deterministically encrypted attributed for data migrated from 7.0. (#48676) #48530 introduced a problem for the system that extends AR queries involving deterministically encrypted at... — committed to rails/rails by jorgemanrubia 3 years ago
- Fix queries for deterministically encrypted attributed for data migrated from 7.0. (#48676) #48530 introduced a problem for the system that extends AR queries involving deterministically encrypted at... — committed to paulreece/rails-paulreece by jorgemanrubia 3 years ago
- Call out pitfalls with Encryption when upgrading As has been noted in a few issues, upgrading from Rails to 7.1 config defaults can lead to data loss or confusion as the hash_digest_class changed fro... — committed to calebhearth/rails by calebhearth 2 years ago
- Call out pitfalls with Encryption when upgrading As has been noted in a few issues, upgrading from Rails to 7.1 config defaults can lead to data loss or confusion as the hash_digest_class changed fro... — committed to calebhearth/rails by calebhearth 2 years ago
If you have some attributes encrypted using SHA1 and others using SHA256, you will get a
ActiveRecord::Encryption::Errors::Decryption, irrelevant whathash_digest_classis set to.To resolve this, set the following option in addition to
config.load_defaults 7.1:Then re-encrypt all your models having encrypted attributes:
You can then disable/remove the above option again.
I feel like
new_framework_defaults_7_1.rbshould be updated to reflect this. Going through the new framework defaults one by one, then usingconfig.load_defaults 7.1will causesupport_sha1_for_non_deterministic_encryption = false, no? Butconfig.load_defaults 7.0causessupport_sha1_for_non_deterministic_encryption = true. To me, there should be a warning to explicitly set that configuration totrueif you have encrypted data and are coming from a prior version of Rails, otherwise your app will fail to decrypt old data when loading 7.1 defaults. Right now, there’s no instructions on setting it totrueprior to callingconfig.load_defaults 7.1.Having to re-encrypt data can be a large undertaking when you have hundreds of millions of records, so I feel like this needs to be more apparent when upgrading, because right now I feel like it’s glossed over and missing some important details.
Still trying to dig into this issue for https://github.com/keygen-sh/keygen-api/pull/764, so apologies if any of this is incorrect.
Edit: I now notice this is the same or similar issue reported by @ezekg and documented in #49610. Sadly I upgraded before that change was merged. I’m leaving this here for those that also missed it and need to fix their database.
I’ve run into this issue after upgrading to 7.0.
When running Rails 7.1, if I try to load a record that was created by Rails 7.0 and try to read an attribute that uses
deterministic: true, I get aCipherErrorI’m using
load_defaults(7.0)and we’ve never had any custom encryption configuration.My problem is I didn’t notice this until deploying it and since then, new records have been created. So I can read the data on new records but not old ones. Looking at
new_framework_defaults_7.1.rb, I didn’t interpret item 2 of the encryption options as something I’d actually have to take action on since I didn’t have anykey_generator_hash_digest_classconfig, and anyway, I understood this file to be things you opt in to later on and not actions that must be taken before upgrading.I was lucky in that I only had a single column with deterministic encryption, and not being able to read the old values wasn’t an immediate emergency. I came up with the following database migration to fix the old records and I wanted to share it here for anyone else finding this issue from Google.
Step 1 is un-comment this line in
new_framework_defaults_7.1.rbThen, fix existing data:
@jorgemanrubia Trying to understand something. I’m in the situation where I never explicitly configured the hash_digest_class (my app is currently 7.1.2 but it started back at Rails 3 and I’ve regularly upgraded it for years).
So, basically my non-deterministic encrypted fields were being encrypted with SHA1. Then at some point the default changed to 256 and now I am sure I have that mix of SHA1 and SHA256 we’re all talking about here.
I have a record of a model that has an encrypted field. When I try to inspect it I get the Decrypt error.
In application.rb I am using the 7.1 defaults.
If after that, I have
then I can inspect this record, proving that it is indeed encrypted with SHA1. Problem solved…
BUT
Thing is, I would like to start encrypting newer fields with SHA256. So, I changed it to:
But now it can’t decrypt the field again.
I was hoping this would be my upgrade path, that it meant I’d be encrypting new fields with SHA256 but still able to read older SHA1-encrypted fields. But apparently I am misunderstanding something because as long as I have set my hash_digest_class to 256, support_sha1_for_non_deterministic_encryption appears to have no effect.
Ugh… it’s an initializer. The problem is on my end. I have an initializer that if removed makes this all work as you guys described.
Yeah, I confirmed the issue on my side. The problem is that, before #44873, data encrypted with non-deterministic encryption was always using
SHA-1. The reason is thatActiveSupport::KeyGenerator.hash_digest_classis set in anafter_initializeblock in the railtie config, but encryption config was running before that, so it was effectively using the previous default SHA1. That means that we were using SHA256 for non deterministic, and SHA1 for deterministic encryption as you saw @duduribeiro.The initialization problem is being fixed in #48520, but we now have the problem of existing data encrypted with two hash digests. This will affect everyone using the default key providers in 7.0.x.
I’ll prepare a patch for this. My first idea is adding an option to support decrypting data with both digest classes, so that we offer an upgrade path where things don’t break for existing users. We have support in place to support different encryption schemes, so it shouldn’t be a big change.
@duduribeiro I’ll work off your changes here https://github.com/rails/rails/pull/48520 in a new branch, since this change should happen at the same time.
Hi,
Here in my project it was necessary to put the variables of the new values in
Application.rb, I believe that the problem is not exactly some other initializer, but any code that initializes Rails BEFORE thenew_framework_defaults_7_1.rbfile is read, in this way even changing the value later by the new defaults initializer this does not work for encryption and generates theDecryptErrorerror.So something like this should work for anyone having trouble with deterministically encrypted attributes:
It is important to follow the alerts on which algorithm is currently running in your project to set the variables accordingly (
SHA1orSHA256).Just to make sure I am clear, with this config:
=> DecryptErrors
=> No decrypt errors.
It’s like support_sha1_for_non_deterministic_encryption does nothing.
@seanabrahams maybe you went to this specific scenario https://github.com/rails/rails/issues/48204#issuecomment-1598373096
Hi @jorgemanrubia. I can confirm that the fix of https://github.com/rails/rails/pull/48577 fixes our problem! Thanks
@phylor Trying to re-encrypt the fields fails with this error. I am assuming because it can’t decrypt them, just as I couldn’t decrypt them by simply finding the record:
In other words:
And
Hello, this happened to me with non-deterministic encryption as well
In order to fix, I’ve used the migration at https://github.com/rails/rails/issues/48204#issuecomment-1779505395 with this relevant change
That is what you should be doing and it is documented here:
https://github.com/rails/rails/blob/5abcfd4454523dbdbd6e9424778bfff86ec1caf1/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_1.rb.tt#L38-L40
We can’t do that automatically because we don’t know which digest class you used to encrypt your data in Rails 7.0.
Hi @jorgemanrubia. I’m afraid I found another issue related to this.
Prior to Rails 7.1 alpha:
Switch to Rails 7.1 alpha:
Any ideas?
Thanks for the report @bforma. I’ll get this solved this week. For the record, this should not be necessary to add (that’s implicit when coming from 7.0 defaults):
the only thing that I see for now is:
On rails 7.0.5
lib/active_record/encryption/cipher/aes256_gcm.rbmy@secretvar isand my rails 7.0.1-alpha is
👋 @bforma can you confirm that this repo script is correct? I am unable to reproduce what you are seeing when pointing to rails
main