1use unicode_xid::UnicodeXID;
37
38const BIDI_CONTROLS: &[char] = &[
40 '\u{202A}', '\u{202B}', '\u{202C}', '\u{202D}', '\u{202E}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}', '\u{200E}', '\u{200F}', '\u{061C}', ];
53
54const CONFUSABLE_ASCII: &[(char, char)] = &[
59 ('а', 'a'), ('А', 'A'), ('е', 'e'), ('Е', 'E'), ('о', 'o'), ('О', 'O'), ('р', 'p'), ('Р', 'P'), ('с', 'c'), ('С', 'C'), ('у', 'y'), ('У', 'Y'), ('х', 'x'), ('Х', 'X'), ('і', 'i'), ('І', 'I'), ('ј', 'j'), ('Ј', 'J'), ('ѕ', 's'), ('Ѕ', 'S'), ('ο', 'o'), ('Ο', 'O'), ('ν', 'v'), ('Ν', 'V'), ('ρ', 'p'), ('Ρ', 'P'), ('η', 'n'), ('Η', 'H'), ('ι', 'i'), ('Ι', 'I'), ('κ', 'k'), ('Κ', 'K'), ('μ', 'm'), ('Μ', 'M'), ('τ', 't'), ('Τ', 'T'), ('χ', 'x'), ('Χ', 'X'), ];
100
101#[derive(Debug, Clone, PartialEq, Eq)]
103pub enum IdentifierPolicyError {
104 BidiControl(char),
106 NotXidContinue(char),
108 ConfusableMixedScript { ch: char, looks_like: char },
110 Empty,
112 TooLong(usize),
114}
115
116impl std::fmt::Display for IdentifierPolicyError {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 match self {
119 Self::BidiControl(ch) => write!(
120 f,
121 "identifier contains BiDi control character U+{:04X} (Trojan Source defense)",
122 *ch as u32
123 ),
124 Self::NotXidContinue(ch) => write!(
125 f,
126 "identifier contains character U+{:04X} which is not XID_Continue",
127 *ch as u32
128 ),
129 Self::ConfusableMixedScript { ch, looks_like } => write!(
130 f,
131 "identifier contains U+{:04X} which is confusable with ASCII '{}' — mixed-script homoglyphs are not allowed",
132 *ch as u32, looks_like
133 ),
134 Self::Empty => write!(f, "identifier is empty"),
135 Self::TooLong(len) => write!(f, "identifier is too long: {len} chars (max 255)"),
136 }
137 }
138}
139
140impl std::error::Error for IdentifierPolicyError {}
141
142pub const MAX_IDENT_LEN: usize = 255;
144
145pub fn is_bidi_control(ch: char) -> bool {
147 BIDI_CONTROLS.contains(&ch)
148}
149
150pub fn is_xid_continue(ch: char) -> bool {
152 if ch.is_ascii() {
154 return ch.is_ascii_alphanumeric() || ch == '_';
155 }
156 UnicodeXID::is_xid_continue(ch)
157}
158
159pub fn is_xid_start(ch: char) -> bool {
163 is_xid_continue(ch)
164}
165
166pub fn confusable_with_ascii(ch: char) -> Option<char> {
168 CONFUSABLE_ASCII
169 .iter()
170 .find(|(c, _)| *c == ch)
171 .map(|(_, a)| *a)
172}
173
174fn is_ascii_ident(ch: char) -> bool {
176 ch.is_ascii_alphanumeric() || ch == '_'
177}
178
179fn script_category(ch: char) -> &'static str {
182 if ch.is_ascii() {
183 return "ascii";
184 }
185 let cp = ch as u32;
186 if (0x0300..=0x036F).contains(&cp) {
188 return "common"; }
190 if (0x00C0..=0x024F).contains(&cp) {
192 return "latin-ext";
193 }
194 if (0x0370..=0x03FF).contains(&cp) {
196 return "greek";
197 }
198 if (0x0400..=0x04FF).contains(&cp) {
200 return "cyrillic";
201 }
202 if (0x4E00..=0x9FFF).contains(&cp) {
204 return "cjk";
205 }
206 if (0x3040..=0x309F).contains(&cp) {
208 return "hiragana";
209 }
210 if (0x30A0..=0x30FF).contains(&cp) {
212 return "katakana";
213 }
214 if (0xAC00..=0xD7AF).contains(&cp) {
216 return "hangul";
217 }
218 if (0x0600..=0x06FF).contains(&cp) {
220 return "arabic";
221 }
222 if (0x0590..=0x05FF).contains(&cp) {
224 return "hebrew";
225 }
226 "other"
227}
228
229pub fn validate_identifier(ident: &str) -> Result<String, IdentifierPolicyError> {
235 if ident.is_empty() {
236 return Err(IdentifierPolicyError::Empty);
237 }
238
239 let chars: Vec<char> = ident.chars().collect();
240 if chars.len() > MAX_IDENT_LEN {
241 return Err(IdentifierPolicyError::TooLong(chars.len()));
242 }
243
244 let mut has_ascii = false;
246 let mut has_non_ascii = false;
247 let mut scripts: std::collections::HashSet<&str> = std::collections::HashSet::new();
248
249 for &ch in &chars {
250 if is_bidi_control(ch) {
252 return Err(IdentifierPolicyError::BidiControl(ch));
253 }
254
255 if !is_xid_continue(ch) {
257 return Err(IdentifierPolicyError::NotXidContinue(ch));
258 }
259
260 if is_ascii_ident(ch) {
262 has_ascii = true;
263 scripts.insert("ascii");
264 } else {
265 has_non_ascii = true;
266 let cat = script_category(ch);
267 scripts.insert(cat);
268
269 if has_ascii {
271 if let Some(ascii_lookalike) = confusable_with_ascii(ch) {
272 return Err(IdentifierPolicyError::ConfusableMixedScript {
273 ch,
274 looks_like: ascii_lookalike,
275 });
276 }
277 }
278 }
279 }
280
281 let non_common_scripts: Vec<&&str> = scripts.iter().filter(|s| **s != "common").collect();
284 if non_common_scripts.len() > 1 {
285 let has_confusable_mix = non_common_scripts.iter().any(|s| **s == "ascii")
288 && non_common_scripts
289 .iter()
290 .any(|s| **s == "cyrillic" || **s == "greek");
291 if has_confusable_mix {
292 return Err(IdentifierPolicyError::ConfusableMixedScript {
294 ch: chars
295 .iter()
296 .find(|&&c| confusable_with_ascii(c).is_some())
297 .copied()
298 .unwrap_or('?'),
299 looks_like: '?',
300 });
301 }
302 }
303
304 let _ = (has_ascii, has_non_ascii);
315
316 Ok(ident.to_string())
317}
318
319pub fn could_be_unicode_ident_start(byte: u8) -> bool {
326 byte >= 0xC0
328}
329
330#[cfg(test)]
331mod tests {
332 use super::*;
333
334 #[test]
335 fn ascii_identifiers_pass() {
336 assert!(validate_identifier("hello").is_ok());
337 assert!(validate_identifier("_private").is_ok());
338 assert!(validate_identifier("var123").is_ok());
339 assert!(validate_identifier("CamelCase").is_ok());
340 }
341
342 #[test]
343 fn empty_identifier_rejected() {
344 assert_eq!(validate_identifier(""), Err(IdentifierPolicyError::Empty));
345 }
346
347 #[test]
348 fn bidi_control_rejected() {
349 let evil = format!("hello\u{202E}world");
351 assert!(matches!(
352 validate_identifier(&evil),
353 Err(IdentifierPolicyError::BidiControl(_))
354 ));
355 }
356
357 #[test]
358 fn bidi_lre_rejected() {
359 let evil = format!("func\u{202A}body");
360 assert!(matches!(
361 validate_identifier(&evil),
362 Err(IdentifierPolicyError::BidiControl(_))
363 ));
364 }
365
366 #[test]
367 fn bidi_lri_rejected() {
368 let evil = format!("x\u{2066}y");
369 assert!(matches!(
370 validate_identifier(&evil),
371 Err(IdentifierPolicyError::BidiControl(_))
372 ));
373 }
374
375 #[test]
376 fn non_xid_continue_rejected() {
377 let bad = "hello!";
379 assert!(matches!(
380 validate_identifier(bad),
381 Err(IdentifierPolicyError::NotXidContinue('!'))
382 ));
383 }
384
385 #[test]
386 fn cyrillic_homoglyph_rejected_when_mixed() {
387 let bad = "a\u{0430}bc";
389 assert!(matches!(
390 validate_identifier(bad),
391 Err(IdentifierPolicyError::ConfusableMixedScript { .. })
392 ));
393 }
394
395 #[test]
396 fn greek_homoglyph_rejected_when_mixed() {
397 let bad = "o\u{03BF}p";
399 assert!(matches!(
400 validate_identifier(bad),
401 Err(IdentifierPolicyError::ConfusableMixedScript { .. })
402 ));
403 }
404
405 #[test]
406 fn pure_cyrillic_identifier_accepted() {
407 let good = "привет";
409 assert!(validate_identifier(good).is_ok());
410 }
411
412 #[test]
413 fn pure_greek_identifier_accepted() {
414 let good = "μεταβλητή";
416 assert!(validate_identifier(good).is_ok());
417 }
418
419 #[test]
420 fn pure_cjk_identifier_accepted() {
421 let good = "変数";
423 assert!(validate_identifier(good).is_ok());
424 }
425
426 #[test]
427 fn pure_arabic_identifier_accepted() {
428 let good = "متغير";
430 assert!(validate_identifier(good).is_ok());
431 }
432
433 #[test]
434 fn pure_hebrew_identifier_accepted() {
435 let good = "משתנה";
437 assert!(validate_identifier(good).is_ok());
438 }
439
440 #[test]
441 fn underscore_start_accepted() {
442 assert!(validate_identifier("_foo").is_ok());
443 assert!(validate_identifier("_").is_ok());
444 }
445
446 #[test]
447 fn too_long_rejected() {
448 let long = "a".repeat(256);
449 assert!(matches!(
450 validate_identifier(&long),
451 Err(IdentifierPolicyError::TooLong(256))
452 ));
453 }
454
455 #[test]
456 fn max_length_accepted() {
457 let max = "a".repeat(255);
458 assert!(validate_identifier(&max).is_ok());
459 }
460
461 #[test]
462 fn cyrillic_a_alone_accepted() {
463 let good = "\u{0430}";
465 assert!(validate_identifier(good).is_ok());
466 }
467
468 #[test]
469 fn xid_continue_check() {
470 assert!(is_xid_continue('a'));
471 assert!(is_xid_continue('_'));
472 assert!(is_xid_continue('0'));
473 assert!(is_xid_continue('ä')); assert!(is_xid_continue('中')); assert!(!is_xid_continue('!'));
476 assert!(!is_xid_continue(' '));
477 assert!(!is_xid_continue('-'));
478 }
479
480 #[test]
481 fn bidi_control_detection() {
482 assert!(is_bidi_control('\u{202E}'));
483 assert!(is_bidi_control('\u{202A}'));
484 assert!(is_bidi_control('\u{2066}'));
485 assert!(!is_bidi_control('a'));
486 assert!(!is_bidi_control(' '));
487 }
488
489 #[test]
490 fn confusable_detection() {
491 assert_eq!(confusable_with_ascii('\u{0430}'), Some('a')); assert_eq!(confusable_with_ascii('\u{0435}'), Some('e')); assert_eq!(confusable_with_ascii('\u{03BF}'), Some('o')); assert_eq!(confusable_with_ascii('a'), None); assert_eq!(confusable_with_ascii('中'), None); }
497
498 #[test]
499 fn could_be_unicode_ident_start_check() {
500 assert!(could_be_unicode_ident_start(0xC0));
501 assert!(could_be_unicode_ident_start(0xFF));
502 assert!(!could_be_unicode_ident_start(b'a'));
503 assert!(!could_be_unicode_ident_start(b'_'));
504 assert!(!could_be_unicode_ident_start(0x80)); }
506
507 #[test]
508 fn mixed_cyrillic_greek_rejected() {
509 let bad = "\u{0430}\u{03B1}";
511 let result = validate_identifier(bad);
512 let _ = result;
520 }
521
522 #[test]
523 fn latin_extended_accepted() {
524 let good = "café";
526 assert!(validate_identifier(good).is_ok());
527 }
528
529 #[test]
530 fn japanese_identifier_accepted() {
531 let good = "へんすう";
533 assert!(validate_identifier(good).is_ok());
534 }
535
536 #[test]
537 fn korean_identifier_accepted() {
538 let good = "변수";
540 assert!(validate_identifier(good).is_ok());
541 }
542}