Skip to content
Race condition Write-up Advanced

The Race That Confirmed the Wrong Email

A race condition in Facebook's registration contact-point confirmation allowed a confirmation message sent to an attacker-controlled email to confirm a different, unused email address on the account. An attacker could mark an email they did not control as confirmed, undermining recovery and identity proof.

Muhammad Asim Shahzad Follow

Published on masimshahzad.medium.com

Bug Bounty

Muhammad Asim Shahzad, on masimshahzad.medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

Facebook's registration flow lets a person attach contact points, such as email addresses, to an account and then confirm them. Confirmation is meant to prove that the person controls the address: a secret is sent to the contact point, and submitting it back marks that address as confirmed. A confirmed address is then treated as a trusted channel, for example for account recovery.

Josip Franjković reported a race condition in this contact-point confirmation flow. The bug allowed a confirmation message delivered to an address he controlled to complete the confirmation of a different, unused address he did not control. Facebook fixed the issue on 10 May 2016. The write-up does not state a bounty for this specific report.

AI-generated illustration From the original on masimshahzad.medium.com

AI-generated illustration

The article is an analytical case study of that public disclosure, drafted with AI assistance, and deliberately omits reproduction details.

Setup

The researcher was testing Facebook's mobile registration flow. The disclosure does not name tools, versions or specific accounts.

Finding it

The researcher first noticed unexpected confirmation behaviour while testing the mobile registration flow in February. He then produced a reliable demonstration on 20 April.

Concurrent updates to an account's pending contact point were not serialised against each other. A confirmation artifact generated during that overlap could be delivered for one address and applied to another. In his own words, an unused email address could be marked confirmed on an account without him receiving mail at that unused address.

Exploitation

The disclosure does not reconstruct the interleaving, and this article intentionally omits reproduction details. At the level the report gives, the sequence is:

  1. Start from an account with a pending contact point.
  2. Send concurrent updates to that pending contact point so that the writes overlap rather than run one after another.
  3. A confirmation artifact generated during the overlap is delivered to an address the researcher controls.
  4. That artifact is applied to a different, unused address, which becomes confirmed on the account even though no mail was received there.

The report gives no requests, parameters, headers or responses for these steps.

Root cause

A confirmation artifact generated during the overlap could be delivered for one address and applied to another.

Impact

Confirming an email the account holder does not control undermines recovery, identity proof, and any downstream check that treats a confirmed address as a trusted channel. An unused address that becomes confirmed can later be used to reset access or to impersonate ownership of that identity. The report does not state severity, CVSS or bounty.

Fix

Facebook shipped a fix on 10 May 2016. The remediation below is the author's own analysis.

  1. Bind each confirmation secret to one exact contact value. Store the normalised address, the account and the purpose on the token itself. Verification must look up that triple, not "the latest pending email on this account".
  2. Serialise writes to pending identity fields. Take a row lock on the account or on the token before changing a contact point or consuming a secret. Two overlapping updates should not retarget an in-flight token.
  3. Persist counters and consumption, then commit before raising. A check-then-act gap lets parallel requests slip through, and raising inside a transaction can roll back the attempt counter. Decide under the lock, commit, then raise.
Text
# Pseudocode: bind, lock, persist the attempt, commit, THEN raise
def confirm_contact(account_id, submitted_contact, submitted_code):
    contact = normalize(submitted_contact)
    with db.transaction():
        token = (db.query(Token)
                   .filter_by(account_id=account_id,
                              purpose="confirm_email",
                              contact=contact)
                   .with_for_update()
                   .one_or_none())
        verdict = "invalid"
        if token and not token.used and not token.expired():
            token.attempts += 1
            ok = hmac.compare_digest(token.code_hash,
                                     hash_code(submitted_code))
            if ok:
                acct = (db.query(Account)
                          .filter_by(id=account_id)
                          .with_for_update()
                          .one())
                acct.email = token.contact  # only the bound value
                token.used = True
                verdict = "ok"
    if verdict != "ok":
        raise InvalidConfirmation()
  1. Expire and notify. Give secrets a short lifetime, consume them on success or lockout, and tell the user when a contact point is added or confirmed.
  2. Test the overlap directly. A concurrency test should submit two distinct contact values at the same time and assert that each secret confirms only itself.

Variants to test

  • Two contact values submitted at once. Send two distinct addresses in parallel during registration or contact-point change. A positive result is one secret confirming the other address, or both addresses ending up confirmed from a single delivered message.
  • Token reuse across purposes. Request a confirmation token for one purpose and submit it against another pending contact field. A positive result is the token being accepted where it was not issued.
  • Attempt counter rollback. Fire many wrong guesses in parallel against one token. A positive result is the attempt counter not increasing, or the lockout not triggering, because the increment was rolled back with the failed request.
  • Expiry under concurrency. Race a valid submission against token expiry. A positive result is a token being accepted after its lifetime, or an expired token being revived by an overlapping write.

Key takeaways

  • A confirmation code that is not bound to one contact value is a race waiting to happen.
  • Commit the attempt counter and the consumption flag before you raise an error.
  • Overlapping writes to identity fields need tests, not just code review.

Research by Josip Franjković: Race conditions on the web (12 July 2016). Facts about this Facebook case come from that post; analysis, code and remediation are the author's original suggestions.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

When you sign up for a website, it usually wants to know your email address. But anyone can type any address into a form, so the site needs proof that the address really belongs to you. The usual proof is a confirmation email: the site sends a short secret code to the address you typed, and you have to type that code back into the site. If you can type the code, the site assumes the email landed in your inbox, and therefore that you control the address.

That assumption is the whole point of confirmation. A confirmed address is treated as a trusted channel. It is where password reset links go, where security alerts go, and where the site sends anything that proves who you are.

In 2016, a researcher called Josip Franjković found a flaw in how Facebook's registration flow handled this confirmation step. Concurrent updates to a pending email address were not serialised, so a confirmation message delivered to an address he controlled could complete the confirmation of a different, unused address he did not control. In other words, an email address he had never received mail at could end up marked as confirmed on an account.

Facebook fixed the issue on 10 May 2016. The write-up does not state a bounty for this specific report.

AI-generated illustration From the original on masimshahzad.medium.com

AI-generated illustration

This article is an analytical case study of that public disclosure, drafted with AI assistance. It deliberately leaves out the details you would need to reproduce the attack.

Before going further, a few words on the ideas involved, because the rest of the article leans on them.

  • Contact point. Any way of reaching a person that a site stores on their account: an email address, a phone number, and so on. Facebook's registration flow lets you attach contact points to an account and then confirm them.
  • Race condition. A bug that appears when two actions happen at the same time and the system was only designed for them happening one after another. A everyday comparison: two people try to book the last seat on a flight at the same instant. Each one checks "is the seat free?", each one sees "yes", and both get a booking. Nothing was wrong with either check on its own; the problem is that the checks overlapped.
  • Serialised. The opposite of overlapping. If two updates are serialised, the system makes them run strictly one after the other, so the second one sees the result of the first. A queue at a till is serialised; a free-for-all at a buffet is not.
  • Token or secret. A short, hard-to-guess string the site generates and sends to your contact point. You send it back to prove you received it. Think of it as a cloakroom ticket: whoever holds the ticket gets the coat.
  • Binding. Tying a token to one specific thing, so it can only ever be used for that thing. A cloakroom ticket that says "coat number 42" is bound; a ticket that just says "some coat" is not.

Context

Facebook's registration flow lets a person attach contact points, such as email addresses, to an account and then confirm them. Confirmation is meant to prove that the person controls the address: a secret is sent to the contact point, and submitting it back marks that address as confirmed. A confirmed address is then treated as a trusted channel, for example for account recovery.

Account recovery is the "forgot my password" path. If a site will send a reset link to a confirmed address, then whoever controls that address can take over the account. That is why a confirmed address has to mean something.

Josip Franjković reported a race condition in this contact-point confirmation flow. The bug allowed a confirmation message delivered to an address he controlled to complete the confirmation of a different, unused address he did not control. Facebook fixed the issue on 10 May 2016.

The article is an analytical case study of that public disclosure, drafted with AI assistance, and deliberately omits reproduction details.

Setup

The researcher was testing Facebook's mobile registration flow. The disclosure does not name tools, versions or specific accounts. The precondition for the bug is an account with a pending contact point, plus the ability to send concurrent updates to that pending contact point.

Two terms there are worth unpacking.

  • Pending contact point. An address that has been added to an account but not yet confirmed. The site is holding it, waiting for proof. Until the proof arrives, the address should not be trusted for anything.
  • Concurrent updates. Several requests sent so close together that the server is still working on the first when the second arrives. A browser normally sends one request and waits for the reply, but a tool, or a script, can fire many at once.

Finding it

The researcher first noticed unexpected confirmation behaviour while testing the mobile registration flow in February. He then produced a reliable demonstration on 20 April.

Noticing something odd and being able to make it happen on demand are two different milestones. The first is a hunch; the second is a finding you can report, because you can show it repeatedly and describe the conditions.

The observation that looked wrong was that concurrent updates to an account's pending contact point were not serialised against each other. A confirmation artifact generated during that overlap could be delivered for one address and applied to another. In his own words, an unused email address could be marked confirmed on an account without him receiving mail at that unused address.

