mrapids 0.1.31

Your OpenAPI, but executable
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! Response redaction for sensitive data
//!
//! Automatically redacts sensitive information from API responses before
//! displaying to users or returning to AI agents.

use regex::Regex;
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::sync::LazyLock;

/// Common patterns for sensitive field names
static SENSITIVE_KEY_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
    vec![
        Regex::new(r"(?i)(api[_-]?key|apikey)").unwrap(),
        Regex::new(r"(?i)(password|passwd|pwd)").unwrap(),
        Regex::new(r"(?i)(secret|private[_-]?key)").unwrap(),
        Regex::new(r"(?i)(token|access[_-]?token|refresh[_-]?token)").unwrap(),
        Regex::new(r"(?i)(auth|authorization)").unwrap(),
        Regex::new(r"(?i)(credential|cred)").unwrap(),
        Regex::new(r"(?i)(ssn|social[_-]?security)").unwrap(),
        Regex::new(r"(?i)(credit[_-]?card|card[_-]?number|cvv|cvc)").unwrap(),
    ]
});

/// Patterns for sensitive values
// Matches SSN with dashes (123-45-6789) or without (123456789)
static SSN_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(\d{3})-?(\d{2})-?(\d{4})\b").unwrap());

static CARD_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(\d{4})[\s-]?(\d{4})[\s-]?(\d{4})[\s-]?(\d{4})\b").unwrap());

static JWT_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$").unwrap());

static EMAIL_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b").unwrap());

static PHONE_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\b(\d{3})[-.]?(\d{3})[-.]?(\d{4})\b").unwrap());

/// Known token prefixes that indicate sensitive values
static TOKEN_PREFIXES: [&str; 7] = ["Bearer ", "Basic ", "Token ", "AWS", "sk_", "pk_", "ghp_"];

/// Controls how sensitive values are masked
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MaskingMode {
    /// Replace entirely with `[REDACTED]` — legacy behavior
    Full,
    /// Keep last 4 chars visible (or type-specific partial mask)
    Partial,
}

/// Redactor for sensitive data in API responses
pub struct ResponseRedactor {
    custom_patterns: Vec<Regex>,
    sensitive_keys: HashSet<String>,
    enabled: bool,
    masking_mode: MaskingMode,
}

impl ResponseRedactor {
    /// Create a new redactor with optional custom patterns (defaults to Partial masking)
    pub fn new(custom_patterns: Vec<String>, enabled: bool) -> Self {
        Self::new_with_mode(custom_patterns, enabled, MaskingMode::Partial)
    }

    /// Create a new redactor with explicit masking mode
    pub fn new_with_mode(custom_patterns: Vec<String>, enabled: bool, mode: MaskingMode) -> Self {
        let mut sensitive_keys = HashSet::new();

        // Default sensitive key names
        for key in &[
            "password",
            "secret",
            "token",
            "api_key",
            "apikey",
            "auth",
            "authorization",
            "credential",
            "private_key",
            "access_token",
            "refresh_token",
            "ssn",
            "credit_card",
            "cvv",
        ] {
            sensitive_keys.insert(key.to_string());
        }

        let compiled_patterns = custom_patterns
            .into_iter()
            .filter_map(|pattern| match Regex::new(&pattern) {
                Ok(regex) => Some(regex),
                Err(e) => {
                    eprintln!("Warning: Invalid redact pattern '{}': {}", pattern, e);
                    None
                }
            })
            .collect();

        Self {
            custom_patterns: compiled_patterns,
            sensitive_keys,
            enabled,
            masking_mode: mode,
        }
    }

    /// Create a default redactor (enabled, Partial masking)
    #[allow(dead_code)]
    pub fn default_enabled() -> Self {
        Self::new(vec![], true)
    }

    /// Redact sensitive data from a JSON value
    pub fn redact(&self, value: &Value) -> Value {
        if !self.enabled {
            return value.clone();
        }
        self.redact_value(value)
    }

