by poutine · 13y ago · 0 comments
- Dont listen on HTTP, force HTTPS (Tune your ciphers! see https://www.ssllabs.com/ssltest/index.html)
- Ensure that any client libraries you supply for API interaction must verify the server certificate
- Only use the users password to obtain a long random key from the server either through an admin web interface where possible or by an API request that supplies the username and password and receives the key.
- Make authenticated requests of the API with the key
- Store any passwords as bcrypt hash in the DB for security
- Only store the key as SHA2-256 hash in your DB for performance
- Actually yes do use HTTP Basic as it's a convenient way to transmit the key (see how Stripe does it) making things easy for your users and perfectly secure as long as you prohibit plain HTTP.
- Stay the heck away from HMAC's and signing requests, you have SSL, properly tuned it's secure. HMAC is pointless here and really confuses the users.
Use a config line like the following in nginx:
ssl_ciphers ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH;
RC4 is a problem right now but unfortunately there's not a good solution until we get more TLS 1.2 support out there.
If you don't do this sort of tuning you're exposed to some weak ciphers that can result in attacks.
But you should store "the key as SHA2-256 hash in your DB"
why hash it?
While you're likely in serious trouble if someone gets your DB this helps minimize damage. Besides to authenticate a request you dont need the plaintext of the key, a hash is fine.
I guess the trade-off is here that the api key cannot then be retrieved from a GUI either (because the server doesn't actually know the original key!) .. from a UX perspective, most companies show your active keys in a GUI with ability to selectively remove or replace.
Though I can't think of a mainstream API that hasn't let me retrieve api key from GUI
Ideally you'd regenerate the key each time the user wants to show it or just use OAuth 2.0 (it's not as bad as everyone says it is).
You cannot brute force search against a large random number that is hashed with unsalted SHA or the like. This would require you to guess the number, hash it and check to see if the hashes match. A large random number encoded as a 64 character string is simply too big to guess, even at millions of guesses per second.
Thus using bcrypt to protect your api keys does nothing but impose a serious bottleneck in how many api requests per second you may authenticate.
The reason you need bcrypt is that user supplied passwords are crap, and can be cracked very quickly.
It doesn't matter how many machines you have or how fast you can compute the SHA. An attacker will not find the API key (work out the math).
How else can you guard against replay attacks, and authenticate the request? Only a key isn't enough, you need secrets as well. Right?
Use SSL only, tune ciphers and you're good. You're wasting time trying to overthink the problem by adding another layer on top.