2011-06-05 07:03:04 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
|
|
|
require 'active_support/core_ext/hash/keys'
|
2010-04-05 04:52:47 -04:00
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
module ActionDispatch
|
|
|
|
class Request
|
2010-04-05 04:52:47 -04:00
|
|
|
def cookie_jar
|
|
|
|
env['action_dispatch.cookies'] ||= Cookies::CookieJar.build(self)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-27 16:31:02 -04:00
|
|
|
# \Cookies are read and written through ActionController#cookies.
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
|
|
|
# The cookies being read are the ones received along with the request, the cookies
|
|
|
|
# being written will be sent out with the response. Reading a cookie does not get
|
|
|
|
# the cookie object itself back, just the value it holds.
|
|
|
|
#
|
|
|
|
# Examples for writing:
|
|
|
|
#
|
|
|
|
# # Sets a simple session cookie.
|
2010-11-26 06:11:33 -05:00
|
|
|
# # This cookie will be deleted when the user's browser is closed.
|
2010-01-16 18:21:46 -05:00
|
|
|
# cookies[:user_name] = "david"
|
|
|
|
#
|
2010-11-26 06:11:33 -05:00
|
|
|
# # Assign an array of values to a cookie.
|
|
|
|
# cookies[:lat_lon] = [47.68, -122.37]
|
|
|
|
#
|
2010-01-16 18:21:46 -05:00
|
|
|
# # Sets a cookie that expires in 1 hour.
|
|
|
|
# cookies[:login] = { :value => "XJ-122", :expires => 1.hour.from_now }
|
|
|
|
#
|
2010-08-27 16:31:02 -04:00
|
|
|
# # Sets a signed cookie, which prevents a user from tampering with its value.
|
2010-11-26 06:11:33 -05:00
|
|
|
# # The cookie is signed by your app's <tt>config.secret_token</tt> value.
|
|
|
|
# # Rails generates this value by default when you create a new Rails app.
|
|
|
|
# cookies.signed[:user_id] = current_user.id
|
2010-08-27 16:31:02 -04:00
|
|
|
#
|
|
|
|
# # Sets a "permanent" cookie (which expires in 20 years from now).
|
|
|
|
# cookies.permanent[:login] = "XJ-122"
|
2010-11-26 06:11:33 -05:00
|
|
|
#
|
2010-08-27 16:31:02 -04:00
|
|
|
# # You can also chain these methods:
|
|
|
|
# cookies.permanent.signed[:login] = "XJ-122"
|
|
|
|
#
|
2010-01-16 18:21:46 -05:00
|
|
|
# Examples for reading:
|
|
|
|
#
|
|
|
|
# cookies[:user_name] # => "david"
|
|
|
|
# cookies.size # => 2
|
2010-11-26 06:11:33 -05:00
|
|
|
# cookies[:lat_lon] # => [47.68, -122.37]
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
|
|
|
# Example for deleting:
|
|
|
|
#
|
|
|
|
# cookies.delete :user_name
|
|
|
|
#
|
|
|
|
# Please note that if you specify a :domain when setting a cookie, you must also specify the domain when deleting the cookie:
|
|
|
|
#
|
|
|
|
# cookies[:key] = {
|
|
|
|
# :value => 'a yummy cookie',
|
|
|
|
# :expires => 1.year.from_now,
|
|
|
|
# :domain => 'domain.com'
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# cookies.delete(:key, :domain => 'domain.com')
|
|
|
|
#
|
|
|
|
# The option symbols for setting cookies are:
|
|
|
|
#
|
|
|
|
# * <tt>:value</tt> - The cookie's value or list of values (as an array).
|
2011-05-23 19:22:33 -04:00
|
|
|
# * <tt>:path</tt> - The path for which this cookie applies. Defaults to the root
|
2010-01-16 18:21:46 -05:00
|
|
|
# of the application.
|
2010-07-15 17:41:29 -04:00
|
|
|
# * <tt>:domain</tt> - The domain for which this cookie applies so you can
|
|
|
|
# restrict to the domain level. If you use a schema like www.example.com
|
2010-06-11 05:00:35 -04:00
|
|
|
# and want to share session with user.example.com set <tt>:domain</tt>
|
2010-07-15 17:41:29 -04:00
|
|
|
# to <tt>:all</tt>. Make sure to specify the <tt>:domain</tt> option with
|
2010-06-11 05:08:03 -04:00
|
|
|
# <tt>:all</tt> again when deleting keys.
|
2010-06-11 05:00:35 -04:00
|
|
|
#
|
|
|
|
# :domain => nil # Does not sets cookie domain. (default)
|
|
|
|
# :domain => :all # Allow the cookie for the top most level
|
|
|
|
# domain and subdomains.
|
|
|
|
#
|
2010-08-27 16:31:02 -04:00
|
|
|
# * <tt>:expires</tt> - The time at which this cookie expires, as a \Time object.
|
2010-01-16 18:21:46 -05:00
|
|
|
# * <tt>:secure</tt> - Whether this cookie is a only transmitted to HTTPS servers.
|
|
|
|
# Default is +false+.
|
|
|
|
# * <tt>:httponly</tt> - Whether this cookie is accessible via scripting or
|
|
|
|
# only HTTP. Defaults to +false+.
|
|
|
|
class Cookies
|
2010-05-18 11:45:05 -04:00
|
|
|
HTTP_HEADER = "Set-Cookie".freeze
|
|
|
|
TOKEN_KEY = "action_dispatch.secret_token".freeze
|
2010-07-15 17:41:29 -04:00
|
|
|
|
2010-05-17 19:43:06 -04:00
|
|
|
# Raised when storing more than 4K of session data.
|
|
|
|
class CookieOverflow < StandardError; end
|
|
|
|
|
2011-04-06 19:27:05 -04:00
|
|
|
class CookieJar #:nodoc:
|
2011-09-01 16:37:14 -04:00
|
|
|
include Enumerable
|
2010-06-11 05:00:35 -04:00
|
|
|
|
2010-08-14 15:35:01 -04:00
|
|
|
# This regular expression is used to split the levels of a domain.
|
|
|
|
# The top level domain can be any string without a period or
|
|
|
|
# **.**, ***.** style TLDs like co.uk or com.au
|
|
|
|
#
|
|
|
|
# www.example.co.uk gives:
|
2011-01-21 06:59:49 -05:00
|
|
|
# $& => example.co.uk
|
2010-08-14 15:35:01 -04:00
|
|
|
#
|
|
|
|
# example.com gives:
|
2011-01-21 06:59:49 -05:00
|
|
|
# $& => example.com
|
2010-08-14 15:35:01 -04:00
|
|
|
#
|
|
|
|
# lots.of.subdomains.example.local gives:
|
2011-01-21 06:59:49 -05:00
|
|
|
# $& => example.local
|
|
|
|
DOMAIN_REGEXP = /[^.]*\.([^.]*|..\...|...\...)$/
|
2010-06-11 05:00:35 -04:00
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
def self.build(request)
|
2010-05-18 11:45:05 -04:00
|
|
|
secret = request.env[TOKEN_KEY]
|
2010-08-14 15:35:01 -04:00
|
|
|
host = request.host
|
2010-10-22 10:34:45 -04:00
|
|
|
secure = request.ssl?
|
2010-06-11 05:20:09 -04:00
|
|
|
|
2010-10-22 10:34:45 -04:00
|
|
|
new(secret, host, secure).tap do |hash|
|
2010-01-16 18:21:46 -05:00
|
|
|
hash.update(request.cookies)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 10:34:45 -04:00
|
|
|
def initialize(secret = nil, host = nil, secure = false)
|
2010-04-05 04:52:47 -04:00
|
|
|
@secret = secret
|
2010-01-16 18:21:46 -05:00
|
|
|
@set_cookies = {}
|
|
|
|
@delete_cookies = {}
|
2010-06-11 05:20:09 -04:00
|
|
|
@host = host
|
2010-10-22 10:34:45 -04:00
|
|
|
@secure = secure
|
2011-04-06 11:05:58 -04:00
|
|
|
@closed = false
|
2011-04-06 19:27:05 -04:00
|
|
|
@cookies = {}
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2011-09-01 16:37:14 -04:00
|
|
|
def each(&block)
|
|
|
|
@cookies.each(&block)
|
|
|
|
end
|
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
# Returns the value of the cookie by +name+, or +nil+ if no such cookie exists.
|
|
|
|
def [](name)
|
2011-04-06 19:27:05 -04:00
|
|
|
@cookies[name.to_s]
|
|
|
|
end
|
|
|
|
|
2011-06-30 07:01:26 -04:00
|
|
|
def key?(name)
|
|
|
|
@cookies.key?(name.to_s)
|
|
|
|
end
|
|
|
|
alias :has_key? :key?
|
|
|
|
|
2011-04-06 19:27:05 -04:00
|
|
|
def update(other_hash)
|
2011-06-05 07:03:04 -04:00
|
|
|
@cookies.update other_hash.stringify_keys
|
2011-04-06 19:27:05 -04:00
|
|
|
self
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2010-06-11 05:20:09 -04:00
|
|
|
def handle_options(options) #:nodoc:
|
|
|
|
options[:path] ||= "/"
|
2010-07-15 17:41:29 -04:00
|
|
|
|
2010-06-11 05:20:09 -04:00
|
|
|
if options[:domain] == :all
|
2011-01-21 06:59:49 -05:00
|
|
|
# if there is a provided tld length then we use it otherwise default domain regexp
|
|
|
|
domain_regexp = options[:tld_length] ? /([^.]+\.?){#{options[:tld_length]}}$/ : DOMAIN_REGEXP
|
|
|
|
|
2010-12-09 10:38:52 -05:00
|
|
|
# if host is not ip and matches domain regexp
|
|
|
|
# (ip confirms to domain regexp so we explicitly check for ip)
|
2011-01-21 06:59:49 -05:00
|
|
|
options[:domain] = if (@host !~ /^[\d.]+$/) && (@host =~ domain_regexp)
|
|
|
|
".#{$&}"
|
2010-12-09 10:38:52 -05:00
|
|
|
end
|
2011-01-21 06:58:33 -05:00
|
|
|
elsif options[:domain].is_a? Array
|
|
|
|
# if host matches one of the supplied domains without a dot in front of it
|
|
|
|
options[:domain] = options[:domain].find {|domain| @host.include? domain[/^\.?(.*)$/, 1] }
|
2010-06-11 05:20:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
# Sets the cookie named +name+. The second argument may be the very cookie
|
|
|
|
# value, or a hash of options as documented above.
|
|
|
|
def []=(key, options)
|
|
|
|
if options.is_a?(Hash)
|
|
|
|
options.symbolize_keys!
|
|
|
|
value = options[:value]
|
|
|
|
else
|
|
|
|
value = options
|
|
|
|
options = { :value => value }
|
|
|
|
end
|
|
|
|
|
2011-10-18 14:57:55 -04:00
|
|
|
@cookies[key.to_s] = value
|
2010-01-16 18:21:46 -05:00
|
|
|
|
2010-06-11 05:20:09 -04:00
|
|
|
handle_options(options)
|
2010-07-15 17:41:29 -04:00
|
|
|
|
2011-05-19 09:49:16 -04:00
|
|
|
@set_cookies[key.to_s] = options
|
|
|
|
@delete_cookies.delete(key.to_s)
|
2010-01-16 18:21:46 -05:00
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Removes the cookie on the client machine by setting the value to an empty string
|
|
|
|
# and setting its expiration date into the past. Like <tt>[]=</tt>, you can pass in
|
|
|
|
# an options hash to delete cookies with extra data such as a <tt>:path</tt>.
|
|
|
|
def delete(key, options = {})
|
|
|
|
options.symbolize_keys!
|
2010-06-11 05:00:35 -04:00
|
|
|
|
2010-06-11 05:20:09 -04:00
|
|
|
handle_options(options)
|
2010-06-11 05:00:35 -04:00
|
|
|
|
2011-04-06 19:27:05 -04:00
|
|
|
value = @cookies.delete(key.to_s)
|
2011-05-19 09:49:16 -04:00
|
|
|
@delete_cookies[key.to_s] = options
|
2010-01-16 18:21:46 -05:00
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2012-01-23 08:10:43 -05:00
|
|
|
# Whether the given cookie is to be deleted by this CookieJar.
|
|
|
|
# Like <tt>[]=</tt>, you can pass in an options hash to test if a
|
|
|
|
# deletion applies to a specific <tt>:path</tt>, <tt>:domain</tt> etc.
|
|
|
|
def deleted?(key, options = {})
|
|
|
|
options.symbolize_keys!
|
|
|
|
handle_options(options)
|
|
|
|
@delete_cookies[key.to_s] == options
|
|
|
|
end
|
|
|
|
|
2011-03-29 19:46:27 -04:00
|
|
|
# Removes all cookies on the client machine by calling <tt>delete</tt> for each cookie
|
|
|
|
def clear(options = {})
|
|
|
|
@cookies.each_key{ |k| delete(k, options) }
|
|
|
|
end
|
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
# Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now. Example:
|
|
|
|
#
|
|
|
|
# cookies.permanent[:prefers_open_id] = true
|
|
|
|
# # => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
|
|
|
|
#
|
|
|
|
# This jar is only meant for writing. You'll read permanent cookies through the regular accessor.
|
|
|
|
#
|
|
|
|
# This jar allows chaining with the signed jar as well, so you can set permanent, signed cookies. Examples:
|
|
|
|
#
|
|
|
|
# cookies.permanent.signed[:remember_me] = current_user.id
|
2010-07-15 17:41:29 -04:00
|
|
|
# # => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
|
2010-01-16 18:21:46 -05:00
|
|
|
def permanent
|
2010-04-05 04:52:47 -04:00
|
|
|
@permanent ||= PermanentCookieJar.new(self, @secret)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from
|
|
|
|
# the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed
|
|
|
|
# cookie was tampered with by the user (or a 3rd party), an ActiveSupport::MessageVerifier::InvalidSignature exception will
|
|
|
|
# be raised.
|
|
|
|
#
|
2010-04-05 04:52:47 -04:00
|
|
|
# This jar requires that you set a suitable secret for the verification on your app's config.secret_token.
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# cookies.signed[:discount] = 45
|
|
|
|
# # => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
|
|
|
|
#
|
|
|
|
# cookies.signed[:discount] # => 45
|
|
|
|
def signed
|
2010-04-05 04:52:47 -04:00
|
|
|
@signed ||= SignedCookieJar.new(self, @secret)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2010-05-18 11:45:05 -04:00
|
|
|
def write(headers)
|
2010-10-22 10:34:45 -04:00
|
|
|
@set_cookies.each { |k, v| ::Rack::Utils.set_cookie_header!(headers, k, v) if write_cookie?(v) }
|
2010-05-18 11:45:05 -04:00
|
|
|
@delete_cookies.each { |k, v| ::Rack::Utils.delete_cookie_header!(headers, k, v) }
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
2010-10-22 10:34:45 -04:00
|
|
|
|
2011-06-05 07:34:27 -04:00
|
|
|
def recycle! #:nodoc:
|
2011-03-29 19:46:27 -04:00
|
|
|
@set_cookies.clear
|
|
|
|
@delete_cookies.clear
|
|
|
|
end
|
|
|
|
|
2011-11-23 15:36:56 -05:00
|
|
|
mattr_accessor :always_write_cookie
|
|
|
|
self.always_write_cookie = false
|
|
|
|
|
2010-10-22 10:34:45 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def write_cookie?(cookie)
|
2011-11-23 15:36:56 -05:00
|
|
|
@secure || !cookie[:secure] || always_write_cookie
|
2010-10-22 10:34:45 -04:00
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class PermanentCookieJar < CookieJar #:nodoc:
|
2010-04-05 04:52:47 -04:00
|
|
|
def initialize(parent_jar, secret)
|
|
|
|
@parent_jar, @secret = parent_jar, secret
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, options)
|
|
|
|
if options.is_a?(Hash)
|
|
|
|
options.symbolize_keys!
|
|
|
|
else
|
|
|
|
options = { :value => options }
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:expires] = 20.years.from_now
|
|
|
|
@parent_jar[key] = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def signed
|
2010-04-05 04:52:47 -04:00
|
|
|
@signed ||= SignedCookieJar.new(self, @secret)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *arguments, &block)
|
|
|
|
@parent_jar.send(method, *arguments, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SignedCookieJar < CookieJar #:nodoc:
|
2010-05-17 19:43:06 -04:00
|
|
|
MAX_COOKIE_SIZE = 4096 # Cookies can typically store 4096 bytes.
|
|
|
|
SECRET_MIN_LENGTH = 30 # Characters
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
def initialize(parent_jar, secret)
|
2010-05-17 19:43:06 -04:00
|
|
|
ensure_secret_secure(secret)
|
2010-01-16 18:21:46 -05:00
|
|
|
@parent_jar = parent_jar
|
2010-04-05 04:52:47 -04:00
|
|
|
@verifier = ActiveSupport::MessageVerifier.new(secret)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](name)
|
2010-04-04 13:16:27 -04:00
|
|
|
if signed_message = @parent_jar[name]
|
|
|
|
@verifier.verify(signed_message)
|
2010-01-17 22:30:38 -05:00
|
|
|
end
|
2010-05-17 19:43:06 -04:00
|
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
|
|
|
nil
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, options)
|
|
|
|
if options.is_a?(Hash)
|
|
|
|
options.symbolize_keys!
|
|
|
|
options[:value] = @verifier.generate(options[:value])
|
|
|
|
else
|
|
|
|
options = { :value => @verifier.generate(options) }
|
|
|
|
end
|
|
|
|
|
2010-05-17 19:43:06 -04:00
|
|
|
raise CookieOverflow if options[:value].size > MAX_COOKIE_SIZE
|
2010-01-16 18:21:46 -05:00
|
|
|
@parent_jar[key] = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *arguments, &block)
|
|
|
|
@parent_jar.send(method, *arguments, &block)
|
|
|
|
end
|
2010-05-17 19:43:06 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def ensure_secret_secure(secret)
|
|
|
|
if secret.blank?
|
|
|
|
raise ArgumentError, "A secret is required to generate an " +
|
|
|
|
"integrity hash for cookie session data. Use " +
|
|
|
|
"config.secret_token = \"some secret phrase of at " +
|
|
|
|
"least #{SECRET_MIN_LENGTH} characters\"" +
|
2010-11-26 06:11:33 -05:00
|
|
|
"in config/initializers/secret_token.rb"
|
2010-05-17 19:43:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if secret.length < SECRET_MIN_LENGTH
|
|
|
|
raise ArgumentError, "Secret should be something secure, " +
|
2011-05-23 19:53:18 -04:00
|
|
|
"like \"#{SecureRandom.hex(16)}\". The value you " +
|
2010-05-17 19:43:06 -04:00
|
|
|
"provided, \"#{secret}\", is shorter than the minimum length " +
|
|
|
|
"of #{SECRET_MIN_LENGTH} characters"
|
|
|
|
end
|
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2011-04-06 11:05:58 -04:00
|
|
|
cookie_jar = nil
|
2010-01-16 18:21:46 -05:00
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
|
|
|
|
if cookie_jar = env['action_dispatch.cookies']
|
2010-05-18 11:45:05 -04:00
|
|
|
cookie_jar.write(headers)
|
|
|
|
if headers[HTTP_HEADER].respond_to?(:join)
|
|
|
|
headers[HTTP_HEADER] = headers[HTTP_HEADER].join("\n")
|
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
2010-05-18 11:45:05 -04:00
|
|
|
|
|
|
|
[status, headers, body]
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|