Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
Use secure_compare when checking CSRF token
Browse files Browse the repository at this point in the history
Since string comparisions may return early we want to use a constant
time comparsion function to protect the CSRF token against timing
attacks. Rack::Utils provides a such function.
  • Loading branch information
jeltz authored and Zachary Scott committed Jul 26, 2016
1 parent 0bf1125 commit d8068e8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rack/protection/authenticity_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def accepts?(env)
session = session env
token = session[:csrf] ||= session['_csrf_token'] || random_string
safe?(env) ||
env['HTTP_X_CSRF_TOKEN'] == token ||
Request.new(env).params[options[:authenticity_param]] == token
secure_compare(env['HTTP_X_CSRF_TOKEN'], token) ||
secure_compare(Request.new(env).params[options[:authenticity_param]], token)
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/rack/protection/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rack/protection'
require 'rack/utils'
require 'digest'
require 'logger'
require 'uri'
Expand Down Expand Up @@ -110,6 +111,10 @@ def encrypt(value)
options[:encryptor].hexdigest value.to_s
end

def secure_compare(a, b)
Rack::Utils.secure_compare(a.to_s, b.to_s)
end

alias default_reaction deny

def html?(headers)
Expand Down

0 comments on commit d8068e8

Please sign in to comment.