From 667ecb31685386e20c48e805744b0ad5e0a212a1 Mon Sep 17 00:00:00 2001 From: blu3beri Date: Tue, 10 Jan 2023 19:48:14 +0100 Subject: [PATCH] seperate regex to new validation.rs util Signed-off-by: blu3beri --- anoncreds/src/data_types/anoncreds/macros.rs | 27 ++++++-------------- anoncreds/src/lib.rs | 2 ++ anoncreds/src/services/verifier.rs | 16 +++++------- anoncreds/src/utils/mod.rs | 2 ++ anoncreds/src/utils/validation.rs | 16 ++++++++++++ 5 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 anoncreds/src/utils/mod.rs create mode 100644 anoncreds/src/utils/validation.rs diff --git a/anoncreds/src/data_types/anoncreds/macros.rs b/anoncreds/src/data_types/anoncreds/macros.rs index e07336ae..55fd2cc3 100644 --- a/anoncreds/src/data_types/anoncreds/macros.rs +++ b/anoncreds/src/data_types/anoncreds/macros.rs @@ -1,9 +1,6 @@ #[macro_export] macro_rules! impl_anoncreds_object_identifier { ($i:ident) => { - use once_cell::sync::Lazy; - use regex::Regex; - #[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, Default)] pub struct $i(pub String); @@ -21,25 +18,17 @@ macro_rules! impl_anoncreds_object_identifier { impl $crate::data_types::Validatable for $i { fn validate(&self) -> Result<(), $crate::data_types::ValidationError> { - // TODO: stricten the URI regex. - // Right now everything after the first colon is allowed, - // we might want to restrict this - static REGEX_URI: Lazy = - Lazy::new(|| Regex::new(r"^[a-zA-Z0-9\+\-\.]+:.+$").unwrap()); - - /// base58 alpahet as defined in - /// https://datatracker.ietf.org/doc/html/draft-msporny-base58#section-2 - /// This is used for legacy indy identifiers that we will keep supporting for - /// backwards compatibility. This might validate invalid identifiers if they happen - /// to fall within the base58 alphabet, but there is not much we can do about that. - static LEGACY_IDENTIFIER: Lazy = - Lazy::new(|| Regex::new("^[1-9A-HJ-NP-Za-km-z]{21,22}$").unwrap()); - - if REGEX_URI.captures(&self.0).is_some() { + if $crate::utils::validation::URI_IDENTIFIER + .captures(&self.0) + .is_some() + { return Ok(()); } - if LEGACY_IDENTIFIER.captures(&self.0).is_some() { + if $crate::utils::validation::LEGACY_IDENTIFIER + .captures(&self.0) + .is_some() + { return Ok(()); } diff --git a/anoncreds/src/lib.rs b/anoncreds/src/lib.rs index 3bc5f597..e24b1e56 100644 --- a/anoncreds/src/lib.rs +++ b/anoncreds/src/lib.rs @@ -23,6 +23,8 @@ pub use self::error::{Error, ErrorKind}; mod services; pub use services::*; +mod utils; + #[cfg(feature = "ffi")] mod ffi; diff --git a/anoncreds/src/services/verifier.rs b/anoncreds/src/services/verifier.rs index 69644e60..e1ba17b2 100644 --- a/anoncreds/src/services/verifier.rs +++ b/anoncreds/src/services/verifier.rs @@ -19,6 +19,7 @@ use crate::data_types::anoncreds::{ }; use crate::error::Result; use crate::ursa::cl::{verifier::Verifier as CryptoVerifier, CredentialPublicKey}; +use crate::utils::validation::LEGACY_IDENTIFIER; use indy_utils::query::Query; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] @@ -571,21 +572,18 @@ fn verify_requested_restrictions( .map(|(referent, info)| (referent.to_string(), info.clone())) .collect(); - let requested_attributes_queries: Vec = pres_req + let requested_attributes_queries = pres_req .requested_attributes .iter() - .filter_map(|(_, info)| info.restrictions.to_owned()) - .collect(); + .filter_map(|(_, info)| info.restrictions.to_owned()); - let requested_predicates_queries: Vec = pres_req + let requested_predicates_queries = pres_req .requested_predicates .iter() - .filter_map(|(_, info)| info.restrictions.to_owned()) - .collect(); + .filter_map(|(_, info)| info.restrictions.to_owned()); let filter_tags: Vec = requested_attributes_queries - .iter() - .chain(requested_predicates_queries.iter()) + .chain(requested_predicates_queries) .flat_map(|r| { r.get_name() .iter() @@ -862,8 +860,6 @@ fn precess_filed(filed: &str, filter_value: impl Into, tag_value: &str) // means that we only allow legacy identifiers which can be detected with a simple regex. If // they are not in the legacy format, we do not support this. if filed == "schema_issuer_did" || filed == "issuer_did" { - static LEGACY_IDENTIFIER: Lazy = - Lazy::new(|| Regex::new("^[1-9A-HJ-NP-Za-km-z]{21,22}$").unwrap()); if LEGACY_IDENTIFIER.captures(&filter_value).is_none() { return Err(err_msg!( ProofRejected, diff --git a/anoncreds/src/utils/mod.rs b/anoncreds/src/utils/mod.rs new file mode 100644 index 00000000..be9c90dc --- /dev/null +++ b/anoncreds/src/utils/mod.rs @@ -0,0 +1,2 @@ +/// Functions for quick validation +pub mod validation; diff --git a/anoncreds/src/utils/validation.rs b/anoncreds/src/utils/validation.rs new file mode 100644 index 00000000..f352f163 --- /dev/null +++ b/anoncreds/src/utils/validation.rs @@ -0,0 +1,16 @@ +use once_cell::sync::Lazy; +use regex::Regex; + +// TODO: stricten the URI regex. +// Right now everything after the first colon is allowed, +// we might want to restrict this +pub const URI_IDENTIFIER: Lazy = + Lazy::new(|| Regex::new(r"^[a-zA-Z0-9\+\-\.]+:.+$").unwrap()); + +/// base58 alpahet as defined in +/// https://datatracker.ietf.org/doc/html/draft-msporny-base58#section-2 +/// This is used for legacy indy identifiers that we will keep supporting for +/// backwards compatibility. This might validate invalid identifiers if they happen +/// to fall within the base58 alphabet, but there is not much we can do about that. +pub const LEGACY_IDENTIFIER: Lazy = + Lazy::new(|| Regex::new("^[1-9A-HJ-NP-Za-km-z]{21,22}$").unwrap());