Skip to content

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

fix(routing): validate profile ID from authentication layer in routing functions (#15126)

Gaurav Rawat61ea9e6fe2d4
diff --git a/crates/router/src/routes/routing.rs b/crates/router/src/routes/routing.rs
index b857d9e9a8..1d68cc86c3 100644
--- a/crates/router/src/routes/routing.rs
+++ b/crates/router/src/routes/routing.rs
@@ -105,20 +105,27 @@ pub async fn routing_create_config(
json_payload: web::Json<routing_types::RoutingConfigRequest>,
transaction_type: enums::TransactionType,
) -> impl Responder {
+ let payload = json_payload.into_inner();
+ let profile_id = payload.profile_id.clone();
let flow = Flow::RoutingCreateConfig;
Box::pin(oss_api::server_wrap(
flow,
state,
&req,
- json_payload.into_inner(),
- |state, auth: auth::AuthenticationData, payload, _| {
+ payload,
+ |state, auth: auth::AuthenticationData, payload, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(payload.profile_id.clone()),
+ &auth.profile,
+ )?;
routing::create_routing_algorithm_under_profile(
state,
auth.platform,
- Some(auth.profile.get_id().clone()),
+ None,
payload,
transaction_type,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -126,16 +133,18 @@ pub async fn routing_create_config(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
},
- &auth::JWTAuth {
- permission: Permission::ProfileRoutingWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::ProfileRoutingWrite,
allow_connected: true,
allow_platform: false,
},
req.headers(),
),
#[cfg(feature = "release")]
- &auth::JWTAuth {
- permission: Permission::ProfileRoutingWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::ProfileRoutingWrite,
allow_connected: true,
allow_platform: false,
},
@@ -208,7 +217,11 @@ pub async fn routing_link_config(
state,
&req,
wrapper.clone(),
- |state, auth: auth::AuthenticationData, wrapper, _| {
+ |state, auth: auth::AuthenticationData, wrapper, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(wrapper.profile_id.clone()),
+ &auth.profile,
+ )?;
routing::link_routing_config_under_profile(
state,
auth.platform.get_processor().clone(),
@@ -216,6 +229,7 @@ pub async fn routing_link_config(
wrapper.algorithm_id.routing_algorithm_id,
transaction_type,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -428,13 +442,18 @@ pub async fn routing_unlink_config(
state,
&req,
path.clone(),
- |state, auth: auth::AuthenticationData, path, _| {
+ |state, auth: auth::AuthenticationData, path, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(path.clone()),
+ &auth.profile,
+ )?;
routing::unlink_routing_config_under_profile(
state,
auth.platform.get_processor().clone(),
path,
transaction_type,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -513,8 +532,9 @@ pub async fn routing_update_default_config(
path: web::Path<common_utils::id_type::ProfileId>,
json_payload: web::Json<Vec<routing_types::RoutableConnectorChoice>>,
) -> impl Responder {
+ let profile_id = path.into_inner();
let wrapper = routing_types::ProfileDefaultRoutingConfig {
- profile_id: path.into_inner(),
+ profile_id: profile_id.clone(),
connectors: json_payload.into_inner(),
};
Box::pin(oss_api::server_wrap(
@@ -522,13 +542,18 @@ pub async fn routing_update_default_config(
state,
&req,
wrapper,
- |state, auth: auth::AuthenticationData, wrapper, _| {
+ |state, auth: auth::AuthenticationData, wrapper, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(wrapper.profile_id.clone()),
+ &auth.profile,
+ )?;
routing::update_default_fallback_routing(
state,
auth.platform.get_processor().clone(),
wrapper.profile_id,
wrapper.connectors,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -536,16 +561,18 @@ pub async fn routing_update_default_config(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
},
- &auth::JWTAuth {
- permission: Permission::MerchantRoutingWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::MerchantRoutingWrite,
allow_connected: true,
allow_platform: false,
},
req.headers(),
),
#[cfg(feature = "release")]
- &auth::JWTAuth {
- permission: Permission::MerchantRoutingWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::MerchantRoutingWrite,
allow_connected: true,
allow_platform: false,
},
@@ -605,12 +632,17 @@ pub async fn routing_retrieve_default_config(
state,
&req,
path.clone(),
- |state, auth: auth::AuthenticationData, profile_id, _| {
+ |state, auth: auth::AuthenticationData, profile_id, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(profile_id.clone()),
+ &auth.profile,
+ )?;
routing::retrieve_default_fallback_algorithm_for_profile(
state,
auth.platform.get_processor().clone(),
profile_id,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -852,8 +884,10 @@ pub async fn upsert_decision_manager_config(
pub async fn upsert_decision_manager_config(
state: web::Data<AppState>,
req: HttpRequest,
+ path: web::Path<common_utils::id_type::ProfileId>,
json_payload: web::Json<api_models::conditional_configs::DecisionManagerRequest>,
) -> impl Responder {
+ let profile_id = path.into_inner();
let flow = Flow::DecisionManagerUpsertConfig;
Box::pin(oss_api::server_wrap(
flow,
@@ -861,17 +895,25 @@ pub async fn upsert_decision_manager_config(
&req,
json_payload.into_inner(),
|state, auth: auth::AuthenticationData, update_decision, _| {
- conditional_config::upsert_conditional_config(
- state,
- auth.platform
- .get_processor()
- .clone()
- .get_key_store()
- .inner()
- .clone(),
- update_decision,
- auth.profile,
- )
+ let profile_id = profile_id.clone();
+ async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(profile_id),
+ &auth.profile,
+ )?;
+ conditional_config::upsert_conditional_config(
+ state,
+ auth.platform
+ .get_processor()
+ .clone()
+ .get_key_store()
+ .inner()
+ .clone(),
+ update_decision,
+ auth.profile,
+ )
+ .await
+ }
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -879,16 +921,18 @@ pub async fn upsert_decision_manager_config(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
},
- &auth::JWTAuth {
- permission: Permission::ProfileThreeDsDecisionManagerWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::ProfileThreeDsDecisionManagerWrite,
allow_connected: true,
allow_platform: false,
},
req.headers(),
),
#[cfg(feature = "release")]
- &auth::JWTAuth {
- permission: Permission::ProfileThreeDsDecisionManagerWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::ProfileThreeDsDecisionManagerWrite,
allow_connected: true,
allow_platform: false,
},
@@ -945,14 +989,20 @@ pub async fn delete_decision_manager_config(
pub async fn retrieve_decision_manager_config(
state: web::Data<AppState>,
req: HttpRequest,
+ path: web::Path<common_utils::id_type::ProfileId>,
) -> impl Responder {
+ let profile_id = path.into_inner();
let flow = Flow::DecisionManagerRetrieveConfig;
Box::pin(oss_api::server_wrap(
flow,
state,
&req,
- (),
- |state, auth: auth::AuthenticationData, _, _| {
+ profile_id.clone(),
+ |state, auth: auth::AuthenticationData, profile_id, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(profile_id),
+ &auth.profile,
+ )?;
conditional_config::retrieve_conditional_config(
state,
auth.platform
@@ -963,6 +1013,7 @@ pub async fn retrieve_decision_manager_config(
.clone(),
auth.profile,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
@@ -970,16 +1021,18 @@ pub async fn retrieve_decision_manager_config(
allow_connected_scope_operation: true,
allow_platform_self_operation: false,
},
- &auth::JWTAuth {
- permission: Permission::ProfileThreeDsDecisionManagerWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::ProfileThreeDsDecisionManagerWrite,
allow_connected: true,
allow_platform: false,
},
req.headers(),
),
#[cfg(feature = "release")]
- &auth::JWTAuth {
- permission: Permission::ProfileThreeDsDecisionManagerWrite,
+ &auth::JWTAuthProfileFromRoute {
+ profile_id: profile_id.clone(),
+ required_permission: Permission::ProfileThreeDsDecisionManagerWrite,
allow_connected: true,
allow_platform: false,
},
@@ -1132,7 +1185,11 @@ pub async fn routing_retrieve_linked_config(
state,
&req,
wrapper.clone(),
- |state, auth: AuthenticationData, wrapper, _| {
+ |state, auth: AuthenticationData, wrapper, _| async move {
+ crate::core::utils::validate_profile_id_from_auth_layer(
+ Some(wrapper.profile_id.clone()),
+ &auth.profile,
+ )?;
routing::retrieve_routing_config_under_profile(
state,
auth.platform.get_processor().clone(),
@@ -1140,6 +1197,7 @@ pub async fn routing_retrieve_linked_config(
wrapper.profile_id,
transaction_type,
)
+ .await
},
#[cfg(not(feature = "release"))]
auth::auth_type(
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index 17f7775510..cc466d4ef8 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -4980,10 +4980,19 @@ where
if payload.check_in_blacklist(state).await? {
return Err(errors::ApiErrorResponse::InvalidJwtToken.into());
}
+ authorization::check_tenant(
+ payload.tenant_id.clone(),
+ &state.session_state().tenant.tenant_id,
+ )?;
let profile_id =
get_id_type_by_key_from_headers(headers::X_PROFILE_ID.to_string(), request_headers)?
.get_required_value(headers::X_PROFILE_ID)?;
+ fp_utils::when(payload.profile_id != profile_id, || {
+ Err(report!(errors::ApiErrorResponse::InvalidJwtToken))
+ .attach_printable("Profile id in JWT does not match profile id in header")
+ })?;
+
let role_info = authorization::get_role_info(state, &payload).await?;
authorization::check_permission(self.required_permission, &role_info)?;