-
Notifications
You must be signed in to change notification settings - Fork 22.1k
Restore some config.secret_key_base functionality #52062
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
Conversation
| end | ||
|
|
||
| test "config.secret_key_base over-writes a blank app.secret_key_base" do | ||
| test "app.secret_key_base falls back to config.secret_key_base in production" do |
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.
I repurposed this test because it was doing the exact same thing as the test above
937c090 to
4842e3f
Compare
| self.secret_key_base = if Rails.env.local? || ENV["SECRET_KEY_BASE_DUMMY"] | ||
| generate_local_secret | ||
| else | ||
| ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base |
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.
Is credentials already available as an accessor?
| ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base | |
| ENV["SECRET_KEY_BASE"] || credentials.secret_key_base |
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.
I initially had the same thought. There is a credentials accessor here, however its for configuration and not the actual credentials object on Rails.application
The [deprecated secrets removal][1] ended up removing a bit of non-deprecated functionality related to config.secret_key_base: - the original implementation prioritized the value of config.secret_key_base over other sources in all environments - if unset, the value of config.secret_key_base would be updated to whichever fallback value was found The new implementation only sets config.secret_key_base to a fallback value when Rails.env.local?, and never considers it at all in production. This commit aims to restore this missing functionality as well as simplify the implementation: - Rails.application.secret_key_base now always delegates to config.secret_key_base (like the pre-secret-removal implementation) - secret_key_base validation was moved from the reader to the writer - config.secret_key_base now handles setting itself to a fallback value when unset - In addition, generate_local_secret was simplified because it previously did 3 things: file manipulation, setting config.secret_key_base, and returning a value. Now it only creates the file if necessary and returns the value stored in it The new implementation has an additional benefit, which is that manually set config.secret_key_base values are now validated, whereas previously only fallback values were validated. [1]: 0c76f17 Co-authored-by: Petrik <[email protected]>
4842e3f to
c2901eb
Compare
Restore some config.secret_key_base functionality
|
I think nil support is valid so that you can have code like this: # config/init/secrets.rb
Rails.application.config.secret_key_base = ENV["SECRET_KEY_BASE"]
This will now crash if you don't have The workaround is not as nice: if ENV["SECRET_KEY_BASE"]
Rails.application.config.secret_key_base = ENV["SECRET_KEY_BASE"]
end |
|
That's interesting, I definitely didn't expect applications to be setting it to Is there a reason you're explicitly using |
|
This code is many years old, so I'm not sure. I guess I could see if removing it breaks anything. |
|
I also am noticing this in a legacy application pointed against # config/application.rb
config.secret_key_base = Configster.config_for('Global').secret_key_baseThis would return Did you end up deciding on whether this behavior should be restored? |
|
I opened a PR above, since I think we should either restore the old behavior or at least deprecate to not break application unnecessarily 👍 |

Motivation / Background
The deprecated secrets removal ended up removing a bit of non-deprecated functionality related to config.secret_key_base:
The new implementation only sets config.secret_key_base to a fallback value when Rails.env.local?, and never considers it at all in production.
Detail
This commit aims to restore this missing functionality as well as simplify the implementation:
The new implementation has an additional benefit, which is that manually set config.secret_key_base values are now validated, whereas previously only fallback values were validated.
Detail
Add it back with a test
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]