Skip to content

Commit

Permalink
seperate regex to new validation.rs util
Browse files Browse the repository at this point in the history
Signed-off-by: blu3beri <[email protected]>
  • Loading branch information
blu3beri committed Jan 11, 2023
1 parent 12e79c2 commit 54e0c0e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
27 changes: 8 additions & 19 deletions anoncreds/src/data_types/anoncreds/macros.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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<Regex> =
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<Regex> =
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(());
}

Expand Down
2 changes: 2 additions & 0 deletions anoncreds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub use self::error::{Error, ErrorKind};
mod services;
pub use services::*;

mod utils;

#[cfg(feature = "ffi")]
mod ffi;

Expand Down
26 changes: 16 additions & 10 deletions anoncreds/src/services/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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 crate::utils::validation::URI_IDENTIFIER;
use indy_utils::query::Query;

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -571,21 +573,18 @@ fn verify_requested_restrictions(
.map(|(referent, info)| (referent.to_string(), info.clone()))
.collect();

let requested_attributes_queries: Vec<Query> = 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<Query> = 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<String> = requested_attributes_queries
.iter()
.chain(requested_predicates_queries.iter())
.chain(requested_predicates_queries)
.flat_map(|r| {
r.get_name()
.iter()
Expand Down Expand Up @@ -862,8 +861,6 @@ fn precess_filed(filed: &str, filter_value: impl Into<String>, 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<Regex> =
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,
Expand All @@ -872,6 +869,15 @@ fn precess_filed(filed: &str, filter_value: impl Into<String>, tag_value: &str)
));
}
}
if filed == "schema_issuer_id" || filed == "issuer_id" {
if URI_IDENTIFIER.captures(&filter_value).is_none() {
return Err(err_msg!(
ProofRejected,
"\"{}\" value is a URI identifier tag and therefore only URI identifiers can be used",
filed,
));
}
}
if filter_value == tag_value {
Ok(())
} else {
Expand Down
2 changes: 2 additions & 0 deletions anoncreds/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Functions for quick validation
pub mod validation;
16 changes: 16 additions & 0 deletions anoncreds/src/utils/validation.rs
Original file line number Diff line number Diff line change
@@ -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<Regex> =
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<Regex> =
Lazy::new(|| Regex::new("^[1-9A-HJ-NP-Za-km-z]{21,22}$").unwrap());

0 comments on commit 54e0c0e

Please sign in to comment.