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(routing): add field length validation for the routing config name field (#9575)
spritianeja03735259f5201b
diff --git a/crates/api_models/src/routing.rs b/crates/api_models/src/routing.rsindex 73c8b12233..3bd81409bb 100644--- a/crates/api_models/src/routing.rs+++ b/crates/api_models/src/routing.rs@@ -1,4 +1,4 @@-use std::fmt::Debug;+use std::{fmt::Debug, ops::Deref};use common_types::three_ds_decision_rule_engine::{ThreeDSDecision, ThreeDSDecisionRule};use common_utils::{@@ -28,6 +28,7 @@ const DEFAULT_BUCKET_SIZE: i32 = 200;const DEFAULT_HEDGING_PERCENT: f64 = 5.0;const DEFAULT_ELIMINATION_THRESHOLD: f64 = 0.35;const DEFAULT_PAYMENT_METHOD: &str = "CARD";+const MAX_NAME_LENGTH: usize = 64;#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]#[serde(tag = "type", content = "data", rename_all = "snake_case")]@@ -59,7 +60,8 @@ pub struct RoutingConfigRequest {#[cfg(feature = "v1")]#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]pub struct RoutingConfigRequest {- pub name: Option<String>,+ #[schema(value_type = Option<String>)]+ pub name: Option<RoutingConfigName>,pub description: Option<String>,pub algorithm: Option<StaticRoutingAlgorithm>,#[schema(value_type = Option<String>)]@@ -67,6 +69,43 @@ pub struct RoutingConfigRequest {pub transaction_type: Option<TransactionType>,}+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]+#[serde(try_from = "String")]+#[schema(value_type = String)]+pub struct RoutingConfigName(String);++impl RoutingConfigName {+ pub fn new(name: impl Into<String>) -> Result<Self, ValidationError> {+ let name = name.into();+ if name.len() > MAX_NAME_LENGTH {+ return Err(ValidationError::InvalidValue {+ message: format!(+ "Length of name field must not exceed {} characters",+ MAX_NAME_LENGTH+ ),+ });+ }++ Ok(Self(name))+ }+}++impl TryFrom<String> for RoutingConfigName {+ type Error = ValidationError;++ fn try_from(value: String) -> Result<Self, Self::Error> {+ Self::new(value)+ }+}++impl Deref for RoutingConfigName {+ type Target = String;++ fn deref(&self) -> &Self::Target {+ &self.0+ }+}+#[derive(Debug, serde::Serialize, ToSchema)]pub struct ProfileDefaultRoutingConfig {#[schema(value_type = String)]diff --git a/crates/router/src/core/routing.rs b/crates/router/src/core/routing.rsindex 61f32808b0..715312bd3b 100644--- a/crates/router/src/core/routing.rs+++ b/crates/router/src/core/routing.rs@@ -446,7 +446,7 @@ pub async fn create_routing_algorithm_under_profile(if let Some(static_algorithm) = maybe_static_algorithm {let routing_rule = RoutingRule {rule_id: Some(algorithm_id.clone().get_string_repr().to_owned()),- name: name.clone(),+ name: name.to_string(),description: Some(description.clone()),created_by: profile_id.get_string_repr().to_string(),algorithm: static_algorithm,@@ -498,7 +498,7 @@ pub async fn create_routing_algorithm_under_profile(algorithm_id: algorithm_id.clone(),profile_id,merchant_id: merchant_context.get_merchant_account().get_id().to_owned(),- name: name.clone(),+ name: name.to_string(),description: Some(description.clone()),kind: algorithm.get_kind().foreign_into(),algorithm_data: serde_json::json!(algorithm),