Sign in with Google, then change the locks
A sign-in that worked, a page that refused, and a password nobody could vouch for. Let’s take it apart.
A new user signed up with an email and a password. They didn't wait for the verification email. They went back to the sign-in page and clicked Continue with Google instead. Google said yes. Our app said yes. Then every page they opened said no.
Ten seconds later they had been refused a few hundred times, and our rate limiter had locked them out of signing in at all. They left.
The short version
When a person proves an inbox through Google, treat everything attached to that account before the proof as untrusted, including its password.
Verify the account, replace the password, and drop sign-in methods linked before the proof.
Getting the first half wrong locks real users out.
Getting the second half wrong hands their account to whoever registered the address first.
How would you like to read the rest?
Signed in, and refused everywhere
The sign-in itself worked. The provider confirmed the email, our server found
an account with that address, linked the Google identity to it and issued a
session. Every request after that came back 403.
- Continue with GoogleGoogle confirmed the email.
- Open the dashboardSession issued. Welcome back.
- Load your documents403 Forbidden
The account had been created by the password sign-up a minute earlier, and it was still unverified. Accounts created through Google were marked verified when they were created. Existing accounts found by email were linked as they were. So the person held a valid session for an account that every protected endpoint refused to serve.
Reading the access log by size
The access log kept no response bodies, only status codes and sizes. The sizes were enough. Every refusal was 152 bytes, and exactly one error payload in our code had that length: "please verify your email address". The generic "forbidden" response was 52 bytes. The suspended-account one was 60.
| Response size | Error payload |
|---|---|
| 52 bytes | Forbidden |
| 60 bytes | Account suspended |
| 152 bytes | Please verify your email address every refusal |
When a log only gives you sizes, render each candidate error and count its bytes. It's a cheap way to tell them apart.
A loop made of two correct rules
The frontend had two rules, and each made sense on its own.
The dashboard sends anyone with an unverified email to the sign-in page, with a
note asking them to verify and a next parameter so they can come back later.
The sign-in page sends anyone already signed in straight on to next, because
nobody should see a login form while logged in.
nextnext4 refused calls a laprate limit reached
The dashboard had already loaded the user, so the sign-in page saw a signed-in user and sent them back. The dashboard saw an unverified user and sent them away again. Each lap fired four API calls, all refused, until the rate limiter blocked the browser entirely, including its next attempt to sign in with Google.
Both rules were asking "is someone signed in?" Only one of them also asked "is the email verified?" Two redirect rules that point at each other have to agree on what a live session means, or they pass the user back and forth until something else steps in.
The obvious fix
If Google proves the person owns the inbox, mark the email verified. They don't need our verification email, and they can keep using the password they chose.
That works when the person who signed up and the person who signed in with Google are the same person. The server has no way to know that they are.
Whose password is it?
Typing an email address at sign-up proves nothing. Anyone can register
priya@example.com with a password of their choosing. They can't verify it,
because the email goes to Priya, but they don't need to. The account just
waits.
Weeks later Priya finds the app and clicks Continue with Google. Google proves the inbox is hers. With the obvious fix, the account becomes verified and keeps the password the stranger chose. Priya uploads her documents and fills in her profile. The stranger signs in with their password and sees all of it. Priya never knew there was a password.
This is a pre-account takeover. The attacker never breaks into an account. They create it first and wait for the owner to adopt it. It works because the system trusts something that was attached before anyone proved the address.
Think of moving into a house where someone had a copy of the key cut before you arrived. The moment you prove the house is yours is the moment to change the locks, because you have no idea who else holds a key.
Someone had a key cut before you moved in. Proving you own the house is when you change the locks.
Change the locks
When a provider's verified email matches an account still waiting on verification, we now treat that sign-in as the account's real start:
- mark the email verified,
- replace the password with a random one,
- remove other sign-in identities linked before the proof,
- close any pending verification links,
- revoke every older session, and record what changed in the audit log.
The cost lands on one kind of person: someone who really did sign up with a password and then used Google. Their password stops working. So the next time they try it, the error says so plainly: you signed in with Google, so we reset your password for security; use Google or reset it. A confusing moment is recoverable. A silent takeover isn't.
The race inside the fix
Two sign-ins for the same account can overlap: a double click, or two tabs. One of them changes the locks while the other is halfway through.
Our sessions are revoked by timestamp. Any token issued before the account's "revoked at" time is rejected. If the second request finished its checks, released the lock on the account, and only then issued its tokens, those tokens would be newer than the revocation and would keep working. A sign-in that lost the race would hold a live session on an account that had just been locked down.
Tokens issued outside the lock
- Tab A releases the lock
- Tab B revokes older sessions
- Tab A issues tokens
Issued after the revocation: still valid.
Tokens issued inside the lock
- Tab A issues tokens
- Tab A releases the lock
- Tab B revokes older sessions
Issued before the revocation: rejected.
The fix is to issue tokens inside the same lock the claim runs under. Every token is then either issued before a later revocation, which rejects it, or issued by a request that already saw the account in its new state.
A limit that never fired
While adding a "resend the verification email" button, we checked how often someone can press it. The endpoint sends an email to any address, with no session needed. We capped it per recipient, because the person harmed by a flood is the one receiving it, and a per-IP limit can't protect an inbox from an attacker with many IPs.
Checking that, we found an older per-email limit that had never fired in production. It read the address from form parameters, and our frontend sends JSON. Its test posted form data, so it stayed green the whole time.
Review then caught one more mismatch. Our first version read the email from a slightly different place than the controller did, so a request shaped to confuse one but not the other slipped through. A limiter has to key on exactly the value the endpoint acts on, read exactly the way the framework reads it.
A limiter has to key on exactly the value the endpoint acts on.
Take it with you
- Proof of the inbox is the moment to distrust everything attached before it: verify, replace the password, drop pre-proof identities, revoke old sessions.
- Tell the user what you did at the moment it surprises them.
- Redirect rules that point at each other must share one definition of "signed in".
- Issue sessions under the same lock your revocation runs under.
- Rate-limit by the party being harmed, and key on the exact value the endpoint uses.
- Test with the request your client actually sends. A green test of a request nobody makes proves nothing.