1
1
use std:: {
2
+ borrow:: Cow ,
2
3
collections:: {
3
4
btree_map:: Entry ,
4
5
BTreeMap ,
@@ -37,10 +38,20 @@ use value::{
37
38
38
39
use crate :: metrics:: log_errors_reported_total;
39
40
40
- /// Regex to match PII where we show the object that doesn't match the
41
- /// validator.
42
- static SCHEMA_VALIDATION_OBJECT_PII : LazyLock < Regex > =
43
- LazyLock :: new ( || Regex :: new ( r"(?s)Object:.*Validator" ) . unwrap ( ) ) ;
41
+ /// Replacers for PII in errors before reporting to thirdparty services
42
+ /// (sentry/datadog)
43
+ static PII_REPLACEMENTS : LazyLock < Vec < ( Regex , & ' static str ) > > = LazyLock :: new ( || {
44
+ vec ! [
45
+ // Regex to match PII where we show the object that doesn't match the
46
+ // validator.
47
+ ( Regex :: new( r"(?s)Object:.*Validator" ) . unwrap( ) , "Validator" ) ,
48
+ // Regex to match emails
49
+ (
50
+ Regex :: new( r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" ) . unwrap( ) ,
51
+ "*****@*****.***" ,
52
+ ) ,
53
+ ]
54
+ } ) ;
44
55
45
56
/// Return Result<(), MainError> from main functions to report returned errors
46
57
/// to Sentry.
@@ -63,9 +74,12 @@ impl std::fmt::Debug for MainError {
63
74
64
75
fn strip_pii ( err : & mut anyhow:: Error ) {
65
76
if let Some ( error_metadata) = err. downcast_mut :: < ErrorMetadata > ( ) {
66
- let stripped_msg =
67
- SCHEMA_VALIDATION_OBJECT_PII . replace_all ( & error_metadata. msg , "Validator" ) ;
68
- error_metadata. msg = stripped_msg. to_string ( ) . into ( ) ;
77
+ for ( regex, replacement) in PII_REPLACEMENTS . iter ( ) {
78
+ match regex. replace_all ( & error_metadata. msg , * replacement) {
79
+ Cow :: Borrowed ( b) if b == error_metadata. msg => ( ) ,
80
+ cow => error_metadata. msg = Cow :: Owned ( cow. into_owned ( ) ) ,
81
+ }
82
+ }
69
83
}
70
84
}
71
85
@@ -654,7 +668,7 @@ mod tests {
654
668
}
655
669
656
670
#[ test]
657
- fn test_strip_pii ( ) -> anyhow:: Result < ( ) > {
671
+ fn test_strip_pii_obj ( ) -> anyhow:: Result < ( ) > {
658
672
let object = obj ! ( "foo" => "bar" ) ?;
659
673
let validation_error = ValidationError :: ExtraField {
660
674
object : object. clone ( ) ,
@@ -678,6 +692,17 @@ mod tests {
678
692
Ok ( ( ) )
679
693
}
680
694
695
+ #[ test]
696
+ fn test_strip_pii_email ( ) -> anyhow:: Result < ( ) > {
697
+ let mut e = anyhow:: anyhow!( ErrorMetadata :: bad_request(
698
+ "DIY" ,
699
+ "Need DIY advice? Email [email protected] "
700
+ ) ) ;
701
+ strip_pii ( & mut e) ;
702
+ assert_eq ! ( e. to_string( ) , "Need DIY advice? Email *****@*****.***" ) ;
703
+ Ok ( ( ) )
704
+ }
705
+
681
706
#[ test]
682
707
fn test_js_error_conversion_anyhow_macro ( ) -> anyhow:: Result < ( ) > {
683
708
let js_error = JsError :: from_message ( "Big Error" . into ( ) ) ;
0 commit comments