zeph-sanitizer 0.22.3

Content sanitization, exfiltration guard, PII filtering, and quarantine for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! PAAC typed-placeholder secret masking registry.
//!
//! Prevents vault-resolved secrets from appearing in LLM payloads by substituting them
//! with opaque, session-scoped placeholder tokens before context assembly.
//! Placeholders are reversed only at the tool-execution boundary.
//!
//! # Security model
//!
//! - Placeholder format: `<SECRET:category:NONCE_HEX:INDEX>` — the per-session nonce makes
//!   placeholders unguessable by an adversary, preventing placeholder-injection attacks.
//! - Secrets shorter than [`MIN_SECRET_LEN`] bytes are not masked (avoids false matches on
//!   common short strings).
//! - Replacement iterates secrets sorted by length descending (longest first) to prevent
//!   substring collision when one secret is a prefix of another.
//!
//! # Examples
//!
//! ```rust
//! use zeph_sanitizer::secret_mask::{SecretCategory, SecretMaskRegistry};
//!
//! let registry = SecretMaskRegistry::new();
//! registry.register("ZEPH_OPENAI_API_KEY", "sk-supersecretvalue12345678", SecretCategory::ApiKey);
//!
//! let masked = registry.mask("Using key sk-supersecretvalue12345678 here");
//! assert!(!masked.contains("sk-supersecretvalue12345678"));
//! assert!(masked.contains("<SECRET:api_key:"));
//!
//! let unmasked = registry.unmask(&masked);
//! assert!(unmasked.contains("sk-supersecretvalue12345678"));
//! ```

use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};

use parking_lot::RwLock;
use rand::TryRng;
use rand::rngs::SysRng;

/// Minimum byte length a secret value must have to be registered for masking.
///
/// Shorter values are not masked because they are more likely to appear as substrings
/// of legitimate content, causing false substitutions.
pub const MIN_SECRET_LEN: usize = 8;

/// Category of a masked secret, encoded in the placeholder token.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SecretCategory {
    /// API key (vault key name contains `api_key` or `apikey`).
    ApiKey,
    /// Bearer or session token.
    Token,
    /// Password or passphrase.
    Password,
    /// TLS certificate or private key.
    Certificate,
    /// Webhook URL or secret.
    Webhook,
    /// Anything that does not fit a more specific category.
    Generic,
}

impl SecretCategory {
    /// Infer category from a vault key name.
    ///
    /// Matching is case-insensitive and performed on the lowercased key name.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_sanitizer::secret_mask::SecretCategory;
    ///
    /// assert_eq!(SecretCategory::from_key_name("ZEPH_OPENAI_API_KEY"), SecretCategory::ApiKey);
    /// assert_eq!(SecretCategory::from_key_name("TELEGRAM_TOKEN"), SecretCategory::Token);
    /// assert_eq!(SecretCategory::from_key_name("DB_PASSWORD"), SecretCategory::Password);
    /// assert_eq!(SecretCategory::from_key_name("SOME_CERT"), SecretCategory::Certificate);
    /// assert_eq!(SecretCategory::from_key_name("SLACK_WEBHOOK"), SecretCategory::Webhook);
    /// assert_eq!(SecretCategory::from_key_name("SOMETHING_ELSE"), SecretCategory::Generic);
    /// ```
    #[must_use]
    pub fn from_key_name(key: &str) -> Self {
        let lower = key.to_ascii_lowercase();
        if lower.contains("api_key") || lower.contains("apikey") {
            Self::ApiKey
        } else if lower.contains("token") {
            Self::Token
        } else if lower.contains("password") || lower.contains("passwd") {
            Self::Password
        } else if lower.contains("cert") {
            Self::Certificate
        } else if lower.contains("webhook") {
            Self::Webhook
        } else {
            Self::Generic
        }
    }

    fn as_str(self) -> &'static str {
        match self {
            Self::ApiKey => "api_key",
            Self::Token => "token",
            Self::Password => "password",
            Self::Certificate => "certificate",
            Self::Webhook => "webhook",
            Self::Generic => "generic",
        }
    }
}

/// Per-session registry mapping vault secret values to typed placeholder tokens.
///
/// Thread-safe via `parking_lot::RwLock`. Designed to be wrapped in `Arc` and shared
/// across the agent session. Cleared on agent restart — never persisted to disk.
///
/// # Placeholder format
///
/// `<SECRET:{category}:{nonce_hex}:{index}>` where:
/// - `category` is the lowercase [`SecretCategory`] name.
/// - `nonce_hex` is a random 16-character hex string generated once at registry creation.
/// - `index` is a per-registry monotonic counter.
///
/// The nonce makes placeholders unguessable, preventing an attacker from injecting a
/// crafted placeholder into tool output to trigger secret exfiltration via `unmask`.
#[derive(Default)]
pub struct SecretMaskRegistry {
    /// `secret_value` → placeholder
    forward: RwLock<HashMap<String, String>>,
    /// `placeholder` → `secret_value`  (reverse mapping for unmask)
    reverse: RwLock<HashMap<String, String>>,
    /// Pre-sorted pairs `(secret, placeholder)` by secret length descending, cached for `mask()`.
    sorted_pairs: RwLock<Vec<(String, String)>>,
    /// Per-session random nonce (16 hex chars, generated at construction).
    nonce: String,
    /// Monotonic counter for unique placeholder indexes (lock-free).
    counter: AtomicUsize,
}

impl std::fmt::Debug for SecretMaskRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SecretMaskRegistry")
            .field("nonce", &self.nonce)
            .field("entries", &self.forward.read().len())
            .finish_non_exhaustive()
    }
}

impl SecretMaskRegistry {
    /// Create a new registry with a freshly generated session nonce.
    ///
    /// # Panics
    ///
    /// Panics if the OS entropy source is unavailable to seed the nonce — this would
    /// indicate a broken host environment, not a recoverable application error.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_sanitizer::secret_mask::SecretMaskRegistry;
    ///
    /// let r1 = SecretMaskRegistry::new();
    /// let r2 = SecretMaskRegistry::new();
    /// // Each registry has a unique nonce — placeholders from different sessions never collide.
    /// ```
    #[must_use]
    pub fn new() -> Self {
        // Explicit OS-backed CSPRNG, rather than `rand::random`'s default thread-local
        // generator, so the nonce's unguessability requirement is self-evident here.
        let nonce: u64 = SysRng
            .try_next_u64()
            .expect("OS entropy source must be available to generate the secret-mask nonce");
        let nonce = format!("{nonce:016x}");
        Self {
            forward: RwLock::new(HashMap::new()),
            reverse: RwLock::new(HashMap::new()),
            sorted_pairs: RwLock::new(Vec::new()),
            nonce,
            counter: AtomicUsize::new(0),
        }
    }

    /// Register a vault-resolved secret for masking.
    ///
    /// Secrets shorter than [`MIN_SECRET_LEN`] bytes are silently ignored.
    /// If the same secret value is registered again (possibly under a different key
    /// name), the existing placeholder is reused — no duplicate entries are created.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_sanitizer::secret_mask::{SecretCategory, SecretMaskRegistry};
    ///
    /// let registry = SecretMaskRegistry::new();
    /// registry.register("MY_KEY", "secret-value-here", SecretCategory::ApiKey);
    /// let masked = registry.mask("value is secret-value-here");
    /// assert!(!masked.contains("secret-value-here"));
    /// ```
    pub fn register(&self, _key_name: &str, secret_value: &str, category: SecretCategory) {
        if secret_value.len() < MIN_SECRET_LEN {
            return;
        }
        // Hold the write lock for the entire check-then-insert sequence to prevent a
        // TOCTOU race where two concurrent callers both pass the contains_key check and
        // produce duplicate placeholders for the same secret.
        let mut forward = self.forward.write();
        if forward.contains_key(secret_value) {
            return;
        }
        let index = self.counter.fetch_add(1, Ordering::Relaxed);
        let placeholder = format!("<SECRET:{}:{}:{}>", category.as_str(), self.nonce, index);
        // Acquire sorted_pairs BEFORE inserting into forward so mask() never observes
        // a state where forward contains the secret but sorted_pairs does not.
        let mut pairs = self.sorted_pairs.write();
        forward.insert(secret_value.to_owned(), placeholder.clone());
        self.reverse
            .write()
            .insert(placeholder.clone(), secret_value.to_owned());
        pairs.push((secret_value.to_owned(), placeholder));
        pairs.sort_unstable_by_key(|(s, _)| std::cmp::Reverse(s.len()));
    }

    /// Replace all registered secret values in `text` with their placeholder tokens.
    ///
    /// Replacement is performed longest-secret-first to prevent substring collision
    /// (e.g. if secret A is a prefix of secret B, B is replaced before A).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_sanitizer::secret_mask::{SecretCategory, SecretMaskRegistry};
    ///
    /// let registry = SecretMaskRegistry::new();
    /// registry.register("SHORT", "abcdefghij", SecretCategory::Token);
    /// registry.register("LONG", "abcdefghijklmno", SecretCategory::Token);
    ///
    /// let text = "prefix abcdefghijklmno suffix";
    /// let masked = registry.mask(text);
    /// // Longer secret replaced first — no leftover 'abcdefghij' fragment.
    /// assert!(!masked.contains("abcdefghij"));
    /// ```
    #[must_use]
    pub fn mask(&self, text: &str) -> String {
        let pairs = self.sorted_pairs.read();
        if pairs.is_empty() {
            return text.to_owned();
        }
        let mut result = text.to_owned();
        for (secret, placeholder) in pairs.iter() {
            result = result.replace(secret.as_str(), placeholder.as_str());
        }
        result
    }

    /// Return `true` if any registered secret appears verbatim in `text`.
    ///
    /// A cheap, allocation-free pre-check (no `String` construction, unlike [`Self::mask`]).
    /// Callers on a hot path should use this to decide whether the more expensive clone-and-mask
    /// pass over a batch of text is needed at all, instead of unconditionally cloning and masking
    /// every candidate — most turns contain no registered secret.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_sanitizer::secret_mask::{SecretCategory, SecretMaskRegistry};
    ///
    /// let registry = SecretMaskRegistry::new();
    /// registry.register("KEY", "supersecretvalue1", SecretCategory::ApiKey);
    ///
    /// assert!(registry.would_mask("value is supersecretvalue1"));
    /// assert!(!registry.would_mask("nothing sensitive here"));
    /// ```
    #[must_use]
    pub fn would_mask(&self, text: &str) -> bool {
        let pairs = self.sorted_pairs.read();
        pairs.iter().any(|(secret, _)| text.contains(secret))
    }

    /// Replace all placeholder tokens in `text` with their original secret values.
    ///
    /// Used only at the tool-execution boundary (shell commands, HTTP headers).
    /// This method is infallible — on any lookup miss the placeholder is left as-is
    /// (never panics, never produces partial output).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_sanitizer::secret_mask::{SecretCategory, SecretMaskRegistry};
    ///
    /// let registry = SecretMaskRegistry::new();
    /// registry.register("MY_KEY", "mysecret12345678", SecretCategory::ApiKey);
    ///
    /// let text = "value is mysecret12345678";
    /// let masked = registry.mask(text);
    /// let restored = registry.unmask(&masked);
    /// assert_eq!(restored, text);
    /// ```
    #[must_use]
    pub fn unmask(&self, text: &str) -> String {
        let reverse = self.reverse.read();
        if reverse.is_empty() {
            return text.to_owned();
        }
        let mut result = text.to_owned();
        for (placeholder, secret) in reverse.iter() {
            result = result.replace(placeholder.as_str(), secret.as_str());
        }
        result
    }

    /// Return the number of registered secrets.
    #[must_use]
    pub fn len(&self) -> usize {
        self.forward.read().len()
    }

    /// Return `true` when no secrets are registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.forward.read().is_empty()
    }

    /// Return the per-session nonce embedded in all placeholders from this registry.
    ///
    /// Exposed for testing nonce uniqueness across registry instances.
    #[must_use]
    pub fn nonce(&self) -> &str {
        &self.nonce
    }
}

impl zeph_llm::masking::OutboundMasker for SecretMaskRegistry {
    /// Adapts [`SecretMaskRegistry::mask`] to the provider-boundary [`zeph_llm::masking::OutboundMasker`]
    /// capability (#5437) — this is what lets `AnyProvider::masked` wrap a live provider with
    /// this registry via `Arc<dyn OutboundMasker>`, without `zeph-llm` depending on
    /// `zeph-sanitizer` (masking is applied structurally at the provider boundary, so every
    /// `chat`/`chat_with_tools`/`chat_stream` call is covered by construction, not per-call-site
    /// enumeration).
    fn mask(&self, text: &str) -> Option<String> {
        if self.would_mask(text) {
            Some(self.mask(text))
        } else {
            None
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn registry_with(secrets: &[(&str, &str, SecretCategory)]) -> SecretMaskRegistry {
        let r = SecretMaskRegistry::new();
        for (key, val, cat) in secrets {
            r.register(key, val, *cat);
        }
        r
    }

    // --- category inference ---

    #[test]
    fn category_from_key_name_api_key() {
        assert_eq!(
            SecretCategory::from_key_name("ZEPH_OPENAI_API_KEY"),
            SecretCategory::ApiKey
        );
        assert_eq!(
            SecretCategory::from_key_name("APIKEY_STRIPE"),
            SecretCategory::ApiKey
        );
    }

    #[test]
    fn category_from_key_name_token() {
        assert_eq!(
            SecretCategory::from_key_name("TELEGRAM_BOT_TOKEN"),
            SecretCategory::Token
        );
    }

    #[test]
    fn category_from_key_name_password() {
        assert_eq!(
            SecretCategory::from_key_name("DB_PASSWORD"),
            SecretCategory::Password
        );
        assert_eq!(
            SecretCategory::from_key_name("REDIS_PASSWD"),
            SecretCategory::Password
        );
    }

    #[test]
    fn category_from_key_name_generic() {
        assert_eq!(
            SecretCategory::from_key_name("SOMETHING_RANDOM"),
            SecretCategory::Generic
        );
    }

    // --- min length threshold ---

    #[test]
    fn short_secret_below_min_len_not_registered() {
        let r = SecretMaskRegistry::new();
        r.register("K", "short", SecretCategory::Generic); // len=5 < 8
        assert!(
            r.is_empty(),
            "secret shorter than MIN_SECRET_LEN must be ignored"
        );
    }

    #[test]
    fn secret_exactly_at_min_len_is_registered() {
        let r = SecretMaskRegistry::new();
        r.register("K", "12345678", SecretCategory::Generic); // len=8 == MIN_SECRET_LEN
        assert_eq!(r.len(), 1);
    }

    // --- mask / unmask round-trip ---

    #[test]
    fn mask_unmask_roundtrip() {
        let r = registry_with(&[("KEY", "mysecretvalue!!", SecretCategory::ApiKey)]);
        let original = "Authorization: Bearer mysecretvalue!!";
        let masked = r.mask(original);
        assert!(
            !masked.contains("mysecretvalue!!"),
            "secret must not appear in masked output"
        );
        assert!(
            masked.contains("<SECRET:api_key:"),
            "placeholder prefix must appear"
        );
        let restored = r.unmask(&masked);
        assert_eq!(restored, original, "unmask must restore original text");
    }

    #[test]
    fn mask_text_without_secret_unchanged() {
        let r = registry_with(&[("KEY", "mysecretvalue!!", SecretCategory::ApiKey)]);
        let text = "no secrets here at all";
        assert_eq!(r.mask(text), text);
    }

    #[test]
    fn unmask_text_without_placeholder_unchanged() {
        let r = registry_with(&[("KEY", "mysecretvalue!!", SecretCategory::ApiKey)]);
        let text = "no placeholders here";
        assert_eq!(r.unmask(text), text);
    }

    // --- nonce uniqueness ---

    #[test]
    fn nonce_unique_across_registries() {
        // Create many registries — with a 64-bit random nonce the probability of any
        // collision across 1000 instances is negligible (birthday bound ~2^32).
        let nonces: Vec<String> = (0..20)
            .map(|_| SecretMaskRegistry::new().nonce().to_owned())
            .collect();
        // Every nonce must be non-empty and 16 hex chars.
        for n in &nonces {
            assert_eq!(n.len(), 16, "nonce must be 16 hex chars");
            assert!(
                n.chars().all(|c| c.is_ascii_hexdigit()),
                "nonce must be hex"
            );
        }
        // Check all 20 are pairwise distinct (extremely unlikely to collide with rand u64).
        let unique: std::collections::HashSet<&str> = nonces.iter().map(String::as_str).collect();
        assert_eq!(
            unique.len(),
            nonces.len(),
            "all registry nonces must be distinct"
        );
    }

    #[test]
    fn placeholder_contains_nonce() {
        let r = SecretMaskRegistry::new();
        r.register("KEY", "secretpassword1", SecretCategory::Password);
        let masked = r.mask("secretpassword1");
        assert!(
            masked.contains(r.nonce()),
            "placeholder must embed the session nonce"
        );
    }

    // --- sort by length (substring collision prevention) ---

    #[test]
    fn longer_secret_replaced_before_shorter_prefix() {
        let r = SecretMaskRegistry::new();
        r.register("SHORT", "abcdefgh", SecretCategory::Generic);
        r.register("LONG", "abcdefghijklmnop", SecretCategory::Generic);

        let text = "value: abcdefghijklmnop extra";
        let masked = r.mask(text);
        // After masking the long secret, the short prefix must also be gone.
        assert!(
            !masked.contains("abcdefgh"),
            "no secret fragment must remain after mask"
        );
    }

    // --- duplicate secret value ---

    #[test]
    fn duplicate_secret_value_reuses_placeholder() {
        let r = SecretMaskRegistry::new();
        r.register("KEY1", "shared-secret-abc", SecretCategory::Token);
        r.register("KEY2", "shared-secret-abc", SecretCategory::Token); // same value
        assert_eq!(
            r.len(),
            1,
            "duplicate secret value must not create a second entry"
        );
    }

    // --- empty registry ---

    #[test]
    fn empty_registry_mask_is_identity() {
        let r = SecretMaskRegistry::new();
        assert_eq!(r.mask("any text"), "any text");
        assert_eq!(r.unmask("any text"), "any text");
    }

    // --- would_mask cheap pre-check ---

    #[test]
    fn would_mask_true_when_secret_present() {
        let r = registry_with(&[("KEY", "supersecretvalue1", SecretCategory::ApiKey)]);
        assert!(r.would_mask("prefix supersecretvalue1 suffix"));
    }

    #[test]
    fn would_mask_false_when_no_secret_present() {
        let r = registry_with(&[("KEY", "supersecretvalue1", SecretCategory::ApiKey)]);
        assert!(!r.would_mask("nothing sensitive in this text"));
    }

    #[test]
    fn would_mask_false_on_empty_registry() {
        let r = SecretMaskRegistry::new();
        assert!(!r.would_mask("supersecretvalue1"));
    }

    #[test]
    fn would_mask_matches_mask_outcome() {
        // would_mask must never disagree with whether mask() actually changes the text.
        let r = registry_with(&[("KEY", "supersecretvalue1", SecretCategory::ApiKey)]);
        for text in ["supersecretvalue1 here", "nothing here", ""] {
            let predicted = r.would_mask(text);
            let actual_changed = r.mask(text) != text;
            assert_eq!(predicted, actual_changed, "mismatch for text: {text:?}");
        }
    }

    // --- TOCTOU regression (#4280): after register() returns, concurrent mask() must never
    // expose the raw secret — tests the atomic sorted_pairs+forward critical section.
    #[test]
    fn concurrent_register_and_mask_never_expose_raw_secret() {
        use std::sync::{Arc, Barrier};
        use std::thread;

        let registry = Arc::new(SecretMaskRegistry::new());
        let secret = "super-secret-value-9999";
        let text = format!("token={secret} end");

        // Pre-register so the secret is fully committed before concurrent access starts.
        registry.register("KEY", secret, SecretCategory::ApiKey);

        // Barrier: both threads start simultaneously to stress-test concurrent mask() reads
        // and the dedup re-registration path (which also touches sorted_pairs).
        let barrier = Arc::new(Barrier::new(2));
        let iterations = 2_000;

        let reg_clone = Arc::clone(&registry);
        let barrier_clone = Arc::clone(&barrier);
        // Thread 1: repeatedly re-registers the same secret (hits the dedup early-return path,
        // which still acquires forward.write() and must not corrupt sorted_pairs).
        let register_thread = thread::spawn(move || {
            barrier_clone.wait();
            for _ in 0..iterations {
                reg_clone.register("KEY", secret, SecretCategory::ApiKey);
            }
        });

        // Thread 2: calls mask() — after the pre-registration above, the secret must always
        // be masked regardless of concurrent dedup calls in thread 1.
        barrier.wait();
        for _ in 0..iterations {
            let masked = registry.mask(&text);
            assert!(
                !masked.contains(secret),
                "raw secret must not appear in masked output: {masked}"
            );
        }

        register_thread.join().expect("register thread panicked");
    }

    // --- cross-registry isolation: placeholder from r2 is opaque to r1 ---

    #[test]
    fn cross_registry_unmask_isolation() {
        let r1 = SecretMaskRegistry::new();
        r1.register("K1", "secret-alpha-xyz1", SecretCategory::ApiKey);

        let r2 = SecretMaskRegistry::new();
        r2.register("K2", "secret-beta-abc99", SecretCategory::Token);

        // Mask with r2 to get a placeholder that contains r2's nonce.
        let masked_by_r2 = r2.mask("value secret-beta-abc99 end");
        assert!(!masked_by_r2.contains("secret-beta-abc99"));

        // r1 does not know r2's secrets or placeholders — must pass through unchanged.
        let result = r1.unmask(&masked_by_r2);
        assert_eq!(
            result, masked_by_r2,
            "r1 must not unmask a placeholder it never registered (nonce isolation)"
        );

        // r2 must correctly unmask its own placeholder.
        let restored = r2.unmask(&masked_by_r2);
        assert!(
            restored.contains("secret-beta-abc99"),
            "r2 must unmask its own placeholder"
        );
    }
}