How to Prevent Account Enumeration
Stop attackers from learning which accounts exist: uniform responses, constant-time paths, and identical timing for login, signup, and reset.
On this page
Introduction
Account enumeration is learning which accounts exist by observing how a service responds. A login that says ‘no such user’ for an unknown email and ‘incorrect password’ for a known one tells an attacker which emails are registered. The same happens on signup that reports a taken email, on reset that confirms an address, and in the timing when one case does more work than the other. With a list of leaked emails, enumeration separates the valid from the noise and focuses an attack.
This tutorial maps the revealing endpoints, makes responses uniform, equalizes the work so timing does not leak, and tests both paths. The examples are backend-agnostic and focus on the response and timing design that closes the channel.
Map the revealing endpoints
Start by listing every endpoint that could behave differently for a known and an unknown account: login, registration, password reset, email verification resend, and any invitation or lookup that takes an email. For each, ask what an attacker learns from the response and its timing. The enumeration surface is often wider than the obvious login, because features added later reveal existence without anyone noticing.
For each endpoint, decide the intended behavior for an unknown account. The safest is identical to the known case: the same status, the same body, and the same work. Where the product genuinely needs to tell the user why a login failed, do so only after the user proves control of the email, so the information is not available to someone who merely knows the address.
Make responses uniform
Return the same response for known and unknown accounts. Login should say ‘invalid credentials’ in both cases, not ‘unknown user’ versus ‘wrong password’. Signup should not confirm that an email is already taken in a way an attacker can probe; instead, accept the attempt and send a notice to the existing owner, or reveal it only through an email the real owner controls. Reset should say a message was sent regardless of whether the account exists.
The uniform response must cover the status code, the body, the headers, and any redirect. A different status code or a different error field is just as revealing as different words. Return the same shape with a generic message, and log the real reason server-side for operators, so debugging is possible without exposing the distinction to the caller.
async function login(email, password) {
const user = await findByEmail(email);
// same error and same work whether or not the user exists
const hash = user ? user.password_hash : DUMMY_HASH;
const ok = await argon2.verify(hash, password);
if (!user || !ok) return { status: 401, body: { error: 'invalid_credentials' } };
return { status: 200, body: { token: issueToken(user) } };
}
Equalize the work to close timing
Uniform responses are not enough if the two paths take measurably different times. A known account triggers a password hash, a database read, and maybe a token build, while an unknown one returns at the first lookup. Over many requests, that difference is a channel. Equalize the work: always perform the expensive steps, even when the account is absent, using a fixed dummy value.
For login, hash the presented password against a stored dummy hash when the user is absent, so the hashing cost is paid either way. For reset, enqueue the same work whether or not the account exists, though only the real owner receives the email. Keep the response off the critical path by acknowledging immediately and doing the conditional work asynchronously, so the response time does not depend on the account’s existence.
// precomputed once at startup: a hash of a random value with current params
const DUMMY_HASH = await argon2.hash(randomBytes(32).toString('hex'), params);
async function loginEqualized(email, password) {
const user = await findByEmail(email);
const stored = user ? user.password_hash : DUMMY_HASH;
const ok = await argon2.verify(stored, password); // same cost either way
return ok && user ? buildSession(user) : null;
}
Cover signup and invite too
Signup is a common enumeration leak because it wants to tell the user ‘this email is taken’. That message lets an attacker test addresses. Options that avoid the leak: accept the signup and send an email to the existing owner asking whether they intended a new account, or tell the user a message was sent and reveal the conflict only through the email. The user who controls the email learns the truth; an attacker who does not, learns nothing.
Invitations and ‘resend verification’ have the same shape. Respond identically whether or not the account exists, and perform the conditional action, sending an email or not, asynchronously so it does not affect the response. Rate-limit these endpoints so an attacker cannot use them as a fast oracle even through the uniform response.
async function signup(email, password, res) {
// always acknowledge; never confirm existence to the caller
const existing = await findByEmail(email);
if (existing) {
await notifyExistingOwner(existing, 'signup_attempt');
} else {
await queue.publish('create-account', { email, password });
}
res.writeHead(202).end(JSON.stringify({ message: 'Check your email to continue.' }));
// the owner learns if the account existed; the caller does not
}
Test both paths for response and timing
For each revealing endpoint, send a request with a known account and one with an unknown account and assert the status, body, and headers are identical. Then compare timing over many requests and assert the distributions are not distinguishable, which catches a path that does a hash in one case and not the other. Test the signup and reset endpoints the same way, because their confirmation messages are the usual leak.
for E in [email protected] [email protected]; do
curl -s -o /dev/null -w "%{http_code} %{time_total} $E\n" -X POST -d "email=$E&password=x" http://localhost:3000/login
done
# status must match; time_total must be comparable across many runs
Common pitfalls
Different error messages for unknown user versus wrong password, a signup that confirms a taken email, a reset that says whether the address exists, a response that returns immediately for unknown accounts while hashing for known ones, and a different status code between the two cases are the recurring mistakes. Uniform responses, equalized work with a dummy hash, asynchronous conditional actions, and rate limits address them.
Conclusion
Account enumeration turns a response difference into a list of valid addresses. Map the revealing endpoints, return identical responses, equalize the work so timing does not leak, cover signup and reset, and rate-limit. Self-check by asserting a known and an unknown account get the same response and comparable timing on every endpoint. Then knowing an email tells an attacker nothing about whether it is registered.
Sources: OWASP: Authentication cheat sheet, OWASP: Forgot password cheat sheet, MITRE: CWE-204 observable response discrepancy.
FAQ
- Isn't showing 'email already registered' helpful UX?
- It is helpful and it hands an attacker a way to test a leaked email list against your service. Prefer a flow that does not reveal existence, such as always saying a message was sent, or reveal existence only after the user proves they control the email by clicking a link.
- Does timing really leak existence?
- It can. If a known account triggers a password hash and an unknown one returns immediately, the difference is measurable over many requests. Equalize the work, for example by hashing against a fixed dummy value when the user is absent, so both paths take the same time.
Report a problem with this articleOpens a pre-filled issue on GitHub.