class Slimer::Web::CsrfProtection
Constants
- TOKEN_LENGTH
Public Class Methods
new(app, options = nil)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 36 def initialize(app, options = nil) @app = app end
Public Instance Methods
call(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 40 def call(env) accept?(env) ? admit(env) : deny(env) end
Private Instance Methods
accept?(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 72 def accept?(env) return true if safe?(env) giventoken = ::Rack::Request.new(env).params["authenticity_token"] valid_token?(env, giventoken) end
admit(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 46 def admit(env) # On each successful request, we create a fresh masked token # which will be used in any forms rendered for this request. s = session(env) s[:csrf] ||= SecureRandom.base64(TOKEN_LENGTH) env[:csrf_token] = mask_token(s[:csrf]) @app.call(env) end
compare_with_real_token(token, local)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 143 def compare_with_real_token(token, local) ::Rack::Utils.secure_compare(token.to_s, decode_token(local).to_s) end
decode_token(token)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 147 def decode_token(token) Base64.strict_decode64(token) end
deny(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 63 def deny(env) logger(env).warn "attack prevented by #{self.class}" [403, {"Content-Type" => "text/plain"}, ["Forbidden"]] end
logger(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 59 def logger(env) @logger ||= (env["rack.logger"] || ::Logger.new(env["rack.errors"])) end
mask_token(token)
click to toggle source
Creates a masked version of the authenticity token that varies on each request. The masking is used to mitigate SSL attacks like BREACH.
# File lib/slimer/web/csfr_protection.rb, line 117 def mask_token(token) token = decode_token(token) one_time_pad = SecureRandom.random_bytes(token.length) encrypted_token = xor_byte_strings(one_time_pad, token) masked_token = one_time_pad + encrypted_token Base64.strict_encode64(masked_token) end
masked_token?(token)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 139 def masked_token?(token) token.length == TOKEN_LENGTH * 2 end
safe?(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 55 def safe?(env) %w[GET HEAD OPTIONS TRACE].include? env["REQUEST_METHOD"] end
session(env)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 68 def session(env) env["rack.session"] || fail("you need to set up a session middleware *before* #{self.class}") end
unmask_token(masked_token)
click to toggle source
Essentially the inverse of mask_token
.
# File lib/slimer/web/csfr_protection.rb, line 126 def unmask_token(masked_token) # Split the token into the one-time pad and the encrypted # value and decrypt it token_length = masked_token.length / 2 one_time_pad = masked_token[0...token_length] encrypted_token = masked_token[token_length..-1] xor_byte_strings(one_time_pad, encrypted_token) end
unmasked_token?(token)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 135 def unmasked_token?(token) token.length == TOKEN_LENGTH end
valid_token?(env, giventoken)
click to toggle source
Checks that the token given to us as a parameter matches the token stored in the session.
# File lib/slimer/web/csfr_protection.rb, line 83 def valid_token?(env, giventoken) return false if giventoken.nil? || giventoken.empty? begin token = decode_token(giventoken) rescue ArgumentError # client input is invalid return false end sess = session(env) localtoken = sess[:csrf] # Checks that Rack::Session::Cookie actualy contains the csrf toekn return false if localtoken.nil? # Rotate the session token after every use sess[:csrf] = SecureRandom.base64(TOKEN_LENGTH) # See if it's actually a masked token or not. We should be able # to handle any unmasked tokens that we've issued without error. if unmasked_token?(token) compare_with_real_token token, localtoken elsif masked_token?(token) unmasked = unmask_token(token) compare_with_real_token unmasked, localtoken else false # Token is malformed end end
xor_byte_strings(s1, s2)
click to toggle source
# File lib/slimer/web/csfr_protection.rb, line 151 def xor_byte_strings(s1, s2) s1.bytes.zip(s2.bytes).map { |(c1, c2)| c1 ^ c2 }.pack("c*") end