by tptacek · 13y ago · 0 comments
The implementation I feel most comfortable with:
1. Generate a 256+ bit cryptographically secure random number and base64 it to create a token.
2. Record that token in your database, timestamped, along with the user account for which the token was requested.
3. Mail the token to the user's email address.
4. When the user returns to the site after recovering the token, use that token to look up their account from the database.
5. Expire tokens within single-digit hours so users don't end up accidentally banking password-equivalents in their email accounts.
6. When a user changes their password or requests another password reset, expire all tokens already associated with their account.
I would also recommend:
(a) Not having any in-band administration functionality in your application; instead, have a separate admin application, attached to the same databases, available only on a VPN.
(b) Require 2-factor authentication (such as Duo Security) for both admin VPN access and admin login.
A knock-on benefit of (a): your admin functionality is easier to build, because it doesn't have to do the UI/UX chinups your normal exposed app code has to do; crappy looking admin screens nobody but your employees see are generally fine.
No nonce is generated and nothing is stored. The user is emailed a link with her user ID and a token that's a hash of (last login timestamp + the user's ID + the user's (hashed) password + current timestamp). The token is HMAC-signed with the site's secret key.
This way the token automatically expires if the user either successfully changes her password (the password hash will change) or manages to log in (last login timestamp changes).
It seems that in Django password reset tokens are valid forever, but it would be trivial to add the current timestamp to the token and include it when computing the HMAC signature; then the password reset form would check if the token has been generated recently enough.
I like this method because you never need to touch the database and store tokens; it's all fairly stateless.
Personally, I have this particular bit of appsec down cold, and if I was building a new app, I wouldn't even think about it: I'd use a random token and save it in the database.
https://docs.djangoproject.com/en/1.4/ref/settings/#std:sett...
Haven't looked too closely at it but Bruno Renié has a Django app that has done most of the heavy-lifting to make password reset customizable by providing as class-based views and changing the timeout period granularity to seconds (it defaults to 172800 seconds or 2 days):
It doesn't handle point 6 of tptacek's list though; that subsequent tokens should invalidate all those prior. You could do that by adding an incrementing 'password_resets' or 'last_reset_issued_at' field to the User model, and including that in the token generator state, but it feels a bit clunky.
I'm not sure why people are placing such an emphasis on DB avoidance; it seems to me that password-reset activities should be a relatively minor source of load for your application in virtually all circumstances.
Additionally, this token could be valid for a very long time.
I would probably flag this approach in an assessment.
I'm not trying to drum up business. I would strongly prefer that you not try to be clever with this feature. Or, you can ignore the audit and just be the subject of a blog post like this sometime in the future.
And with 6, i suggest expire at step 4.
(I thought about it and decided to just have one expiry instruction).
The token is a password equivalent, and should thus not be stored in clear (use scrypt or a similar scheme).
I've tested many many many many applications in the last X years and not one of them has ever done this, nor would I ever recommend that they do it.
2. Email
3. Thick clients.
4. File upload.
5. File download.
6. Templating (as an app feature, not as a dev tool).
7. "Advanced Search".
This list is a couple years old. We're going to start tying bug reports to functional areas in targets to get a better empirical list by the end of the year. I expect the new empirical list to be more boring and less useful, though.
1. Breaks out of the web security domain, requiring devs to think through and implement controls that compensate for things like sessions and access control.
2. New quoting domain creates opportunities for injection to leverage apps to send unexpected messages.
3. Often involves shelling out, with all the attendant risks of that.
4. Inbound mail has different input restrictions than web apps do, creating opportunities for submarined XSS or even SQLI.
It's just a really common place where apps suddenly sprout unexpected moving parts, is what it boils down to.