Skip to content

Commit 9f22838

Browse files
Document solution for issue hotwired#340
Applications that are affected by secrets changing when upgrading pass 1.1.0 can use a key rotation to avoid ActiveStorage assets breaking. This adds documentation to explain the issue, with code showing how to rotate the key.
1 parent 9281d62 commit 9f22838

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

UPGRADING.md

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
# Upgrading from previous Turbo Rails versions
2+
3+
## Key digest changes in 1.1.1
4+
5+
Prior to version 1.1.1, Turbo Rails inadvertently caused applications to use SHA1 when deriving application secrets,
6+
even if another digest class was specified in `config.active_support.key_generator_hash_digest_class`. Starting with
7+
Rails 7, new applications default to SHA256 for key generation, and so are more likely to be affected by this.
8+
9+
This behavior was [fixed][1] in Turbo Rails 1.1.1. As a result, upgrading from an older version can cause an unexpected
10+
change to application secrets.
11+
12+
For applications that use ActiveStorage, this causes a change to the secret used by its message verifier, which will make
13+
assets previously stored by the application [inaccessible][2].
14+
15+
If your application is affected by this, you can use a key rotation to ensure the old asset digests remain readable.
16+
Placing the following code inside `config/initializers` will add the necessary rotation:
17+
18+
```ruby
19+
Rails.application.config.after_initialize do |app|
20+
key_generator = ActiveSupport::KeyGenerator.new app.secret_key_base,
21+
iterations: 1000,
22+
hash_digest_class: OpenSSL::Digest::SHA1
23+
24+
app.message_verifier("ActiveStorage").rotate(key_generator.generate_key("ActiveStorage"))
25+
end
26+
```
27+
28+
Alternatively, you can configure the application to continue using SHA1-based secrets, by overriding the default:
29+
30+
```ruby
31+
config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA1
32+
```
33+
34+
[1]: https://github.com/hotwired/turbo-rails/pull/335
35+
[2]: https://github.com/hotwired/turbo-rails/issues/340
36+
137
# Upgrading from Rails UJS / Turbolinks to Turbo
238

339
Turbo supersedes the functionality offered by Rails UJS to turn links and form submissions into XMLHttpRequests, so if you're making a complete switch from Rails UJS / Turbolinks to Turbo, you should ensure that you have `config.action_view.form_with_generates_remote_forms = false` set in your `config/application.rb`. But not all applications can upgrade in one jump, and may need to have Rails UJS coexist alongside Turbo. Here are the steps you need to follow:

0 commit comments

Comments
 (0)