Skip to content

Commit

Permalink
Merge pull request #49839 from skipkayhil/hm-skb-deprecation
Browse files Browse the repository at this point in the history
Fix config.secret_key_base warning about secrets
  • Loading branch information
rafaelfranca committed Nov 10, 2023
2 parents c37efa1 + 1abd331 commit fd20970
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion railties/lib/rails/application.rb
Expand Up @@ -477,7 +477,21 @@ def secret_key_base
config.secret_key_base ||= generate_local_secret
else
validate_secret_key_base(
ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base
ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || begin
secret_skb = secrets_secret_key_base

if secret_skb.equal?(config.secret_key_base)
config.secret_key_base
else
Rails.deprecator.warn(<<~MSG.squish)
Your `secret_key_base is configured in `Rails.application.secrets`,
which is deprecated in favor of `Rails.application.credentials` and
will be removed in Rails 7.2.
MSG

secret_skb
end
end
)
end
end
Expand Down
14 changes: 14 additions & 0 deletions railties/test/application/configuration_test.rb
Expand Up @@ -965,6 +965,20 @@ def index
assert_equal "3b7cd727ee24e8444053437c36cc66c3", app.secret_key_base
end

test "config.secret_key_base does not lead to a deprecation" do
remove_file "config/secrets.yml"
app_file "config/initializers/secret_token.rb", <<-RUBY
Rails.application.credentials.secret_key_base = nil
Rails.application.config.secret_key_base = "3b7cd727ee24e8444053437c36cc66c3"
RUBY

app "production"

assert_not_deprecated(Rails.deprecator) do
assert_equal "3b7cd727ee24e8444053437c36cc66c3", app.secret_key_base
end
end

test "custom secrets saved in config/secrets.yml are loaded in app secrets" do
app_file "config/secrets.yml", <<-YAML
development:
Expand Down

0 comments on commit fd20970

Please sign in to comment.