pub fn normalize_homoglyphs(s: &str) -> String {
s.chars()
.map(|c| match c {
'\u{0430}' => 'a', '\u{0432}' => 'b', '\u{0435}' => 'e', '\u{043A}' => 'k', '\u{043C}' => 'm', '\u{043D}' => 'h', '\u{043E}' => 'o', '\u{0440}' => 'p', '\u{0441}' => 'c', '\u{0442}' => 't', '\u{0443}' => 'y', '\u{0445}' => 'x', '\u{0456}' => 'i', '\u{0458}' => 'j', '\u{04BB}' => 'h', '\u{0455}' => 's', '\u{0454}' => 'e', '\u{044A}' => 'b',
'\u{0410}' => 'a', '\u{0412}' => 'b', '\u{0415}' => 'e', '\u{041A}' => 'k', '\u{041C}' => 'm', '\u{041D}' => 'h', '\u{041E}' => 'o', '\u{0420}' => 'p', '\u{0421}' => 'c', '\u{0422}' => 't', '\u{0423}' => 'y', '\u{0425}' => 'x', '\u{0405}' => 's', '\u{0406}' => 'i', '\u{0408}' => 'j',
'\u{03B1}' => 'a', '\u{03B2}' => 'b', '\u{03B5}' => 'e', '\u{03B7}' => 'h', '\u{03B9}' => 'i', '\u{03BA}' => 'k', '\u{03BC}' => 'm', '\u{03BD}' => 'v', '\u{03BF}' => 'o', '\u{03C1}' => 'p', '\u{03C4}' => 't', '\u{03C5}' => 'u', '\u{03C7}' => 'x', '\u{03C9}' => 'w', '\u{03B6}' => 'z',
'\u{0391}' => 'a', '\u{0392}' => 'b', '\u{0395}' => 'e', '\u{0397}' => 'h', '\u{0399}' => 'i', '\u{039A}' => 'k', '\u{039C}' => 'm', '\u{039D}' => 'n', '\u{039F}' => 'o', '\u{03A1}' => 'p', '\u{03A4}' => 't', '\u{03A7}' => 'x', '\u{03A5}' => 'y', '\u{0396}' => 'z',
'\u{0561}' => 'a', '\u{0570}' => 'h', '\u{0578}' => 'n', '\u{0585}' => 'o', '\u{057D}' => 's', '\u{0582}' => 'u',
c @ '\u{FF21}'..='\u{FF3A}' => (c as u32 - 0xFF21 + b'a' as u32) as u8 as char,
c @ '\u{FF41}'..='\u{FF5A}' => (c as u32 - 0xFF41 + b'a' as u32) as u8 as char,
c @ '\u{FF10}'..='\u{FF19}' => (c as u32 - 0xFF10 + b'0' as u32) as u8 as char,
'\u{FF3F}' => '_',
'\u{04C0}' => 'i', '\u{04CF}' => 'l',
'\u{13AA}' => 'g', '\u{13B3}' => 'w', '\u{13CB}' => 'h', '\u{13A1}' => 'e', '\u{13A2}' => 'r', '\u{13DA}' => 's', '\u{13E4}' => 't', '\u{13AC}' => 'h', '\u{13D9}' => 'v', '\u{13CF}' => 'b', '\u{13D2}' => 'p', '\u{13A0}' => 'd',
'\u{0131}' => 'i', '\u{1D00}' => 'a', '\u{0261}' => 'g', '\u{01C0}' => 'l', '\u{2010}' | '\u{2011}' | '\u{2012}' | '\u{2013}' | '\u{2014}' | '\u{2015}' => '-', '\u{2018}' | '\u{2019}' | '\u{02BC}' => '\'',
other => other,
})
.collect()
}
pub fn normalize_identity(s: &str) -> String {
normalize_homoglyphs(&s.to_lowercase())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cyrillic_a_to_latin_a() {
assert_eq!(normalize_homoglyphs("аdmin"), "admin");
assert_eq!(normalize_homoglyphs("\u{0430}dmin"), "admin");
}
#[test]
fn test_cyrillic_spoofed_admin() {
assert_eq!(normalize_homoglyphs("аdmin"), "admin"); assert_eq!(normalize_homoglyphs("admіn"), "admin"); assert_eq!(normalize_homoglyphs("аdmіn"), "admin"); }
#[test]
fn test_cyrillic_spoofed_password() {
assert_eq!(normalize_homoglyphs("раssword"), "password"); assert_eq!(normalize_homoglyphs("pаsswоrd"), "password"); }
#[test]
fn test_greek_spoofed_admin() {
assert_eq!(normalize_homoglyphs("αdmin"), "admin"); assert_eq!(normalize_homoglyphs("admιn"), "admin"); }
#[test]
fn test_fully_spoofed_tool_name() {
let spoofed = "r\u{0435}\u{0430}d_f\u{0456}l\u{0435}";
assert_eq!(normalize_homoglyphs(spoofed), "read_file");
}
#[test]
fn test_cyrillic_lowercase_comprehensive() {
assert_eq!(normalize_homoglyphs("\u{0430}"), "a"); assert_eq!(normalize_homoglyphs("\u{0435}"), "e"); assert_eq!(normalize_homoglyphs("\u{043E}"), "o"); assert_eq!(normalize_homoglyphs("\u{0440}"), "p"); assert_eq!(normalize_homoglyphs("\u{0441}"), "c"); assert_eq!(normalize_homoglyphs("\u{0445}"), "x"); assert_eq!(normalize_homoglyphs("\u{0443}"), "y"); assert_eq!(normalize_homoglyphs("\u{0456}"), "i"); assert_eq!(normalize_homoglyphs("\u{0458}"), "j"); }
#[test]
fn test_cyrillic_uppercase_comprehensive() {
assert_eq!(normalize_homoglyphs("\u{0410}"), "a"); assert_eq!(normalize_homoglyphs("\u{0415}"), "e"); assert_eq!(normalize_homoglyphs("\u{041E}"), "o"); assert_eq!(normalize_homoglyphs("\u{0420}"), "p"); assert_eq!(normalize_homoglyphs("\u{0421}"), "c"); assert_eq!(normalize_homoglyphs("\u{0423}"), "y"); assert_eq!(normalize_homoglyphs("\u{0408}"), "j"); }
#[test]
fn test_fullwidth_latin() {
assert_eq!(
normalize_homoglyphs("\u{FF21}\u{FF24}\u{FF2D}\u{FF29}\u{FF2E}"),
"admin"
);
assert_eq!(
normalize_homoglyphs("\u{FF42}\u{FF41}\u{FF53}\u{FF48}"),
"bash"
);
assert_eq!(normalize_homoglyphs("\u{FF10}\u{FF11}\u{FF12}"), "012");
}
#[test]
fn test_greek_confusables() {
assert_eq!(normalize_homoglyphs("\u{03B1}"), "a"); assert_eq!(normalize_homoglyphs("\u{03B5}"), "e"); assert_eq!(normalize_homoglyphs("\u{03B9}"), "i"); assert_eq!(normalize_homoglyphs("\u{03BF}"), "o"); assert_eq!(normalize_homoglyphs("\u{03C1}"), "p"); assert_eq!(normalize_homoglyphs("\u{03C4}"), "t"); assert_eq!(normalize_homoglyphs("\u{03C7}"), "x"); }
#[test]
fn test_already_ascii_unchanged() {
assert_eq!(normalize_homoglyphs("admin"), "admin");
assert_eq!(normalize_homoglyphs("read_file"), "read_file");
assert_eq!(normalize_homoglyphs("user@example.com"), "user@example.com");
}
#[test]
fn test_empty_string() {
assert_eq!(normalize_homoglyphs(""), "");
}
#[test]
fn test_various_dashes() {
assert_eq!(normalize_homoglyphs("foo\u{2010}bar"), "foo-bar"); assert_eq!(normalize_homoglyphs("foo\u{2013}bar"), "foo-bar"); assert_eq!(normalize_homoglyphs("foo\u{2014}bar"), "foo-bar"); }
#[test]
fn test_curly_quotes() {
assert_eq!(normalize_homoglyphs("it\u{2019}s"), "it's"); assert_eq!(normalize_homoglyphs("\u{2018}test\u{2019}"), "'test'"); }
#[test]
fn test_self_approval_bypass_prevented() {
let requester = "admin";
let attacker_approver = "аdmin";
let normalized_req = normalize_homoglyphs(requester);
let normalized_app = normalize_homoglyphs(attacker_approver);
assert_eq!(
normalized_req, normalized_app,
"Self-approval bypass: Cyrillic homoglyph must normalize to same value"
);
}
#[test]
fn test_email_style_identity_spoofing() {
let real = "alice@example.com";
let spoofed = "аlice@example.com";
assert_eq!(
normalize_homoglyphs(real),
normalize_homoglyphs(spoofed),
"Email identity spoofing must be detected"
);
}
#[test]
fn test_normalize_identity_empty_string() {
assert_eq!(normalize_identity(""), "");
}
#[test]
fn test_normalize_identity_pure_ascii() {
assert_eq!(normalize_identity("admin"), "admin");
}
#[test]
fn test_normalize_identity_case_folding() {
assert_eq!(normalize_identity("AgentAlpha"), "agentalpha");
assert_eq!(normalize_identity("ADMIN"), "admin");
}
#[test]
fn test_normalize_identity_cyrillic_with_case() {
assert_eq!(
normalize_identity("\u{0410}GENT"),
normalize_identity("agent")
);
}
#[test]
fn test_normalize_identity_mixed_homoglyphs_case() {
assert_eq!(
normalize_identity("\u{0430}dmin"),
normalize_identity("Admin")
);
assert_eq!(
normalize_identity("\u{0410}DMIN"),
normalize_identity("admin")
);
}
#[test]
fn test_normalize_identity_reflexive() {
let input = "AgentAlph\u{0430}";
let once = normalize_identity(input);
let twice = normalize_identity(&once);
assert_eq!(once, twice, "normalize_identity must be idempotent");
}
}