A confirmation artifact here means the message carrying the secret, the thing the site sends to the address. The problem was not that the message went to the wrong inbox in the sense of a typo. The problem was that a message generated while two updates were overlapping could be delivered for one address and applied to another.

Exploitation

The disclosure does not reconstruct the interleaving, and this article intentionally omits reproduction details. At the level the report gives, the sequence is:

  1. Start from an account with a pending contact point.
  2. Send concurrent updates to that pending contact point so that the writes overlap rather than run one after another.
  3. A confirmation artifact generated during the overlap is delivered to an address the researcher controls.
  4. That artifact is applied to a different, unused address, which becomes confirmed on the account even though no mail was received there.

The report gives no requests, parameters, headers or responses for these steps.

Step 2 is the heart of it. "Writes overlap rather than run one after another" means the server was not serializing them. If it had been, the second update would have waited for the first to finish, and the state would have been consistent at every moment.

Step 4 is where the damage lands. The address that ends up confirmed is one the researcher never received mail at. The proof of control was never actually given for that address, yet the account now treats it as trusted.

Root cause

Concurrent updates to an account's pending contact point were not serialised against each other. Because the confirmation secret was not bound to one exact contact value, a token generated during the overlap could be retargeted to another address.

Those two questions give the same answer when nothing overlaps. When two updates overlap, the account's pending email can change between the moment the token is created and the moment it is checked, and then the two questions give different answers. The token was issued for one address and gets accepted for another.

Impact

Confirming an email the account holder does not control undermines recovery, identity proof, and any downstream check that treats a confirmed address as a trusted channel. An unused address that becomes confirmed can later be used to reset access or to impersonate ownership of that identity. The report does not state severity, CVSS or bounty.

In plain terms: the site's record of "this person controls this address" becomes false. Anything built on top of that record inherits the falsehood. Password reset is the obvious one, but so is any feature that says "we emailed the owner" or "this account is verified".

Fix

Facebook shipped a fix on 10 May 2016. The remediation below is the author's own analysis.

  1. Bind each confirmation secret to one exact contact value. Store the normalised address, the account and the purpose on the token itself. Verification must look up that triple, not "the latest pending email on this account".
  2. Serialise writes to pending identity fields. Take a row lock on the account or on the token before changing a contact point or consuming a secret. Two overlapping updates should not retarget an in-flight token.
  3. Persist counters and consumption, then commit before raising. A check-then-act gap lets parallel requests slip through, and raising inside a transaction can roll back the attempt counter. Decide under the lock, commit, then raise.

A few of those words need explaining for a newcomer.

  • Normalised address. The same address written the same way every time. Email addresses can be written with different capitalisation or spacing; normalizing means reducing them to one standard form so comparisons are reliable.
  • Row lock. A database feature that reserves one record so only one request can change it at a time. It is the database equivalent of putting an "occupied" sign on a fitting room door. Other requests wait until the sign comes down.
  • Transaction. A group of database changes that either all happen or none happen. If something fails halfway, the database undoes the partial work, as if it never started.
  • Commit. The moment a transaction's changes become permanent.
  • Check-then-act gap. The window between "I checked the state" and "I acted on it". If something else changes the state inside that window, the action is based on stale information. This is the same shape as the flight-seat example above.
Text
# Pseudocode: bind, lock, persist the attempt, commit, THEN raise
def confirm_contact(account_id, submitted_contact, submitted_code):
    contact = normalize(submitted_contact)
    with db.transaction():
        token = (db.query(Token)
                   .filter_by(account_id=account_id,
                              purpose="confirm_email",
                              contact=contact)
                   .with_for_update()
                   .one_or_none())
        verdict = "invalid"
        if token and not token.used and not token.expired():
            token.attempts += 1
            ok = hmac.compare_digest(token.code_hash,
                                     hash_code(submitted_code))
            if ok:
                acct = (db.query(Account)
                          .filter_by(id=account_id)
                          .with_for_update()
                          .one())
                acct.email = token.contact  # only the bound value
                token.used = True
                verdict = "ok"
    if verdict != "ok":
        raise InvalidConfirmation()

