I have a application where users can log in by providing a username or email address (both case insensitive) and a password. In the users table in the database, the relevant account information is stored in three columns lowercase_username
, lowercase_email
, and password_hash_and_salt
. There are indices on both lowercase_username
and lowercase_email
. The authentication logic uses an ORM to perform a SQL query of the form
SELECT … FROM `Users` AS `User` WHERE (`User`.`name` = … OR `User`.`email` = …) LIMIT 1;
and then uses a library-provided constant-time function to hash the submitted password with the record's salt and compare the result to the record's password hash (the computation instead runs on a fake password_hash_and_salt
if no record was returned). (I am also using a pepper, but I don't believe that that is relevant to the question.) Then an authentication decision is made based on whether there were both a record found and a password hash match.
My concern is that I might potentially be leaking information about which emails are in the database through variations in the query time. If the query returns faster for email addresses in the database, then an attacker could attempt to log in with the target email address and observe the application's response time. Is that the case, and if so, would the threat be mitigated by the DB index and/or by removing the LIMIT 1
clause from the query?
I admit that in this example I maybe shouldn't be so worried because an attacker could also find out whether an email is in the database by trying to create an account with that email address—despite the registration page's generic error message, there are not that many other reasons that account creation could fail—and the only downside of approach is that the application will send an email to the target if the account creation actually succeeds. But please ignore this alternative attack, since I would still like to know what information my DB query might be leaking through timing and if there are best practices to avoid such leaks.