Configures Combustion app and adds failing test#168
Configures Combustion app and adds failing test#168tulak wants to merge 3 commits intoankane:masterfrom
Conversation
|
Hi @tulak, thanks for reporting. I think a minimal fix would be: module AhoyEmail
class Engine < ::Rails::Engine
initializer "ahoy_email" do |app|
- AhoyEmail.secret_token ||= app.key_generator.generate_key("ahoy_email")
+ AhoyEmail.secret_token ||= ActiveSupport::KeyGenerator.new(app.secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1).generate_key("ahoy_email")
end
end
endHowever, any fix will impact applications that rely on the current behavior, so I'd like to think about the best way forward. Also, I think this is a bug with Rails (this problem will happen with any application calling |
|
I've found that the same problem happend in hotwired/turbo-rails#325 and DHH himself suggested wrapping the call to But @ankane you're right, we're in a different situation here. The change to call So even if we fix it your way, when we explicitly use I'm look forward your thoughts on this @ankane |
|
I've just read through hotwired/turbo-rails#340 where people discussed how the fix hotwired/turbo-rails#325 affected users that were running on the buggy version of turbo_rails for a while and then upgraded to the fixed version. There's much more stuff that can break just inside rails (ActionText attachments, Signed IDs) and some of them are much works than cookies. When cookies get broken, users can just sign-in again, but for ActionText Attachments a maintainer needs to migrate the assets with new message verifier. I didn't find any comprehensive fix that Rails would implement to avoid this from happening. But since the same problem happened in another gem ( |
Co-authored-by: Filip Zachar <tulak45@gmail.com>
|
Thanks @tulak. I've applied the fix in the commit above, but still need to figure out the best way to communicate it (existing installations will need to set This also impacts Mailkick, so will apply the fix there as well. |
|
@ankane I'd not rush into releasing new gem versions with this fix until we have some strategy on how to communicate this properly. Seems like a lot of people were affected by this kind of fix for |
|
Yes, that's the plan / why I mentioned communication above. At the moment, I think the best way is probably a major version bump (and docs) to call attention to a potentially breaking change. It'd also be good to see if there are plans to address this in Rails. Unfortunately, applications that add the gem today will experience similar breakage. |
|
Reported to Rails: rails/rails#56736 |
The Problem
Currently, the
ahoy_emailinitializer callsapp.key_generatorduring the engine initialization phase. This creates a significant "global side effect" in Rails applications because thekey_generatoris a singleton.In modern Rails (7.0+),
ActiveSupportsets thekey_generator_hash_digest_class(toSHA256) inside aconfig.after_initializeblock inactive_support.set_key_generator_hash_digest_classinitializer. Ifapp.key_generatoris invoked before that block runs, the generator is instantiated with the legacy Rails default (SHA1) and memoized.Impact
This effectively "poisons" the global key generator for the entire application:
SHA1for all cryptographic operations even if configured forSHA256.ActionDispatch::Cookiesrelies on this same generator, adding this gem to an existing Rails 7+ app can break session verification, effectively logging out all users because the app can no longer verify cookies signed with the modern default.The Fix
This PR delays the initialization of
AhoyEmail.secret_tokenby:after: :load_config_initializers.app.config.after_initialize.This ensures that all framework and user-defined configurations are fully loaded before the
key_generatoris touched.