Class | CGI::Session::CookieStore |
In: |
vendor/rails/actionpack/lib/action_controller/session/cookie_store.rb
|
Parent: | Object |
This cookie-based session store is the Rails default. Sessions typically contain at most a user_id and flash message; both fit within the 4K cookie size limit. Cookie-based sessions are dramatically faster than the alternatives.
If you have more than 4K of session data or don‘t want your data to be visible to the user, pick another session store.
CookieOverflow is raised if you attempt to store more than 4K of data. TamperedWithCookie is raised if the data integrity check fails.
A message digest is included with the cookie to ensure data integrity: a user cannot alter his user_id without knowing the secret key included in the hash. New apps are generated with a pregenerated secret in config/environment.rb. Set your own for old apps you‘re upgrading.
Session options:
:secret An application-wide key string or block returning a string called per generated digest. The block is called with the CGI::Session instance as an argument. It's important that the secret is not vulnerable to a dictionary attack. Therefore, you should choose a secret consisting of random numbers and letters and more than 30 characters. Example: :secret => '449fe2e7daee471bffae2fd8dc02313d' :secret => Proc.new { User.current_user.secret_key } :digest The message digest algorithm used to verify session integrity defaults to 'SHA1' but may be any digest provided by OpenSSL, such as 'MD5', 'RIPEMD160', 'SHA256', etc.
To generate a secret key for an existing application, run `rake secret` and set the key in config/environment.rb
Note that changing digest or secret invalidates all existing sessions!
MAX | = | 4096 | Cookies can typically store 4096 bytes. | |
SECRET_MIN_LENGTH | = | 30 |
Called from CGI::Session only.
# File vendor/rails/actionpack/lib/action_controller/session/cookie_store.rb, line 53 53: def initialize(session, options = {}) 54: # The session_key option is required. 55: if options['session_key'].blank? 56: raise ArgumentError, 'A session_key is required to write a cookie containing the session data. Use config.action_controller.session = { :session_key => "_myapp_session", :secret => "some secret phrase" } in config/environment.rb' 57: end 58: 59: # The secret option is required. 60: ensure_secret_secure(options['secret']) 61: 62: # Keep the session and its secret on hand so we can read and write cookies. 63: @session, @secret = session, options['secret'] 64: 65: # Message digest defaults to SHA1. 66: @digest = options['digest'] || 'SHA1' 67: 68: # Default cookie options derived from session settings. 69: @cookie_options = { 70: 'name' => options['session_key'], 71: 'path' => options['session_path'], 72: 'domain' => options['session_domain'], 73: 'expires' => options['session_expires'], 74: 'secure' => options['session_secure'] 75: } 76: 77: # Set no_hidden and no_cookies since the session id is unused and we 78: # set our own data cookie. 79: options['no_hidden'] = true 80: options['no_cookies'] = true 81: end
Write the session data cookie if it was loaded and has changed.
# File vendor/rails/actionpack/lib/action_controller/session/cookie_store.rb, line 109 109: def close 110: if defined?(@data) && !@data.blank? 111: updated = marshal(@data) 112: raise CookieOverflow if updated.size > MAX 113: write_cookie('value' => updated) unless updated == @original 114: end 115: end
Delete the session data by setting an expired cookie with no data.
# File vendor/rails/actionpack/lib/action_controller/session/cookie_store.rb, line 118 118: def delete 119: @data = nil 120: clear_old_cookie_value 121: write_cookie('value' => '', 'expires' => 1.year.ago) 122: end
To prevent users from using something insecure like "Password" we make sure that the secret they‘ve provided is at least 30 characters in length.
# File vendor/rails/actionpack/lib/action_controller/session/cookie_store.rb, line 85 85: def ensure_secret_secure(secret) 86: # There's no way we can do this check if they've provided a proc for the 87: # secret. 88: return true if secret.is_a?(Proc) 89: 90: if secret.blank? 91: raise ArgumentError, %Q{A secret is required to generate an integrity hash for cookie session data. Use config.action_controller.session = { :session_key => "_myapp_session", :secret => "some secret phrase of at least #{SECRET_MIN_LENGTH} characters" } in config/environment.rb} 92: end 93: 94: if secret.length < SECRET_MIN_LENGTH 95: raise ArgumentError, %Q{Secret should be something secure, like "#{CGI::Session.generate_unique_id}". The value you provided, "#{secret}", is shorter than the minimum length of #{SECRET_MIN_LENGTH} characters} 96: end 97: end
Generate the HMAC keyed message digest. Uses SHA1 by default.
# File vendor/rails/actionpack/lib/action_controller/session/cookie_store.rb, line 125 125: def generate_digest(data) 126: key = @secret.respond_to?(:call) ? @secret.call(@session) : @secret 127: OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@digest), key, data) 128: end