Practical technical guides / Core IT: security (Unicode identifier normalization)

Two Accounts That Look Like One: Normalize Unicode Usernames Before Comparing Them

Fullwidth, decomposed and differently cased names compare unequal. Measured NFC, NFKC, lower-casing and casefold in Python 3.13 and applied the RFC 8265 username order.

By Sandeep Belgavi · Published 2026-11-11

Fullwidth, decomposed and differently cased names compare unequal. Measured NFC, NFKC, lower-casing and casefold in Python 3.13 and applied the RFC 8265 username order.

Two accounts, one name on screen

The customer forum at Belgavi's Book Store has a rule: usernames are unique, ignoring case. The check is a database query with lower(username) = lower(?). One morning Geetha, who moderates the forum, saw two accounts that both displayed as "Geetha". One was hers. The other had been registered with fullwidth letters, the forms used in East Asian typography, which look almost the same in most fonts and compare as different characters.

The same week a customer could not log in as "José": the account had been created on a phone that sent the accented e as a plain e followed by a combining acute accent, and the laptop's browser sent the single precomposed character. Both render identically. Byte for byte they differ.

Both problems have one cause. Strings that people read as the same can be different sequences of Unicode code points, and a uniqueness check or a login lookup that compares raw strings, or only lower-cases them, treats them as different. This article measures what Unicode normalization and case mapping do to such pairs in Python 3.13, applies the order of operations from RFC 8265, the IETF profile for usernames, and shows how to store and enforce a canonical key.

Four kinds of look-alike

Unicode has several reasons why different code point sequences render the same way.

  • Canonical equivalence. "é" can be one code point, U+00E9, or "e" plus U+0301 COMBINING ACUTE ACCENT. Unicode treats them as the same character. Normalization Form C (NFC) composes them; NFD decomposes them.
  • Compatibility equivalence. Fullwidth "G" (U+FF27), the ligature "fi" (U+FB01) and many others are compatibility variants of ordinary letters. NFKC and NFKD map them to the ordinary forms. NFC leaves them alone.
  • Case. "Jenya" and "jenya". Lower-casing or case folding maps them together; normalization does not.
  • Confusables. Cyrillic "і" (U+0456) looks like Latin "i" but is a different letter of a different script. No normalization form maps one to the other. Unicode Technical Standard 39 defines confusable detection separately.

What each method merges

The companion compares nine pairs under five methods, using Python 3.13, which ships Unicode 15.1. A cell is "same" when the two names give the same key.

PairexactNFCNFKClower then NFCNFKC casefold
Jenya, jenyadifferentdifferentdifferentsamesame
Geetha, fullwidth Geethadifferentdifferentsamedifferentsame
José composed, decomposeddifferentsamesamesamesame
Åsa with U+00C5, with U+212B ANGSTROM SIGNdifferentsamesamesamesame
fiona, U+FB01 ligature onadifferentdifferentsamedifferentsame
strasse, straßedifferentdifferentdifferentdifferentsame
dinky, Cyrillic dіnkydifferentdifferentdifferentdifferentdifferent
hansini, with a zero-width spacedifferentdifferentdifferentdifferentdifferent
tanishka, with a trailing spacedifferentdifferentdifferentdifferentdifferent

Reading down the columns:

  • Exact comparison merges nothing. It is the bug.
  • NFC fixes composed and decomposed accents and the Angstrom sign, which has a canonical mapping to Å. It does nothing about case or width.
  • Lower-casing then NFC is what many systems do. It fixes case and accents and still lets fullwidth "Geetha" in.
  • NFKC with casefold merges the most, including "straße" and "strasse", because case folding maps ß to "ss". Whether that is desirable depends on the language community; it is more aggressive than lower-casing.
  • Nothing merges the confusable. "dinky" with a Cyrillic і stays distinct under every method. Neither do invisible characters or spaces; those need to be rejected, not normalized.

The RFC 8265 order of operations

RFC 8265 (October 2017) defines PRECIS profiles for usernames and passwords on top of the PRECIS framework in RFC 8264. Its UsernameCaseMapped profile is a good default for login names. The order matters, and the RFC spells it out.

