Skip to content

Commit

Permalink
[TAN-3184] Configure cookie rotation for SHA1 to SHA256 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
adessy committed Dec 18, 2024
1 parent 558b1c4 commit 0518c08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions back/config/initializers/cookie_rotator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This initializer sets up cookie rotation to ensure a smooth transition from SHA1 to SHA256,
# which is the hash algorithm used by default by the key generator digest class in Rails 7.0.
# For more information, refer to the Rails 7.0 upgrade guide:
# https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#key-generator-digest-class-changing-to-use-sha256

Rails.application.config.after_initialize do
Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies|
authenticated_encrypted_cookie_salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt
signed_cookie_salt = Rails.application.config.action_dispatch.signed_cookie_salt

secret_key_base = Rails.application.secret_key_base

key_generator = ActiveSupport::KeyGenerator.new(
secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1
)

key_len = ActiveSupport::MessageEncryptor.key_len

old_encrypted_secret = key_generator.generate_key(authenticated_encrypted_cookie_salt, key_len)
old_signed_secret = key_generator.generate_key(signed_cookie_salt)

cookies.rotate :encrypted, old_encrypted_secret
cookies.rotate :signed, old_signed_secret
end
end
2 changes: 1 addition & 1 deletion back/config/initializers/new_framework_defaults_7_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#
# See upgrading guide for more information on how to build a rotator.
# https://guides.rubyonrails.org/v7.0/upgrading_ruby_on_rails.html
# Rails.application.config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA256
Rails.application.config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA256

# Change the digest class for ActiveSupport::Digest.
# Changing this default means that for example Etags change and
Expand Down

0 comments on commit 0518c08

Please sign in to comment.