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(webhooks): remove default verification secret fallbacks (#14965)
Sakil Mostakc22b88ba3f98
diff --git a/crates/common_utils/src/crypto.rs b/crates/common_utils/src/crypto.rsindex 7f64acd677..2cbfaa7707 100644--- a/crates/common_utils/src/crypto.rs+++ b/crates/common_utils/src/crypto.rs@@ -157,6 +157,11 @@ pub trait EncodeMessage {/// Trait for cryptographically decoding a messagepub trait DecodeMessage {+ /// Whether decoding requires a configured secret.+ fn requires_secret(&self) -> bool {+ true+ }+/// Takes in a secret, an encoded messages and attempts to decode it, returning bytesfn decode_message(&self,@@ -166,7 +171,7 @@ pub trait DecodeMessage {}/// Represents no cryptographic algorithm.-/// Implements all crypto traits and acts like a Nop+/// Passes messages through during encoding and decoding, but never verifies signatures.#[derive(Debug)]pub struct NoAlgorithm;@@ -187,7 +192,7 @@ impl VerifySignature for NoAlgorithm {_signature: &[u8],_msg: &[u8],) -> CustomResult<bool, errors::CryptoError> {- Ok(true)+ Ok(false)}}@@ -202,6 +207,10 @@ impl EncodeMessage for NoAlgorithm {}impl DecodeMessage for NoAlgorithm {+ fn requires_secret(&self) -> bool {+ false+ }+fn decode_message(&self,_secret: &[u8],diff --git a/crates/hyperswitch_connectors/src/connectors/facilitapay.rs b/crates/hyperswitch_connectors/src/connectors/facilitapay.rsindex 3f18169467..38670e6e91 100644--- a/crates/hyperswitch_connectors/src/connectors/facilitapay.rs+++ b/crates/hyperswitch_connectors/src/connectors/facilitapay.rs@@ -46,7 +46,7 @@ use hyperswitch_interfaces::{types::{self, RefreshTokenType, Response},webhooks,};-use hyperswitch_masking::{ExposeInterface, Mask, PeekInterface};+use hyperswitch_masking::{Mask, PeekInterface};use lazy_static::lazy_static;use requests::{FacilitapayAuthRequest, FacilitapayCustomerRequest, FacilitapayPaymentsRequest,@@ -824,20 +824,17 @@ impl webhooks::IncomingWebhook for Facilitapay {.parse_struct("FacilitapayWebhookNotification").change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;- let connector_webhook_secrets = match connector_webhook_details {- Some(secret_value) => {- let secret = secret_value- .parse_value::<api_models::admin::MerchantConnectorWebhookDetails>(- "MerchantConnectorWebhookDetails",- )- .change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;- secret.merchant_secret.expose()- }- None => "default_secret".to_string(),- };+ let connector_webhook_secrets = connector_webhook_details+ .ok_or(errors::ConnectorError::WebhookVerificationSecretNotFound)+ .change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?+ .parse_value::<api_models::admin::MerchantConnectorWebhookDetails>(+ "MerchantConnectorWebhookDetails",+ )+ .change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;// FacilitaPay uses a simple 4-digit secret for verification- Ok(webhook_body.notification.secret.peek() == &connector_webhook_secrets)+ Ok(webhook_body.notification.secret.peek()+ == connector_webhook_secrets.merchant_secret.peek())}fn get_webhook_object_reference_id(diff --git a/crates/hyperswitch_interfaces/src/webhooks.rs b/crates/hyperswitch_interfaces/src/webhooks.rsindex 6b94c8bf9e..2d62a18a0c 100644--- a/crates/hyperswitch_interfaces/src/webhooks.rs+++ b/crates/hyperswitch_interfaces/src/webhooks.rs@@ -188,17 +188,22 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {let message = self.get_webhook_body_decoding_message(request).change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;- let secret = self- .get_webhook_source_verification_merchant_secret(+ // Ordinary bodies can be decoded before the merchant connector account is resolved.+ let secret = if algorithm.requires_secret() {+ self.get_webhook_source_verification_merchant_secret(merchant_id,connector_name,connector_webhook_details,).await- .change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?;+ .change_context(errors::ConnectorError::WebhookSourceVerificationFailed)?+ .secret+ } else {+ Vec::new()+ };algorithm- .decode_message(&secret.secret, message.into())+ .decode_message(&secret, message.into()).change_context(errors::ConnectorError::WebhookBodyDecodingFailed)}@@ -210,7 +215,8 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {Ok(Box::new(crypto::NoAlgorithm))}- /// fn get_webhook_source_verification_merchant_secret+ /// Get the merchant's configured webhook verification secrets.+ /// Returns an error when webhook details are not configured.async fn get_webhook_source_verification_merchant_secret(&self,merchant_id: &common_utils::id_type::MerchantId,@@ -219,8 +225,8 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {) -> CustomResult<api_models::webhooks::ConnectorWebhookSecrets, errors::ConnectorError> {let debug_suffix =format!("For merchant_id: {merchant_id:?}, and connector_name: {connector_name}");- let default_secret = "default_secret".to_string();- let merchant_secret = match connector_webhook_details {+ //need to fetch merchant secret from config table with caching in future for enhanced performance+ match connector_webhook_details {Some(merchant_connector_webhook_details) => {let connector_webhook_details = merchant_connector_webhook_details.parse_value::<api_models::admin::MerchantConnectorWebhookDetails>(@@ -232,26 +238,20 @@ pub trait IncomingWebhook: ConnectorCommon + Sync {"Deserializing MerchantConnectorWebhookDetails failed {debug_suffix}",)})?;- api_models::webhooks::ConnectorWebhookSecrets {+ Ok(api_models::webhooks::ConnectorWebhookSecrets {secret: connector_webhook_details.merchant_secret.expose().into_bytes(),additional_secret: connector_webhook_details.additional_secret,- }+ })}- None => api_models::webhooks::ConnectorWebhookSecrets {- secret: default_secret.into_bytes(),- additional_secret: None,- },- };-- //need to fetch merchant secret from config table with caching in future for enhanced performance-- //If merchant has not set the secret for webhook source verification, "default_secret" is returned.- //So it will fail during verification step and goes to psync flow.- Ok(merchant_secret)+ None => Err(errors::ConnectorError::WebhookVerificationSecretNotFound)+ .attach_printable_lazy(|| {+ format!("Merchant connector webhook details not configured {debug_suffix}")+ }),+ }}/// fn get_webhook_source_verification_signature