Skip to content

Commit cfba941

Browse files
authored
Merge pull request rails#52351 from skipkayhil/hm-allow-nil-skb-in-local
Reallow setting secret_key_base to nil when local
2 parents ccb4e70 + 9dccb5c commit cfba941

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

railties/lib/rails/application/configuration.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def colorize_logging=(val)
512512

513513
def secret_key_base
514514
@secret_key_base || begin
515-
self.secret_key_base = if Rails.env.local? || ENV["SECRET_KEY_BASE_DUMMY"]
515+
self.secret_key_base = if generate_local_secret?
516516
generate_local_secret
517517
else
518518
ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base
@@ -521,7 +521,9 @@ def secret_key_base
521521
end
522522

523523
def secret_key_base=(new_secret_key_base)
524-
if new_secret_key_base.is_a?(String) && new_secret_key_base.present?
524+
if new_secret_key_base.nil? && generate_local_secret?
525+
@secret_key_base = generate_local_secret
526+
elsif new_secret_key_base.is_a?(String) && new_secret_key_base.present?
525527
@secret_key_base = new_secret_key_base
526528
elsif new_secret_key_base
527529
raise ArgumentError, "`secret_key_base` for #{Rails.env} environment must be a type of String`"
@@ -647,6 +649,10 @@ def generate_local_secret
647649

648650
File.binread(key_file)
649651
end
652+
653+
def generate_local_secret?
654+
Rails.env.local? || ENV["SECRET_KEY_BASE_DUMMY"]
655+
end
650656
end
651657
end
652658
end

railties/test/application/configuration_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def index
790790

791791
test "application will generate secret_key_base in tmp file if blank in development" do
792792
app_file "config/initializers/secret_token.rb", <<-RUBY
793-
Rails.application.credentials.secret_key_base = nil
793+
Rails.application.config.secret_key_base = nil
794794
RUBY
795795

796796
# For test that works even if tmp dir does not exist.
@@ -804,7 +804,7 @@ def index
804804

805805
test "application will generate secret_key_base in tmp file if blank in test" do
806806
app_file "config/initializers/secret_token.rb", <<-RUBY
807-
Rails.application.credentials.secret_key_base = nil
807+
Rails.application.config.secret_key_base = nil
808808
RUBY
809809

810810
# For test that works even if tmp dir does not exist.

0 commit comments

Comments
 (0)