Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save inopinatus/e523f36b468f94cf6d34410b73fef15e to your computer and use it in GitHub Desktop.
Save inopinatus/e523f36b468f94cf6d34410b73fef15e to your computer and use it in GitHub Desktop.
Decrypt Rails 5.2 session cookies
require 'cgi'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base = Rails.application.secret_key_base)
cookie = CGI::unescape(cookie)
salt = 'authenticated encrypted cookie'
encrypted_cookie_cipher = 'aes-256-gcm'
serializer = ActiveSupport::MessageEncryptor::NullSerializer
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000)
key_len = ActiveSupport::MessageEncryptor.key_len(encrypted_cookie_cipher)
secret = key_generator.generate_key(salt, key_len)
encryptor = ActiveSupport::MessageEncryptor.new(secret, cipher: encrypted_cookie_cipher, serializer: serializer)
encryptor.decrypt_and_verify(cookie)
end
@wildjcrt
Copy link

@inopinatus
Copy link
Author

@wildjcrt Thank you. I wish you prosperity and a long life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment