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): Prevent panic when masking non-ASCII strings (#10682)
Anurag Thakur9f4a57bd5fae
diff --git a/crates/masking/src/secret.rs b/crates/masking/src/secret.rsindex 0dcf77a4ce..bbedc0d543 100644--- a/crates/masking/src/secret.rs+++ b/crates/masking/src/secret.rs@@ -222,18 +222,21 @@ impl Strategy<serde_json::Value> for JsonMaskStrategy {}serde_json::Value::String(s) => {// For strings, we show a masked version that gives a hint about the content- let masked = if s.len() <= 2 {+ // String::chars().count() is an approximation and could be greater than+ // the actual "character" count, since it does not consider graphemes (what we+ // usually consider as a "character"). To be more accurate at the cost of some+ // performance we can use "graphemes()" from the unicode-segmentation crate+ let char_count = s.chars().count();+ let masked = if char_count <= 2 {"**".to_string()- } else if s.len() <= 6 {- format!("{}**", &s[0..1])+ } else if char_count <= 6 {+ let first = s.chars().next().unwrap_or('*');+ format!("{first}**")} else {// For longer strings, show first and last character with length in between- format!(- "{}**{}**{}",- &s[0..1],- s.len() - 2,- &s[s.len() - 1..s.len()]- )+ let first = s.chars().next().unwrap_or('*');+ let last = s.chars().last().unwrap_or('*');+ format!("{first}**{count}**{last}", count = char_count - 2)};write!(f, "\"{masked}\"")}@@ -368,32 +371,32 @@ mod tests {// 1. String masking - pattern: first char + ** + length - 2 + ** + last charlet expected_name_mask = format!("\"{}**{}**{}\"",- &name[0..1],- name.len() - 2,- &name[name.len() - 1..]+ name.chars().next().unwrap(),+ name.chars().count() - 2,+ name.chars().last().unwrap());let expected_email_mask = format!("\"{}**{}**{}\"",- &email[0..1],- email.len() - 2,- &email[email.len() - 1..]+ email.chars().next().unwrap(),+ email.chars().count() - 2,+ email.chars().last().unwrap());let expected_card_mask = format!("\"{}**{}**{}\"",- &card_number[0..1],- card_number.len() - 2,- &card_number[card_number.len() - 1..]+ card_number.chars().next().unwrap(),+ card_number.chars().count() - 2,+ card_number.chars().last().unwrap());- let expected_tag1_mask = if tag1.len() <= 2 {+ let expected_tag1_mask = if tag1.chars().count() <= 2 {"\"**\"".to_string()- } else if tag1.len() <= 6 {- format!("\"{}**\"", &tag1[0..1])+ } else if tag1.chars().count() <= 6 {+ format!("\"{}**\"", &tag1.chars().next().unwrap())} else {format!("\"{}**{}**{}\"",- &tag1[0..1],- tag1.len() - 2,- &tag1[tag1.len() - 1..]+ tag1.chars().next().unwrap(),+ tag1.chars().count() - 2,+ tag1.chars().last().unwrap())};let expected_short_mask = "\"**\"".to_string(); // For "hi"@@ -489,4 +492,32 @@ mod tests {"Original short string value exposed in masked output");}++ #[test]+ fn test_unicode_masking_safety() {+ // This test ensures that masking does not panic on multi-byte unicode characters,+ // and that the output is as expected.+ let unicode_test_cases = vec![+ ("ü", r#""**""#), // 1 char <= 2+ ("😀", r#""**""#), // 1 char <= 2+ ("überspringen", r#""ü**10**n""#), // 12 chars > 6+ ("end with ü", r#""e**8**ü""#), // 10 chars > 6+ ("😀-a-long-string-with-emoji-😀", r#""😀**26**😀""#), // 28 chars > 6+ ("a", r#""**""#), // 1 char <= 2+ ("ab", r#""**""#), // 2 chars <= 2+ ("abc", r#""a**""#), // 3 chars <= 6+ ("abcdef", r#""a**""#), // 6 chars <= 6+ ("abcdefg", r#""a**5**g""#), // 7 chars > 6+ ("Homyel’ Voblasc’", r#""H**14**’""#),+ ("кпукпукп", r#""к**6**п""#),+ ];++ for (input, expected_mask) in unicode_test_cases {+ let json = json!({ "key": input });+ let secret = Secret::<_, JsonMaskStrategy>::new(json);+ let masked_str = format!("{secret:?}");+ let expected_output = format!(r#"{{"key":{}}}"#, expected_mask);+ assert_eq!(masked_str, expected_output, "Failed for input: '{}'", input);+ }+ }}