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(router): limit payment response hash key exposure (#15350)
Hrithikeshb748596a95b8
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.jsonindex bce333f166..6b5aa7217d 100644--- a/api-reference/v1/openapi_spec_v1.json+++ b/api-reference/v1/openapi_spec_v1.json@@ -28265,7 +28265,8 @@"payment_response_hash_key": {"type": "string","description": "Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.",- "nullable": true+ "nullable": true,+ "minLength": 32},"redirect_to_merchant_with_http_post": {"type": "boolean",@@ -28682,10 +28683,10 @@},"payment_response_hash_key": {"type": "string",- "description": "Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.",- "example": "xkkdf909012sdjki2dkh5sdf",+ "description": "The first 4 characters of the hash key used to sign webhooks and redirect responses.",+ "example": "xkkd","nullable": true,- "maxLength": 255+ "maxLength": 4},"redirect_to_merchant_with_http_post": {"type": "boolean",@@ -28893,7 +28894,8 @@"payment_response_hash_key": {"type": "string","description": "Refers to the hash key used for calculating the signature for webhooks and redirect response.",- "nullable": true+ "nullable": true,+ "minLength": 32},"redirect_to_merchant_with_http_post": {"type": "boolean",@@ -43737,7 +43739,8 @@"payment_response_hash_key": {"type": "string","description": "Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.",- "nullable": true+ "nullable": true,+ "minLength": 32},"redirect_to_merchant_with_http_post": {"type": "boolean",@@ -44116,8 +44119,10 @@},"payment_response_hash_key": {"type": "string",- "description": "Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.",- "nullable": true+ "description": "The first 4 characters of the hash key used to sign webhooks and redirect responses.",+ "example": "xkkd",+ "nullable": true,+ "maxLength": 4},"redirect_to_merchant_with_http_post": {"type": "boolean",diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.jsonindex 95be82e115..f7bd654026 100644--- a/api-reference/v2/openapi_spec_v2.json+++ b/api-reference/v2/openapi_spec_v2.json@@ -30912,7 +30912,8 @@"payment_response_hash_key": {"type": "string","description": "Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.",- "nullable": true+ "nullable": true,+ "minLength": 32},"redirect_to_merchant_with_http_post": {"type": "boolean",@@ -31235,8 +31236,10 @@},"payment_response_hash_key": {"type": "string",- "description": "Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.",- "nullable": true+ "description": "The first 4 characters of the hash key used to sign webhooks and redirect responses.",+ "example": "xkkd",+ "nullable": true,+ "maxLength": 4},"redirect_to_merchant_with_http_post": {"type": "boolean",diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rsindex 871046983b..016c4394bf 100644--- a/crates/api_models/src/admin.rs+++ b/crates/api_models/src/admin.rs@@ -23,6 +23,26 @@ use crate::{enums as api_enums, payment_methods, profile_acquirer,};+const MIN_PAYMENT_RESPONSE_HASH_KEY_LENGTH: usize = 32;++fn deserialize_payment_response_hash_key<'de, D>(+ deserializer: D,+) -> Result<Option<String>, D::Error>+where+ D: serde::Deserializer<'de>,+{+ let key = Option::<String>::deserialize(deserializer)?;+ if key+ .as_ref()+ .is_some_and(|key| key.chars().count() < MIN_PAYMENT_RESPONSE_HASH_KEY_LENGTH)+ {+ return Err(serde::de::Error::custom(format!(+ "payment_response_hash_key must be at least {MIN_PAYMENT_RESPONSE_HASH_KEY_LENGTH} characters long"+ )));+ }+ Ok(key)+}+#[derive(Clone, Debug, Deserialize, ToSchema, Serialize)]pub struct MerchantAccountListRequest {pub organization_id: id_type::OrganizationId,@@ -73,6 +93,8 @@ pub struct MerchantAccountCreate {pub enable_payment_response_hash: Option<bool>,/// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.+ #[schema(min_length = 32)]+ #[serde(default, deserialize_with = "deserialize_payment_response_hash_key")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled.@@ -475,6 +497,8 @@ pub struct MerchantAccountUpdate {pub enable_payment_response_hash: Option<bool>,/// Refers to the hash key used for calculating the signature for webhooks and redirect response.+ #[schema(min_length = 32)]+ #[serde(default, deserialize_with = "deserialize_payment_response_hash_key")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -660,8 +684,8 @@ pub struct MerchantAccountResponse {#[schema(default = false, example = true)]pub enable_payment_response_hash: bool,- /// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.- #[schema(max_length = 255, example = "xkkdf909012sdjki2dkh5sdf")]+ /// The first 4 characters of the hash key used to sign webhooks and redirect responses.+ #[schema(max_length = 4, example = "xkkd")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -2492,6 +2516,8 @@ pub struct ProfileCreate {pub enable_payment_response_hash: Option<bool>,/// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.+ #[schema(min_length = 32)]+ #[serde(default, deserialize_with = "deserialize_payment_response_hash_key")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -2706,6 +2732,8 @@ pub struct ProfileCreate {pub enable_payment_response_hash: Option<bool>,/// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.+ #[schema(min_length = 32)]+ #[serde(default, deserialize_with = "deserialize_payment_response_hash_key")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -2881,7 +2909,8 @@ pub struct ProfileResponse {#[schema(default = true, example = true)]pub enable_payment_response_hash: bool,- /// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.+ /// The first 4 characters of the hash key used to sign webhooks and redirect responses.+ #[schema(max_length = 4, example = "xkkd")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -3109,7 +3138,8 @@ pub struct ProfileResponse {#[schema(default = true, example = true)]pub enable_payment_response_hash: bool,- /// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.+ /// The first 4 characters of the hash key used to sign webhooks and redirect responses.+ #[schema(max_length = 4, example = "xkkd")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -3291,6 +3321,8 @@ pub struct ProfileUpdate {pub enable_payment_response_hash: Option<bool>,/// Refers to the hash key used for calculating the signature for webhooks and redirect response.+ #[schema(min_length = 32)]+ #[serde(default, deserialize_with = "deserialize_payment_response_hash_key")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -3504,6 +3536,8 @@ pub struct ProfileUpdate {pub enable_payment_response_hash: Option<bool>,/// Refers to the hash key used for calculating the signature for webhooks and redirect response. If the value is not provided, a value is automatically generated.+ #[schema(min_length = 32)]+ #[serde(default, deserialize_with = "deserialize_payment_response_hash_key")]pub payment_response_hash_key: Option<String>,/// A boolean value to indicate if redirect to merchant with http post needs to be enabled@@ -4134,6 +4168,53 @@ pub struct MCACGraphData {mod tests {use super::*;+ fn assert_payment_response_hash_key_validation<T: serde::de::DeserializeOwned>(+ request: serde_json::Value,+ ) {+ assert!(serde_json::from_value::<T>(request.clone()).is_ok());++ let mut with_key = request;+ with_key+ .as_object_mut()+ .expect("request must be a JSON object")+ .insert("payment_response_hash_key".into(), serde_json::Value::Null);+ assert!(serde_json::from_value::<T>(with_key.clone()).is_ok());++ for key in [String::new(), "a".repeat(4), "a".repeat(31), "é".repeat(16)] {+ *with_key+ .get_mut("payment_response_hash_key")+ .expect("signing key field must be present") = serde_json::json!(key);+ let error = serde_json::from_value::<T>(with_key.clone())+ .err()+ .expect("short signing keys must be rejected");+ assert!(error+ .to_string()+ .contains("payment_response_hash_key must be at least 32 characters long"));+ }++ for key in ["a".repeat(32), "a".repeat(64), "é".repeat(32)] {+ *with_key+ .get_mut("payment_response_hash_key")+ .expect("signing key field must be present") = serde_json::json!(key);+ assert!(serde_json::from_value::<T>(with_key.clone()).is_ok());+ }+ }++ #[cfg(feature = "v1")]+ #[test]+ fn merchant_requests_validate_payment_response_hash_key_length() {+ let request = serde_json::json!({ "merchant_id": "merchant_hash_key_test" });+ assert_payment_response_hash_key_validation::<MerchantAccountCreate>(request.clone());+ assert_payment_response_hash_key_validation::<MerchantAccountUpdate>(request);+ }++ #[test]+ fn profile_requests_validate_payment_response_hash_key_length() {+ let request = serde_json::json!({ "profile_name": "Signing key test" });+ assert_payment_response_hash_key_validation::<ProfileCreate>(request.clone());+ assert_payment_response_hash_key_validation::<ProfileUpdate>(request);+ }+#[test]fn empty_webhook_url_fails_validation_while_omission_preserves_the_destination() {let existing: WebhookDetailsResponse = serde_json::from_value(serde_json::json!({diff --git a/crates/router/src/consts.rs b/crates/router/src/consts.rsindex 2306537d91..106ebeaf99 100644--- a/crates/router/src/consts.rs+++ b/crates/router/src/consts.rs@@ -52,6 +52,8 @@ pub(crate) const BASE64_ENGINE: base64::engine::GeneralPurpose = consts::BASE64_pub(crate) const API_KEY_LENGTH: usize = 64;+pub(crate) const PAYMENT_RESPONSE_HASH_KEY_PREFIX_LEN: usize = 4;+// OID (Object Identifier) for the merchant ID field extension.pub(crate) const MERCHANT_ID_FIELD_EXTENSION_ID: &str = "1.2.840.113635.100.6.32";diff --git a/crates/router/src/types/api/admin.rs b/crates/router/src/types/api/admin.rsindex 3b76f651a2..9501c85bec 100644--- a/crates/router/src/types/api/admin.rs+++ b/crates/router/src/types/api/admin.rs@@ -119,7 +119,11 @@ impl ForeignTryFrom<domain::MerchantAccount> for MerchantAccountResponse {merchant_name: item.merchant_name,return_url: item.return_url,enable_payment_response_hash: item.enable_payment_response_hash,- payment_response_hash_key: item.payment_response_hash_key,+ payment_response_hash_key: item.payment_response_hash_key.map(|key| {+ key.chars()+ .take(consts::PAYMENT_RESPONSE_HASH_KEY_PREFIX_LEN)+ .collect()+ }),redirect_to_merchant_with_http_post: item.redirect_to_merchant_with_http_post,merchant_details: item.merchant_details,webhook_details: item.webhook_details.clone().map(ForeignInto::foreign_into),@@ -213,7 +217,11 @@ impl ForeignTryFrom<domain::Profile> for ProfileResponse {profile_name: item.profile_name,return_url: item.return_url,enable_payment_response_hash: item.enable_payment_response_hash,- payment_response_hash_key: item.payment_response_hash_key,+ payment_response_hash_key: item.payment_response_hash_key.map(|key| {+ key.chars()+ .take(consts::PAYMENT_RESPONSE_HASH_KEY_PREFIX_LEN)+ .collect()+ }),redirect_to_merchant_with_http_post: item.redirect_to_merchant_with_http_post,webhook_details: item.webhook_details.map(ForeignInto::foreign_into),metadata: item.metadata,@@ -336,7 +344,11 @@ impl ForeignTryFrom<domain::Profile> for ProfileResponse {profile_name: item.profile_name,return_url: item.return_url,enable_payment_response_hash: item.enable_payment_response_hash,- payment_response_hash_key: item.payment_response_hash_key,+ payment_response_hash_key: item.payment_response_hash_key.map(|key| {+ key.chars()+ .take(consts::PAYMENT_RESPONSE_HASH_KEY_PREFIX_LEN)+ .collect()+ }),redirect_to_merchant_with_http_post: item.redirect_to_merchant_with_http_post,webhook_details: item.webhook_details.map(ForeignInto::foreign_into),metadata: item.metadata,diff --git a/cypress-tests/README.md b/cypress-tests/README.mdindex 3220029579..984624f7be 100644--- a/cypress-tests/README.md+++ b/cypress-tests/README.md@@ -122,6 +122,10 @@ CYPRESS_CONNECTOR="connector_id" npm run cypress:ci# modular customer / saved card steps in# `Payment/54-ConnectorAgnosticMandates`.export PM_SERVICE_URL="pm_service_url"++ # Payment/52-PaymentResponseHash requires the full key configured on the+ # merchant account. API responses expose only the first four characters.+ export CYPRESS_PAYMENT_RESPONSE_HASH_KEY="merchant_payment_response_hash_key"```> [!IMPORTANT]diff --git a/cypress-tests/cypress/support/commands.js b/cypress-tests/cypress/support/commands.jsindex 7ff1f8f4ea..e64af121a4 100644--- a/cypress-tests/cypress/support/commands.js+++ b/cypress-tests/cypress/support/commands.js@@ -962,6 +962,9 @@ Cypress.Commands.add("merchantRetrieveCall", (globalState, options = {}) => {response.body.payment_response_hash_key,"payment_response_hash_key").to.not.be.null;+ expect(+ Array.from(response.body.payment_response_hash_key).length+ ).to.be.at.most(4);expect(response.body.publishable_key, "publishable_key").to.not.be.null;expect(response.body.default_profile, "default_profile").to.not.be.null;expect(response.body.organization_id, "organization_id").to.not.be.null;@@ -12496,6 +12499,8 @@ Cypress.Commands.add("retrieveNonExistentPaymentLinkTest", (globalState) => {/*** Fetches merchant account config and stores payment response hash settings in globalState.+ * The full signing key must be supplied as CYPRESS_PAYMENT_RESPONSE_HASH_KEY;+ * account responses expose only its first four characters.* Also skips the suite if enable_payment_response_hash is false/absent.** The backend falls back to merchant-account-level settings when business profile@@ -12528,7 +12533,8 @@ Cypress.Commands.add("fetchPaymentResponseHashConfig", (globalState) => {const enablePaymentResponseHash =response.body.enable_payment_response_hash;- const paymentResponseHashKey = response.body.payment_response_hash_key;+ const paymentResponseHashKeyPrefix =+ response.body.payment_response_hash_key;if (!enablePaymentResponseHash) {cy.task(@@ -12541,17 +12547,22 @@ Cypress.Commands.add("fetchPaymentResponseHashConfig", (globalState) => {return;}- // Validate that the hash key is present and non-empty- // A null or empty key would cause cryptic Node crypto errors downstream+ // Signature verification requires the full configured key, not the API prefix.+ const paymentResponseHashKey = Cypress.env("PAYMENT_RESPONSE_HASH_KEY");if (typeof paymentResponseHashKey !== "string" ||paymentResponseHashKey.length === 0) {throw new Error(- "fetchPaymentResponseHashConfig: enable_payment_response_hash is true but payment_response_hash_key is null or empty"+ "Set CYPRESS_PAYMENT_RESPONSE_HASH_KEY to the merchant's full signing key for signature verification");}+ expect(+ paymentResponseHashKeyPrefix,+ "payment_response_hash_key prefix"+ ).to.equal(Array.from(paymentResponseHashKey).slice(0, 4).join(""));+globalState.set("paymentResponseHashKey", paymentResponseHashKey);globalState.set("enablePaymentResponseHash", true);