The Username That Changed When Asked Twice

Hard

· Backend

Incident

SEV-1

BUG-SPOT-1306

Priority: Critical (account takeover) · reported by security

Time to fix: 35 min

Failing checks on the shipped build

  • a name whose canonical form is unstable is refused

    canonical(canonical(name)) differs from canonical(name): expected an error to be raised

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Spotify · 2013

Modelled on Spotify's June 2013 account-hijacking bug, described in Spotify's own engineering write-up. Usernames were canonicalised with an XMPP "nodeprep" routine that was not idempotent for some Unicode characters: canonicalising ᴮᴵᴳᴮᴵᴿᴰ gave BIGBIRD, and canonicalising that again gave bigbird. Because the password-reset flow canonicalised a name that was already canonical, an attacker could register the odd-looking name and reset the password of the ordinary account it collapsed into. Spotify's fix rejected names whose canonical form is not stable.

nodeprep.py (locked) reproduces the non-idempotent mapping; accounts.py trusts it.

Fix AccountStore so no name can be steered into another account.

Code map

nodeprep = bug_require("./nodeprep.py")


class AccountStore:
    def __init__(self):
        self.accounts = {}

    def register(self, name, password):
        key = nodeprep.canonical(name)
        if key in self.accounts:
            raise ValueError("username taken")
        self.accounts[key] = {"display": name, "password": password}
        return key

    def check_password(self, name, password):
        acct = self.accounts.get(nodeprep.canonical(name))
        return acct is not None and acct["password"] == password

    def request_reset(self, name):
        key = nodeprep.canonical(name)
        if key not in self.accounts:
            return None
        return {"account": key}

    def complete_reset(self, ticket, new_password):
        acct = self.accounts.get(nodeprep.canonical(ticket["account"]))
        if acct is None:
            return False
        acct["password"] = new_password
        return True

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Username That Changed When Asked Twice — Python Bug Hunt

Modelled on Spotify's June 2013 account-hijacking bug, described in Spotify's own engineering write-up.

  • Language: Python
  • Layer: Backend
  • Difficulty: Hard
  • Concepts: Security, Unicode, Auth
  • Modelled on: Spotify · 2013
  • Visible tests: an ordinary account registers and resets; a name whose canonical form is unstable is refused
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-1 (priority: Critical (account takeover))
  • Ticket: BUG-SPOT-1306
  • Reported by: security
  • Time to fix: 35 minutes, the target for a hard incident

Failing checks on the shipped build

  • a name whose canonical form is unstable is refused — canonical(canonical(name)) differs from canonical(name): expected an error to be raised

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Modelled on Spotify's June 2013 account-hijacking bug, described in Spotify's own engineering write-up. Usernames were canonicalised with an XMPP "nodeprep" routine that was not idempotent for some Unicode characters: canonicalising ᴮᴵᴳᴮᴵᴿᴰ gave BIGBIRD, and canonicalising that again gave bigbird. Because the password-reset flow canonicalised a name that was already canonical, an attacker could register the odd-looking name and reset the password of the ordinary account it collapsed into. Spotify's fix rejected names whose canonical form is not stable.

nodeprep.py (locked) reproduces the non-idempotent mapping; accounts.py trusts it.

Fix AccountStore so no name can be steered into another account.

Bug report

BUG-SPOT-1306 · Priority: Critical (account takeover) · Reported by: security

AccountStore:

  • register(name, password): key = canonical(name). Raise ValueError if the key is not stable (canonical(key) != key) or is already taken. Return key.
  • check_password(name, password): looks up canonical(name).
  • request_reset(name): {"account": canonical(name)} or None if no account.
  • complete_reset(ticket, new_password): ticket["account"] is already a canonical key — apply the change to exactly that account, without canonicalising again. Unknown key -> False, nothing changes.

Observed: after registering a name written in modifier letters, resetting its password changed the password of the account "bigbird".

Logs

[accounts] register display="ᴮᴵᴳᴮᴵᴿᴰ" key=BIGBIRD
[reset] ticket account=BIGBIRD -> applied to account=bigbird

The code as shipped

src/accounts/accounts.py (editable)

nodeprep = bug_require("./nodeprep.py")


class AccountStore:
    def __init__(self):
        self.accounts = {}

    def register(self, name, password):
        key = nodeprep.canonical(name)
        if key in self.accounts:
            raise ValueError("username taken")
        self.accounts[key] = {"display": name, "password": password}
        return key

    def check_password(self, name, password):
        acct = self.accounts.get(nodeprep.canonical(name))
        return acct is not None and acct["password"] == password

    def request_reset(self, name):
        key = nodeprep.canonical(name)
        if key not in self.accounts:
            return None
        return {"account": key}

    def complete_reset(self, ticket, new_password):
        acct = self.accounts.get(nodeprep.canonical(ticket["account"]))
        if acct is None:
            return False
        acct["password"] = new_password
        return True

Read-only context: src/accounts/nodeprep.py.

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Python bug hunts.