Preparation:

  1. Apply the width mapping rule: map fullwidth and halfwidth code points to their decomposition mappings.
  2. Check that every code point is allowed by the IdentifierClass defined in RFC 8264.

Enforcement, in order:

  1. Case mapping: map uppercase and titlecase code points to lowercase, preferably with Unicode toLowerCase.
  2. Normalization: apply NFC.
  3. Directionality: apply the Bidi Rule from RFC 5893 to strings that contain right-to-left code points.
  4. Make sure the result is not empty.

Two strings are the same username if and only if their enforced forms are identical code point for code point.

Width mapping comes first so that fullwidth letters become ordinary ones instead of being rejected. Everything else that NFKC would change is rejected by the IdentifierClass: RFC 8264 puts any code point for which toNFKC(cp) != cp in its HasCompat category and disallows it. That is why the profile rejects the "fi" ligature instead of mapping it, and it also rejects U+212B ANGSTROM SIGN, whose canonical singleton mapping to U+00C5 changes it under NFKC too. The IdentifierClass also disallows spaces, symbols and punctuation outside printable ASCII, and ignorable code points such as the zero-width space.

A canonical key in code

The companion's username_key follows that order. Its class check is an approximation built on Python's unicodedata, not the full PRECIS derived property table, and it rejects right-to-left strings instead of implementing the Bidi Rule; the comments say so.

import unicodedata

def username_key(s):
    prepared = width_map(s)                      # fullwidth and halfwidth to ordinary forms
    ok, reason = identifier_class_ok(prepared)   # spaces, controls, HasCompat, symbols, RTL
    if not ok:
        raise ValueError(reason)
    key = unicodedata.normalize('NFC', prepared.lower())
    if not key:
        raise ValueError('empty after enforcement')
    return key

In production, use a maintained PRECIS implementation for your language rather than this sketch, and pin the Unicode version it uses.

Store the key, keep the display name

Normalization belongs in the data model, not only in the login form:

  • Two columns. Store what the user typed for display, and the canonical key for comparison.
  • A unique index on the key. The companion creates CREATE UNIQUE INDEX users_key ON users (username_key) in SQLite. Registering "Jenya" succeeds; "jenya", "JENYA" and "Jen" with a fullwidth "y" then fail as duplicates; "José" in either encoding is one account.
  • The same function everywhere. Registration, login, password reset, mentions and admin search must compute the key the same way. A second implementation in another language is a second chance to disagree.
  • Reject rather than silently map what the profile disallows, and tell the user which character is the problem.

Migrating existing accounts

Adding a key to a table that already has users is where the real work is. Compute the key for every row, then group by key. Each group with more than one account is a collision that already exists. Do not merge accounts automatically: they may belong to different people. Keep the existing accounts working, block new registrations that collide, and handle each group by hand, for example by asking the newer account to choose another name at next login. Rows whose names the new profile rejects need the same treatment.

Confusables need a separate check

Normalization will not catch "dinky" registered with a Cyrillic і. Unicode Technical Standard 39 defines a skeleton function that maps confusable characters to a common prototype; two strings with the same skeleton are visually confusable. Use it as a warning or a review flag at registration, not as the uniqueness key, because it merges strings that are legitimately different in other scripts. A simpler policy, adequate for many forums, is to reject usernames that mix scripts.

Version drift

Normalization and case mapping depend on the Unicode version of the library. Unicode's stability policies guarantee that the normalization of assigned characters does not change between versions, but new characters are added, and a key computed by one runtime may be rejected by another that does not know a newer character. Record the Unicode version used to compute stored keys, and test the key function in every runtime that computes it. The companion prints the version it ran with.

Limits

The class check is an approximation for teaching, and the Bidi Rule is not implemented. Results depend on the Unicode version of the Python build. Display names, nicknames and email addresses have different rules (RFC 8266 covers nicknames); this article covers only login usernames.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/identifier_normalization. Views are my own and do not represent any employer, past or present.