Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hungpk/8d15e53d69ce93080b9dc558d06127e6 to your computer and use it in GitHub Desktop.
Save hungpk/8d15e53d69ce93080b9dc558d06127e6 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment