-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use secret_key_base
from ENV or credentials when present locally
#53705
base: main
Are you sure you want to change the base?
Conversation
3b3a992
to
339e38b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new complexity of generate_local_secret?
makes me think the abstraction is wrong (especially since it only gets used in a single place).
What do you think about just making secret_key_base
look more like the pre-secret removal:
def secret_key_base
@secret_key_base || begin
self.secret_key_base = if ENV["SECRET_KEY_BASE_DUMMY"]
generate_local_secret
else
ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base || (generate_local_secret if Rails.env.local?)
end
end
end
@@ -514,7 +514,7 @@ def secret_key_base | |||
end | |||
|
|||
def secret_key_base=(new_secret_key_base) | |||
if new_secret_key_base.nil? && generate_local_secret? | |||
if new_secret_key_base.nil? && Rails.env.local? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this still work if an app does secret_key_base = nil
and has ENV["SECRET_KEY_BASE_DUMMY"]
set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, yes probably not. There isn't a test case for that now. I'll try to add it.
Maybe that is a good direction. I'll try to update the PR. |
339e38b
to
c3474dc
Compare
Since 0c76f17 a `tmp/local_secret.txt` is always used for `Rails.config.secret_key_base` in the test and development environments, where previously `ENV["SECRET_KEY_BASE"]` or `Rails.application.credentials.secret_key_base` were used if present. The race condition in generating the local secret file, as reported in rails#53661, should be avoided by setting `ENV["SECRET_KEY_BASE"]` or `Rails.application.credentials.secret_key_base`, but this currently doesn't work. When ENV["SECRET_KEY_BASE"] or `Rails.application.credentials.secret_key_base` is set for test or development, it should be used for the `Rails.config.secret_key_base`, instead of generating a `tmp/local_secret.txt`.
c3474dc
to
0fb5371
Compare
Thanks @skipkayhil ! This is much cleaner! ❤️ |
When is this planned for release? |
Motivation / Background
Since 0c76f17 a
tmp/local_secret.txt
is always used forRails.config.secret_key_base
in the test and development environments. Previously,ENV["SECRET_KEY_BASE"]
orRails.application.credentials.secret_key_base
were used if present.As the
secret_key_base
is used to generate signed IDs, it breaks Action Text attachments in development if they previously used thesecret_key_base
from ENV var or credentials. Accidentally deletingtmp/local_secret.txt
will also break all local Action Text attachments.The race condition in generating the local secret file, as reported in #53661, should be avoidable by setting
ENV["SECRET_KEY_BASE"]
orRails.application.credentials.secret_key_base
, but this currently doesn't work.Details
When
ENV["SECRET_KEY_BASE"]
orRails.application.credentials.secret_key_base
is set for test or development, it should be used for theRails.config.secret_key_base
, instead of generating atmp/local_secret.txt
.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]