Skip to content

Instantly share code, notes, and snippets.

@mctaylorpants
Created July 30, 2017 23:35
Show Gist options
  • Save mctaylorpants/4973f06e4724bd71ac794b4bfada01b9 to your computer and use it in GitHub Desktop.
Save mctaylorpants/4973f06e4724bd71ac794b4bfada01b9 to your computer and use it in GitHub Desktop.
# actionpack/lib/action_controller/metal/request_forgery_protection.rb
# Sets the token value for the current session.
def form_authenticity_token(form_options: {})
  masked_authenticity_token(session, form_options: form_options)
end
# Creates a masked version of the authenticity token that varies
# on each request. The masking is used to mitigate SSL attacks
# like BREACH.
def masked_authenticity_token(session, form_options: {}) # :doc:
  # ...
raw_token = if per_form_csrf_tokens && action && method
  # ...
  else
  real_csrf_token(session)
  end
one_time_pad = SecureRandom.random_bytes(AUTHENTICITY_TOKEN_LENGTH)
  encrypted_csrf_token = xor_byte_strings(one_time_pad, raw_token)
  masked_token = one_time_pad + encrypted_csrf_token
  Base64.strict_encode64(masked_token)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment