rails: Migrating from Rails 7.0.8 to Rails 7.1.2 without any config breaks encryption

Steps to reproduce

Setting up the App

rails _7.0.8_ new test_rails_enc
cd test_rails_enc
bin/rails db:encryption:init
rails credentials:edit # Copying the keys
rails g model user email:string name:string
class User < ApplicationRecord
  encrypts :email, deterministic: true
  encrypts :name
end
User.create(name: "Zaratan", email: "zaratan@hey.com")

Migrating to Rails 7.1

In the Gemfile

gem "rails", "~> 7.1.2"
rails app:update # Answering Yes to every question
rails db:migrate

Testing

Email:

User.pluck(:email).first
  User Pluck (0.3ms)  SELECT "users"."email" FROM "users"
/Users/zaratan/.rvm/gems/ruby-3.2.2/gems/activerecord-7.1.2/lib/active_record/encryption/encryptor.rb:58:in `rescue in decrypt': ActiveRecord::Encryption::Errors::Decryption (ActiveRecord::Encryption::Errors::Decryption)
/Users/zaratan/.rvm/gems/ruby-3.2.2/gems/activerecord-7.1.2/lib/active_record/encryption/cipher/aes256_gcm.rb:79:in `rescue in decrypt': ActiveRecord::Encryption::Errors::Decryption (ActiveRecord::Encryption::Errors::Decryption)
/Users/zaratan/.rvm/gems/ruby-3.2.2/gems/activerecord-7.1.2/lib/active_record/encryption/cipher/aes256_gcm.rb:75:in `final': OpenSSL::Cipher::CipherError

Name:

User.pluck(:name).first
  User Pluck (0.1ms)  SELECT "users"."name" FROM "users"
=> "igor"

If Adding :

config.active_record.encryption.support_sha1_for_non_deterministic_encryption = true

=> Same behaviour

If Changing to Rails 7.1 defaults

    config.load_defaults 7.1

Reading the name will trigger a cipher error and the email will be read correctly

If setting config.active_record.encryption.hash_digest_class to OpenSSL::Digest::SHA1

config.active_record.encryption.hash_digest_class = OpenSSL::Digest::SHA1

=> Email: Cipher Error, Name: Ok

If setting config.active_record.encryption.hash_digest_class to OpenSSL::Digest::SHA256

=> Email: Ok, Name: Cipher Error

Expected behavior

If a user didn’t configure anything the upgrade path should exist or be documented in a way. I think I tried every configuration option documented and couldn’t make it work.

Production is still on 7.0.8 so I didn’t get any terrible damage but what would be an upgrade path for me (and for others)

Actual behavior

OpenSSL::Cipher::CipherError

System configuration

Rails version: 7.0.8 => 7.1.2

Ruby version: 3.2.2

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I’m running into a similar issue myself, so I stepped through the exact process as described by @zaratan to try to debug. However, I was able to find a working config as follows:

config.load_defaults 7.1
config.active_record.encryption.hash_digest_class = OpenSSL::Digest::SHA256
config.active_record.encryption.support_sha1_for_non_deterministic_encryption = true

This is with rails 7.1.3 now, though, so perhaps a fix was added in the meantime? If someone else would like to try to confirm the issue still exists, please do, but I think this might be safe to close.

Configuring Active Record Encryption should have no effect on how Active Storage encrypts URLs … You should use the rotator mentioned in the article you linked to read both versions.

Ok, thank you! We’ll try out the rotator code. Seems like this should be burning more users, but perhaps they simply don’t know they’ve been affected yet? We’ll report back.

Update: seems to relate to https://github.com/hotwired/turbo-rails/issues/340#issuecomment-1888074080.

It sounds like two completely different things are being conflated here. Configuring Active Record Encryption should have no effect on how Active Storage encrypts URLs.

Our initial upgrade to 7.1 was still loading the 7.0 defaults It doesn’t explicitly mention how it affects permanent URLs in Active Storage and to my knowledge, we didn’t have config.active_support.key_generator_hash_digest_class explicitly set.

The default value for config.active_support.key_generator_hash_digest_class and config.active_support.hash_digest_class both changed with load_defaults 7.0: https://guides.rubyonrails.org/configuring.html#default-values-for-target-version-7-0

Assuming this config does affect Active Storage URLS, wouldn’t everything following our move to Rails 7, which should be OpenSSL::Digest::SHA256, break if we go back to the prior default? And this would be worse as it’s the newer / more current URLs. Seems like support_sha1_for_non_deterministic_encryption is supposed to provide the fallback for the prior SHA1 default.

Yes, SHA256 has been used since the config changed so you likely have both now. You should use the rotator mentioned in the article you linked to read both versions.

(support_sha1_for_non_deterministic_encryption has nothing to do with Active Storage and so will not be helpful)

Yeah, I’m sorry, but I’m not sure how this all interacts with Active Storage. You might want to go spelunking through the relevant commits (via the Rails Guides and referenced PRs and/or some git blaming) to see what you can find. Perhaps the PR introducing support_sha1_for_non_deterministic_encryption would be a good place to look, to see if you can trace it back to possible interactions (or lack thereof) with Active Storage. Chattering on those issues could get better visibility from people in the know and/or the Rails Discord is a good place to ping as well.

Apologies I don’t know more about this area – I wish I could be more helpful!

I’m wondering if we should reopen this issue.

My thinking is that at a minimum we should clarify the instructions in the upgrade guide to suggest a new recommended config. In my case, had I not checked against my production database first, I would have run into decryption errors unexpectedly, even though I’ve been using the default config (not having any explicit config).

If I’m understanding things correctly re https://github.com/rails/rails/issues/48204 and https://github.com/rails/rails/pull/48530, if upgrading from 7.0.8 to 7.1.3 with no config, you’d need to add the following:

config.active_record.encryption.hash_digest_class = OpenSSL::Digest::SHA256
config.active_record.encryption.support_sha1_for_non_deterministic_encryption = true

…or else you won’t be able to read preexisting encrypted data (with the caveat that you won’t be able to roll back per https://github.com/rails/rails/issues/50226).

If that’s correct, I think it should be made more clear somehow because it’s a major gotcha!

Potentially related possible fix: https://github.com/rails/rails/pull/50606 Potentially related possible workaround: https://github.com/rails/rails/issues/50604#issuecomment-1880990392