Skip to content
jshacks.dev
Security

Prevent user enumeration in sign up

Respond the same way to a sign-up with an existing email as to a new one, and send the real information by email only.

1 min read
Summarize in AI

A sign-up form that says "email already registered" tells anyone who types an address whether that person uses your service. The fix is to show the same screen either way.

signup.jsJavaScript
app.post('/signup', async (req, res) => {
  const { email, password } = req.body;
  const existing = await db.findUserByEmail(email);

  if (existing) {
    await mail(email, 'Someone tried to register with your address. Reset your password if that was you.');
  } else {
    await db.createUser({ email, passwordHash: await argon2.hash(password) });
    await mail(email, 'Welcome! Confirm your address with this link: ...');
  }

  res.status(202).json({ message: 'Check your inbox to continue.' });
});
Text
HTTP/1.1 202 Accepted
{"message":"Check your inbox to continue."}

The browser sees the same status and message in both cases. Only the owner of the mailbox learns what happened. Hash the password in the existing-user branch too, or do the equivalent work, so the timing is similar; moving the mail to a queue also hides the time spent sending. See the OWASP guidance on account enumeration.

The cost is a worse experience for someone who forgot they registered. A neutral message plus the email is the accepted trade-off for services where the list of users is sensitive.

Share

More snippets

Security

Report CSP violations to an endpoint

Receive Content Security Policy violation reports at an endpoint with the report-to and report-uri directives, and parse what the browser sends.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.