feldera-types 0.386.0

Public API types for Feldera
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
//! Redaction of referenced and declared credential values in diagnostics.
//!
//! Terms:
//!
//! - A reference names a secret in a configuration, such as
//!   `${secret:kubernetes:pg/uri}`.
//! - The value of a secret is its plaintext, which the pipeline reads from
//!   the secret store.
//! - The resolver replaces each reference in a configuration with its value.
//! - The registry, [`SecretRedactor`], holds each resolved value with its
//!   reference.
//! - A declared credential is the value of a configuration field that the
//!   schema of a connector marks as a credential. It can have no reference.
//!
//! The resolver registers each value it resolves. Error responses, connector
//! error lists, the panic hook and the pipeline log pass through the
//! registry, so a value that a message copies by mistake leaves the process
//! as its reference. This is a safety net: code that builds a diagnostic
//! must not rely on it.
//!
//! What replaces a registered value:
//!
//! | The text holds | It becomes |
//! |---|---|
//! | the value, as resolved or trimmed | the reference |
//! | a form derived from the value: percent-encoded, percent-decoded, debug-escaped or JSON-escaped, or the password of a value that is a URL | `[REDACTED <reference>]` |
//! | a declared credential with no reference, or a form derived from it | `[REDACTED]` |
//!
//! The registry is process-wide and never logged or serialized. A value
//! registered twice keeps its first reference; references take precedence over
//! supplemental credential markers. Unit tests share it, so a
//! test registers a value that no other message can contain.

use crate::json_strings::rewrite_json_strings;
use aho_corasick::{AhoCorasick, MatchKind};
use serde_json::Value;
use std::borrow::Cow;
use std::sync::{PoisonError, RwLock, TryLockError};
use url::Url;

/// Shortest value the registry redacts, measured once the value is trimmed.
/// A shorter value, such as `5432` or `admin`, also occurs in ordinary text,
/// so it is not registered. Neither is a derived form shorter than this.
pub const MIN_REDACTED_LEN: usize = 8;

#[derive(Clone)]
enum Replacement {
    Reference(String),
    Credential,
}

impl Replacement {
    fn text(&self) -> &str {
        match self {
            Self::Reference(text) => text,
            Self::Credential => "[REDACTED]",
        }
    }
}

#[derive(Default)]
struct Entries {
    values: Vec<String>,
    replacements: Vec<Replacement>,
    /// Finds candidate values, preferring the longest match at each position.
    automaton: Option<AhoCorasick>,
}

/// Registry of resolved secret values and the text that replaces each one.
///
/// All state is behind one lock: registration takes it for writing, redaction
/// for reading, so both are safe from any thread.
#[derive(Default)]
pub struct SecretRedactor {
    entries: RwLock<Entries>,
}

static GLOBAL: SecretRedactor = SecretRedactor::new();

/// The registry of this process.
pub fn secret_redactor() -> &'static SecretRedactor {
    &GLOBAL
}

impl SecretRedactor {
    pub const fn new() -> Self {
        Self {
            entries: RwLock::new(Entries {
                values: Vec::new(),
                replacements: Vec::new(),
                automaton: None,
            }),
        }
    }

    /// Registers `value`, resolved from `reference`, and the forms of it
    /// that a connector or a formatter can derive: see the module
    /// documentation.
    pub fn register(&self, reference: &str, value: &str) {
        self.register_value(Some(reference), value);
    }

    /// Adds a schema-declared credential. A reference registered for the same
    /// value, before or after this call, takes precedence over the marker.
    /// The global length floor applies here too.
    pub fn register_credential(&self, value: &str) {
        self.register_value(None, value);
    }

