Skip to content

Commit 4c74358

Browse files
tenderloveeileencodesjhawthorn
committed
Fix possible dev mode RCE
If the secret_key_base is nil in dev or test generate a key from random bytes and store it in a tmp file. This prevents the app developers from having to share / checkin the secret key for dev / test but also maintains a key between app restarts in dev/test. [CVE-2019-5420] Co-Authored-By: eileencodes <eileencodes@gmail.com> Co-Authored-By: John Hawthorn <john@hawthorn.email>
1 parent f4c70c2 commit 4c74358

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff 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.

‎railties/lib/rails/application.rb‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff 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

‎railties/test/application/configuration_test.rb‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff 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

‎railties/test/isolation/abstract_unit.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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!

0 commit comments

Comments
 (0)