    /// Produce a masked representation of `value` based on `data_type`.
    ///
    /// `data_type` hints: `"ssn"`, `"card"`, `"email"`, `"phone"`, `"jwt"`,
    /// `"api_key"` / `"token"`, or `"generic"`.
    fn mask_value(&self, value: &str, data_type: &str) -> String {
        if self.masking_mode == MaskingMode::Full {
            return "[REDACTED]".to_string();
        }

        match data_type {
            "ssn" => {
                // 123-45-6789 → ***-**-6789
                if let Some(caps) = SSN_PATTERN.captures(value) {
                    if let Some(last) = caps.get(3) {
                        return format!("***-**-{}", last.as_str());
                    }
                }
                mask_generic(value)
            }
            "card" => {
                // 4111-1111-1111-1234 → ****-****-****-1234
                if let Some(caps) = CARD_PATTERN.captures(value) {
                    if let Some(last) = caps.get(4) {
                        return format!("****-****-****-{}", last.as_str());
                    }
                }
                mask_generic(value)
            }
            "email" => {
                // alice@example.com → a***@***.com
                if let Some(at_pos) = value.find('@') {
                    let local = &value[..at_pos];
                    let domain = &value[at_pos + 1..];
                    let first_char = local.chars().next().unwrap_or('*');
                    let tld = domain.rsplit('.').next().unwrap_or("com");
                    return format!("{}***@***.{}", first_char, tld);
                }
                mask_generic(value)
            }
            "phone" => {
                // 555-123-7890 → ***-***-7890
                if let Some(caps) = PHONE_PATTERN.captures(value) {
                    if let Some(last) = caps.get(3) {
                        return format!("***-***-{}", last.as_str());
                    }
                }
                mask_generic(value)
            }
            "jwt" => {
                // Compute a short fingerprint
                let mut hasher = Sha256::new();
                hasher.update(value.as_bytes());
                let hash = hasher.finalize();
                let hex = hex::encode(hash);
                format!("[JWT:{}]", &hex[..12])
            }
            "api_key" | "token" => {
                // sk_test_abc123 → sk_t****bc23
                let len = value.len();
                if len <= 8 {
                    return mask_generic(value);
                }
                let prefix: String = value.chars().take(4).collect();
                let suffix: String = value.chars().skip(len - 4).collect();
                format!("{}****{}", prefix, suffix)
            }
            _ => mask_generic(value),
        }
    }

