Skip to content

Commit 9dccb5c

Browse files
skipkayhilrafaelfranca
authored andcommitted
Reallow setting secret_key_base to nil when local
Previously, secret_key_base was allowed to be set to `nil` in local environments (or with SECRET_KEY_BASE_DUMMY) because validation would only happen on usage and not on the setter. This was recently [changed][1] to make it easier to identify exactly where a secret_key_base was being set to an invalid value. However, this broke some applications which unconditionally set secret_key_base to some external value in dev/test. Before the change, the set value could be `nil` and fall back to the generated local secret on usage. This commit restores that behavior so that applications can continue to set secret_key_base unconditionally, since the nil value will end up getting replaced by the generated local secret anyways. [1]: c2901eb
1 parent 55c4ade commit 9dccb5c

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)