Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor Devise.secure_compare #3579

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/devise.rb
Expand Up @@ -472,11 +472,11 @@ def self.friendly_token
# constant-time comparison algorithm to prevent timing attacks
def self.secure_compare(a, b)
return false if a.blank? || b.blank? || a.bytesize != b.bytesize
l = a.unpack "C#{a.bytesize}"
l = a.each_byte

res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
b.each_byte.inject(0) do |res, byte|
res | byte ^ l.next
end.zero?
end
end

Expand Down
1 change: 1 addition & 0 deletions test/devise_test.rb
Expand Up @@ -91,6 +91,7 @@ class DeviseTest < ActiveSupport::TestCase
assert_not Devise.secure_compare(empty, empty)
end
assert_not Devise.secure_compare("size_1", "size_four")
assert Devise.secure_compare("size_four", "size_four")
end

test 'Devise.email_regexp should match valid email addresses' do
Expand Down