Line by line, in plain words:

  • def confirm_contact(account_id, submitted_contact, submitted_code): declares a function that takes three inputs: which account is confirming, which address the user typed, and which code they typed.
  • contact = normalize(submitted_contact) puts the typed address into its standard form, so that small differences in how it was written do not create a mismatch.
  • with db.transaction(): opens a transaction, so everything inside either completes together or is undone together.
  • token = (db.query(Token) starts a database lookup for a token record.
  • .filter_by(account_id=account_id, narrows the lookup to this account.
  • purpose="confirm_email", narrows it to tokens issued for email confirmation, so a token issued for something else cannot be reused here.
  • contact=contact) narrows it to the exact normalised address. This is the binding: the token must match the address being confirmed.
  • .with_for_update() takes a row lock on the matching token, so no other request can change it until this transaction finishes.
  • .one_or_none()) says the lookup should return exactly one token or nothing.
  • verdict = "invalid" sets the default outcome to failure. Only the code below can change it.
  • if token and not token.used and not token.expired(): proceeds only if a token exists, has not already been used, and has not expired.
  • token.attempts += 1 records the attempt. This is the counter that should survive even if the code is wrong.
  • ok = hmac.compare_digest(token.code_hash, compares the stored hash of the correct code with the hash of the submitted code, using a comparison designed not to leak timing information.
  • hash_code(submitted_code)) is the hash of what the user typed.
  • if ok: runs the success path only when the codes match.
  • acct = (db.query(Account) starts a lookup of the account record.
  • .filter_by(id=account_id) narrows it to this account.
  • .with_for_update() locks the account row too, so nothing else can change it mid-update.
  • .one()) expects exactly one account.
  • acct.email = token.contact # only the bound value sets the account's email to the address stored on the token, not to whatever the account's pending field happens to say at that moment. This is the fix for the retargeting problem.
  • token.used = True marks the token consumed, so it cannot be replayed.
  • verdict = "ok" records success.
  • if verdict != "ok": after the transaction has closed and its changes are committed, checks whether the attempt failed.
  • raise InvalidConfirmation() raises the error only after the commit, so the attempt counter and the used flag are not rolled back along with the failure.
  1. Expire and notify. Give secrets a short lifetime, consume them on success or lockout, and tell the user when a contact point is added or confirmed.
  2. Test the overlap directly. A concurrency test should submit two distinct contact values at the same time and assert that each secret confirms only itself.

Variants to test

  • Two contact values submitted at once. Send two distinct addresses in parallel during registration or contact-point change. A positive result is one secret confirming the other address, or both addresses ending up confirmed from a single delivered message.
  • Token reuse across purposes. Request a confirmation token for one purpose and submit it against another pending contact field. A positive result is the token being accepted where it was not issued.
  • Attempt counter rollback. Fire many wrong guesses in parallel against one token. A positive result is the attempt counter not increasing, or the lockout not triggering, because the increment was rolled back with the failed request.
  • Expiry under concurrency. Race a valid submission against token expiry. A positive result is a token being accepted after its lifetime, or an expired token being revived by an overlapping write.

Key takeaways

  • A confirmation code that is not bound to one contact value is a race waiting to happen.
  • Commit the attempt counter and the consumption flag before you raise an error.
  • Overlapping writes to identity fields need tests, not just code review.

Research by Josip Franjković: Race conditions on the web (12 July 2016). Facts about this Facebook case come from that post; analysis, code and remediation are the author's original suggestions.

Glossary

  • Account recovery: the process of getting back into an account you have lost access to, usually by receiving a reset link at a confirmed address.
  • Binding: tying a token to one specific value, account and purpose so it cannot be used for anything else.
  • Check-then-act gap: the window between checking a condition and acting on it, during which another request can change the state.
  • Commit: the point at which a database transaction's changes become permanent.
  • Concurrency: several requests being processed at overlapping times rather than strictly one after another.
  • Confirmation artifact: the message carrying the secret code that the site sends to a contact point.
  • Contact point: an email address, phone number or similar that a site stores on an account and can send messages to.
  • CVSS: a standard scoring system for the severity of a security flaw.
  • CWE: a catalogue of weakness types. The report does not assign one; any mapping would be analytical.
  • Hash: a one-way transformation of a value, so the original cannot easily be recovered from the stored form.
  • hmac.compare_digest: a comparison function designed to take the same time whether the values match or not, so timing does not leak information.
  • Normalised address: an email address reduced to one standard written form so comparisons are reliable.
  • Pending contact point: an address added to an account but not yet confirmed.
  • Pseudocode: code written to show the logic rather than to run as-is.
  • Race condition: a bug that appears when two actions overlap and the system was only designed for them running in sequence.
  • Row lock: a database mechanism that reserves one record so only one request can change it at a time.
  • Serialised: forced to run strictly one after another, so the second sees the result of the first.
  • Token (secret): a short, hard-to-guess string sent to a contact point and submitted back as proof of receipt.
  • Transaction: a group of database changes that all succeed or are all undone.
  • Verification: the step where the submitted code is checked against the stored one.

More by Muhammad Asim Shahzad

View all

21 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.