    /// Determine the data_type hint for a key-based redaction
    fn data_type_for_key(key: &str) -> &'static str {
        let k = key.to_lowercase();
        if k.contains("ssn") || k.contains("social_security") || k.contains("social-security") {
            "ssn"
        } else if k.contains("card") || k.contains("cvv") || k.contains("cvc") {
            "card"
        } else if k.contains("email") {
            "email"
        } else if k.contains("phone") || k.contains("mobile") || k.contains("tel") {
            "phone"
        } else if k.contains("token")
            || k.contains("api_key")
            || k.contains("apikey")
            || k.contains("api-key")
            || k.contains("secret")
            || k.contains("private_key")
            || k.contains("credential")
        {
            "api_key"
        } else {
            "generic"
        }
    }

    fn redact_value(&self, value: &Value) -> Value {
        match value {
            Value::Object(map) => {
                let mut redacted_map = Map::new();

                for (key, val) in map {
                    let key_lower = key.to_lowercase();

                    if self.is_sensitive_key(&key_lower) {
                        let masked =
                            match val {
                                Value::String(s) => {
                                    let dt = Self::data_type_for_key(&key_lower);
                                    Value::String(self.mask_value(s, dt))
                                }
                                _ => Value::String(self.mask_value(
                                    &val.to_string(),
                                    Self::data_type_for_key(&key_lower),
                                )),
                            };
                        redacted_map.insert(key.clone(), masked);
                    } else {
                        let redacted_val = match val {
                            Value::String(s) => {
                                if self.is_sensitive_value(s) {
                                    let dt = self.detect_value_type(s);
                                    Value::String(self.mask_value(s, dt))
                                } else {
                                    Value::String(self.redact_patterns_in_string(s))
                                }
                            }
                            _ => self.redact_value(val),
                        };
                        redacted_map.insert(key.clone(), redacted_val);
                    }
                }

                Value::Object(redacted_map)
            }
            Value::Array(arr) => Value::Array(arr.iter().map(|v| self.redact_value(v)).collect()),
            Value::String(s) => {
                if self.is_sensitive_value(s) {
                    let dt = self.detect_value_type(s);
                    Value::String(self.mask_value(s, dt))
                } else {
                    Value::String(self.redact_patterns_in_string(s))
                }
            }
            _ => value.clone(),
        }
    }

    /// Detect the data type of a sensitive value by inspecting its content
    fn detect_value_type(&self, value: &str) -> &'static str {
        if JWT_PATTERN.is_match(value) {
            return "jwt";
        }
        for prefix in &TOKEN_PREFIXES {
            if value.starts_with(prefix) {
                return "api_key";
            }
        }
        "generic"
    }

    pub(crate) fn is_sensitive_key(&self, key: &str) -> bool {
        // Check exact matches
        if self.sensitive_keys.contains(key) {
            return true;
        }

        // Check built-in patterns
        for pattern in SENSITIVE_KEY_PATTERNS.iter() {
            if pattern.is_match(key) {
                return true;
            }
        }

        // Check custom patterns
        for pattern in &self.custom_patterns {
            if pattern.is_match(key) {
                return true;
            }
        }

        false
    }

    pub(crate) fn is_sensitive_value(&self, value: &str) -> bool {
        // Skip short values
        if value.len() < 8 {
            return false;
        }

        // Check for JWT
        if JWT_PATTERN.is_match(value) {
            return true;
        }

        // Check for known token prefixes
        for prefix in &TOKEN_PREFIXES {
            if value.starts_with(prefix) {
                return true;
            }
        }

        // Check custom patterns against value
        for pattern in &self.custom_patterns {
            if pattern.is_match(value) {
                return true;
            }
        }

        false
    }

    fn redact_patterns_in_string(&self, value: &str) -> String {
        let mut result = value.to_string();

        // Redact SSN patterns (123-45-6789)
        if SSN_PATTERN.is_match(&result) {
            if self.masking_mode == MaskingMode::Partial {
                result = SSN_PATTERN
                    .replace_all(&result, |caps: &regex::Captures| {
                        format!("***-**-{}", &caps[3])
                    })
                    .to_string();
            } else {
                result = SSN_PATTERN
                    .replace_all(&result, "[SSN-REDACTED]")
                    .to_string();
            }
        }

        // Redact credit card patterns
        if CARD_PATTERN.is_match(&result) {
            if self.masking_mode == MaskingMode::Partial {
                result = CARD_PATTERN
                    .replace_all(&result, |caps: &regex::Captures| {
                        format!("****-****-****-{}", &caps[4])
                    })
                    .to_string();
            } else {
                result = CARD_PATTERN
                    .replace_all(&result, "[CARD-REDACTED]")
                    .to_string();
            }
        }

        // Redact email patterns
        if EMAIL_PATTERN.is_match(&result) {
            if self.masking_mode == MaskingMode::Partial {
                result = EMAIL_PATTERN
                    .replace_all(&result, |caps: &regex::Captures| {
                        let full = &caps[0];
                        self.mask_value(full, "email")
                    })
                    .to_string();
            } else {
                result = EMAIL_PATTERN
                    .replace_all(&result, "[EMAIL-REDACTED]")
                    .to_string();
            }
        }

        // Redact phone patterns
        if PHONE_PATTERN.is_match(&result) {
            // Avoid matching fragments already masked (e.g. card last-4)
            if self.masking_mode == MaskingMode::Partial {
                result = PHONE_PATTERN
                    .replace_all(&result, |caps: &regex::Captures| {
                        format!("***-***-{}", &caps[3])
                    })
                    .to_string();
            } else {
                result = PHONE_PATTERN
                    .replace_all(&result, "[PHONE-REDACTED]")
                    .to_string();
            }
        }

        result
    }
}

