rails/rails — pull request #16457 by NZKoz (2014-08-11 04:46:26+00:00)
Title: Harden http_basic_authenticate_with against timing attacks

As has been disclosed previously, the current implementation of `http_basic_authenticate_with` uses `String#==` on both the username and password values. Because `String#==` eventually involves a call to `memcmp` an attacker could potentially flood the site with requests and derive valid credentials.

To mitigate this risk this pull request rejigs the implementation to avoid short circuiting and also uses bcrypt (with a low cost) to make the response timings immune to iterative guessing. We can't just drop in `secure_compare` because it's designed for byte arrays of a known and fixed size.

I'm submitting this for review because it would be good to hear from any security researchers whether:

# we should use bcrypt, or any other hashing function
# If there's anything else we should consider