Skip to content

A platform for community discussion. Free, open, simple.

FIX: Do not error when json-serialized cookies are used (#16522)

David Taylor1551eaab01da
diff --git a/lib/auth/default_current_user_provider.rb b/lib/auth/default_current_user_provider.rb
index 31a68790eaf..8f10226254a 100644
--- a/lib/auth/default_current_user_provider.rb
+++ b/lib/auth/default_current_user_provider.rb
@@ -90,7 +90,7 @@ class Auth::DefaultCurrentUserProvider
request = ActionDispatch::Request.new(env)
# don't even initialize a cookie jar if we don't have a cookie at all
if request.cookies[TOKEN_COOKIE].present?
- request.cookie_jar.encrypted[TOKEN_COOKIE]
+ request.cookie_jar.encrypted[TOKEN_COOKIE]&.with_indifferent_access
end
end
end
diff --git a/spec/lib/auth/default_current_user_provider_spec.rb b/spec/lib/auth/default_current_user_provider_spec.rb
index 7fd7d4a3ca9..ef5fe47dc02 100644
--- a/spec/lib/auth/default_current_user_provider_spec.rb
+++ b/spec/lib/auth/default_current_user_provider_spec.rb
@@ -738,4 +738,22 @@ describe Auth::DefaultCurrentUserProvider do
env = { "HTTP_COOKIE" => "_t=#{cookie}", "REMOTE_ADDR" => ip }
expect(provider('/', env).current_user).to eq(nil)
end
+
+ it "copes with json-serialized auth cookies" do
+ # We're switching to :json during the Rails 7 upgrade, but we want a clean revert path
+ # back to Rails 6 if needed
+
+ @provider = provider('/', { # The upcoming default
+ ActionDispatch::Cookies::COOKIES_SERIALIZER => :json,
+ method: "GET",
+ })
+ @provider.log_on_user(user, {}, @provider.cookie_jar)
+ cookie = @provider.cookie_jar["_t"]
+
+ ip = "10.0.0.1"
+ env = { "HTTP_COOKIE" => "_t=#{cookie}", "REMOTE_ADDR" => ip }
+ provider2 = provider('/', env)
+ expect(provider2.current_user).to eq(user)
+ expect(provider2.cookie_jar.encrypted["_t"].keys).to include("user_id", "token") # (strings)
+ end
end