/// Produce a generic partial mask: `****` + last 4 chars, or full mask for short values
fn mask_generic(value: &str) -> String {
    let len = value.len();
    if len <= 4 {
        return "****".to_string();
    }
    let suffix: String = value.chars().skip(len - 4).collect();
    format!("****{}", suffix)
}

impl Default for ResponseRedactor {
    fn default() -> Self {
        Self::new(vec![], false) // Disabled by default
    }
}

/// Convenience function to redact a response (defaults to Partial masking)
pub fn redact_response(response: &Value, enabled: bool) -> Value {
    let redactor = ResponseRedactor::new(vec![], enabled);
    redactor.redact(response)
}

/// Sanitize headers JSON for database storage.
/// Always enabled — never persist secrets in cleartext.
/// Preserves header names, redacts sensitive values.
/// Uses Full redaction for storage safety.
pub fn sanitize_headers_for_storage(headers: &Value) -> Value {
    let redactor = ResponseRedactor::new_with_mode(vec![], true, MaskingMode::Full);
    redactor.redact(headers)
}

/// Compute SHA-256 fingerprints for sensitive header values.
/// Returns a JSON object mapping header names to truncated hashes
/// for correlation (e.g., "did I use the same token across runs?").
/// Only includes entries for headers that contain sensitive values.
pub fn compute_secret_fingerprints(headers: &Value) -> Value {
    let redactor = ResponseRedactor::new_with_mode(vec![], true, MaskingMode::Full);
    let mut fingerprints = Map::new();

    if let Value::Object(map) = headers {
        for (key, val) in map {
            let key_lower = key.to_lowercase();
            let is_secret = redactor.is_sensitive_key(&key_lower)
                || matches!(val, Value::String(s) if redactor.is_sensitive_value(s));

            if is_secret {
                if let Value::String(raw) = val {
                    let mut hasher = Sha256::new();
                    hasher.update(raw.as_bytes());
                    let hash = hasher.finalize();
                    let hex = hex::encode(hash);
                    fingerprints.insert(key.clone(), Value::String(hex[..12].to_string()));
                }
            }
        }
    }

    Value::Object(fingerprints)
}

