2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:51:43 -04:00
|
|
|
require "active_support/core_ext/hash/keys"
|
|
|
|
require "active_support/key_generator"
|
|
|
|
require "active_support/message_verifier"
|
|
|
|
require "active_support/json"
|
|
|
|
require "rack/utils"
|
2010-04-05 04:52:47 -04:00
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
module ActionDispatch
|
2015-09-04 20:36:15 -04:00
|
|
|
class Request
|
2010-04-05 04:52:47 -04:00
|
|
|
def cookie_jar
|
2016-08-06 12:51:43 -04:00
|
|
|
fetch_header("action_dispatch.cookies".freeze) do
|
2015-08-23 19:36:43 -04:00
|
|
|
self.cookie_jar = Cookies::CookieJar.build(self, cookies)
|
|
|
|
end
|
2015-08-06 10:54:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# :stopdoc:
|
2015-12-01 14:37:20 -05:00
|
|
|
prepend Module.new {
|
|
|
|
def commit_cookie_jar!
|
|
|
|
cookie_jar.commit!
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2015-08-06 10:57:33 -04:00
|
|
|
def have_cookie_jar?
|
2016-08-06 12:51:43 -04:00
|
|
|
has_header? "action_dispatch.cookies".freeze
|
2015-08-06 10:57:33 -04:00
|
|
|
end
|
|
|
|
|
2015-08-06 10:54:47 -04:00
|
|
|
def cookie_jar=(jar)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header "action_dispatch.cookies".freeze, jar
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def key_generator
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::GENERATOR_KEY
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def signed_cookie_salt
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::SIGNED_COOKIE_SALT
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted_cookie_salt
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::ENCRYPTED_COOKIE_SALT
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted_signed_cookie_salt
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::ENCRYPTED_SIGNED_COOKIE_SALT
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 13:54:17 -05:00
|
|
|
def authenticated_encrypted_cookie_salt
|
|
|
|
get_header Cookies::AUTHENTICATED_ENCRYPTED_COOKIE_SALT
|
|
|
|
end
|
|
|
|
|
2015-08-05 20:24:48 -04:00
|
|
|
def secret_token
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::SECRET_TOKEN
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def secret_key_base
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::SECRET_KEY_BASE
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cookies_serializer
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::COOKIES_SERIALIZER
|
2015-08-05 20:24:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cookies_digest
|
2015-08-23 19:36:43 -04:00
|
|
|
get_header Cookies::COOKIES_DIGEST
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
2015-08-06 10:54:47 -04:00
|
|
|
# :startdoc:
|
2010-01-16 18:21:46 -05:00
|
|
|
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.
|
|
|
|
#
|
2013-01-03 17:33:00 -05:00
|
|
|
# Examples of writing:
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
|
|
|
# # 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"
|
|
|
|
#
|
2014-02-08 07:42:10 -05:00
|
|
|
# # Cookie values are String based. Other data types need to be serialized.
|
2014-02-08 13:00:09 -05:00
|
|
|
# cookies[:lat_lon] = JSON.generate([47.68, -122.37])
|
2010-11-26 06:11:33 -05:00
|
|
|
#
|
2010-01-16 18:21:46 -05:00
|
|
|
# # Sets a cookie that expires in 1 hour.
|
2012-10-31 15:19:44 -04:00
|
|
|
# cookies[:login] = { value: "XJ-122", expires: 1.hour.from_now }
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
2012-05-06 13:02:22 -04:00
|
|
|
# # Sets a signed cookie, which prevents users from tampering with its value.
|
2014-02-08 10:31:12 -05:00
|
|
|
# # The cookie is signed by your app's `secrets.secret_key_base` value.
|
|
|
|
# # It can be read using the signed method `cookies.signed[:name]`
|
2010-11-26 06:11:33 -05:00
|
|
|
# cookies.signed[:user_id] = current_user.id
|
2010-08-27 16:31:02 -04:00
|
|
|
#
|
2015-11-20 05:19:16 -05:00
|
|
|
# # Sets an encrypted cookie value before sending it to the client which
|
|
|
|
# # prevent users from reading and tampering with its value.
|
|
|
|
# # The cookie is signed by your app's `secrets.secret_key_base` value.
|
|
|
|
# # It can be read using the encrypted method `cookies.encrypted[:name]`
|
|
|
|
# cookies.encrypted[:discount] = 45
|
|
|
|
#
|
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"
|
|
|
|
#
|
2013-01-03 17:33:00 -05:00
|
|
|
# Examples of reading:
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
2014-02-08 13:00:09 -05:00
|
|
|
# cookies[:user_name] # => "david"
|
|
|
|
# cookies.size # => 2
|
|
|
|
# JSON.parse(cookies[:lat_lon]) # => [47.68, -122.37]
|
|
|
|
# cookies.signed[:login] # => "XJ-122"
|
2015-11-20 05:19:16 -05:00
|
|
|
# cookies.encrypted[:discount] # => 45
|
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:
|
|
|
|
#
|
2013-04-02 19:29:57 -04:00
|
|
|
# cookies[:name] = {
|
2012-10-31 15:19:44 -04:00
|
|
|
# value: 'a yummy cookie',
|
|
|
|
# expires: 1.year.from_now,
|
|
|
|
# domain: 'domain.com'
|
2010-01-16 18:21:46 -05:00
|
|
|
# }
|
|
|
|
#
|
2013-04-02 19:29:57 -04:00
|
|
|
# cookies.delete(:name, domain: 'domain.com')
|
2010-01-16 18:21:46 -05:00
|
|
|
#
|
|
|
|
# The option symbols for setting cookies are:
|
|
|
|
#
|
2014-02-08 07:42:10 -05:00
|
|
|
# * <tt>:value</tt> - The cookie's value.
|
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
|
2014-11-27 13:21:43 -05:00
|
|
|
# <tt>:all</tt> or <tt>Array</tt> again when deleting cookies.
|
2010-06-11 05:00:35 -04:00
|
|
|
#
|
2015-01-06 13:08:34 -05:00
|
|
|
# domain: nil # Does not set cookie domain. (default)
|
2012-10-31 15:19:44 -04:00
|
|
|
# domain: :all # Allow the cookie for the top most level
|
2014-02-23 04:19:15 -05:00
|
|
|
# # domain and subdomains.
|
2014-11-27 13:21:43 -05:00
|
|
|
# domain: %w(.example.com .example.org) # Allow the cookie
|
|
|
|
# # for concrete domain names.
|
2010-06-11 05:00:35 -04:00
|
|
|
#
|
2015-04-29 17:15:18 -04:00
|
|
|
# * <tt>:tld_length</tt> - When using <tt>:domain => :all</tt>, this option can be used to explicitly
|
|
|
|
# set the TLD length when using a short (<= 3 character) domain that is being interpreted as part of a TLD.
|
|
|
|
# For example, to share cookies between user1.lvh.me and user2.lvh.me, set <tt>:tld_length</tt> to 1.
|
2010-08-27 16:31:02 -04:00
|
|
|
# * <tt>:expires</tt> - The time at which this cookie expires, as a \Time object.
|
2013-06-12 08:10:02 -04:00
|
|
|
# * <tt>:secure</tt> - Whether this cookie is only transmitted to HTTPS servers.
|
2010-01-16 18:21:46 -05:00
|
|
|
# Default is +false+.
|
|
|
|
# * <tt>:httponly</tt> - Whether this cookie is accessible via scripting or
|
|
|
|
# only HTTP. Defaults to +false+.
|
|
|
|
class Cookies
|
2012-10-30 23:06:46 -04:00
|
|
|
HTTP_HEADER = "Set-Cookie".freeze
|
|
|
|
GENERATOR_KEY = "action_dispatch.key_generator".freeze
|
2012-11-01 18:02:09 -04:00
|
|
|
SIGNED_COOKIE_SALT = "action_dispatch.signed_cookie_salt".freeze
|
|
|
|
ENCRYPTED_COOKIE_SALT = "action_dispatch.encrypted_cookie_salt".freeze
|
|
|
|
ENCRYPTED_SIGNED_COOKIE_SALT = "action_dispatch.encrypted_signed_cookie_salt".freeze
|
2017-02-23 13:54:17 -05:00
|
|
|
AUTHENTICATED_ENCRYPTED_COOKIE_SALT = "action_dispatch.authenticated_encrypted_cookie_salt".freeze
|
2013-03-24 19:20:24 -04:00
|
|
|
SECRET_TOKEN = "action_dispatch.secret_token".freeze
|
|
|
|
SECRET_KEY_BASE = "action_dispatch.secret_key_base".freeze
|
2014-02-04 12:31:48 -05:00
|
|
|
COOKIES_SERIALIZER = "action_dispatch.cookies_serializer".freeze
|
2014-08-12 15:57:51 -04:00
|
|
|
COOKIES_DIGEST = "action_dispatch.cookies_digest".freeze
|
2010-07-15 17:41:29 -04:00
|
|
|
|
2012-12-30 20:50:26 -05:00
|
|
|
# Cookies can typically store 4096 bytes.
|
|
|
|
MAX_COOKIE_SIZE = 4096
|
|
|
|
|
2010-05-17 19:43:06 -04:00
|
|
|
# Raised when storing more than 4K of session data.
|
2012-04-30 07:42:25 -04:00
|
|
|
CookieOverflow = Class.new StandardError
|
2010-05-17 19:43:06 -04:00
|
|
|
|
2017-03-12 12:51:26 -04:00
|
|
|
# Include in a cookie jar to allow chaining, e.g. cookies.permanent.signed.
|
2013-03-24 19:20:24 -04:00
|
|
|
module ChainedCookieJars
|
|
|
|
# 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
|
|
|
|
# # => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
|
|
|
|
def permanent
|
2015-08-06 10:42:38 -04:00
|
|
|
@permanent ||= PermanentCookieJar.new(self)
|
2013-03-24 19:20:24 -04: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
|
2016-10-26 17:13:15 -04:00
|
|
|
# cookie was tampered with by the user (or a 3rd party), +nil+ will be returned.
|
2013-03-24 19:20:24 -04:00
|
|
|
#
|
2014-10-27 13:04:37 -04:00
|
|
|
# If +secrets.secret_key_base+ and +secrets.secret_token+ (deprecated) are both set,
|
2013-03-28 15:35:48 -04:00
|
|
|
# legacy cookies signed with the old key generator will be transparently upgraded.
|
|
|
|
#
|
2013-12-15 10:32:41 -05:00
|
|
|
# This jar requires that you set a suitable secret for the verification on your app's +secrets.secret_key_base+.
|
2013-03-24 19:20:24 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# cookies.signed[:discount] = 45
|
|
|
|
# # => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
|
|
|
|
#
|
|
|
|
# cookies.signed[:discount] # => 45
|
|
|
|
def signed
|
2013-03-28 15:35:48 -04:00
|
|
|
@signed ||=
|
2015-08-05 20:40:28 -04:00
|
|
|
if upgrade_legacy_signed_cookies?
|
2015-08-06 10:42:38 -04:00
|
|
|
UpgradeLegacySignedCookieJar.new(self)
|
2013-03-24 19:20:24 -04:00
|
|
|
else
|
2015-08-06 10:42:38 -04:00
|
|
|
SignedCookieJar.new(self)
|
2013-03-24 19:20:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a jar that'll automatically encrypt cookie values before sending them to the client and will decrypt them for read.
|
2016-10-26 17:13:15 -04:00
|
|
|
# If the cookie was tampered with by the user (or a 3rd party), +nil+ will be returned.
|
2013-03-24 19:20:24 -04:00
|
|
|
#
|
2014-10-27 13:04:37 -04:00
|
|
|
# If +secrets.secret_key_base+ and +secrets.secret_token+ (deprecated) are both set,
|
2013-03-28 15:35:48 -04:00
|
|
|
# legacy cookies signed with the old key generator will be transparently upgraded.
|
|
|
|
#
|
2017-02-23 13:54:17 -05:00
|
|
|
# If +config.action_dispatch.encrypted_cookie_salt+ and +config.action_dispatch.encrypted_signed_cookie_salt+
|
|
|
|
# are both set, legacy cookies encrypted with HMAC AES-256-CBC will be transparently upgraded.
|
|
|
|
#
|
2013-12-15 10:32:41 -05:00
|
|
|
# This jar requires that you set a suitable secret for the verification on your app's +secrets.secret_key_base+.
|
2013-03-24 19:20:24 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# cookies.encrypted[:discount] = 45
|
|
|
|
# # => Set-Cookie: discount=ZS9ZZ1R4cG1pcUJ1bm80anhQang3dz09LS1mbDZDSU5scGdOT3ltQ2dTdlhSdWpRPT0%3D--ab54663c9f4e3bc340c790d6d2b71e92f5b60315; path=/
|
|
|
|
#
|
|
|
|
# cookies.encrypted[:discount] # => 45
|
|
|
|
def encrypted
|
2013-03-28 15:35:48 -04:00
|
|
|
@encrypted ||=
|
2015-08-05 20:40:28 -04:00
|
|
|
if upgrade_legacy_signed_cookies?
|
2015-08-06 10:42:38 -04:00
|
|
|
UpgradeLegacyEncryptedCookieJar.new(self)
|
2017-02-23 13:54:17 -05:00
|
|
|
elsif upgrade_legacy_hmac_aes_cbc_cookies?
|
|
|
|
UpgradeLegacyHmacAesCbcCookieJar.new(self)
|
2013-03-28 15:35:48 -04:00
|
|
|
else
|
2015-08-06 10:42:38 -04:00
|
|
|
EncryptedCookieJar.new(self)
|
2013-03-28 15:35:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-04 00:09:38 -04:00
|
|
|
# Returns the +signed+ or +encrypted+ jar, preferring +encrypted+ if +secret_key_base+ is set.
|
2013-03-28 15:35:48 -04:00
|
|
|
# Used by ActionDispatch::Session::CookieStore to avoid the need to introduce new cookie stores.
|
|
|
|
def signed_or_encrypted
|
|
|
|
@signed_or_encrypted ||=
|
2015-08-06 10:42:38 -04:00
|
|
|
if request.secret_key_base.present?
|
2013-03-28 15:35:48 -04:00
|
|
|
encrypted
|
|
|
|
else
|
|
|
|
signed
|
|
|
|
end
|
|
|
|
end
|
2015-08-05 20:40:28 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def upgrade_legacy_signed_cookies?
|
|
|
|
request.secret_token.present? && request.secret_key_base.present?
|
|
|
|
end
|
2017-02-23 13:54:17 -05:00
|
|
|
|
|
|
|
def upgrade_legacy_hmac_aes_cbc_cookies?
|
|
|
|
request.secret_key_base.present? &&
|
|
|
|
request.authenticated_encrypted_cookie_salt.present? &&
|
|
|
|
request.encrypted_signed_cookie_salt.present? &&
|
|
|
|
request.encrypted_cookie_salt.present?
|
|
|
|
end
|
2013-03-28 15:35:48 -04:00
|
|
|
end
|
|
|
|
|
2014-08-13 16:51:53 -04:00
|
|
|
# Passing the ActiveSupport::MessageEncryptor::NullSerializer downstream
|
|
|
|
# to the Message{Encryptor,Verifier} allows us to handle the
|
|
|
|
# (de)serialization step within the cookie jar, which gives us the
|
|
|
|
# opportunity to detect and migrate legacy cookies.
|
2015-04-23 13:54:58 -04:00
|
|
|
module VerifyAndUpgradeLegacySignedMessage # :nodoc:
|
2013-03-28 15:35:48 -04:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
2015-08-06 10:42:38 -04:00
|
|
|
@legacy_verifier = ActiveSupport::MessageVerifier.new(request.secret_token, serializer: ActiveSupport::MessageEncryptor::NullSerializer)
|
2013-03-28 15:35:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify_and_upgrade_legacy_signed_message(name, signed_message)
|
2014-04-23 12:07:50 -04:00
|
|
|
deserialize(name, @legacy_verifier.verify(signed_message)).tap do |value|
|
2014-02-11 05:55:48 -05:00
|
|
|
self[name] = { value: value }
|
2013-03-28 15:35:48 -04:00
|
|
|
end
|
|
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
|
|
|
nil
|
2013-03-24 19:20:24 -04:00
|
|
|
end
|
2015-09-06 12:01:53 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def parse(name, signed_message)
|
|
|
|
super || verify_and_upgrade_legacy_signed_message(name, signed_message)
|
|
|
|
end
|
2013-03-24 19:20:24 -04:00
|
|
|
end
|
|
|
|
|
2011-04-06 19:27:05 -04:00
|
|
|
class CookieJar #:nodoc:
|
2013-03-24 19:20:24 -04:00
|
|
|
include Enumerable, ChainedCookieJars
|
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
|
|
|
|
2015-08-05 20:59:28 -04:00
|
|
|
def self.build(req, cookies)
|
2015-08-05 21:17:13 -04:00
|
|
|
new(req).tap do |hash|
|
2015-07-05 12:59:24 -04:00
|
|
|
hash.update(cookies)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-06 10:42:38 -04:00
|
|
|
attr_reader :request
|
|
|
|
|
2015-08-05 21:17:13 -04:00
|
|
|
def initialize(request)
|
2010-01-16 18:21:46 -05:00
|
|
|
@set_cookies = {}
|
|
|
|
@delete_cookies = {}
|
2015-08-05 20:40:28 -04:00
|
|
|
@request = request
|
2011-04-06 19:27:05 -04:00
|
|
|
@cookies = {}
|
2014-03-12 19:07:04 -04:00
|
|
|
@committed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def committed?; @committed; end
|
|
|
|
|
|
|
|
def commit!
|
|
|
|
@committed = true
|
|
|
|
@set_cookies.freeze
|
|
|
|
@delete_cookies.freeze
|
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
|
|
|
|
|
2013-01-27 17:17:56 -05:00
|
|
|
def fetch(name, *args, &block)
|
|
|
|
@cookies.fetch(name.to_s, *args, &block)
|
|
|
|
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
|
|
|
|
|
2015-09-01 04:20:32 -04:00
|
|
|
def update_cookies_from_jar
|
|
|
|
request_jar = @request.cookie_jar.instance_variable_get(:@cookies)
|
2016-10-28 23:05:58 -04:00
|
|
|
set_cookies = request_jar.reject { |k, _| @delete_cookies.key?(k) }
|
2015-09-01 04:20:32 -04:00
|
|
|
|
|
|
|
@cookies.update set_cookies if set_cookies
|
|
|
|
end
|
|
|
|
|
2015-07-07 14:36:18 -04:00
|
|
|
def to_header
|
2016-10-28 23:05:58 -04:00
|
|
|
@cookies.map { |k, v| "#{escape(k)}=#{escape(v)}" }.join "; "
|
2015-07-07 14:36:18 -04:00
|
|
|
end
|
|
|
|
|
2017-08-14 09:46:58 -04:00
|
|
|
def handle_options(options) # :nodoc:
|
|
|
|
if options[:expires].respond_to?(:from_now)
|
|
|
|
options[:expires] = options[:expires].from_now
|
|
|
|
end
|
|
|
|
|
2010-06-11 05:20:09 -04:00
|
|
|
options[:path] ||= "/"
|
2010-07-15 17:41:29 -04:00
|
|
|
|
2016-08-06 12:51:43 -04:00
|
|
|
if options[:domain] == :all || options[:domain] == "all"
|
2017-03-12 12:51:26 -04:00
|
|
|
# If there is a provided tld length then we use it otherwise default domain regexp.
|
2011-01-21 06:59:49 -05:00
|
|
|
domain_regexp = options[:tld_length] ? /([^.]+\.?){#{options[:tld_length]}}$/ : DOMAIN_REGEXP
|
|
|
|
|
2017-03-12 12:51:26 -04:00
|
|
|
# If host is not ip and matches domain regexp.
|
2010-12-09 10:38:52 -05:00
|
|
|
# (ip confirms to domain regexp so we explicitly check for ip)
|
2015-08-06 10:42:38 -04:00
|
|
|
options[:domain] = if (request.host !~ /^[\d.]+$/) && (request.host =~ domain_regexp)
|
2011-01-21 06:59:49 -05:00
|
|
|
".#{$&}"
|
2010-12-09 10:38:52 -05:00
|
|
|
end
|
2011-01-21 06:58:33 -05:00
|
|
|
elsif options[:domain].is_a? Array
|
2017-03-12 12:51:26 -04:00
|
|
|
# If host matches one of the supplied domains without a dot in front of it.
|
2016-08-16 03:30:11 -04:00
|
|
|
options[:domain] = options[:domain].find { |domain| request.host.include? domain.sub(/^\./, "") }
|
2010-06-11 05:20:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-09 01:19:57 -04:00
|
|
|
# Sets the cookie named +name+. The second argument may be the cookie's
|
|
|
|
# value or a hash of options as documented above.
|
2013-04-02 19:29:57 -04:00
|
|
|
def []=(name, options)
|
2010-01-16 18:21:46 -05:00
|
|
|
if options.is_a?(Hash)
|
|
|
|
options.symbolize_keys!
|
|
|
|
value = options[:value]
|
|
|
|
else
|
|
|
|
value = options
|
2016-08-06 13:35:13 -04:00
|
|
|
options = { value: value }
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2010-06-11 05:20:09 -04:00
|
|
|
handle_options(options)
|
2010-07-15 17:41:29 -04:00
|
|
|
|
2016-07-09 18:51:56 -04:00
|
|
|
if @cookies[name.to_s] != value || options[:expires]
|
2013-04-02 19:29:57 -04:00
|
|
|
@cookies[name.to_s] = value
|
|
|
|
@set_cookies[name.to_s] = options
|
|
|
|
@delete_cookies.delete(name.to_s)
|
2012-04-30 08:55:06 -04:00
|
|
|
end
|
|
|
|
|
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
|
2013-02-19 23:29:30 -05:00
|
|
|
# and the expiration date in the past. Like <tt>[]=</tt>, you can pass in
|
2010-01-16 18:21:46 -05:00
|
|
|
# an options hash to delete cookies with extra data such as a <tt>:path</tt>.
|
2013-04-02 19:29:57 -04:00
|
|
|
def delete(name, options = {})
|
|
|
|
return unless @cookies.has_key? name.to_s
|
2010-06-11 05:00:35 -04:00
|
|
|
|
2012-04-30 08:32:53 -04:00
|
|
|
options.symbolize_keys!
|
2010-06-11 05:20:09 -04:00
|
|
|
handle_options(options)
|
2010-06-11 05:00:35 -04:00
|
|
|
|
2013-04-02 19:29:57 -04:00
|
|
|
value = @cookies.delete(name.to_s)
|
|
|
|
@delete_cookies[name.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.
|
2013-04-02 19:29:57 -04:00
|
|
|
def deleted?(name, options = {})
|
2012-01-23 08:10:43 -05:00
|
|
|
options.symbolize_keys!
|
|
|
|
handle_options(options)
|
2013-04-02 19:29:57 -04:00
|
|
|
@delete_cookies[name.to_s] == options
|
2012-01-23 08:10:43 -05:00
|
|
|
end
|
|
|
|
|
2017-03-12 12:51:26 -04:00
|
|
|
# Removes all cookies on the client machine by calling <tt>delete</tt> for each cookie.
|
2011-03-29 19:46:27 -04:00
|
|
|
def clear(options = {})
|
2016-08-16 03:30:11 -04:00
|
|
|
@cookies.each_key { |k| delete(k, options) }
|
2011-03-29 19:46:27 -04:00
|
|
|
end
|
|
|
|
|
2010-05-18 11:45:05 -04:00
|
|
|
def write(headers)
|
2015-10-01 21:47:13 -04:00
|
|
|
if header = make_set_cookie_header(headers[HTTP_HEADER])
|
|
|
|
headers[HTTP_HEADER] = header
|
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
2010-10-22 10:34:45 -04:00
|
|
|
|
2017-05-31 05:16:20 -04:00
|
|
|
mattr_accessor :always_write_cookie, default: false
|
2011-11-23 15:36:56 -05:00
|
|
|
|
2010-10-22 10:34:45 -04:00
|
|
|
private
|
2015-09-24 16:31:33 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def escape(string)
|
|
|
|
::Rack::Utils.escape(string)
|
|
|
|
end
|
2016-02-16 00:58:28 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def make_set_cookie_header(header)
|
|
|
|
header = @set_cookies.inject(header) { |m, (k, v)|
|
|
|
|
if write_cookie?(v)
|
|
|
|
::Rack::Utils.add_cookie_to_header(m, k, v)
|
|
|
|
else
|
|
|
|
m
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@delete_cookies.inject(header) { |m, (k, v)|
|
|
|
|
::Rack::Utils.add_remove_cookie_to_header(m, k, v)
|
|
|
|
}
|
|
|
|
end
|
2015-09-24 16:31:33 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def write_cookie?(cookie)
|
|
|
|
request.ssl? || !cookie[:secure] || always_write_cookie
|
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:40:08 -04:00
|
|
|
class AbstractCookieJar # :nodoc:
|
2013-03-24 19:20:24 -04:00
|
|
|
include ChainedCookieJars
|
|
|
|
|
2015-08-06 10:42:38 -04:00
|
|
|
def initialize(parent_jar)
|
2012-10-30 23:06:46 -04:00
|
|
|
@parent_jar = parent_jar
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2013-04-02 19:29:57 -04:00
|
|
|
def [](name)
|
2015-09-06 11:55:50 -04:00
|
|
|
if data = @parent_jar[name.to_s]
|
|
|
|
parse name, data
|
|
|
|
end
|
2012-12-30 20:50:26 -05:00
|
|
|
end
|
|
|
|
|
2013-04-02 19:29:57 -04:00
|
|
|
def []=(name, options)
|
2010-01-16 18:21:46 -05:00
|
|
|
if options.is_a?(Hash)
|
|
|
|
options.symbolize_keys!
|
|
|
|
else
|
2015-09-06 11:42:57 -04:00
|
|
|
options = { value: options }
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:42:57 -04:00
|
|
|
commit(options)
|
2013-04-02 19:29:57 -04:00
|
|
|
@parent_jar[name] = options
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
2015-09-06 11:42:57 -04:00
|
|
|
|
2015-09-06 12:06:46 -04:00
|
|
|
protected
|
|
|
|
def request; @parent_jar.request; end
|
|
|
|
|
2015-09-06 11:42:57 -04:00
|
|
|
private
|
2017-08-14 09:46:58 -04:00
|
|
|
def expiry_options(options)
|
|
|
|
if options[:expires].respond_to?(:from_now)
|
|
|
|
{ expires_in: options[:expires] }
|
|
|
|
else
|
|
|
|
{ expires_at: options[:expires] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-06 11:55:50 -04:00
|
|
|
def parse(name, data); data; end
|
2015-09-06 11:42:57 -04:00
|
|
|
def commit(options); end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PermanentCookieJar < AbstractCookieJar # :nodoc:
|
|
|
|
private
|
|
|
|
def commit(options)
|
|
|
|
options[:expires] = 20.years.from_now
|
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:54:58 -04:00
|
|
|
class JsonSerializer # :nodoc:
|
2014-02-04 12:31:48 -05:00
|
|
|
def self.load(value)
|
2014-08-17 15:40:24 -04:00
|
|
|
ActiveSupport::JSON.decode(value)
|
2014-02-04 12:31:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.dump(value)
|
2014-08-17 15:40:24 -04:00
|
|
|
ActiveSupport::JSON.encode(value)
|
2014-02-04 12:31:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-23 13:54:58 -04:00
|
|
|
module SerializedCookieJars # :nodoc:
|
2014-02-09 04:12:11 -05:00
|
|
|
MARSHAL_SIGNATURE = "\x04\x08".freeze
|
|
|
|
|
2014-02-04 12:31:48 -05:00
|
|
|
protected
|
2014-02-09 04:12:11 -05:00
|
|
|
def needs_migration?(value)
|
2015-08-06 10:42:38 -04:00
|
|
|
request.cookies_serializer == :hybrid && value.start_with?(MARSHAL_SIGNATURE)
|
2014-02-09 04:12:11 -05:00
|
|
|
end
|
|
|
|
|
2014-12-24 08:15:55 -05:00
|
|
|
def serialize(value)
|
2014-02-09 04:12:11 -05:00
|
|
|
serializer.dump(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deserialize(name, value)
|
|
|
|
if value
|
|
|
|
if needs_migration?(value)
|
2014-02-11 06:56:35 -05:00
|
|
|
Marshal.load(value).tap do |v|
|
|
|
|
self[name] = { value: v }
|
2014-02-11 05:55:48 -05:00
|
|
|
end
|
2014-02-09 04:12:11 -05:00
|
|
|
else
|
|
|
|
serializer.load(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-04 12:31:48 -05:00
|
|
|
def serializer
|
2015-08-06 10:42:38 -04:00
|
|
|
serializer = request.cookies_serializer || :marshal
|
2014-02-04 12:31:48 -05:00
|
|
|
case serializer
|
|
|
|
when :marshal
|
|
|
|
Marshal
|
2014-02-09 04:12:11 -05:00
|
|
|
when :json, :hybrid
|
2014-02-04 12:31:48 -05:00
|
|
|
JsonSerializer
|
|
|
|
else
|
|
|
|
serializer
|
|
|
|
end
|
|
|
|
end
|
2014-08-12 15:57:51 -04:00
|
|
|
|
|
|
|
def digest
|
2016-08-06 12:51:43 -04:00
|
|
|
request.cookies_digest || "SHA1"
|
2014-08-12 15:57:51 -04:00
|
|
|
end
|
2015-09-06 12:19:34 -04:00
|
|
|
|
|
|
|
def key_generator
|
|
|
|
request.key_generator
|
|
|
|
end
|
2014-02-04 12:31:48 -05:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:46:18 -04:00
|
|
|
class SignedCookieJar < AbstractCookieJar # :nodoc:
|
2014-02-04 12:31:48 -05:00
|
|
|
include SerializedCookieJars
|
2013-03-24 19:20:24 -04:00
|
|
|
|
2015-08-06 10:42:38 -04:00
|
|
|
def initialize(parent_jar)
|
2015-09-06 11:46:18 -04:00
|
|
|
super
|
2015-08-05 20:40:28 -04:00
|
|
|
secret = key_generator.generate_key(request.signed_cookie_salt)
|
2014-08-17 14:23:14 -04:00
|
|
|
@verifier = ActiveSupport::MessageVerifier.new(secret, digest: digest, serializer: ActiveSupport::MessageEncryptor::NullSerializer)
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:55:50 -04:00
|
|
|
private
|
|
|
|
def parse(name, signed_message)
|
2015-09-06 11:56:53 -04:00
|
|
|
deserialize name, @verifier.verified(signed_message)
|
2010-01-17 22:30:38 -05:00
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
|
2015-09-06 11:46:18 -04:00
|
|
|
def commit(options)
|
2017-08-14 09:46:58 -04:00
|
|
|
options[:value] = @verifier.generate(serialize(options[:value]), expiry_options(options))
|
2010-01-16 18:21:46 -05:00
|
|
|
|
2015-09-06 11:46:18 -04:00
|
|
|
raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
|
|
|
|
end
|
2013-03-24 19:20:24 -04:00
|
|
|
end
|
2010-01-16 18:21:46 -05:00
|
|
|
|
2013-03-24 19:20:24 -04:00
|
|
|
# UpgradeLegacySignedCookieJar is used instead of SignedCookieJar if
|
2014-10-27 13:04:37 -04:00
|
|
|
# secrets.secret_token and secrets.secret_key_base are both set. It reads
|
2015-04-22 04:09:38 -04:00
|
|
|
# legacy cookies signed with the old dummy key generator and signs and
|
|
|
|
# re-saves them using the new key generator to provide a smooth upgrade path.
|
2013-03-24 19:20:24 -04:00
|
|
|
class UpgradeLegacySignedCookieJar < SignedCookieJar #:nodoc:
|
2013-03-28 15:35:48 -04:00
|
|
|
include VerifyAndUpgradeLegacySignedMessage
|
2010-01-16 18:21:46 -05:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:49:12 -04:00
|
|
|
class EncryptedCookieJar < AbstractCookieJar # :nodoc:
|
2014-02-04 12:31:48 -05:00
|
|
|
include SerializedCookieJars
|
2013-03-24 19:20:24 -04:00
|
|
|
|
2016-07-09 18:51:56 -04:00
|
|
|
def initialize(parent_jar)
|
|
|
|
super
|
2015-08-05 21:08:05 -04:00
|
|
|
|
2013-04-02 19:41:57 -04:00
|
|
|
if ActiveSupport::LegacyKeyGenerator === key_generator
|
2017-01-12 03:39:16 -05:00
|
|
|
raise "You didn't set secrets.secret_key_base, which is required for this cookie jar. " \
|
2013-03-28 15:35:48 -04:00
|
|
|
"Read the upgrade documentation to learn more about this new config option."
|
2012-11-01 22:43:24 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 13:54:17 -05:00
|
|
|
cipher = "aes-256-gcm"
|
|
|
|
key_len = ActiveSupport::MessageEncryptor.key_len(cipher)
|
|
|
|
secret = key_generator.generate_key(request.authenticated_encrypted_cookie_salt || "")[0, key_len]
|
|
|
|
|
|
|
|
@encryptor = ActiveSupport::MessageEncryptor.new(secret, cipher: cipher, serializer: ActiveSupport::MessageEncryptor::NullSerializer)
|
2012-10-30 14:41:11 -04:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:55:50 -04:00
|
|
|
private
|
|
|
|
def parse(name, encrypted_message)
|
2015-09-06 11:59:10 -04:00
|
|
|
deserialize name, @encryptor.decrypt_and_verify(encrypted_message)
|
|
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveSupport::MessageEncryptor::InvalidMessage
|
|
|
|
nil
|
2012-10-30 14:41:11 -04:00
|
|
|
end
|
|
|
|
|
2015-09-06 11:49:12 -04:00
|
|
|
def commit(options)
|
2017-08-14 09:46:58 -04:00
|
|
|
options[:value] = @encryptor.encrypt_and_sign(serialize(options[:value]), expiry_options(options))
|
2012-10-30 14:41:11 -04:00
|
|
|
|
2015-09-06 11:49:12 -04:00
|
|
|
raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
|
|
|
|
end
|
2013-03-28 15:35:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# UpgradeLegacyEncryptedCookieJar is used by ActionDispatch::Session::CookieStore
|
2014-10-27 13:04:37 -04:00
|
|
|
# instead of EncryptedCookieJar if secrets.secret_token and secrets.secret_key_base
|
2013-03-28 15:35:48 -04:00
|
|
|
# are both set. It reads legacy cookies signed with the old dummy key generator and
|
|
|
|
# encrypts and re-saves them using the new key generator to provide a smooth upgrade path.
|
|
|
|
class UpgradeLegacyEncryptedCookieJar < EncryptedCookieJar #:nodoc:
|
|
|
|
include VerifyAndUpgradeLegacySignedMessage
|
2012-10-30 14:41:11 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 13:54:17 -05:00
|
|
|
# UpgradeLegacyHmacAesCbcCookieJar is used by ActionDispatch::Session::CookieStore
|
|
|
|
# to upgrade cookies encrypted with AES-256-CBC with HMAC to AES-256-GCM
|
|
|
|
class UpgradeLegacyHmacAesCbcCookieJar < EncryptedCookieJar
|
|
|
|
def initialize(parent_jar)
|
|
|
|
super
|
|
|
|
|
|
|
|
secret = key_generator.generate_key(request.encrypted_cookie_salt || "")[0, ActiveSupport::MessageEncryptor.key_len]
|
|
|
|
sign_secret = key_generator.generate_key(request.encrypted_signed_cookie_salt || "")
|
|
|
|
|
2017-06-02 15:51:10 -04:00
|
|
|
@legacy_encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, cipher: "aes-256-cbc", digest: digest, serializer: ActiveSupport::MessageEncryptor::NullSerializer)
|
2017-02-23 13:54:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def decrypt_and_verify_legacy_encrypted_message(name, signed_message)
|
|
|
|
deserialize(name, @legacy_encryptor.decrypt_and_verify(signed_message)).tap do |value|
|
|
|
|
self[name] = { value: value }
|
|
|
|
end
|
|
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveSupport::MessageEncryptor::InvalidMessage
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def parse(name, signed_message)
|
|
|
|
super || decrypt_and_verify_legacy_encrypted_message(name, signed_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2015-08-06 10:57:33 -04:00
|
|
|
request = ActionDispatch::Request.new env
|
|
|
|
|
2010-01-16 18:21:46 -05:00
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
|
2015-08-06 10:57:33 -04:00
|
|
|
if request.have_cookie_jar?
|
|
|
|
cookie_jar = request.cookie_jar
|
2014-03-12 19:07:04 -04:00
|
|
|
unless cookie_jar.committed?
|
|
|
|
cookie_jar.write(headers)
|
|
|
|
if headers[HTTP_HEADER].respond_to?(:join)
|
|
|
|
headers[HTTP_HEADER] = headers[HTTP_HEADER].join("\n")
|
|
|
|
end
|
2010-05-18 11:45:05 -04:00
|
|
|
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
|