2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "stringio"
|
|
|
|
require "active_support/key_generator"
|
2017-09-23 17:18:01 -04:00
|
|
|
require "active_support/messages/rotation_configuration"
|
2007-02-21 04:17:38 -05:00
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class CookieStoreTest < ActionDispatch::IntegrationTest
|
2016-08-06 12:54:50 -04:00
|
|
|
SessionKey = "_myapp_session"
|
|
|
|
SessionSecret = "b3c631c314c0bbca50c1b2843150fe33"
|
2017-11-12 15:19:33 -05:00
|
|
|
SessionSalt = "authenticated encrypted cookie"
|
|
|
|
|
|
|
|
Generator = ActiveSupport::KeyGenerator.new(SessionSecret, iterations: 1000)
|
2017-09-23 17:18:01 -04:00
|
|
|
Rotations = ActiveSupport::Messages::RotationConfiguration.new
|
2007-11-21 16:31:45 -05:00
|
|
|
|
2017-12-03 13:12:06 -05:00
|
|
|
Encryptor = ActiveSupport::MessageEncryptor.new(
|
2017-11-12 15:19:33 -05:00
|
|
|
Generator.generate_key(SessionSalt, 32), cipher: "aes-256-gcm", serializer: Marshal
|
2017-12-03 13:12:06 -05:00
|
|
|
)
|
2007-11-21 16:31:45 -05:00
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
def no_session_access
|
|
|
|
head :ok
|
|
|
|
end
|
2007-02-21 04:17:38 -05:00
|
|
|
|
2008-12-18 12:33:53 -05:00
|
|
|
def persistent_session_id
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: session[:session_id]
|
2008-12-18 12:33:53 -05:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def set_session_value
|
|
|
|
session[:foo] = "bar"
|
2017-11-12 15:19:33 -05:00
|
|
|
render body: nil
|
2017-08-14 09:46:58 -04:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def get_session_value
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: "foo: #{session[:foo].inspect}"
|
2008-12-15 17:33:31 -05:00
|
|
|
end
|
2007-02-21 04:17:38 -05:00
|
|
|
|
2009-02-07 00:15:39 -05:00
|
|
|
def get_session_id
|
2019-12-17 16:44:59 -05:00
|
|
|
render plain: "id: #{request.session.id&.public_id}"
|
2009-02-07 00:15:39 -05:00
|
|
|
end
|
|
|
|
|
2012-09-07 19:00:28 -04:00
|
|
|
def get_class_after_reset_session
|
|
|
|
reset_session
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: "class: #{session.class}"
|
2012-09-07 19:00:28 -04:00
|
|
|
end
|
|
|
|
|
2010-07-01 18:04:33 -04:00
|
|
|
def call_session_clear
|
|
|
|
session.clear
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2008-12-20 15:37:51 -05:00
|
|
|
def call_reset_session
|
|
|
|
reset_session
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def raise_data_overflow
|
2016-08-06 12:54:50 -04:00
|
|
|
session[:foo] = "bye!" * 1024
|
2008-12-15 17:33:31 -05:00
|
|
|
head :ok
|
|
|
|
end
|
2010-07-18 06:51:03 -04:00
|
|
|
|
|
|
|
def change_session_id
|
2015-03-12 10:52:38 -04:00
|
|
|
request.session.options[:id] = nil
|
2010-07-18 06:51:03 -04:00
|
|
|
get_session_id
|
|
|
|
end
|
|
|
|
|
2011-05-04 14:12:27 -04:00
|
|
|
def renew_session_id
|
|
|
|
request.session_options[:renew] = true
|
|
|
|
head :ok
|
|
|
|
end
|
2007-02-21 04:17:38 -05:00
|
|
|
end
|
|
|
|
|
2017-11-12 15:19:33 -05:00
|
|
|
def parse_cookie_from_header
|
|
|
|
cookie_matches = headers["Set-Cookie"].match(/#{SessionKey}=([^;]+)/)
|
|
|
|
cookie_matches && cookie_matches[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_session_cookie(cookie_string, contents)
|
|
|
|
assert_includes headers["Set-Cookie"], cookie_string
|
|
|
|
|
|
|
|
session_value = parse_cookie_from_header
|
|
|
|
session_data = Encryptor.decrypt_and_verify(Rack::Utils.unescape(session_value)) rescue nil
|
|
|
|
|
|
|
|
assert_not_nil session_data, "session failed to decrypt"
|
|
|
|
assert_equal session_data.slice(*contents.keys), contents
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def test_setting_session_value
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
assert_response :success
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; HttpOnly", "foo" => "bar"
|
2010-05-17 19:43:06 -04:00
|
|
|
end
|
2007-03-03 03:18:30 -05:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def test_getting_session_value
|
|
|
|
with_test_route_set do
|
2017-11-12 15:19:33 -05:00
|
|
|
get "/set_session_value"
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_value"
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
assert_response :success
|
|
|
|
assert_equal 'foo: "bar"', response.body
|
2010-05-17 19:43:06 -04:00
|
|
|
end
|
2007-02-25 11:35:24 -05:00
|
|
|
end
|
|
|
|
|
2009-02-07 00:15:39 -05:00
|
|
|
def test_getting_session_id
|
|
|
|
with_test_route_set do
|
2017-11-12 15:19:33 -05:00
|
|
|
get "/set_session_value"
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/persistent_session_id"
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2009-02-07 00:15:39 -05:00
|
|
|
assert_response :success
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 32, response.body.size
|
2009-02-07 00:15:39 -05:00
|
|
|
session_id = response.body
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_id"
|
2009-02-07 00:15:39 -05:00
|
|
|
assert_response :success
|
2010-06-22 09:55:50 -04:00
|
|
|
assert_equal "id: #{session_id}", response.body, "should be able to read session id without accessing the session hash"
|
2009-02-07 00:15:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def test_disregards_tampered_sessions
|
|
|
|
with_test_route_set do
|
2017-11-12 15:19:33 -05:00
|
|
|
encryptor = ActiveSupport::MessageEncryptor.new("A" * 32, cipher: "aes-256-gcm", serializer: Marshal)
|
|
|
|
|
2019-09-03 19:49:36 -04:00
|
|
|
cookies[SessionKey] = encryptor.encrypt_and_sign({ "foo" => "bar", "session_id" => "abc" })
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_value"
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo: nil", response.body
|
2007-03-03 03:18:30 -05:00
|
|
|
end
|
|
|
|
end
|
2010-09-24 20:15:52 -04:00
|
|
|
|
2010-09-13 17:29:25 -04:00
|
|
|
def test_does_not_set_secure_cookies_over_http
|
2016-08-06 13:35:13 -04:00
|
|
|
with_test_route_set(secure: true) do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
2010-09-13 17:29:25 -04:00
|
|
|
assert_response :success
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil headers["Set-Cookie"]
|
2010-09-13 17:29:25 -04:00
|
|
|
end
|
|
|
|
end
|
2010-09-24 20:15:52 -04:00
|
|
|
|
2011-05-04 14:12:27 -04:00
|
|
|
def test_properly_renew_cookies
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
|
|
|
get "/persistent_session_id"
|
2011-05-04 14:12:27 -04:00
|
|
|
session_id = response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/renew_session_id"
|
|
|
|
get "/persistent_session_id"
|
2011-05-04 14:12:27 -04:00
|
|
|
assert_not_equal response.body, session_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-13 17:29:25 -04:00
|
|
|
def test_does_set_secure_cookies_over_https
|
2016-08-06 13:35:13 -04:00
|
|
|
with_test_route_set(secure: true) do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value", headers: { "HTTPS" => "on" }
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2010-09-13 17:29:25 -04:00
|
|
|
assert_response :success
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; secure; HttpOnly", "foo" => "bar"
|
2010-09-13 17:29:25 -04:00
|
|
|
end
|
|
|
|
end
|
2007-03-03 03:18:30 -05:00
|
|
|
|
2010-06-27 14:35:31 -04:00
|
|
|
# {:foo=>#<SessionAutoloadTest::Foo bar:"baz">, :session_id=>"ce8b0752a6ab7c7af3cdb8a80e6b9e46"}
|
2017-11-12 15:19:33 -05:00
|
|
|
EncryptedSerializedCookie = "9RZ2Fij0qLveUwM4s+CCjGqhpjyUC8jiBIf/AiBr9M3TB8xh2vQZtvSOMfN3uf6oYbbpIDHAcOFIEl69FcW1ozQYeSrCLonYCazoh34ZdYskIQfGwCiSYleVXG1OD9Z4jFqeVArw4Ewm0paOOPLbN1rc6A==--I359v/KWdZ1ok0ey--JFFhuPOY7WUo6tB/eP05Aw=="
|
2010-06-27 14:35:31 -04:00
|
|
|
|
|
|
|
def test_deserializes_unloaded_classes_on_get_id
|
|
|
|
with_test_route_set do
|
|
|
|
with_autoload_path "session_autoload_test" do
|
2017-11-12 15:19:33 -05:00
|
|
|
cookies[SessionKey] = EncryptedSerializedCookie
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_id"
|
2010-06-27 14:35:31 -04:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "id: ce8b0752a6ab7c7af3cdb8a80e6b9e46", response.body, "should auto-load unloaded class"
|
2010-06-27 14:35:31 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
end
|
|
|
|
|
2010-06-27 14:35:31 -04:00
|
|
|
def test_deserializes_unloaded_classes_on_get_value
|
|
|
|
with_test_route_set do
|
2010-08-14 01:13:00 -04:00
|
|
|
with_autoload_path "session_autoload_test" do
|
2017-11-12 15:19:33 -05:00
|
|
|
cookies[SessionKey] = EncryptedSerializedCookie
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_value"
|
2010-06-27 14:35:31 -04:00
|
|
|
assert_response :success
|
|
|
|
assert_equal 'foo: #<SessionAutoloadTest::Foo bar:"baz">', response.body, "should auto-load unloaded class"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-03 03:18:30 -05:00
|
|
|
def test_close_raises_when_data_overflows
|
2008-12-15 17:33:31 -05:00
|
|
|
with_test_route_set do
|
2010-05-17 19:43:06 -04:00
|
|
|
assert_raise(ActionDispatch::Cookies::CookieOverflow) {
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/raise_data_overflow"
|
2008-12-15 17:33:31 -05:00
|
|
|
}
|
2008-09-16 12:22:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def test_doesnt_write_session_cookie_if_session_is_not_accessed
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/no_session_access"
|
2008-12-15 17:33:31 -05:00
|
|
|
assert_response :success
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil headers["Set-Cookie"]
|
2008-09-16 12:22:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def test_doesnt_write_session_cookie_if_session_is_unchanged
|
|
|
|
with_test_route_set do
|
2017-01-12 03:39:16 -05:00
|
|
|
cookies[SessionKey] = "BAh7BjoIZm9vIghiYXI%3D--" \
|
2008-12-15 17:33:31 -05:00
|
|
|
"fef868465920f415f2c0652d6910d3af288a0367"
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/no_session_access"
|
2008-12-15 17:33:31 -05:00
|
|
|
assert_response :success
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil headers["Set-Cookie"]
|
2007-03-14 07:33:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-20 15:37:51 -05:00
|
|
|
def test_setting_session_value_after_session_reset
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
2008-12-20 15:37:51 -05:00
|
|
|
assert_response :success
|
2008-12-25 07:10:28 -05:00
|
|
|
session_payload = response.body
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; HttpOnly", "foo" => "bar"
|
2008-12-20 15:37:51 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/call_reset_session"
|
2008-12-20 15:37:51 -05:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_not_equal [], headers["Set-Cookie"]
|
2012-09-07 19:00:28 -04:00
|
|
|
assert_not_nil session_payload
|
2008-12-20 15:37:51 -05:00
|
|
|
assert_not_equal session_payload, cookies[SessionKey]
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_value"
|
2008-12-20 15:37:51 -05:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo: nil", response.body
|
2008-12-20 15:37:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-07 19:00:28 -04:00
|
|
|
def test_class_type_after_session_reset
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
2012-09-07 19:00:28 -04:00
|
|
|
assert_response :success
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; HttpOnly", "foo" => "bar"
|
2012-09-07 19:00:28 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_class_after_reset_session"
|
2012-09-07 19:00:28 -04:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_not_equal [], headers["Set-Cookie"]
|
|
|
|
assert_equal "class: ActionDispatch::Request::Session", response.body
|
2012-09-07 19:00:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-22 09:55:50 -04:00
|
|
|
def test_getting_from_nonexistent_session
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_value"
|
2010-06-22 09:55:50 -04:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo: nil", response.body
|
|
|
|
assert_nil headers["Set-Cookie"], "should only create session on write, not read"
|
2010-06-22 09:55:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-01 18:04:33 -04:00
|
|
|
def test_setting_session_value_after_session_clear
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
2010-07-01 18:04:33 -04:00
|
|
|
assert_response :success
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; HttpOnly", "foo" => "bar"
|
2010-07-01 18:04:33 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/call_session_clear"
|
2010-07-01 18:04:33 -04:00
|
|
|
assert_response :success
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/get_session_value"
|
2010-07-01 18:04:33 -04:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo: nil", response.body
|
2010-07-01 18:04:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-18 12:33:53 -05:00
|
|
|
def test_persistent_session_id
|
|
|
|
with_test_route_set do
|
2017-11-12 15:19:33 -05:00
|
|
|
get "/set_session_value"
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/persistent_session_id"
|
2008-12-18 12:33:53 -05:00
|
|
|
assert_response :success
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 32, response.body.size
|
2008-12-18 12:33:53 -05:00
|
|
|
session_id = response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/persistent_session_id"
|
2008-12-18 12:33:53 -05:00
|
|
|
assert_equal session_id, response.body
|
|
|
|
reset!
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/persistent_session_id"
|
2008-12-18 12:33:53 -05:00
|
|
|
assert_not_equal session_id, response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-18 06:51:03 -04:00
|
|
|
def test_setting_session_id_to_nil_is_respected
|
|
|
|
with_test_route_set do
|
2017-11-12 15:19:33 -05:00
|
|
|
get "/set_session_value"
|
2010-07-18 06:51:03 -04:00
|
|
|
get "/get_session_id"
|
|
|
|
sid = response.body
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 36, sid.size
|
2010-07-18 06:51:03 -04:00
|
|
|
|
|
|
|
get "/change_session_id"
|
|
|
|
assert_not_equal sid, response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-28 00:05:07 -05:00
|
|
|
def test_session_store_with_expire_after
|
2016-08-06 13:35:13 -04:00
|
|
|
with_test_route_set(expire_after: 5.hours) do
|
2009-01-28 00:05:07 -05:00
|
|
|
# First request accesses the session
|
|
|
|
time = Time.local(2008, 4, 24)
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2015-08-21 16:33:50 -04:00
|
|
|
Time.stub :now, time do
|
2020-01-10 17:23:10 -05:00
|
|
|
expected_expiry = (time + 5.hours).gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2009-01-28 00:05:07 -05:00
|
|
|
|
2017-11-12 15:19:33 -05:00
|
|
|
get "/set_session_value"
|
2009-01-28 00:05:07 -05:00
|
|
|
|
2015-08-21 16:33:50 -04:00
|
|
|
assert_response :success
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; expires=#{expected_expiry}; HttpOnly", "foo" => "bar"
|
2015-08-21 16:33:50 -04:00
|
|
|
end
|
2009-01-28 00:05:07 -05:00
|
|
|
|
|
|
|
# Second request does not access the session
|
2017-11-12 15:19:33 -05:00
|
|
|
time = time + 3.hours
|
2015-08-21 16:33:50 -04:00
|
|
|
Time.stub :now, time do
|
2020-01-10 17:23:10 -05:00
|
|
|
expected_expiry = (time + 5.hours).gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2009-01-28 00:05:07 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/no_session_access"
|
2017-11-12 15:19:33 -05:00
|
|
|
|
2015-08-21 16:33:50 -04:00
|
|
|
assert_response :success
|
2017-11-12 15:19:33 -05:00
|
|
|
assert_session_cookie "path=/; expires=#{expected_expiry}; HttpOnly", "foo" => "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_session_store_with_expire_after_does_not_accept_expired_session
|
|
|
|
with_test_route_set(expire_after: 5.hours) do
|
|
|
|
# First request accesses the session
|
|
|
|
time = Time.local(2017, 11, 12)
|
|
|
|
|
|
|
|
Time.stub :now, time do
|
2020-01-10 17:23:10 -05:00
|
|
|
expected_expiry = (time + 5.hours).gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2009-01-28 00:05:07 -05:00
|
|
|
|
2017-11-12 15:19:33 -05:00
|
|
|
get "/set_session_value"
|
|
|
|
get "/get_session_value"
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'foo: "bar"', response.body
|
|
|
|
assert_session_cookie "path=/; expires=#{expected_expiry}; HttpOnly", "foo" => "bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Second request is beyond the expiry time and the session is invalidated
|
|
|
|
time += 5.hours + 1.minute
|
|
|
|
|
|
|
|
Time.stub :now, time do
|
|
|
|
get "/get_session_value"
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "foo: nil", response.body
|
2015-08-21 16:33:50 -04:00
|
|
|
end
|
2009-01-28 00:05:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-11 01:55:39 -04:00
|
|
|
def test_session_store_with_explicit_domain
|
2016-08-06 13:35:13 -04:00
|
|
|
with_test_route_set(domain: "example.es") do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
|
|
|
assert_match(/domain=example\.es/, headers["Set-Cookie"])
|
|
|
|
headers["Set-Cookie"]
|
2010-06-11 01:55:39 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-11 03:21:12 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
def test_session_store_without_domain
|
2010-06-11 01:55:39 -04:00
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
|
|
|
assert_no_match(/domain\=/, headers["Set-Cookie"])
|
2010-06-11 01:55:39 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-11 03:21:12 -04:00
|
|
|
|
2010-06-11 01:55:39 -04:00
|
|
|
def test_session_store_with_nil_domain
|
2016-08-06 13:35:13 -04:00
|
|
|
with_test_route_set(domain: nil) do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
|
|
|
assert_no_match(/domain\=/, headers["Set-Cookie"])
|
2010-06-11 01:55:39 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-11 03:21:12 -04:00
|
|
|
|
2010-06-11 01:55:39 -04:00
|
|
|
def test_session_store_with_all_domains
|
2016-08-06 13:35:13 -04:00
|
|
|
with_test_route_set(domain: :all) do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/set_session_value"
|
|
|
|
assert_match(/domain=\.example\.com/, headers["Set-Cookie"])
|
2010-06-11 01:55:39 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-11 03:21:12 -04:00
|
|
|
|
2007-02-21 04:17:38 -05:00
|
|
|
private
|
2010-05-17 19:43:06 -04:00
|
|
|
# Overwrite get to send SessionSecret in env hash
|
2019-09-13 11:54:33 -04:00
|
|
|
def get(path, **options)
|
|
|
|
options[:headers] ||= {}
|
|
|
|
options[:headers].tap do |config|
|
2017-11-12 15:19:33 -05:00
|
|
|
config["action_dispatch.secret_key_base"] = SessionSecret
|
|
|
|
config["action_dispatch.authenticated_encrypted_cookie_salt"] = SessionSalt
|
|
|
|
config["action_dispatch.use_authenticated_cookie_encryption"] = true
|
|
|
|
|
|
|
|
config["action_dispatch.key_generator"] ||= Generator
|
|
|
|
config["action_dispatch.cookies_rotations"] ||= Rotations
|
|
|
|
end
|
2017-09-23 17:18:01 -04:00
|
|
|
|
2019-09-13 11:54:33 -04:00
|
|
|
super
|
2010-05-17 19:43:06 -04:00
|
|
|
end
|
|
|
|
|
2009-09-26 13:56:53 -04:00
|
|
|
def with_test_route_set(options = {})
|
2008-12-15 17:33:31 -05:00
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2016-03-01 03:48:53 -05:00
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 13:35:13 -04:00
|
|
|
get ":action", to: ::CookieStoreTest::TestController
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2008-12-15 17:33:31 -05:00
|
|
|
end
|
2010-05-17 19:43:06 -04:00
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
options = { key: SessionKey }.merge!(options)
|
2010-05-17 19:43:06 -04:00
|
|
|
|
|
|
|
@app = self.class.build_app(set) do |middleware|
|
|
|
|
middleware.use ActionDispatch::Session::CookieStore, options
|
2015-08-07 18:35:39 -04:00
|
|
|
middleware.delete ActionDispatch::ShowExceptions
|
2010-05-17 19:43:06 -04:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
yield
|
2007-02-21 04:17:38 -05:00
|
|
|
end
|
|
|
|
end
|
2007-03-03 08:54:54 -05:00
|
|
|
end
|