| 1 |
require 'cgi' |
|---|
| 2 |
require 'cgi/session' |
|---|
| 3 |
require 'openssl' |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
class CGI::Session::CookieStore |
|---|
| 41 |
|
|---|
| 42 |
MAX = 4096 |
|---|
| 43 |
SECRET_MIN_LENGTH = 30 |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
class CookieOverflow < StandardError; end |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
class TamperedWithCookie < StandardError; end |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def initialize(session, options = {}) |
|---|
| 53 |
|
|---|
| 54 |
if options['session_key'].blank? |
|---|
| 55 |
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' |
|---|
| 56 |
end |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
ensure_secret_secure(options['secret']) |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
@session, @secret = session, options['secret'] |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
@digest = options['digest'] || 'SHA1' |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
@cookie_options = { |
|---|
| 69 |
'name' => options['session_key'], |
|---|
| 70 |
'path' => options['session_path'], |
|---|
| 71 |
'domain' => options['session_domain'], |
|---|
| 72 |
'expires' => options['session_expires'], |
|---|
| 73 |
'secure' => options['session_secure'] |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
options['no_hidden'] = true |
|---|
| 79 |
options['no_cookies'] = true |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
def ensure_secret_secure(secret) |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
return true if secret.is_a?(Proc) |
|---|
| 88 |
|
|---|
| 89 |
if secret.blank? |
|---|
| 90 |
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} |
|---|
| 91 |
end |
|---|
| 92 |
|
|---|
| 93 |
if secret.length < SECRET_MIN_LENGTH |
|---|
| 94 |
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 |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
def restore |
|---|
| 100 |
@original = read_cookie |
|---|
| 101 |
@data = unmarshal(@original) || {} |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
def update; end |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
def close |
|---|
| 109 |
if defined?(@data) && !@data.blank? |
|---|
| 110 |
updated = marshal(@data) |
|---|
| 111 |
raise CookieOverflow if updated.size > MAX |
|---|
| 112 |
write_cookie('value' => updated) unless updated == @original |
|---|
| 113 |
end |
|---|
| 114 |
end |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
def delete |
|---|
| 118 |
@data = nil |
|---|
| 119 |
clear_old_cookie_value |
|---|
| 120 |
write_cookie('value' => nil, 'expires' => 1.year.ago) |
|---|
| 121 |
end |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
def generate_digest(data) |
|---|
| 125 |
key = @secret.respond_to?(:call) ? @secret.call(@session) : @secret |
|---|
| 126 |
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@digest), key, data) |
|---|
| 127 |
end |
|---|
| 128 |
|
|---|
| 129 |
private |
|---|
| 130 |
|
|---|
| 131 |
def marshal(session) |
|---|
| 132 |
data = ActiveSupport::Base64.encode64(Marshal.dump(session)).chop |
|---|
| 133 |
CGI.escape "#{data}--#{generate_digest(data)}" |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
def unmarshal(cookie) |
|---|
| 138 |
if cookie |
|---|
| 139 |
data, digest = CGI.unescape(cookie).split('--') |
|---|
| 140 |
unless digest == generate_digest(data) |
|---|
| 141 |
delete |
|---|
| 142 |
raise TamperedWithCookie |
|---|
| 143 |
end |
|---|
| 144 |
Marshal.load(ActiveSupport::Base64.decode64(data)) |
|---|
| 145 |
end |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
def read_cookie |
|---|
| 150 |
@session.cgi.cookies[@cookie_options['name']].first |
|---|
| 151 |
end |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
def write_cookie(options) |
|---|
| 155 |
cookie = CGI::Cookie.new(@cookie_options.merge(options)) |
|---|
| 156 |
@session.cgi.send :instance_variable_set, '@output_cookies', [cookie] |
|---|
| 157 |
end |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
def clear_old_cookie_value |
|---|
| 161 |
@session.cgi.cookies[@cookie_options['name']].clear |
|---|
| 162 |
end |
|---|
| 163 |
end |
|---|