Skip to content

Commit

Permalink
Use secure_compare to compare passwords and tokens
Browse files Browse the repository at this point in the history
It's unlikely there is an explotable attack here given than network
latencies and variability will swamp any local timing differences but
it's best practice and there's no reason not to.
  • Loading branch information
tomhughes committed Nov 7, 2023
1 parent 5819048 commit 55a05d9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/password_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def self.check(hash, salt, candidate)
if Argon2::HashFormat.valid_hash?(hash)
Argon2::Password.verify_password(candidate, hash)
elsif salt.nil?
hash == Digest::MD5.hexdigest(candidate)
ActiveSupport::SecurityUtils.secure_compare(hash, Digest::MD5.hexdigest(candidate))
elsif salt.include?("!")
algorithm, iterations, salt = salt.split("!")
size = Base64.strict_decode64(hash).length
hash == pbkdf2(candidate, salt, iterations.to_i, size, algorithm)
ActiveSupport::SecurityUtils.secure_compare(hash, pbkdf2(candidate, salt, iterations.to_i, size, algorithm))
else
hash == Digest::MD5.hexdigest(salt + candidate)
ActiveSupport::SecurityUtils.secure_compare(hash, Digest::MD5.hexdigest(salt + candidate))
end
end

Expand Down
2 changes: 1 addition & 1 deletion script/deliver-message
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ else
exit 0
end

exit 0 unless ActiveSupport::SecurityUtils.secure_compare(token, digest[0, 6])
exit 0 unless from.active?
exit 0 unless token == digest[0, 6]
exit 0 if date < 1.month.ago

message&.update(:message_read => true)
Expand Down

0 comments on commit 55a05d9

Please sign in to comment.