Making my admin login harder to break into
A password and a cookie got me a working admin. This week I went back and fixed the gaps I'd been ignoring, including two-factor login without a library.
On this page
My site's admin panel worked, and for a while I let that be enough. It had bcrypt-hashed passwords, a cookie the browser's JavaScript can't read, and a rate limit on login. Then I sat down and read my own code as if I were trying to get in. It didn't take long to find things I wasn't happy with, so I spent a few days fixing them.
A token I trusted too much
When you logged in, the API gave you a signed token that lasted seven days and carried your role inside it. Every request after that was checked against the token alone. Which meant that if I deleted an account, or demoted it, the old token kept working until it expired. A week of access I'd meant to remove.
Now every admin request looks the user up in the database, and takes the role from there. It costs one small query. In return, deleting or demoting someone takes effect immediately. I also cut the session to a day.
Logging out that didn't log you out
Logout cleared the cookie in your browser. That's it. If someone had copied the token, say from a compromised machine, it stayed valid. So I added a version number to each user. It's stored in the token, and when you log out (or turn two-factor off) the number goes up, and any token carrying the old one stops working. Logout now really ends the session, everywhere.
Locking out guessing, without locking out me
I already limited login attempts per IP address. The weakness is that someone with many addresses can spread guesses around. So I added a second limit per email: five wrong passwords and that account is locked for fifteen minutes. A correct login never counts toward it.
It's not free of tradeoffs. Someone could deliberately fail five times on my email to lock me out for a quarter of an hour. I decided that's an acceptable annoyance, and the thing that really solves it is the next part.
Two-factor, from scratch
I wanted a code from an authenticator app, the six digits that change every thirty seconds. These codes follow a public standard (TOTP), and the algorithm is short: an HMAC of the current time step, truncated to six digits. I decided to write it with Node's built-in crypto module instead of installing a package, partly to keep dependencies down and partly because I wanted to understand it.
To make sure I'd got it right, I checked my output against the test values published in the standard (RFC 6238), and they matched. A few details turned out to matter:
- A code can be used once. Without that, someone who sees you type one could replay it within the same thirty seconds. I store the last used time step and refuse anything at or before it.
- The secret is stored encrypted, not in plain text, so a copy of the database alone can't generate codes.
- Eight recovery codes are issued when you turn it on, stored only as hashes, each usable once, for the day you lose your phone.
- Turning it off needs your password and a current code, so a stolen session can't quietly remove it.
The setup shows a QR code you scan with the app, drawn in the browser so the key isn't sent anywhere extra.
A check from an unexpected place
After I pushed the first batch of changes, my automated checks on GitHub failed. The cause was a test that runs against a real Redis and made ten failed logins on one email, which the new lockout now correctly stopped after five. The code was right and the test was out of date. I'm mentioning it because it's the kind of thing I'd have missed if the test suite only ran against fakes.
Still on the list
There's more. The page's security policy still allows inline scripts, which I'd like to tighten. I want a spam check on the contact form, and a dependency upgrade is overdue. Security isn't a thing you finish. It's a list that gets shorter and then, a few weeks later, gets longer again.
Enjoyed this? Pass it on
Written by Vishwajit Bhosle
I build and lead web and mobile products from Pune, and write about what I run into along the way. If something here helped, or you have a project in mind, say hello.
Follow along
New posts show up in any feed reader the moment they are published: no account, no email, no tracking.
Comments
Anyone can comment, no sign-up. Upvote what helped, reply to anyone. I read all of them and reply where I can.
Loading comments…