    fn register_value(&self, reference: Option<&str>, value: &str) {
        if value.trim().len() < MIN_REDACTED_LEN {
            return;
        }
        let original = reference.map_or(Replacement::Credential, |r| {
            Replacement::Reference(r.to_owned())
        });
        let derived = reference.map_or(Replacement::Credential, |r| {
            Replacement::Reference(format!("[REDACTED {r}]"))
        });
        let mut forms = vec![
            (value.to_string(), original.clone()),
            (value.trim().to_string(), original),
            (urlencoding::encode(value).into_owned(), derived.clone()),
            (debug_escaped(value), derived.clone()),
            (json_escaped(value), derived.clone()),
        ];
        if let Ok(decoded) = urlencoding::decode(value) {
            forms.push((decoded.into_owned(), derived.clone()));
        }
        if let Some(password) = Url::parse(value)
            .ok()
            .and_then(|url| url.password().map(str::to_owned))
        {
            if let Ok(decoded) = urlencoding::decode(&password) {
                forms.push((debug_escaped(&decoded), derived.clone()));
                forms.push((decoded.into_owned(), derived.clone()));
            }
            forms.push((debug_escaped(&password), derived.clone()));
            forms.push((password, derived));
        }

        let mut entries = self.entries.write().unwrap_or_else(PoisonError::into_inner);
        let mut changed = false;
        for (value, replacement) in forms {
            if value.len() < MIN_REDACTED_LEN {
                continue;
            }
            if let Some(index) = entries
                .values
                .iter()
                .position(|existing| existing == &value)
            {
                if matches!(entries.replacements[index], Replacement::Credential) {
                    entries.replacements[index] = replacement;
                }
                continue;
            }
            entries.values.push(value);
            entries.replacements.push(replacement);
            changed = true;
        }
        if !changed {
            return;
        }
        // Leftmost-longest: a URL is replaced before the password inside it.
        entries.automaton = Some(
            AhoCorasick::builder()
                .match_kind(MatchKind::LeftmostLongest)
                .build(&entries.values)
                .expect("plain string patterns build"),
        );
    }

    /// Replaces every registered value in `text`.
    ///
    /// A value is replaced where it stands as a whole token: a secret that
    /// is a common word, such as a user name, does not rewrite the longer
    /// identifiers that contain it.
    pub fn redact<'a>(&self, text: &'a str) -> Cow<'a, str> {
        let entries = self.entries.read().unwrap_or_else(PoisonError::into_inner);
        Self::redact_entries(&entries, text)
    }

    /// Redacts a panic payload without waiting for a registry writer. The panic
    /// may have interrupted that writer on this thread. While a writer holds
    /// the lock, omit the payload; the hook keeps its location and backtrace.
    /// A lock poisoned by an earlier panic still holds a usable registry.
    pub fn redact_for_panic<'a>(&self, text: &'a str) -> Cow<'a, str> {
        match self.entries.try_read() {
            Ok(entries) => Self::redact_entries(&entries, text),
            Err(TryLockError::Poisoned(poisoned)) => {
                Self::redact_entries(&poisoned.into_inner(), text)
            }
            Err(TryLockError::WouldBlock) => Cow::Owned("[REDACTED]".to_string()),
        }
    }

    /// Replaces every value of `entries` in `text`, which is one message or
    /// log line. Returns `text` itself when it holds none.
    fn redact_entries<'a>(entries: &Entries, text: &'a str) -> Cow<'a, str> {
        let Some(automaton) = &entries.automaton else {
            return Cow::Borrowed(text);
        };
        let mut out = String::new();
        let mut last = 0;
        let mut search_from = 0;
        while let Some(found) = automaton.find(&text[search_from..]) {
            let start = search_from + found.start();
            let mut end = search_from + found.end();
            let mut pattern = found.pattern().as_usize();
            if !at_token_boundary(text, start, end) {
                // A rejected longer pattern must not hide an eligible shorter
                // credential at the same position or inside it.
                let shorter = entries
                    .values
                    .iter()
                    .enumerate()
                    .filter(|(_, value)| {
                        text[start..].starts_with(value.as_str())
                            && at_token_boundary(text, start, start + value.len())
                    })
                    .max_by_key(|(_, value)| value.len());
                if let Some((index, value)) = shorter {
                    pattern = index;
                    end = start + value.len();
                } else {
                    // Patterns are nonempty UTF-8 strings.
                    search_from = start + text[start..].chars().next().unwrap().len_utf8();
                    continue;
                }
            }
            out.push_str(&text[last..start]);
            out.push_str(entries.replacements[pattern].text());
            last = end;
            search_from = end;
        }
        if last == 0 {
            Cow::Borrowed(text)
        } else {
            out.push_str(&text[last..]);
            Cow::Owned(out)
        }
    }

    /// Whether the registry holds a value.
    fn is_empty(&self) -> bool {
        self.entries
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .automaton
            .is_none()
    }

    /// Replaces every registered value in `bytes`, read as UTF-8.
    pub fn redact_bytes<'a>(&self, bytes: &'a [u8]) -> Cow<'a, [u8]> {
        let text = String::from_utf8_lossy(bytes);
        match self.redact(&text) {
            Cow::Borrowed(_) => Cow::Borrowed(bytes),
            Cow::Owned(redacted) => Cow::Owned(redacted.into_bytes()),
        }
    }

    /// Replaces every registered value in the strings of `value`, keys
    /// included.
    pub fn redact_json(&self, value: &mut Value) {
        if !self.is_empty() {
            rewrite_json_strings(value, &|text| self.redact(text));
        }
    }
}

