From eb9024f81cd3698d70e7d2118b5da2a9a5a69383 Mon Sep 17 00:00:00 2001 From: tuxmain <tuxmain@zettascript.org> Date: Sat, 28 May 2022 17:48:44 +0200 Subject: [PATCH] fix(common-runtime): stricter idty name validation --- runtime/common/src/lib.rs | 15 ++++++++++++++- runtime/gdev/tests/integration_tests.rs | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index d06efc221..9d8c1f30c 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -83,7 +83,20 @@ impl<Runtime: pallet_babe::Config> frame_support::pallet_prelude::Get<u64> pub struct IdtyNameValidatorImpl; impl pallet_identity::traits::IdtyNameValidator for IdtyNameValidatorImpl { + // Bound length; forbid trailing or double spaces; accept only ascii alphanumeric or punctuation or space fn validate(idty_name: &pallet_identity::IdtyName) -> bool { - idty_name.0.len() >= 3 && idty_name.0.len() <= 64 + idty_name.0.len() >= 3 + && idty_name.0.len() <= 64 + && idty_name.0[0] != 32 + && idty_name.0[idty_name.0.len() - 1] != 32 + && idty_name + .0 + .iter() + .all(|c| c.is_ascii_alphanumeric() || c.is_ascii_punctuation() || *c == 32) + && idty_name + .0 + .iter() + .zip(idty_name.0.iter().skip(1)) + .all(|(c1, c2)| *c1 != 32 || *c2 != 32) } } diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs index 6ffc2f586..7e7656c0c 100644 --- a/runtime/gdev/tests/integration_tests.rs +++ b/runtime/gdev/tests/integration_tests.rs @@ -342,5 +342,14 @@ fn test_create_new_idty() { Account::pending_random_id_assignments(0), Some(AccountKeyring::Eve.to_account_id()) ); + + // Double space is forbidden in identity name + assert_err!( + Identity::confirm_identity( + frame_system::RawOrigin::Signed(AccountKeyring::Eve.to_account_id()).into(), + pallet_identity::IdtyName::from("invalid name"), + ), + pallet_identity::Error::<Runtime>::IdtyNameInvalid + ); }); } -- GitLab