Use secure compare.

This commit is contained in:
José Valim
2011-02-15 11:16:31 +01:00
parent d8dedec1f8
commit 4803d5d6b1
2 changed files with 12 additions and 1 deletions

View File

@@ -290,6 +290,17 @@ module Devise
def self.friendly_token
ActiveSupport::SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
end
# constant-time comparison algorithm to prevent timing attacks
def self.secure_compare(a, b)
return false unless a.present? && b.present?
return false unless a.bytesize == b.bytesize
l = a.unpack "C#{a.bytesize}"
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end
end
require 'warden'

View File

@@ -44,7 +44,7 @@ module Devise
# Verifies whether an incoming_password (ie from sign in) is the user password.
def valid_password?(incoming_password)
password_digest(incoming_password) == self.encrypted_password
Devise.secure_compare(password_digest(incoming_password), self.encrypted_password)
end
# Set password and password confirmation to nil