/// Whether `c` can be part of an identifier or a word.
fn is_token_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

/// Whether the match at `text[start..end]` is not part of a longer token.
/// An edge of the match that is itself no token character, such as a quote
/// or a bracket, needs no boundary.
fn at_token_boundary(text: &str, start: usize, end: usize) -> bool {
    let found = &text[start..end];
    let before = &text[..start];
    let before_ok = !found.starts_with(is_token_char)
        || before.chars().next_back().is_none_or(|c| !is_token_char(c))
        || ends_with_escape(before);
    let after_ok = !found.ends_with(is_token_char)
        || text[end..].chars().next().is_none_or(|c| !is_token_char(c));
    before_ok && after_ok
}

/// Whether `text` ends with an escape sequence that a formatter glued to
/// the next token: `\n` from `{:?}`, or `%3D` from percent-encoding.
fn ends_with_escape(text: &str) -> bool {
    let bytes = text.as_bytes();
    let n = bytes.len();
    (n >= 2 && bytes[n - 2] == b'\\')
        || (n >= 3
            && bytes[n - 3] == b'%'
            && bytes[n - 2].is_ascii_hexdigit()
            && bytes[n - 1].is_ascii_hexdigit())
}

/// `value` as `{:?}` prints it, without the quotes.
fn debug_escaped(value: &str) -> String {
    let escaped = format!("{value:?}");
    escaped[1..escaped.len() - 1].to_string()
}

/// `value` as serde_json writes it, without the quotes. It differs from the
/// debug form for control characters: `\u0001` against `\u{1}`.
fn json_escaped(value: &str) -> String {
    let escaped = Value::String(value.to_string()).to_string();
    escaped[1..escaped.len() - 1].to_string()
}

#[cfg(test)]
mod tests {
    use super::{MIN_REDACTED_LEN, SecretRedactor};
    use serde_json::json;

    const REFERENCE: &str = "${secret:kubernetes:pg/uri}";

