Skip to content

Add config.secret_key_base#195

Merged
tom93 merged 3 commits intomasterfrom
rails-4-secret-key-base
Dec 21, 2023
Merged

Add config.secret_key_base#195
tom93 merged 3 commits intomasterfrom
rails-4-secret-key-base

Conversation

@tom93
Copy link
Contributor

@tom93 tom93 commented Dec 20, 2023

secret_key_base was added in Rails 4 and replaces secret_token.

This PR is intended to merge using the "Rebase" strategy. Update: I reverted the second commit (see below), so now should use squash & merge and clean up the commit message.

secret_key_base was added in Rails 4 and replaces secret_token. It is
used by CookieStore to encrypt cookies (we are not currently using
CookieStore, but we will switch to it in #162).

The Rails 4 convention for setting secret_key_base is to use
config/secrets.yml and the environment variable SECRET_KEY_BASE (see
[1]). We don't use this approach, because secrets.yml will be
deprecated in favour of credentials.yml.enc in Rails 5.2 [2] and
because setting the environment variable is awkward.

Instead, we generate a random secret the first time the app runs and
store it in config/secret_key_base.#{env}.txt. We also support the
environment variable SECRET_KEY_BASE to match the Rails convention; we
don't currently use this environment variable, but it may be useful
(e.g. in situations such as build steps where we don't want to
generate the file, or in environments such as Docker where environment
variables are often more convenient than files).

We don't store the secret in the database (which is how the existing
secret_token is implemented) because it may be useful to start the app
without a database connection. Also, it's probably more secure to keep
the secret out of the database (and its backups).

This commit also adds a rule to .gitignore to make sure the secrets
aren't accidentally committed.

[1]: https://guides.rubyonrails.org/v4.1.8/upgrading_ruby_on_rails.html#config-secrets-yml
[2]: https://www.github.com/rails/rails/pull/30067
It has been replaced by secret_key_base, and doesn't appear to be used
anywhere because we are not using CookieStore yet.

Also rename the file because it is no longer related to secret_token.

The setting sessions/secret_token should eventually be deleted
manually using the admin web interface, but note that this setting is
still used in config/initializers/devise.rb at the moment.
@bagedevimo
Copy link
Contributor

tested this fairly throughly in the file and env modes - pretty big fan, works lovely! i haven't tested the db version, but i can't see any reason it wouldn't work in review.

@tom93
Copy link
Contributor Author

tom93 commented Dec 21, 2023

Thanks!

bagedevimo
bagedevimo previously approved these changes Dec 21, 2023
Holmes98
Holmes98 previously approved these changes Dec 21, 2023
Copy link
Member

@Holmes98 Holmes98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this invalidates Devise remember tokens, so we should deploy at the same time as #162 (which invalidates sessions) on second thought, I don't think this matters (but it does appear to invalidate remember tokens).

I was wrong, we *are* using CookieStore (just not for the session
store). We need to keep secret_token for now, to allow automatic
migration from the legacy signed-but-not-encrypted cookies to the new
store encrypted using secret_key_base.

The cookies are used for the "remember me" functionality for example.
@tom93 tom93 dismissed stale reviews from Holmes98 and bagedevimo via 1b8b238 December 21, 2023 05:23
@tom93
Copy link
Contributor Author

tom93 commented Dec 21, 2023

Oops. As per the chat, I made a mistake -- we were already using CookieJar, just not for the session. So removing secret_token breaks e.g. the "remember me" feature because the signed cookie can't be verified without the old secret_token. I reverted that commit, to fix the issue and allow automatic migration. We should keep secret_token for a while to give users a chance to open the site and get migrated.

So this should be good to merge now (let me squash and edit the commit message to exclude the secret_token stuff).

@tom93 tom93 changed the title Upgrade from config.secret_token to config.secret_key_base Add config.secret_key_base Dec 21, 2023
Holmes98 pushed a commit to Holmes98/nztrain that referenced this pull request Dec 21, 2023
secret_key_base was added in Rails 4 and replaces secret_token. It is
used by CookieStore to encrypt cookies (we are not currently using
CookieStore, but we will switch to it in NZOI#162).

The Rails 4 convention for setting secret_key_base is to use
config/secrets.yml and the environment variable SECRET_KEY_BASE (see
[1]). We don't use this approach, because secrets.yml will be
deprecated in favour of credentials.yml.enc in Rails 5.2 [2] and
because setting the environment variable is awkward.

Instead, we generate a random secret the first time the app runs and
store it in config/secret_key_base.#{env}.txt. We also support the
environment variable SECRET_KEY_BASE to match the Rails convention; we
don't currently use this environment variable, but it may be useful
(e.g. in situations such as build steps where we don't want to
generate the file, or in environments such as Docker where environment
variables are often more convenient than files).

We don't store the secret in the database (which is how the existing
secret_token is implemented) because it may be useful to start the app
without a database connection. Also, it's probably more secure to keep
the secret out of the database (and its backups).

This commit also adds a rule to .gitignore to make sure the secrets
aren't accidentally committed.

[1]: https://guides.rubyonrails.org/v4.1.8/upgrading_ruby_on_rails.html#config-secrets-yml
[2]: https://www.github.com/rails/rails/pull/30067
@tom93 tom93 merged commit d9957a5 into master Dec 21, 2023
@tom93 tom93 mentioned this pull request Dec 22, 2023
@Holmes98 Holmes98 deleted the rails-4-secret-key-base branch December 22, 2023 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants