Skip to content

Commit c20f8eb

Browse files
Merge pull request rails#48471 from mdh/improve-naming-of-local-secret-generation
Improve naming of local secret generation
2 parents 4366095 + f75934f commit c20f8eb

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

actionpack/lib/action_dispatch/middleware/session/cookie_store.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Session
2727
# Rails.application.config.session_store :cookie_store, key: '_your_app_session'
2828
#
2929
# In the development and test environments your application's +secret_key_base+ is
30-
# generated by Rails and stored in a temporary file in <tt>tmp/development_secret.txt</tt>.
30+
# generated by Rails and stored in a temporary file in <tt>tmp/local_secret.txt</tt>.
3131
# In all other environments, it is stored encrypted in the
3232
# <tt>config/credentials.yml.enc</tt> file.
3333
#

railties/lib/rails/application.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def secrets
458458
# including the ones that sign and encrypt cookies.
459459
#
460460
# In development and test, this is randomly generated and stored in a
461-
# temporary file in <tt>tmp/development_secret.txt</tt>.
461+
# temporary file in <tt>tmp/local_secret.txt</tt>.
462462
#
463463
# You can also set <tt>ENV["SECRET_KEY_BASE_DUMMY"]</tt> to trigger the use of a randomly generated
464464
# secret_key_base that's stored in a temporary file. This is useful when precompiling assets for
@@ -471,7 +471,7 @@ def secrets
471471
# the correct place to store it is in the encrypted credentials file.
472472
def secret_key_base
473473
if Rails.env.local? || ENV["SECRET_KEY_BASE_DUMMY"]
474-
config.secret_key_base ||= generate_development_secret
474+
config.secret_key_base ||= generate_local_secret
475475
else
476476
validate_secret_key_base(
477477
ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base
@@ -645,9 +645,9 @@ def ensure_generator_templates_added
645645
end
646646

647647
private
648-
def generate_development_secret
648+
def generate_local_secret
649649
if config.secret_key_base.nil?
650-
key_file = Rails.root.join("tmp/development_secret.txt")
650+
key_file = Rails.root.join("tmp/local_secret.txt")
651651

652652
if File.exist?(key_file)
653653
config.secret_key_base = File.binread(key_file)

railties/test/application/configuration_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,21 @@ def index
733733
app "development"
734734

735735
assert_not_nil app.secret_key_base
736-
assert File.exist?(app_path("tmp/development_secret.txt"))
736+
assert File.exist?(app_path("tmp/local_secret.txt"))
737+
end
738+
739+
test "application will generate secret_key_base in tmp file if blank in test" do
740+
app_file "config/initializers/secret_token.rb", <<-RUBY
741+
Rails.application.credentials.secret_key_base = nil
742+
RUBY
743+
744+
# For test that works even if tmp dir does not exist.
745+
Dir.chdir(app_path) { FileUtils.remove_dir("tmp") }
746+
747+
app "test"
748+
749+
assert_not_nil app.secret_key_base
750+
assert File.exist?(app_path("tmp/local_secret.txt"))
737751
end
738752

739753
test "application will not generate secret_key_base in tmp file if blank in production" do

0 commit comments

Comments
 (0)