File tree Expand file tree Collapse file tree 4 files changed +43
-6
lines changed
actionpack/lib/action_dispatch/middleware/session Expand file tree Collapse file tree 4 files changed +43
-6
lines changed Original file line number Diff line number Diff line change @@ -29,9 +29,10 @@ module Session
29
29
#
30
30
# Rails.application.config.session_store :cookie_store, key: '_your_app_session'
31
31
#
32
- # By default, your secret key base is derived from your application name in
33
- # the test and development environments. In all other environments, it is stored
34
- # encrypted in the <tt>config/credentials.yml.enc</tt> file.
32
+ # In the development and test environments your application's secret key base is
33
+ # generated by Rails and stored in a temporary file in <tt>tmp/development_secret.txt</tt>.
34
+ # In all other environments, it is stored encrypted in the
35
+ # <tt>config/credentials.yml.enc</tt> file.
35
36
#
36
37
# If your application was not updated to Rails 5.2 defaults, the secret_key_base
37
38
# will be found in the old <tt>config/secrets.yml</tt> file.
Original file line number Diff line number Diff line change @@ -426,8 +426,8 @@ def secrets=(secrets) #:nodoc:
426
426
# then credentials.secret_key_base, and finally secrets.secret_key_base. For most applications,
427
427
# the correct place to store it is in the encrypted credentials file.
428
428
def secret_key_base
429
- if Rails . env . test ? || Rails . env . development ?
430
- secrets . secret_key_base || Digest :: MD5 . hexdigest ( self . class . name )
429
+ if Rails . env . development ? || Rails . env . test ?
430
+ secrets . secret_key_base ||= generate_development_secret
431
431
else
432
432
validate_secret_key_base (
433
433
ENV [ "SECRET_KEY_BASE" ] || credentials . secret_key_base || secrets . secret_key_base
@@ -588,6 +588,21 @@ def validate_secret_key_base(secret_key_base)
588
588
589
589
private
590
590
591
+ def generate_development_secret
592
+ if secrets . secret_key_base . nil?
593
+ key_file = Rails . root . join ( "tmp/development_secret.txt" )
594
+
595
+ if !File . exist? ( key_file )
596
+ random_key = SecureRandom . hex ( 64 )
597
+ File . binwrite ( key_file , random_key )
598
+ end
599
+
600
+ secrets . secret_key_base = File . binread ( key_file )
601
+ end
602
+
603
+ secrets . secret_key_base
604
+ end
605
+
591
606
def build_request ( env )
592
607
req = super
593
608
env [ "ORIGINAL_FULLPATH" ] = req . fullpath
Original file line number Diff line number Diff line change @@ -513,6 +513,27 @@ def index
513
513
end
514
514
515
515
516
+ test "application will generate secret_key_base in tmp file if blank in development" do
517
+ app_file "config/initializers/secret_token.rb" , <<-RUBY
518
+ Rails.application.credentials.secret_key_base = nil
519
+ RUBY
520
+
521
+ app "development"
522
+
523
+ assert_not_nil app . secrets . secret_key_base
524
+ assert File . exist? ( app_path ( "tmp/development_secret.txt" ) )
525
+ end
526
+
527
+ test "application will not generate secret_key_base in tmp file if blank in production" do
528
+ app_file "config/initializers/secret_token.rb" , <<-RUBY
529
+ Rails.application.credentials.secret_key_base = nil
530
+ RUBY
531
+
532
+ assert_raises ArgumentError do
533
+ app "production"
534
+ end
535
+ end
536
+
516
537
test "raises when secret_key_base is blank" do
517
538
app_file "config/initializers/secret_token.rb" , <<-RUBY
518
539
Rails.application.credentials.secret_key_base = nil
@@ -550,7 +571,6 @@ def index
550
571
551
572
test "application verifier can build different verifiers" do
552
573
make_basic_app do |application |
553
- application . credentials . secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
554
574
application . config . session_store :disabled
555
575
end
556
576
Original file line number Diff line number Diff line change @@ -155,6 +155,7 @@ def self.name; "RailtiesTestApp"; end
155
155
@app . config . active_support . deprecation = :log
156
156
@app . config . active_support . test_order = :random
157
157
@app . config . log_level = :info
158
+ @app . secrets . secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
158
159
159
160
yield @app if block_given?
160
161
@app . initialize!
You can’t perform that action at this time.
0 commit comments