    /// `{:?}` and percent-encoding glue an escape to the value: the escape
    /// is a boundary, not part of the token.
    #[test]
    fn an_escape_before_the_value_is_a_boundary() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "hunter2hunter2");
        assert_eq!(
            redactor.redact(r"line\nhunter2hunter2"),
            format!(r"line\n{REFERENCE}")
        );
        assert_eq!(
            redactor.redact("x%3Dhunter2hunter2"),
            format!("x%3D{REFERENCE}")
        );
        // No escape here: the value is glued to a letter, so it is read as
        // part of a longer word and stays.
        assert_eq!(redactor.redact("nhunter2hunter2"), "nhunter2hunter2");
    }

    #[test]
    fn panic_redaction_does_not_wait_for_a_registry_writer() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "panic-lock-secret-2136");
        let writer = redactor.entries.write().unwrap();
        assert_eq!(
            redactor.redact_for_panic("panic-lock-secret-2136"),
            "[REDACTED]"
        );
        drop(writer);
        assert_eq!(
            redactor.redact_for_panic("panic-lock-secret-2136"),
            REFERENCE
        );
        assert_eq!(
            redactor.redact_for_panic("ordinary panic"),
            "ordinary panic"
        );
    }

    /// A panic while a writer held the lock poisons it. Later panics still
    /// get their payload redacted instead of omitted.
    #[test]
    fn a_poisoned_registry_still_redacts_panic_payloads() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "poison-secret-2136");
        let poison = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _writer = redactor.entries.write().unwrap();
            panic!("registration failed");
        }));
        assert!(poison.is_err());
        assert!(redactor.entries.is_poisoned());
        assert_eq!(
            redactor.redact_for_panic("bad poison-secret-2136"),
            format!("bad {REFERENCE}")
        );
        assert_eq!(
            redactor.redact_for_panic("ordinary panic"),
            "ordinary panic"
        );
    }

    #[test]
    fn concurrent_registration_preserves_every_reference() {
        let redactor = SecretRedactor::new();
        let start = std::sync::Barrier::new(4);
        std::thread::scope(|scope| {
            for worker in 0..4 {
                let redactor = &redactor;
                let start = &start;
                scope.spawn(move || {
                    start.wait();
                    for index in 0..25 {
                        let reference = format!("${{secret:kubernetes:worker{worker}/key{index}}}");
                        let value = format!("concurrent-secret-{worker}-{index}-2136");
                        redactor.register(&reference, &value);
                        assert_eq!(redactor.redact(&value), reference);
                    }
                });
            }
        });
        for worker in 0..4 {
            for index in 0..25 {
                assert_eq!(
                    redactor.redact(&format!("concurrent-secret-{worker}-{index}-2136")),
                    format!("${{secret:kubernetes:worker{worker}/key{index}}}")
                );
            }
        }
    }

    #[test]
    fn a_rejected_longer_match_does_not_hide_a_reference() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "nested-secret-2136");
        redactor.register_credential("prefix/nested-secret-2136");
        assert_eq!(
            redactor.redact("xprefix/nested-secret-2136"),
            format!("xprefix/{REFERENCE}")
        );
        redactor.register_credential("nested-secret-2136/suffix");
        assert_eq!(
            redactor.redact("nested-secret-2136/suffixx"),
            format!("{REFERENCE}/suffixx")
        );
    }

    #[test]
    fn credential_key_collisions_preserve_all_details() {
        let redactor = SecretRedactor::new();
        redactor.register_credential("first-credential-2136");
        redactor.register_credential("second-credential-2136");
        let mut value =
            json!({"first-credential-2136":1, "second-credential-2136":2, "[REDACTED]":3});
        redactor.redact_json(&mut value);
        let mut values: Vec<_> = value
            .as_object()
            .unwrap()
            .values()
            .map(|v| v.as_i64().unwrap())
            .collect();
        values.sort();
        assert_eq!(values, vec![1, 2, 3]);
        assert!(!value.to_string().contains("credential-2136"));
    }

    #[test]
    fn references_win_over_credentials_in_either_order() {
        for credential_first in [true, false] {
            let redactor = SecretRedactor::new();
            let value = "quote\"and\\secret-2136";
            if credential_first {
                redactor.register_credential(value);
                assert_eq!(redactor.redact(value), "[REDACTED]");
            }
            redactor.register(REFERENCE, value);
            redactor.register_credential(value);
            redactor.register("${secret:kubernetes:other/key}", value);
            assert_eq!(redactor.redact(value), REFERENCE);
            assert_eq!(
                redactor.redact(&super::json_escaped(value)),
                format!("[REDACTED {REFERENCE}]")
            );
            assert_eq!(
                redactor.redact(&urlencoding::encode(value)),
                format!("[REDACTED {REFERENCE}]")
            );
        }
    }

    /// A rejected match scans the registry for a shorter form. Many near
    /// misses against a large registry still give the right result quickly.
    #[test]
    fn many_near_misses_against_a_large_registry() {
        let redactor = SecretRedactor::new();
        for index in 0..200 {
            redactor.register_credential(&format!("near-miss-value-{index:03}-2136"));
        }
        let line = (0..100)
            .map(|index| format!("xnear-miss-value-{:03}-2136x", index % 200))
            .collect::<Vec<_>>()
            .join(" ");
        assert_eq!(redactor.redact(&line), line);
        assert_eq!(
            redactor.redact(&format!("{line} near-miss-value-007-2136")),
            format!("{line} [REDACTED]")
        );
    }

    #[test]
    fn inline_credentials_keep_the_length_floor_and_token_boundaries() {
        let redactor = SecretRedactor::new();
        for value in ["on", "1", "  short  "] {
            redactor.register_credential(value);
        }
        assert_eq!(redactor.redact("on 1 short"), "on 1 short");
        redactor.register_credential("inline-credential-2136");
        assert_eq!(
            redactor.redact("password inline-credential-2136 rejected"),
            "password [REDACTED] rejected"
        );
        assert_eq!(
            redactor.redact("prefixinline-credential-2136suffix"),
            "prefixinline-credential-2136suffix"
        );
    }

    #[test]
    fn value_becomes_its_reference() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "hunter2-hunter2");
        assert_eq!(
            redactor.redact("password hunter2-hunter2 rejected"),
            format!("password {REFERENCE} rejected")
        );
        assert_eq!(
            redactor.redact_bytes(b"ok hunter2-hunter2\n").as_ref(),
            format!("ok {REFERENCE}\n").as_bytes()
        );
    }

    #[test]
    fn url_password_and_encoded_forms_are_redacted() {
        let redactor = SecretRedactor::new();
        let uri = "postgres://feldera:p%40ss-word-2136@db.internal:5432/app";
        redactor.register(REFERENCE, uri);
        assert_eq!(redactor.redact(uri), REFERENCE);
        assert_eq!(
            redactor.redact("password \"p@ss-word-2136\" rejected"),
            format!("password \"[REDACTED {REFERENCE}]\" rejected")
        );
        assert_eq!(
            redactor.redact("password \"p%40ss-word-2136\" rejected"),
            format!("password \"[REDACTED {REFERENCE}]\" rejected")
        );
        assert_eq!(
            redactor.redact(&urlencoding::encode(uri)),
            format!("[REDACTED {REFERENCE}]")
        );
    }

    #[test]
    fn trimmed_and_escaped_forms_are_redacted() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "quote\"and\\slash\n");
        assert_eq!(
            redactor.redact("x quote\"and\\slash y"),
            format!("x {REFERENCE} y")
        );
        assert_eq!(
            redactor.redact("x quote\\\"and\\\\slash\\n y"),
            format!("x [REDACTED {REFERENCE}] y")
        );
    }

    /// A secret that is a common word stays inside longer identifiers; the
    /// word on its own is still replaced.
    #[test]
    fn a_common_word_leaves_longer_tokens_alone() {
        let redactor = SecretRedactor::new();
        redactor.register("${secret:kubernetes:pg/user}", "postgres");
        assert_eq!(
            redactor.redact("Unable to connect to postgres instance via postgres_input"),
            "Unable to connect to ${secret:kubernetes:pg/user} instance via postgres_input"
        );
        assert_eq!(
            redactor.redact("postgresql://h/db postgres1 'postgres'"),
            "postgresql://h/db postgres1 '${secret:kubernetes:pg/user}'"
        );
    }

    /// A control character is written differently by serde_json and by
    /// `{:?}`; both forms are recognized.
    #[test]
    fn json_escaped_control_characters_are_redacted() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "ctrl\u{1}secret-2136");
        let json = serde_json::to_string("value ctrl\u{1}secret-2136 end").unwrap();
        assert_eq!(
            redactor.redact(&json),
            format!("\"value [REDACTED {REFERENCE}] end\"")
        );
        assert_eq!(
            redactor.redact("value ctrl\\u{1}secret-2136 end"),
            format!("value [REDACTED {REFERENCE}] end")
        );
    }

    #[test]
    fn short_values_are_not_registered() {
        let redactor = SecretRedactor::new();
        let short = "a".repeat(MIN_REDACTED_LEN - 1);
        redactor.register(REFERENCE, &short);
        assert_eq!(redactor.redact(&short), short);
        // The floor counts the trimmed value.
        redactor.register(REFERENCE, &format!("{short}\n"));
        assert_eq!(redactor.redact(&short), short);
    }

    #[test]
    fn first_registration_wins_and_several_values_share_a_line() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "shared-secret-value");
        redactor.register("${secret:kubernetes:other/key}", "shared-secret-value");
        redactor.register("${secret:kubernetes:kafka/pw}", "kafka-secret-value");
        assert_eq!(
            redactor.redact("a shared-secret-value b kafka-secret-value c"),
            format!("a {REFERENCE} b ${{secret:kubernetes:kafka/pw}} c")
        );
    }

    #[test]
    fn json_strings_are_redacted() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "json-secret-value");
        let mut details = json!({
            "endpoint": "pg_in",
            "error": "connect json-secret-value failed",
            "chain": [{"cause": "json-secret-value"}, 1, null, true],
        });
        redactor.redact_json(&mut details);
        assert_eq!(
            details,
            json!({
                "endpoint": "pg_in",
                "error": format!("connect {REFERENCE} failed"),
                "chain": [{"cause": REFERENCE}, 1, null, true],
            })
        );
    }

    #[test]
    fn escaped_url_passwords_and_json_keys_are_redacted() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "postgres://u:pass%22word2136@db/app");
        assert_eq!(
            redactor.redact(r#"password pass\"word2136"#),
            format!("password [REDACTED {REFERENCE}]")
        );
        redactor.register(REFERENCE, "json-key-secret-2136");
        let mut details = json!({"json-key-secret-2136": "details"});
        redactor.redact_json(&mut details);
        assert_eq!(details, json!({REFERENCE: "details"}));
    }

    /// A URL match rejected at a token boundary must not hide the password
    /// inside it: the scan resumes one character further, not after the
    /// rejected match.
    #[test]
    fn a_rejected_longer_match_does_not_hide_the_password_inside() {
        let redactor = SecretRedactor::new();
        redactor.register(REFERENCE, "postgres://u:Sup3rSecretPw@db/app");
        assert_eq!(
            redactor.redact("connect postgres://u:Sup3rSecretPw@db/app2 failed"),
            format!("connect postgres://u:[REDACTED {REFERENCE}]@db/app2 failed")
        );
    }
}