/// Sanitize a JSON response body for database storage.
/// Always enabled — never persist secrets in cleartext.
/// Uses Full redaction for storage safety.
pub fn sanitize_body_for_storage(body: &str) -> String {
    match serde_json::from_str::<Value>(body) {
        Ok(json_val) => {
            let redactor = ResponseRedactor::new_with_mode(vec![], true, MaskingMode::Full);
            let redacted = redactor.redact(&json_val);
            serde_json::to_string(&redacted).unwrap_or_else(|_| body.to_string())
        }
        Err(_) => body.to_string(), // non-JSON body, store as-is
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_redact_sensitive_keys() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "username": "john_doe",
            "password": "super_secret_123",
            "api_key": "sk_test_123456789",
            "data": {
                "token": "ghp_abcdef123456",
                "safe_field": "normal_value"
            }
        });

        let redacted = redactor.redact(&data);

        assert_eq!(redacted["username"], "john_doe");
        // Partial masking: password → generic mask (last 4)
        assert_ne!(redacted["password"].as_str().unwrap(), "super_secret_123");
        assert!(redacted["password"].as_str().unwrap().contains("****"));
        // api_key → token-style mask (first 4 + **** + last 4)
        let api_key_masked = redacted["api_key"].as_str().unwrap();
        assert!(api_key_masked.starts_with("sk_t"));
        assert!(api_key_masked.contains("****"));
        // token → token-style mask
        let token_masked = redacted["data"]["token"].as_str().unwrap();
        assert!(token_masked.contains("****"));
        assert_eq!(redacted["data"]["safe_field"], "normal_value");
    }

    #[test]
    fn test_redact_ssn_in_string() {
        let redactor = ResponseRedactor::default_enabled();

        // Dashed format
        let data = json!({
            "message": "Customer SSN is 123-45-6789 on file"
        });
        let redacted = redactor.redact(&data);
        let msg = redacted["message"].as_str().unwrap();
        assert!(msg.contains("***-**-6789"));
        assert!(!msg.contains("123-45"));

        // Undashed format (also matched)
        let data2 = json!({
            "message": "SSN: 123456789"
        });
        let redacted2 = redactor.redact(&data2);
        let msg2 = redacted2["message"].as_str().unwrap();
        assert!(
            msg2.contains("***-**-6789"),
            "Undashed SSN should be masked: {}",
            msg2
        );
        assert!(!msg2.contains("12345"));
    }

    #[test]
    fn test_redact_credit_card() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "card": "4111-1111-1111-1111"
        });

        let redacted = redactor.redact(&data);
        let card = redacted["card"].as_str().unwrap();
        // Partial masking: last 4 digits visible
        assert!(card.contains("****-****-****-1111"));
        assert!(!card.contains("4111-1111-1111-1111"));
    }

    #[test]
    fn test_redact_jwt() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
        });

        let redacted = redactor.redact(&data);
        let jwt_val = redacted["jwt"].as_str().unwrap();
        // Partial mode: JWT fingerprint
        assert!(jwt_val.starts_with("[JWT:"));
        assert!(jwt_val.ends_with(']'));
        assert_eq!(jwt_val.len(), 18); // [JWT: + 12 hex + ]
    }

    #[test]
    fn test_redact_disabled() {
        let redactor = ResponseRedactor::new(vec![], false);

        let data = json!({
            "password": "secret123"
        });

        let redacted = redactor.redact(&data);
        assert_eq!(redacted["password"], "secret123"); // Not redacted
    }

    #[test]
    fn test_redact_array() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "users": [
                {"name": "alice", "password": "pass1234"},
                {"name": "bob", "password": "pass5678"}
            ]
        });

        let redacted = redactor.redact(&data);
        assert_eq!(redacted["users"][0]["name"], "alice");
        assert!(redacted["users"][0]["password"]
            .as_str()
            .unwrap()
            .contains("****"));
        assert!(redacted["users"][1]["password"]
            .as_str()
            .unwrap()
            .contains("****"));
    }

    #[test]
    fn test_sanitize_headers_for_storage() {
        let headers = json!({
            "Authorization": "Bearer sk_test_abc123456789",
            "Accept": "application/json",
            "X-API-Key": "secret_key_value_12345",
            "Content-Type": "application/json",
            "User-Agent": "mrapids/1.0"
        });

        let sanitized = sanitize_headers_for_storage(&headers);
        // Storage always uses Full redaction
        assert_eq!(sanitized["Authorization"], "[REDACTED]");
        assert_eq!(sanitized["X-API-Key"], "[REDACTED]");
        assert_eq!(sanitized["Accept"], "application/json");
        assert_eq!(sanitized["Content-Type"], "application/json");
        assert_eq!(sanitized["User-Agent"], "mrapids/1.0");
    }

    #[test]
    fn test_compute_secret_fingerprints() {
        let headers = json!({
            "Authorization": "Bearer sk_test_abc123456789",
            "Accept": "application/json",
            "X-API-Key": "secret_key_value_12345"
        });

        let fingerprints = compute_secret_fingerprints(&headers);
        let fp_map = fingerprints.as_object().unwrap();

        // Should have fingerprints for sensitive headers only
        assert!(fp_map.contains_key("Authorization"));
        assert!(fp_map.contains_key("X-API-Key"));
        assert!(!fp_map.contains_key("Accept"));

        // Fingerprints should be 12 hex chars
        let auth_fp = fp_map["Authorization"].as_str().unwrap();
        assert_eq!(auth_fp.len(), 12);
        assert!(auth_fp.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn test_fingerprint_correlation() {
        let token = "Bearer sk_test_same_token_12345";
        let headers1 = json!({"Authorization": token});
        let headers2 = json!({"Authorization": token});
        let headers3 = json!({"Authorization": "Bearer sk_test_different_token"});

        let fp1 = compute_secret_fingerprints(&headers1);
        let fp2 = compute_secret_fingerprints(&headers2);
        let fp3 = compute_secret_fingerprints(&headers3);

        // Same token → same fingerprint
        assert_eq!(
            fp1["Authorization"].as_str().unwrap(),
            fp2["Authorization"].as_str().unwrap()
        );
        // Different token → different fingerprint
        assert_ne!(
            fp1["Authorization"].as_str().unwrap(),
            fp3["Authorization"].as_str().unwrap()
        );
    }

    #[test]
    fn test_sanitize_body_for_storage() {
        let body = r#"{"user": "alice", "password": "secret123", "email": "alice@test.com"}"#;
        let sanitized = sanitize_body_for_storage(body);
        let parsed: Value = serde_json::from_str(&sanitized).unwrap();

        assert_eq!(parsed["user"], "alice");
        // Storage uses Full mode
        assert_eq!(parsed["password"], "[REDACTED]");
    }

    #[test]
    fn test_sanitize_body_non_json() {
        let body = "plain text response body";
        let sanitized = sanitize_body_for_storage(body);
        assert_eq!(sanitized, body);
    }

    // --- New tests for partial masking ---

    #[test]
    fn test_partial_mask_email() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "message": "Contact alice.smith@example.com for details"
        });

        let redacted = redactor.redact(&data);
        let msg = redacted["message"].as_str().unwrap();
        // Should partially mask: first char + *** @ ***.tld
        assert!(msg.contains("a***@***.com"), "got: {}", msg);
        assert!(!msg.contains("alice.smith@example.com"));
    }

    #[test]
    fn test_partial_mask_phone() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "info": "Call 555-123-7890 for support"
        });

        let redacted = redactor.redact(&data);
        let info = redacted["info"].as_str().unwrap();
        assert!(info.contains("***-***-7890"), "got: {}", info);
        assert!(!info.contains("555-123"));
    }

    #[test]
    fn test_partial_mask_api_key() {
        let redactor = ResponseRedactor::default_enabled();

        let data = json!({
            "api_key": "sk_test_abc123xyz"
        });

        let redacted = redactor.redact(&data);
        let masked = redacted["api_key"].as_str().unwrap();
        // token-style: first 4 + **** + last 4
        assert!(masked.starts_with("sk_t"), "got: {}", masked);
        assert!(masked.contains("****"), "got: {}", masked);
        assert!(masked.ends_with("3xyz"), "got: {}", masked);
    }

    #[test]
    fn test_full_redaction_mode() {
        let redactor = ResponseRedactor::new_with_mode(vec![], true, MaskingMode::Full);

        let data = json!({
            "password": "super_secret_123",
            "api_key": "sk_test_123456789",
            "message": "SSN is 123-45-6789",
            "card": "4111-1111-1111-1111"
        });

        let redacted = redactor.redact(&data);
        assert_eq!(redacted["password"], "[REDACTED]");
        assert_eq!(redacted["api_key"], "[REDACTED]");
        assert!(redacted["message"]
            .as_str()
            .unwrap()
            .contains("[SSN-REDACTED]"));
        assert!(redacted["card"]
            .as_str()
            .unwrap()
            .contains("[CARD-REDACTED]"));
    }
}