Last active
January 29, 2018 09:44
-
-
Save chumakoff/c76fffbe19ab5bf70291f0085ce881f7 to your computer and use it in GitHub Desktop.
Unmask Rails Form Authenticity Token (CSRF Token) to get the original token stored in session[:_csrf_token]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unmask_authenticity_token(authenticity_token) | |
# this must be the same as ActionController::RequestForgeryProtection::AUTHENTICITY_TOKEN_LENGTH | |
token_length = 32 | |
masked_token = Base64.strict_decode64(authenticity_token) | |
one_time_pad = masked_token[0...token_length] | |
encrypted_csrf_token = masked_token[token_length..-1] | |
bytes = encrypted_csrf_token.bytes | |
one_time_pad.each_byte.with_index { |b, i| bytes[i] ^= b } | |
Base64.strict_encode64(bytes.pack("C*")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment