Skip to content
Get started
Bastionary › Knowledge base › Security and abuse

Secure email change flows: double confirmation and takeover defense

By Bastionary · Security and abuse

The email address on an account is usually the master key. It is where password resets go, where MFA recovery lands, where security alerts arrive. So the flow that lets a user change that address is not a profile-settings nicety; it is one of the highest-value account-takeover vectors you own. Get it wrong and an attacker who gains a brief foothold can quietly rewrite the recovery address, lock out the owner, and inherit the account permanently.

The dangerous version is the one that looks reasonable: a form field, a save button, done. The secure version confirms both the old address and the new one, keeps the original recoverable for a window, and shouts about the change on every channel it can reach. Each of those steps closes a specific attack.

The attack you are defending against

Picture an attacker with a hijacked session, say a token stolen from an unlocked laptop, or a session that survived a password the user thought they had changed. The attacker does not need the password if they can change the email: swap it to an address they control, trigger a password reset to that new address, and now they hold the real credentials while the legitimate owner is locked out and unaware. The email-change flow is the pivot that turns temporary access into permanent control.

Every defense below exists to break that chain: to make the change require proof the attacker does not have, to give the owner a chance to notice and reverse it, and to ensure the old address is not instantly orphaned.

Confirm the new address before it becomes live

The new email is unverified until proven otherwise. Never switch the account's email to the new value on form submit. Instead, record it as a pending change and send a verification link there; the change takes effect only when that link is clicked. This confirms the user controls the destination and prevents a typo (or a malicious address) from capturing the account's recovery path.

CREATE TABLE email_change_requests (
  id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id       UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  old_email     CITEXT NOT NULL,
  new_email     CITEXT NOT NULL,
  new_token_hash  BYTEA NOT NULL,      -- confirm control of new address
  old_token_hash  BYTEA NOT NULL,      -- confirm control of / notify old
  requested_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
  expires_at    TIMESTAMPTZ NOT NULL,  -- short, e.g. +1 hour
  confirmed_new BOOLEAN NOT NULL DEFAULT false,
  confirmed_old BOOLEAN NOT NULL DEFAULT false,
  completed_at  TIMESTAMPTZ
);

Confirm the old address too, and notify it regardless

Confirming only the new address misses the point. The owner of the old address is the person you are trying to protect, so they must be in the loop. Send a notification to the old address the instant a change is requested, and ideally require a confirmation there as well. If the attacker controls the session but not the original inbox, this is the step that stops them: the real owner gets an email saying "someone is changing your address" with a one-click "freeze my account" link.

Double confirmation, stated plainly: the new address proves the destination is real; the old address gives the true owner a veto. Requiring both means an attacker needs control of the session and the original inbox, which is a much higher bar than either alone.

Re-authenticate before you even start

Changing email is a sensitive operation and should demand a fresh proof of identity, not just a valid session. Prompt for the password or a passkey tap at the moment of the request, regardless of how recently the user logged in. This defeats the stolen-session attacker who has a live token but not the actual credential.

async function requestEmailChange(userId: string, newEmail: string, proof: ReauthProof) {
  await requireFreshAuth(userId, proof);          // password or passkey, now
  if (await emailInUse(newEmail)) {
    // Do not reveal whether it is taken via timing or message differences.
    return genericPending();
  }
  const { newToken, oldToken } = issueChangeTokens();
  await db.emailChangeRequests.create({
    userId, newEmail, expiresAt: addHours(new Date(), 1),
    newTokenHash: sha256(newToken), oldTokenHash: sha256(oldToken),
  });
  await mail.sendVerify(newEmail, newToken);      // "confirm this address"
  await mail.sendAlert(currentEmail(userId), oldToken); // "was this you? freeze"
}

Keep the old address recoverable

Even after a change completes, do not immediately and irreversibly forget the previous address. Retain it for a grace period so that if the change was fraudulent and slipped through, the original owner still has a lever: a reversal link sent to the old address, or a support path keyed to it, that works for some days afterward. This converts an irreversible takeover into a recoverable incident.

Gotcha: when the email change completes, treat it as a security event and revoke every other active session and trusted device. An attacker who changed the email will otherwise keep their own session alive; a legitimate user barely notices one re-login. Also invalidate any in-flight password-reset tokens issued to the old address.

Handle the enumeration and collision edges

Two quieter risks hide in this flow. First, enumeration: if your response differs when the new email already belongs to another account, you have built an account-existence oracle. Return the same "check your inbox" result either way and resolve collisions asynchronously. Second, the race: scope the pending change so a second request supersedes the first cleanly, and never let two pending changes for the same account both be confirmable.

  • Expire change tokens quickly (an hour is plenty) and make them single-use.
  • Rate-limit change requests per account to blunt harassment and probing.
  • Log every request, confirmation, and completion to the audit trail with IP and device.

A checklist that actually holds

A secure email change flow re-authenticates at the start, writes the new address as pending rather than live, confirms control of the new inbox, notifies and ideally confirms the old one, keeps the previous address recoverable for a grace window, and revokes other sessions on completion. Skip any one of those and you have left a seam an attacker can work.

This is exactly the kind of flow where generic form-builder auth falls short, because the security lives in the sequencing and the notifications, not the UI. Owning your identity layer means you can wire the double confirmation, the grace period, and the session revocation to behave the way your threat model demands, rather than trusting that a hosted profile page got every one of these steps right.

Run it yourself

Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.

Get started