Skip to content

Commit 605ba70

Browse files
authored
Merge pull request rails#30709 from mikeycgto/actiondispatch-use-aead-encrypted-cookies-patch
Fixes for use_authenticated_cookie_encryption
2 parents fbcc4bf + 0fb6b2d commit 605ba70

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

actionpack/lib/action_dispatch/middleware/cookies.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,16 @@ class EncryptedKeyRotatingCookieJar < AbstractCookieJar # :nodoc:
599599
def initialize(parent_jar)
600600
super
601601

602-
key_len = ActiveSupport::MessageEncryptor.key_len(encrypted_cookie_cipher)
603-
secret = request.key_generator.generate_key(request.authenticated_encrypted_cookie_salt, key_len)
604-
@encryptor = ActiveSupport::MessageEncryptor.new(secret, cipher: encrypted_cookie_cipher, serializer: SERIALIZER)
602+
if request.use_authenticated_cookie_encryption
603+
key_len = ActiveSupport::MessageEncryptor.key_len(encrypted_cookie_cipher)
604+
secret = request.key_generator.generate_key(request.authenticated_encrypted_cookie_salt, key_len)
605+
@encryptor = ActiveSupport::MessageEncryptor.new(secret, cipher: encrypted_cookie_cipher, serializer: SERIALIZER)
606+
else
607+
key_len = ActiveSupport::MessageEncryptor.key_len("aes-256-cbc")
608+
secret = request.key_generator.generate_key(request.encrypted_cookie_salt, key_len)
609+
sign_secret = request.key_generator.generate_key(request.encrypted_signed_cookie_salt)
610+
@encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, cipher: "aes-256-cbc", serializer: SERIALIZER)
611+
end
605612

606613
request.cookies_rotations.encrypted.each do |*secrets, **options|
607614
@encryptor.rotate(*secrets, serializer: SERIALIZER, **options)

actionpack/test/dispatch/cookies_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,24 @@ def test_legacy_signed_cookie_is_treated_as_nil_by_encrypted_cookie_jar_if_tampe
899899
assert_nil @response.cookies["foo"]
900900
end
901901

902+
def test_use_authenticated_cookie_encryption_uses_legacy_hmac_aes_cbc_encrypiton
903+
@request.env["action_dispatch.use_authenticated_cookie_encryption"] = false
904+
905+
key_generator = @request.env["action_dispatch.key_generator"]
906+
encrypted_cookie_salt = @request.env["action_dispatch.encrypted_cookie_salt"]
907+
encrypted_signed_cookie_salt = @request.env["action_dispatch.encrypted_signed_cookie_salt"]
908+
secret = key_generator.generate_key(encrypted_cookie_salt, ActiveSupport::MessageEncryptor.key_len("aes-256-cbc"))
909+
sign_secret = key_generator.generate_key(encrypted_signed_cookie_salt)
910+
encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, cipher: "aes-256-cbc", digest: "SHA1", serializer: Marshal)
911+
912+
get :set_encrypted_cookie
913+
914+
cookies = @controller.send :cookies
915+
assert_not_equal "bar", cookies[:foo]
916+
assert_equal "bar", cookies.encrypted[:foo]
917+
assert_equal "bar", encryptor.decrypt_and_verify(@response.cookies["foo"])
918+
end
919+
902920
def test_legacy_hmac_aes_cbc_encrypted_marshal_cookie_is_upgraded_to_authenticated_encrypted_cookie
903921
key_generator = @request.env["action_dispatch.key_generator"]
904922
encrypted_cookie_salt = @request.env["action_dispatch.encrypted_cookie_salt"]

0 commit comments

Comments
 (0)