Skip to content
Snippets Groups Projects
Commit bb12ac37 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

restrict identity name

parent f6836691
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !119. Comments created here will be created in the context of that merge request.
...@@ -17,23 +17,15 @@ ...@@ -17,23 +17,15 @@
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
/// Rules for valid identity names are defined below /// Rules for valid identity names are defined below
/// - Bound length to 64 /// - Bound length to 42
/// - forbid trailing or double spaces /// - accept only ascii alphanumeric or - or _
/// - accept only ascii alphanumeric or punctuation or space
pub fn validate_idty_name(idty_name: &[u8]) -> bool { pub fn validate_idty_name(idty_name: &[u8]) -> bool {
idty_name.len() >= 3 idty_name.len() >= 3
&& idty_name.len() <= 64 // length smaller than 64 && idty_name.len() <= 42 // length smaller than 42
&& idty_name[0] != 32 // does not start with space // all characters are alphanumeric or - or _
&& idty_name[idty_name.len() - 1] != 32 // does not end with space
// all characters are alphanumeric or punctuation or space
&& idty_name && idty_name
.iter() .iter()
.all(|c| c.is_ascii_alphanumeric() || c.is_ascii_punctuation() || *c == 32) .all(|c| c.is_ascii_alphanumeric() || *c == b'-' || *c == b'_')
// disallow two consecutive spaces
&& idty_name
.iter()
.zip(idty_name.iter().skip(1))
.all(|(c1, c2)| *c1 != 32 || *c2 != 32)
} }
#[cfg(test)] #[cfg(test)]
...@@ -42,12 +34,19 @@ mod tests { ...@@ -42,12 +34,19 @@ mod tests {
#[test] #[test]
fn test_validate_idty_name() { fn test_validate_idty_name() {
// --- allow
assert!(validate_idty_name(b"B0b")); assert!(validate_idty_name(b"B0b"));
assert!(validate_idty_name(b"lorem ipsum dolor-sit_amet.")); assert!(validate_idty_name(b"lorem_ipsum-dolor-sit_amet"));
assert!(!validate_idty_name(b" space")); assert!(validate_idty_name(
assert!(!validate_idty_name(b"space ")); b"1_______________________________________42"
assert!(!validate_idty_name(b"double space")); ));
// --- disallow
assert!(!validate_idty_name(
b"1________________________________________43"
));
assert!(!validate_idty_name(b"with space"));
assert!(!validate_idty_name("non-ascii🌵".as_bytes())); assert!(!validate_idty_name("non-ascii🌵".as_bytes()));
assert!(!validate_idty_name("ğune".as_bytes())); assert!(!validate_idty_name("ğune".as_bytes()));
assert!(!validate_idty_name("toto!".as_bytes()));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment