All notes
MFA5 min read

MFA is a chain, and I found the weak links

Adding a six-digit code to the login screen is the easy part. Making sure there is no way around it is the work.

Our platform has had two-factor login with authenticator apps for a long time. Users scan a QR code once, then type a six-digit code at every login. It looked done. When I audited it properly, I found that the code on the screen was solid, and almost everything around it had a gap.

MFA is a chain: enrolment, the secret, the login steps, the session that comes out at the end, password resets, and every other way into an account. An attacker only needs the weakest link. Here are the ones I found, and how we fixed them.

The password that skipped the second factor

This was the worst finding of the audit. Login was two steps: send the password, then send the code. After a correct password, the API returned a short-lived session ID so the second step knew who you were. So far, normal.

But another endpoint, the one that lets a user pick which company to work in, accepted that same session ID and returned a full access token. It was anonymous, because it was part of the login flow. So a correct password alone was enough to get a fully authenticated session. The code screen was a door in a wall that you could walk around.

Nothing was wrong with either endpoint in isolation. The bug was in the state between them. The fix was to make that state explicit:

// The login session only becomes useful once MFA has passed.
if (session.RequiresMfa && !session.MfaPassed)
    return Unauthorized();

var token = IssueToken(session);
await _sessions.RetireAsync(session.Id);   // single use, then gone

A session is marked "MFA passed" only after a valid code, every step that issues a token checks the mark, and the session ID is retired the moment it is used.

LessonFor any multi-step flow, write down the states (password ok, MFA ok, token issued) and check, for every endpoint, which state it requires. The bug lives in the endpoint that forgot to ask.

One seed behind every code

The next finding was about the secret itself. A TOTP code is derived from a secret shared between the server and the authenticator app. Ours was not random per user. It was derived from the user's e-mail address plus one shared seed stored on the server.

That means anyone who ever saw that one value, in an environment file, a backup or a screen share, could compute the current code for every user. Two factors had quietly become one password plus one server secret.

The new design is the standard one: each user gets 160 random bits, Base32-encoded, shown once as a QR code, and stored encrypted with AES-GCM. The interesting part was the migration, because every existing authenticator app already held codes based on the old values.

  • New enrolments always get a per-user secret. Turning MFA off and on again moves a user to the new scheme.
  • The old seed is accepted only for accounts already active on it, behind a server switch (MFA_ALLOW_LEGACY_SEED). It can never be used to enrol, and it is never shown in a QR code.
  • When the switch is turned off, a legacy account is not locked out. Its next login puts it back into "pending", and it scans a fresh QR code.
  • If a per-user secret cannot be decrypted, the login fails. It never falls back to the shared seed. A fallback to the weaker scheme is exactly what an attacker would aim for.

The switch gave the business a choice: wait for users to move over by themselves, or pick a date. That decision did not belong to me, and building the switch meant I did not have to force it.

The dollar sign that truncated a secret

During the migration we hit a bug that took far too long to understand. Codes that worked on one server failed on another. The cause: the shared seed contained a $, and the library that loaded our .env file treated it as a variable reference and silently cut the value short. Different hosts, different loaders, different seeds.

We had to accept both values for a while so nobody was locked out. The permanent fix was to stop depending on that seed at all. The lasting lesson: always check the secret your process actually received, not the one you think you configured.

Five minutes is a long time

The authenticator library defaulted to accepting codes within a five-minute drift window. That is generous to users with badly set phone clocks, and generous to an attacker who has shoulder-surfed or phished a code: they have minutes, not seconds. We reduced it to one 30-second step either side, which still absorbs normal clock drift.

Every other way in

Once the login itself was solid, I went through every other path that touches the account:

  • Wrong codes count. For one user type, a wrong code never counted toward the lockout counter, so codes could be guessed without limit. Now a wrong code and a wrong password go through the same failed-login handler.
  • Password reset in the right order. The reset flow revoked every session before checking the MFA code, so anyone with an e-mail address could log a user out of every device. Revocation now happens only after the code is verified.
  • Changing the password or MFA revokes refresh tokens. Otherwise an attacker's existing session survives the very change meant to lock them out.
  • Users cannot switch their own MFA off through the back door. One update endpoint bound the request body straight to the database entity, so a customer could send "mfaStatus": 0 along with their name change. Request DTOs fixed it: a DTO only has the fields you meant to accept.
  • MFA checks outside login (activation, resets) have their own attempt limit, and the e-mail endpoints are rate limited, so nobody can flood an inbox.

What I would build in from day one

If I started again tomorrow
  • Per-user random secrets, encrypted at rest. Never derive a TOTP secret from something you can compute.
  • Remember the last used time step per user, so a code cannot be replayed even inside its window.
  • Recovery codes at enrolment, hashed like passwords, so a lost phone is not a support ticket.
  • A written state machine for login, with a test for every transition that must be refused.
  • The same lockout and rate limit for every factor. A second factor that can be guessed endlessly is not a factor.

None of these fixes changed what users see. The login screen looks exactly as it did. That is the strange thing about this kind of work: the better it is done, the less anyone notices it. I have made my peace with that.