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(router): reject blank webhook verification secrets (#15312)

Hrithikeshf9407d56ca14
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index d6e2c00be9..e3ecec9802 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -29629,11 +29629,15 @@
"properties": {
"merchant_secret": {
"type": "string",
- "example": "12345678900987654321"
+ "description": "Webhook verification secret. Must contain at least one non-whitespace character.",
+ "example": "12345678900987654321",
+ "minLength": 1
},
"additional_secret": {
"type": "string",
- "example": "12345678900987654321"
+ "description": "Optional additional secret. When supplied, must contain a non-whitespace character.",
+ "example": "12345678900987654321",
+ "minLength": 1
}
},
"additionalProperties": false
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 4cc52a8f8d..a30ce9db31 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -20478,11 +20478,15 @@
"properties": {
"merchant_secret": {
"type": "string",
- "example": "12345678900987654321"
+ "description": "Webhook verification secret. Must contain at least one non-whitespace character.",
+ "example": "12345678900987654321",
+ "minLength": 1
},
"additional_secret": {
"type": "string",
- "example": "12345678900987654321"
+ "description": "Optional additional secret. When supplied, must contain a non-whitespace character.",
+ "example": "12345678900987654321",
+ "minLength": 1
}
},
"additionalProperties": false
diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rs
index 7faea65804..871046983b 100644
--- a/crates/api_models/src/admin.rs
+++ b/crates/api_models/src/admin.rs
@@ -1617,12 +1617,37 @@ pub enum ConnectorAuthType {
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct MerchantConnectorWebhookDetails {
- #[schema(value_type = String, example = "12345678900987654321")]
+ /// Webhook verification secret. Must contain at least one non-whitespace character.
+ #[schema(value_type = String, min_length = 1, example = "12345678900987654321")]
pub merchant_secret: Secret<String>,
- #[schema(value_type = String, example = "12345678900987654321")]
+ /// Optional additional secret. When supplied, must contain a non-whitespace character.
+ #[schema(value_type = String, min_length = 1, example = "12345678900987654321")]
pub additional_secret: Option<Secret<String>>,
}
+impl MerchantConnectorWebhookDetails {
+ /// Validate newly supplied secrets without changing stored values or deserialization.
+ pub fn validate(&self) -> Result<(), String> {
+ if self.merchant_secret.peek().trim().is_empty() {
+ Err(
+ "connector_webhook_details.merchant_secret must not be empty or whitespace-only"
+ .to_string(),
+ )
+ } else if self
+ .additional_secret
+ .as_ref()
+ .is_some_and(|secret| secret.peek().trim().is_empty())
+ {
+ Err(
+ "connector_webhook_details.additional_secret must not be empty or whitespace-only"
+ .to_string(),
+ )
+ } else {
+ Ok(())
+ }
+ }
+}
+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, ToSchema)]
pub struct MerchantConnectorInfo {
pub connector_label: String,
diff --git a/crates/hyperswitch_connectors/src/connectors/facilitapay.rs b/crates/hyperswitch_connectors/src/connectors/facilitapay.rs
index 38670e6e91..6dda1625e3 100644
--- a/crates/hyperswitch_connectors/src/connectors/facilitapay.rs
+++ b/crates/hyperswitch_connectors/src/connectors/facilitapay.rs
@@ -832,6 +832,10 @@ impl webhooks::IncomingWebhook for Facilitapay {
)
.change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;
+ connector_webhook_secrets
+ .validate()
+ .map_err(|_| errors::ConnectorError::WebhookSourceVerificationFailed)?;
+
// FacilitaPay uses a simple 4-digit secret for verification
Ok(webhook_body.notification.secret.peek()
== connector_webhook_secrets.merchant_secret.peek())
diff --git a/crates/hyperswitch_interfaces/src/webhooks.rs b/crates/hyperswitch_interfaces/src/webhooks.rs
index 2d62a18a0c..7bc2bb508a 100644
--- a/crates/hyperswitch_interfaces/src/webhooks.rs
+++ b/crates/hyperswitch_interfaces/src/webhooks.rs
@@ -216,7 +216,7 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {
}
/// Get the merchant's configured webhook verification secrets.
- /// Returns an error when webhook details are not configured.
+ /// Returns an error when webhook details are not configured or the secret is blank.
async fn get_webhook_source_verification_merchant_secret(
&self,
merchant_id: &common_utils::id_type::MerchantId,
@@ -238,6 +238,12 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {
"Deserializing MerchantConnectorWebhookDetails failed {debug_suffix}",
)
})?;
+ connector_webhook_details
+ .validate()
+ .map_err(|_| errors::ConnectorError::WebhookVerificationSecretNotFound)
+ .attach_printable_lazy(|| {
+ format!("Merchant connector webhook secret is blank {debug_suffix}")
+ })?;
Ok(api_models::webhooks::ConnectorWebhookSecrets {
secret: connector_webhook_details
.merchant_secret
diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs
index 9675eaa80b..334f371483 100644
--- a/crates/router/src/core/admin.rs
+++ b/crates/router/src/core/admin.rs
@@ -2776,6 +2776,11 @@ pub async fn create_connector(
processor: domain::Processor,
auth_profile_id: Option<id_type::ProfileId>,
) -> RouterResponse<api_models::admin::MerchantConnectorResponse> {
+ if let Some(details) = &req.connector_webhook_details {
+ details
+ .validate()
+ .map_err(|message| errors::ApiErrorResponse::InvalidRequestData { message })?;
+ }
let key_manager_state = &(&state).into();
#[cfg(feature = "dummy_connector")]
fp_utils::when(
@@ -3096,6 +3101,11 @@ pub async fn update_connector(
merchant_connector_id: &id_type::MerchantConnectorAccountId,
req: api_models::admin::MerchantConnectorUpdate,
) -> RouterResponse<api_models::admin::MerchantConnectorResponse> {
+ if let Some(details) = &req.connector_webhook_details {
+ details
+ .validate()
+ .map_err(|message| errors::ApiErrorResponse::InvalidRequestData { message })?;
+ }
let db = state.store.as_ref();
let key_manager_state = &(&state).into();
let key_store_master_key = db.get_master_key().to_vec().into();
diff --git a/crates/router/src/core/merchant_connector_webhook_management/transformers.rs b/crates/router/src/core/merchant_connector_webhook_management/transformers.rs
index 4c4de38413..5050fbe7ef 100644
--- a/crates/router/src/core/merchant_connector_webhook_management/transformers.rs
+++ b/crates/router/src/core/merchant_connector_webhook_management/transformers.rs
@@ -12,6 +12,8 @@ use hyperswitch_domain_models::{
router_request_types::merchant_connector_webhook_management::ConnectorWebhookRegisterRequest,
};
use hyperswitch_interfaces::api::ConnectorSpecifications;
+#[cfg(feature = "v1")]
+use hyperswitch_masking::PeekInterface;
use hyperswitch_masking::{ExposeInterface, Secret};
use router_env::tracing::instrument;
@@ -375,6 +377,10 @@ fn build_connector_webhook_details_with_secret(
merchant_connector_account: &domain::MerchantConnectorAccount,
secret: Secret<String>,
) -> RouterResult<common_utils::pii::SecretSerdeValue> {
+ common_utils::fp_utils::when(secret.peek().trim().is_empty(), || {
+ Err(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Connector generated an empty or whitespace-only webhook secret")
+ })?;
let existing_additional_secret = merchant_connector_account
.connector_webhook_details
.as_ref()
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rs
index d2719354e1..fe4677ff8c 100644
--- a/crates/router/src/core/unified_connector_service.rs
+++ b/crates/router/src/core/unified_connector_service.rs
@@ -4480,6 +4480,11 @@ pub fn build_webhook_secrets_from_merchant_connector_account(
.change_context(UnifiedConnectorServiceError::FailedToObtainAuthType)
.attach_printable("Failed to parse MerchantConnectorWebhookDetails")?;
+ webhook_details
+ .validate()
+ .map_err(|_| UnifiedConnectorServiceError::FailedToObtainAuthType)
+ .attach_printable("MerchantConnectorWebhookDetails contains a blank secret")?;
+
// Build gRPC WebhookSecrets from parsed details
Ok(Some(payments_grpc::WebhookSecrets {
secret: webhook_details.merchant_secret.expose().to_string(),