Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Petname anti-mimicry: Unicode UTS #39 confusable skeleton

- **Date:** 2026-11-02T12-47-00Z
- **Kind:** design
- **Issue:** developing-today/code#2899 (follow-ups to #2069 and #1674)
- **Code:** `pkgs/id/crates/id-core/src/petname.rs` (`skeleton()`), PR #4268

## Problem

Petnames are a trust surface: a contact's petname is how the owner recognizes
who they are talking to, so `Petnames::set` refuses a new petname that is
confusable with an existing petname or an alleged name. Until now the
confusability check was a handwritten ASCII skeleton in `skeleton()`:

- `fold()` strips Cf format characters (#1674) and lowercases;
- `skeleton()` then mapped a fixed set of ASCII look-alikes (`0`/`o`,
`1`/`|`/`i`/`l`, `8`/`b`, ...; the `I`/`8` additions are #2069).

That map cannot enumerate cross-script homoglyphs. Verified on main before
this change: Cyrillic U+0430/U+0410/U+043E/U+0435/U+0421 and Greek U+03BF
pairs (e.g. Latin `PayPal` vs Cyrillic-а `Pаypal`) produced different folds
and different skeletons, so `set()` accepted them — a spoof petname needed
no invisible characters to defeat the guard. `labels()` keys collision
fingerprints by the same skeleton, so legacy cross-script collisions also
went unfingerprinted.

## Decision

`skeleton()` now computes, in order:

1. `fold(name)` — strip Cf format characters, lowercase (unchanged, #1674).
2. The Unicode UTS #39 confusable skeleton of the folded name, via the
`unicode-security` crate (v0.1.3, `unicode_security::skeleton`).
3. `fold()` again over the skeleton output, so any case/format characters
the skeleton mapping introduces are normalized the same way as input.
4. The existing petname-specific ASCII look-alike map, unchanged.

Both comparison paths consume this one function — `Petnames::set` (against
existing petnames and alleged names) and `labels()` collision keying — so
legacy cross-script collisions receive fingerprints from the same skeleton
that now refuses new ones.

## Rationale and alternatives

- **UTS #39 skeleton, not a bigger handwritten map.** The confusable table
is a maintained Unicode data set covering Cyrillic, Greek, and the other
scripts; a hand-maintained map is what left the cross-script hole after
two rounds of ASCII additions (#2069, #1674).
- **ASCII map retained, applied after the Unicode skeleton.** UTS #39 does
not treat leet-style digit/letter pairs (`1`/`l`, `0`/`o`, `8`/`b`,
`rn`/`m`) as confusable, and existing behavior depends on them
(`PayPa1`/`rnurn` controls). Dropping the map would regress #2069.
- **Folding order: fold before and after.** Lowercasing first puts capital
look-alikes into the classes the skeleton data expects; folding the
skeleton output keeps the pipeline idempotent in case/format handling.
- **Rejected: blanket rejection of non-Latin names.** Petnames legitimately
exist in other scripts; a single-script Cyrillic name (e.g. `Иван`) must
stay accepted. The skeleton only makes cross-script *look-alikes of an
existing name* collide; unrelated names in any script are unaffected.
- **Rejected: mixed-script rejection alone.** It misses same-name spoofs
written with homoglyphs and rejects legitimate mixed-script names that
mimic nothing when used as the sole test.

## Compatibility

- Names previously accepted keep the same skeleton unless they are
UTS #39-confusable with another name — in which case the pair was a
spoofing hazard by definition. Single-script non-Latin names and
unrelated names are unchanged.
- `labels()` fingerprints: pairs that previously shared no skeleton key
(cross-script collisions already stored) now key together and receive
distinguishing fingerprints, which is the intended disclosure behavior.

## Verification

Regression tests in `petname.rs` (`test_` prefix per pkgs/id/AGENTS.md):

- six Cyrillic/Greek homoglyph pairs refused against both an existing
petname and an alleged name;
- ASCII controls (`PayPa1`, `rnurn`, `M0m`) still refused;
- unrelated names and a single-script Cyrillic name accepted;
- legacy cross-script petname collisions get distinct `labels()`
fingerprints.

Run: `cargo test --lib petname` in `pkgs/id`.

---

## 2026-11-02T12-47-00Z Decision: record added with the change

This record is filed in the same PR as the implementation (PR #4268) per
the pkgs/id documentation protocol; the pipeline above is the policy the
shipped code implements, with no deviations.
17 changes: 17 additions & 0 deletions pkgs/id/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgs/id/crates/id-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tempfile = "3"
thiserror = "2"
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
unicode-security = "0.1.3"
unicode-segmentation = "1.13"
unicode-width = "0.2"

Expand Down
94 changes: 89 additions & 5 deletions pkgs/id/crates/id-core/src/petname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,17 @@ pub fn fold(name: &str) -> String {
strip_format_chars(name).to_lowercase()
}

/// The name with look-alike characters mapped to one form, so `PayPa1` and
/// `PayPal` compare equal. The fold lowercases first, so capital `I` lands
/// in the `i` class and maps to `l` with `1` and `|` (#2069); `8` maps to
/// `b`, symmetric with the other mapped digits.
/// The name's UTS #39 confusable skeleton, plus the petname-specific ASCII
/// look-alike rules, so cross-script look-alikes and `PayPa1`/`PayPal` compare
/// equal. The fold lowercases first, so capital `I` lands in the `i` class and
/// maps to `l` with `1` and `|` (#2069); `8` maps to `b`, symmetric with the
/// other mapped digits.
#[must_use]
pub fn skeleton(name: &str) -> String {
let mapped: String = fold(name)
let folded = fold(name);
let unicode_skeleton: String = unicode_security::skeleton(&folded).collect();
let folded_skeleton = fold(&unicode_skeleton);
Comment on lines +315 to +317

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add the required dated record for the skeleton policy change

Replacing the handwritten skeleton with Unicode normalization changes which petnames are accepted and which existing names receive collision fingerprints. This also introduces policy choices about folding order and retaining custom ASCII equivalences, but the commit contains no dated design record. A repository-wide Markdown search found only the original petnames design describing the handwritten mapping. Add the dated document required by pkgs/id/AGENTS.md, explaining the normalization pipeline, its rationale, and compatibility with existing names.

AGENTS.md reference: pkgs/id/AGENTS.md:L159-L168

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f587378: added the dated design record required by pkgs/id/AGENTS.md at doc/2026-11-02T12-47-00Z_design_petname_unicode_confusable_skeleton/2026-11-02T12-47-00Z_design_petname_unicode_confusable_skeleton.md. It documents the normalization pipeline (fold -> UTS #39 skeleton via unicode-security 0.1.3 -> fold -> retained petname ASCII map), the folding-order rationale, why the ASCII equivalences are kept (#2069), compatibility for existing names (single-script non-Latin names such as a Cyrillic name stay accepted; no blanket or mixed-script rejection), and the labels() fingerprint effect for legacy cross-script collisions.

let mapped: String = folded_skeleton
.chars()
.map(|c| match c {
'0' => 'o',
Expand Down Expand Up @@ -449,6 +453,86 @@ mod tests {
assert!(store.get(OWNER, CAROL).is_none());
}

#[test]
fn test_cross_script_confusables_are_refused_for_petnames_and_alleged_names() {
for (latin, confusable) in [
("Alice", "Аlice"),
("PayPal", "Pаypal"),
("Bob", "Bоb"),
("Peter", "Pеter"),
("Carol", "Сarol"),
("bob", "bοb"),
] {
let mut store = Petnames::new();
store.set(OWNER, ALICE, latin, []).unwrap();
let error = store.set(OWNER, BOB, confusable, []).unwrap_err();
assert!(
matches!(refusal(&error), Some(Refusal::Conflict(_))),
"{confusable:?} should not mimic the existing petname {latin:?}"
);

let mut store = Petnames::new();
let error = store
.set(OWNER, BOB, confusable, [(ALICE, latin)])
.unwrap_err();
assert!(
matches!(refusal(&error), Some(Refusal::Conflict(_))),
"{confusable:?} should not mimic the alleged name {latin:?}"
);
}
}

#[test]
fn test_ascii_confusable_controls_remain_refused() {
for (latin, confusable) in [("PayPal", "PayPa1"), ("mum", "rnurn"), ("Mom", "M0m")] {
let mut store = Petnames::new();
store.set(OWNER, ALICE, latin, []).unwrap();
let error = store.set(OWNER, BOB, confusable, []).unwrap_err();
assert!(
matches!(refusal(&error), Some(Refusal::Conflict(_))),
"{confusable:?} should remain confusable with {latin:?}"
);
}
}

#[test]
fn test_unrelated_and_single_script_cyrillic_petnames_are_accepted() {
let mut store = Petnames::new();
store.set(OWNER, ALICE, "Иван", []).unwrap();
store.set(OWNER, BOB, "Dana", []).unwrap();
assert_eq!(store.get(OWNER, ALICE), Some("Иван"));
assert_eq!(store.get(OWNER, BOB), Some("Dana"));
}

#[test]
fn test_legacy_cross_script_petname_collisions_get_fingerprints() {
let contacts = [
Contact {
id: ALICE,
alleged: "Alice",
petname: Some("PayPal"),
},
Contact {
id: BOB,
alleged: "Bob",
petname: Some("Pаypal"),
},
];
assert_eq!(
labels(&contacts),
vec![
Label::SharedPetname {
name: "PayPal".to_owned(),
fingerprint: "aa11aa".to_owned(),
},
Label::SharedPetname {
name: "Pаypal".to_owned(),
fingerprint: "bb22bb".to_owned(),
},
]
);
}

#[test]
fn test_a_capital_i_confusable_name_is_refused() {
// #2069: PayPaI must be refused exactly like PayPa1.
Expand Down
Loading