diff --git a/app/services/encryption_service.rb b/app/services/encryption_service.rb index 8949d54a78a..3d0dbb92e21 100644 --- a/app/services/encryption_service.rb +++ b/app/services/encryption_service.rb @@ -7,6 +7,10 @@ def initialize password = Rails.application.secrets.secret_key_base key = ActiveSupport::KeyGenerator.new(password).generate_key(salt, len) @encryptor = ActiveSupport::MessageEncryptor.new(key) + + # Remove after all encrypted attributes have been rotated. + legacy_key = ActiveSupport::KeyGenerator.new(password, hash_digest_class: OpenSSL::Digest::SHA1).generate_key(salt, len) + @encryptor.rotate legacy_key end def encrypt(value) diff --git a/app/tasks/maintenance/rotate_api_particulier_token_encryption_task.rb b/app/tasks/maintenance/rotate_api_particulier_token_encryption_task.rb new file mode 100644 index 00000000000..a47c6e13240 --- /dev/null +++ b/app/tasks/maintenance/rotate_api_particulier_token_encryption_task.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Maintenance + class RotateAPIParticulierTokenEncryptionTask < MaintenanceTasks::Task + def collection + # rubocop:disable DS/Unscoped + Procedure.unscoped.where.not(encrypted_api_particulier_token: nil) + # rubocop:enable DS/Unscoped + end + + def process(procedure) + decrypted_token = procedure.api_particulier_token + + procedure.api_particulier_token = decrypted_token + procedure.save!(validate: false) + end + + def count + collection.count + end + end +end diff --git a/config/application.rb b/config/application.rb index 91b64e496a3..b5798638850 100644 --- a/config/application.rb +++ b/config/application.rb @@ -13,7 +13,7 @@ module TPS class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 6.1 + config.load_defaults 7.0 # Configuration for the application, engines, and railties goes here. # @@ -62,6 +62,8 @@ class Application < Rails::Application config.active_storage.queues.analysis = :active_storage_analysis config.active_storage.queues.purge = :purge + config.active_support.cache_format_version = 7.0 + config.to_prepare do # Make main application helpers available in administrate Administrate::ApplicationController.helper(TPS::Application.helpers) diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb index f35c5e36b87..620e3f022d4 100644 --- a/config/initializers/active_storage.rb +++ b/config/initializers/active_storage.rb @@ -2,6 +2,7 @@ Rails.application.config.active_storage.service_urls_expire_in = 1.hour +Rails.application.config.active_storage.variant_processor = :mini_magick Rails.application.config.active_storage.analyzers.delete ActiveStorage::Analyzer::ImageAnalyzer Rails.application.config.active_storage.analyzers.delete ActiveStorage::Analyzer::VideoAnalyzer diff --git a/config/initializers/cookie_rotator.rb b/config/initializers/cookie_rotator.rb index 53b40ec8f10..e307467638c 100644 --- a/config/initializers/cookie_rotator.rb +++ b/config/initializers/cookie_rotator.rb @@ -1,20 +1,24 @@ # frozen_string_literal: true -# TODO: Enable cookies rotation when new SHA256 will be enforced -# See new_framework_defaults_7.0.rb -# key_generator_hash_digest_class = OpenSSL::Digest::SHA256 will be -# -# Rails.application.config.after_initialize do -# Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies| -# salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt -# secret_key_base = Rails.application.secret_key_base +# This cookie rotator converts cookies from the old SHA1 hash (Rails 6) to SHA256 hash (Rails 7 default). +# It should be kept enabled for approximately 1 month to ensure most users have their cookies rotated. +# After this period, it can be safely removed. +# Without this rotator, all users would have been signed out. +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 -# secret = key_generator.generate_key(salt, key_len) + key_generator = ActiveSupport::KeyGenerator.new( + secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1 # Rails 6 hash + ) + key_len = ActiveSupport::MessageEncryptor.key_len -# cookies.rotate :encrypted, secret -# end -# end + 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 diff --git a/config/initializers/new_framework_defaults_7_0.rb b/config/initializers/new_framework_defaults_7_0.rb deleted file mode 100644 index 389456e6af8..00000000000 --- a/config/initializers/new_framework_defaults_7_0.rb +++ /dev/null @@ -1,135 +0,0 @@ -# frozen_string_literal: true - -# Be sure to restart your server when you modify this file. -# -# This file eases your Rails 7.0 framework defaults upgrade. -# -# Uncomment each configuration one by one to switch to the new default. -# Once your application is ready to run with all new defaults, you can remove -# this file and set the `config.load_defaults` to `7.0`. -# -# Read the Guide for Upgrading Ruby on Rails for more info on each option. -# https://guides.rubyonrails.org/upgrading_ruby_on_rails.html - -# `button_to` view helper will render `