File tree Expand file tree Collapse file tree
actionpack/lib/action_dispatch/middleware/session Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,9 +24,10 @@ module Session
2424 #
2525 # Rails.application.config.session_store :cookie_store, key: '_your_app_session'
2626 #
27- # By default, your secret key base is derived from your application name in
28- # the test and development environments. In all other environments, it is stored
29- # encrypted in the <tt>config/credentials.yml.enc</tt> file.
27+ # In the development and test environments your application's secret key base is
28+ # generated by Rails and stored in a temporary file in <tt>tmp/development_secret.txt</tt>.
29+ # In all other environments, it is stored encrypted in the
30+ # <tt>config/credentials.yml.enc</tt> file.
3031 #
3132 # If your application was not updated to Rails 5.2 defaults, the secret_key_base
3233 # will be found in the old <tt>config/secrets.yml</tt> file.
Original file line number Diff line number Diff line change @@ -415,8 +415,8 @@ def secrets
415415 # then credentials.secret_key_base, and finally secrets.secret_key_base. For most applications,
416416 # the correct place to store it is in the encrypted credentials file.
417417 def secret_key_base
418- if Rails . env . test ? || Rails . env . development ?
419- secrets . secret_key_base || Digest :: MD5 . hexdigest ( self . class . name )
418+ if Rails . env . development ? || Rails . env . test ?
419+ secrets . secret_key_base ||= generate_development_secret
420420 else
421421 validate_secret_key_base (
422422 ENV [ "SECRET_KEY_BASE" ] || credentials . secret_key_base || secrets . secret_key_base
@@ -581,6 +581,21 @@ def validate_secret_key_base(secret_key_base)
581581
582582 private
583583
584+ def generate_development_secret
585+ if secrets . secret_key_base . nil?
586+ key_file = Rails . root . join ( "tmp/development_secret.txt" )
587+
588+ if !File . exist? ( key_file )
589+ random_key = SecureRandom . hex ( 64 )
590+ File . binwrite ( key_file , random_key )
591+ end
592+
593+ secrets . secret_key_base = File . binread ( key_file )
594+ end
595+
596+ secrets . secret_key_base
597+ end
598+
584599 def build_request ( env )
585600 req = super
586601 env [ "ORIGINAL_FULLPATH" ] = req . fullpath
Original file line number Diff line number Diff line change @@ -596,6 +596,27 @@ def index
596596 assert_equal "some_value" , verifier . verify ( message )
597597 end
598598
599+ test "application will generate secret_key_base in tmp file if blank in development" do
600+ app_file "config/initializers/secret_token.rb" , <<-RUBY
601+ Rails.application.credentials.secret_key_base = nil
602+ RUBY
603+
604+ app "development"
605+
606+ assert_not_nil app . secrets . secret_key_base
607+ assert File . exist? ( app_path ( "tmp/development_secret.txt" ) )
608+ end
609+
610+ test "application will not generate secret_key_base in tmp file if blank in production" do
611+ app_file "config/initializers/secret_token.rb" , <<-RUBY
612+ Rails.application.credentials.secret_key_base = nil
613+ RUBY
614+
615+ assert_raises ArgumentError do
616+ app "production"
617+ end
618+ end
619+
599620 test "raises when secret_key_base is blank" do
600621 app_file "config/initializers/secret_token.rb" , <<-RUBY
601622 Rails.application.credentials.secret_key_base = nil
@@ -619,7 +640,6 @@ def index
619640
620641 test "application verifier can build different verifiers" do
621642 make_basic_app do |application |
622- application . credentials . secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
623643 application . config . session_store :disabled
624644 end
625645
Original file line number Diff line number Diff line change @@ -226,6 +226,7 @@ def self.name; "RailtiesTestApp"; end
226226 @app . config . session_store :cookie_store , key : "_myapp_session"
227227 @app . config . active_support . deprecation = :log
228228 @app . config . log_level = :info
229+ @app . secrets . secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
229230
230231 yield @app if block_given?
231232 @app . initialize!
You can’t perform that action at this time.
0 commit comments