Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(user-role): add entity type validation in user role lineage queries (#10608)
Venu Madhav Bandarupalli771cad5c6c65
diff --git a/crates/diesel_models/src/query/user_role.rs b/crates/diesel_models/src/query/user_role.rsindex 01fa05e925..cc511ba94d 100644--- a/crates/diesel_models/src/query/user_role.rs+++ b/crates/diesel_models/src/query/user_role.rs@@ -1,4 +1,5 @@use async_bb8_diesel::AsyncRunQueryDsl;+use common_enums::EntityType;use common_utils::id_type;use diesel::{associations::HasTable,@@ -99,6 +100,77 @@ impl UserRole {generics::generic_find_one::<<Self as HasTable>::Table, _, _>(conn, predicate).await}+ fn check_user_in_lineage_with_entity_type(+ tenant_id: id_type::TenantId,+ org_id: Option<id_type::OrganizationId>,+ merchant_id: Option<id_type::MerchantId>,+ profile_id: Option<id_type::ProfileId>,+ ) -> Box<+ dyn diesel::BoxableExpression<<Self as HasTable>::Table, Pg, SqlType = Nullable<Bool>>+ + 'static,+ > {+ // Checking in user roles, for a user in token hierarchy, only one of the relations will be true:+ // either tenant level, org level, merchant level, or profile level+ // Tenant-level: (tenant_id = ? && org_id = null && merchant_id = null && profile_id = null)+ // Org-level: (org_id = ? && entity_type = organization)+ // Merchant-level: (org_id = ? && merchant_id = ? && entity_id = merchant)+ // Profile-level: (org_id = ? && merchant_id = ? && profile_id = ? && entity_type = profile)+ Box::new(+ // Tenant-level condition+ dsl::tenant_id+ .eq(tenant_id.clone())+ .and(dsl::entity_type.eq(EntityType::Tenant))+ .or(+ // Org-level condition+ dsl::tenant_id+ .eq(tenant_id.clone())+ .and(dsl::org_id.eq(org_id.clone()))+ .and(dsl::entity_type.eq(EntityType::Organization)),+ )+ .or(+ // Merchant-level condition+ dsl::tenant_id+ .eq(tenant_id.clone())+ .and(dsl::org_id.eq(org_id.clone()))+ .and(dsl::merchant_id.eq(merchant_id.clone()))+ .and(dsl::entity_type.eq(EntityType::Merchant)),+ )+ .or(+ // Profile-level condition+ dsl::tenant_id+ .eq(tenant_id)+ .and(dsl::org_id.eq(org_id))+ .and(dsl::merchant_id.eq(merchant_id))+ .and(dsl::profile_id.eq(profile_id))+ .and(dsl::entity_type.eq(EntityType::Profile)),+ ),+ )+ }++ pub async fn find_by_user_id_tenant_id_org_id_merchant_id_profile_id_with_entity_type(+ conn: &PgPooledConn,+ user_id: String,+ tenant_id: id_type::TenantId,+ org_id: id_type::OrganizationId,+ merchant_id: id_type::MerchantId,+ profile_id: id_type::ProfileId,+ version: UserRoleVersion,+ ) -> StorageResult<Self> {+ let check_lineage = Self::check_user_in_lineage_with_entity_type(+ tenant_id,+ Some(org_id),+ Some(merchant_id),+ Some(profile_id),+ );++ let predicate = dsl::user_id+ .eq(user_id)+ .and(check_lineage)+ .and(dsl::version.eq(version));++ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(conn, predicate).await+ }+#[allow(clippy::too_many_arguments)]pub async fn update_by_user_id_tenant_id_org_id_merchant_id_profile_id(conn: &PgPooledConn,diff --git a/crates/router/src/core/user.rs b/crates/router/src/core/user.rsindex 2f4c4cacef..f0d95b5597 100644--- a/crates/router/src/core/user.rs+++ b/crates/router/src/core/user.rs@@ -740,7 +740,7 @@ async fn handle_existing_user_invitation(if state.global_store- .find_user_role_by_user_id_and_lineage(+ .find_user_role_by_user_id_and_lineage_with_entity_type(invitee_user_from_db.get_user_id(),user_from_token.tenant_id@@ -760,7 +760,7 @@ async fn handle_existing_user_invitation(if state.global_store- .find_user_role_by_user_id_and_lineage(+ .find_user_role_by_user_id_and_lineage_with_entity_type(invitee_user_from_db.get_user_id(),user_from_token.tenant_id@@ -1152,7 +1152,7 @@ pub async fn resend_invite(let user_role = match state.global_store- .find_user_role_by_user_id_and_lineage(+ .find_user_role_by_user_id_and_lineage_with_entity_type(user.get_user_id(),user_from_token.tenant_id@@ -1179,7 +1179,7 @@ pub async fn resend_invite(Some(user_role) => user_role,None => state.global_store- .find_user_role_by_user_id_and_lineage(+ .find_user_role_by_user_id_and_lineage_with_entity_type(user.get_user_id(),user_from_token.tenant_iddiff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rsindex 24ad1e6f28..c72986ef17 100644--- a/crates/router/src/db/kafka_store.rs+++ b/crates/router/src/db/kafka_store.rs@@ -3381,6 +3381,27 @@ impl UserRoleInterface for KafkaStore {.await}+ async fn find_user_role_by_user_id_and_lineage_with_entity_type(+ &self,+ user_id: &str,+ tenant_id: &id_type::TenantId,+ org_id: &id_type::OrganizationId,+ merchant_id: &id_type::MerchantId,+ profile_id: &id_type::ProfileId,+ version: enums::UserRoleVersion,+ ) -> CustomResult<storage::UserRole, errors::StorageError> {+ self.diesel_store+ .find_user_role_by_user_id_and_lineage_with_entity_type(+ user_id,+ tenant_id,+ org_id,+ merchant_id,+ profile_id,+ version,+ )+ .await+ }+async fn update_user_role_by_user_id_and_lineage(&self,user_id: &str,diff --git a/crates/router/src/db/user_role.rs b/crates/router/src/db/user_role.rsindex 75bf66da70..26bd0f6fda 100644--- a/crates/router/src/db/user_role.rs+++ b/crates/router/src/db/user_role.rs@@ -1,3 +1,4 @@+use common_enums::EntityType;use common_utils::id_type;use diesel_models::{enums::{self, UserStatus},@@ -52,6 +53,16 @@ pub trait UserRoleInterface {version: enums::UserRoleVersion,) -> CustomResult<storage::UserRole, errors::StorageError>;+ async fn find_user_role_by_user_id_and_lineage_with_entity_type(+ &self,+ user_id: &str,+ tenant_id: &id_type::TenantId,+ org_id: &id_type::OrganizationId,+ merchant_id: &id_type::MerchantId,+ profile_id: &id_type::ProfileId,+ version: enums::UserRoleVersion,+ ) -> CustomResult<storage::UserRole, errors::StorageError>;+#[allow(clippy::too_many_arguments)]async fn update_user_role_by_user_id_and_lineage(&self,@@ -130,6 +141,30 @@ impl UserRoleInterface for Store {.map_err(|error| report!(errors::StorageError::from(error)))}+ #[instrument(skip_all)]+ async fn find_user_role_by_user_id_and_lineage_with_entity_type(+ &self,+ user_id: &str,+ tenant_id: &id_type::TenantId,+ org_id: &id_type::OrganizationId,+ merchant_id: &id_type::MerchantId,+ profile_id: &id_type::ProfileId,+ version: enums::UserRoleVersion,+ ) -> CustomResult<storage::UserRole, errors::StorageError> {+ let conn = connection::pg_connection_read(self).await?;+ storage::UserRole::find_by_user_id_tenant_id_org_id_merchant_id_profile_id_with_entity_type(+ &conn,+ user_id.to_owned(),+ tenant_id.to_owned(),+ org_id.to_owned(),+ merchant_id.to_owned(),+ profile_id.to_owned(),+ version,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ }+#[instrument(skip_all)]async fn update_user_role_by_user_id_and_lineage(&self,@@ -325,6 +360,54 @@ impl UserRoleInterface for MockDb {.into())}+ async fn find_user_role_by_user_id_and_lineage_with_entity_type(+ &self,+ user_id: &str,+ tenant_id: &id_type::TenantId,+ org_id: &id_type::OrganizationId,+ merchant_id: &id_type::MerchantId,+ profile_id: &id_type::ProfileId,+ version: enums::UserRoleVersion,+ ) -> CustomResult<storage::UserRole, errors::StorageError> {+ let user_roles = self.user_roles.lock().await;++ for user_role in user_roles.iter() {+ let tenant_level_check = user_role.tenant_id == *tenant_id+ && user_role.entity_type == Some(EntityType::Tenant);++ let org_level_check = user_role.tenant_id == *tenant_id+ && user_role.org_id.as_ref() == Some(org_id)+ && user_role.entity_type == Some(EntityType::Organization);++ let merchant_level_check = user_role.tenant_id == *tenant_id+ && user_role.org_id.as_ref() == Some(org_id)+ && user_role.merchant_id.as_ref() == Some(merchant_id)+ && user_role.entity_type == Some(EntityType::Merchant);++ let profile_level_check = user_role.tenant_id == *tenant_id+ && user_role.org_id.as_ref() == Some(org_id)+ && user_role.merchant_id.as_ref() == Some(merchant_id)+ && user_role.profile_id.as_ref() == Some(profile_id)+ && user_role.entity_type == Some(EntityType::Profile);++ // Check if any condition matches and the version matches+ if user_role.user_id == user_id+ && (tenant_level_check+ || org_level_check+ || merchant_level_check+ || profile_level_check)+ && user_role.version == version+ {+ return Ok(user_role.clone());+ }+ }++ Err(errors::StorageError::ValueNotFound(format!(+ "No user role available for user_id = {user_id} in the current token hierarchy",+ ))+ .into())+ }+async fn update_user_role_by_user_id_and_lineage(&self,user_id: &str,diff --git a/crates/router/src/types/domain/user/decision_manager.rs b/crates/router/src/types/domain/user/decision_manager.rsindex 15d5463ecf..62f0460b72 100644--- a/crates/router/src/types/domain/user/decision_manager.rs+++ b/crates/router/src/types/domain/user/decision_manager.rs@@ -149,7 +149,7 @@ impl JWTFlow {let tenant_id = ctx.tenant_id.clone();let user_role_match_v2 = state.global_store- .find_user_role_by_user_id_and_lineage(+ .find_user_role_by_user_id_and_lineage_with_entity_type(&ctx.user_id,&tenant_id,&ctx.org_id,@@ -169,7 +169,7 @@ impl JWTFlow {} else {let user_role_match_v1 = state.global_store- .find_user_role_by_user_id_and_lineage(+ .find_user_role_by_user_id_and_lineage_with_entity_type(&ctx.user_id,&tenant_id,&ctx.org_id,