Skip to content

Commit d558ba1

Browse files
committed
Security: Make our session secret actually a secret
This is the first of several patches produced by our security audit. It addresses the concerns mentioned here: http://groups.google.co.nz/group/rubyonrails-core/browse_thread/thread/4d43c1fa2485f3e3/e63662d7d521663e Note that you will be instructed to run 'rake db:bootstrap:session' when you first try to run Mephisto, and that your session cookie name will change in order to prevent errors about invalid cookie signatures. Thank you to Isaac for helping me track down the best way to solve this problem.
1 parent 170fe8c commit d558ba1

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.rake_tasks
22
config/database.yml
33
config/deploy.rb
4+
config/initializers/session_store.rb
45
db/*.sqlite3
56
log/*.log
67
public/assets

config/environment.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def safe_to_load_application?
2525
File.basename($0) != "rake" || !ARGV.any? {|a| a =~ /^db:/ }
2626
end
2727

28+
# Make sure we a site-specific secret key file.
29+
unless File.exists?(File.join(File.dirname(__FILE__),
30+
'initializers/session_store.rb'))
31+
raise "You need to run 'rake db:bootstrap:session' to create a secret key."
32+
end
33+
2834
Rails::Initializer.run do |config|
2935
# Settings in config/environments/* take precedence those specified here
3036

@@ -45,11 +51,6 @@ def safe_to_load_application?
4551
# (by default production uses :info, the others :debug)
4652
# config.log_level = :debug
4753

48-
# Use the database for sessions instead of the file system
49-
# (create the session table with 'rake create_sessions_table')
50-
# config.action_controller.session_store = :active_record_store
51-
config.action_controller.session = { :session_key => "_mephisto_session", :secret => "bd088a0f5b476fe5a2c02653a93ed14a95a8396829ce4e726ee77553ab6438a98d0f3e6d80fc6b120370ba047f28e09f71543ae5f842365e5070e7db51fb2cb9" }
52-
5354
# Make Active Record use UTC-base instead of local time
5455
config.active_record.default_timezone = :utc
5556

lib/tasks/bootstrap.rake

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SITE_THEME_DIR = File.join(THEME_ROOT, "site-#{(ENV['SITE_ID'] || '1')}")
33

44
namespace :db do
55
desc "Loads a schema.rb file into the database and then loads the initial database fixtures."
6-
task :bootstrap do |task_args|
6+
task :bootstrap => 'db:bootstrap:session' do |task_args|
77
mkdir_p File.join(RAILS_ROOT, 'log')
88

99
require 'rubygems' unless Object.const_defined?(:Gem)
@@ -69,5 +69,29 @@ namespace :db do
6969
FileUtils.rm_rf dir
7070
end
7171
end
72+
73+
desc "Create a secret key for use with session cookies"
74+
task :session do
75+
path = File.join(RAILS_ROOT, 'config', 'initializers', 'session_store.rb')
76+
return if File.exists?(path)
77+
File.open(path, 'w') do |f|
78+
f.write <<"EOD"
79+
# This file was generated by 'rake db:bootstrap:session', and should not be
80+
# made visible to public. Do not check it into github! If you have a
81+
# load-balancing Mephisto cluser, you will need to use the same version of
82+
# this file on each machine. And be sure to restart your server when you
83+
# modify this file.
84+
85+
# Your secret key for verifying cookie session data integrity. If you
86+
# change this key, all old sessions will become invalid! Make sure the
87+
# secret is at least 30 characters and all random, no regular words or
88+
# you'll be exposed to dictionary attacks.
89+
ActionController::Base.session = {
90+
:session_key => '_mephisto_session_2',
91+
:secret => '#{SecureRandom.hex(40)}'
92+
}
93+
EOD
94+
end
95+
end
7296
end
73-
end
97+
end

0 commit comments

Comments
 (0)