redact-secret 0.1.0-beta.15

Deterministic secret detection and redaction for runtime data and AI context: scans text locally before it reaches logs, storage, telemetry, tools, or model context. Side-effect free: no runtime network, filesystem, environment, telemetry, secret storage, or UI.
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
761
762
763
764
765
766
767
768
769
//! New Relic User API Key and (ingest) License Key detection.
//!
//! New Relic's own API-key documentation
//! (`https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/`)
//! names several key types but publishes a character-class grammar for
//! neither in-scope one. What it does state, used directly below:
//!
//! - the **License Key** is "a 40-character hexadecimal string" used for
//!   data ingest (all telemetry except browser and mobile);
//! - the **User Key** authenticates NerdGraph/REST API calls and is
//!   per-user, not per-account.
//!
//! The User Key's `NRAK-` prefix itself comes from New Relic's own
//! Terraform provider documentation, not the API-keys page above:
//! `terraform-provider-newrelic`'s `migration_guide_v2.html.markdown` states
//! "Your **User API Key** has a prefix of `NRAK-`" and separately "Most User
//! API keys have the `NRAK-` prefix" -- the "most" is a documented hedge
//! against a legacy key (the same guide's diff example shows the older,
//! now-replaced `NRAA-`-prefixed admin key), acknowledged as a known false
//! negative below rather than fuzzy-matched. Consulted only as external
//! behavioral references per `AGENTS.md`, gitleaks's `new-relic-user-api-key`
//! rule and trufflehog 3.97.4's independent `newrelicuserkey` detector both
//! converge on exactly `NRAK-` followed by 27 bytes of `[A-Z0-9]` (32 bytes
//! total) with no documented counter-evidence of a different length; no code
//! from either project is reproduced here.
//!
//! ## Grammar (frozen before implementation, per issue #307)
//!
//! - **User API Key**: the literal `NRAK-`, then exactly 27 bytes of
//!   [`pattern::is_upper_alnum`] (`[A-Z0-9]`) -- 32 bytes total, bounded on
//!   both sides by a byte outside [`pattern::is_alnum`] (wider than the
//!   match alphabet, so an adjacent lowercase byte still rejects a truncated
//!   slice of a longer identifier, mirroring [`super::aws`]'s own
//!   `is_upper_alnum`/`is_alnum` pairing). `High` confidence, `Provider`
//!   specificity, always redacted -- the prefix and exact length together
//!   are specific enough to be actionable on their own, the same class every
//!   other exact-length prefixed detector in this registry gets.
//! - **License Key**: a bare run of exactly 40 [`is_lower_hex`] (`[0-9a-f]`)
//!   bytes (the legacy all-hex generation), exactly 32 [`is_lower_hex`]
//!   bytes followed by the literal `FFFFNRAL`, or the literal `eu01xx`, then
//!   exactly 26 [`is_lower_hex`] bytes, then `FFFFNRAL` (the current
//!   generation and its EU-region form, 40 bytes each; see "Current-generation
//!   suffix" below), bounded by a byte
//!   outside [`pattern::is_alnum`]. Unlike the User
//!   Key, the legacy all-hex format carries no marker of its own -- a 40-character hex
//!   string is indistinguishable by shape alone from a `git` commit hash, a
//!   SHA-1 digest, or countless other opaque hex blobs; only the current
//!   generation carries a marker (the `FFFFNRAL` suffix, see
//!   "Current-generation suffix" below). For that legacy shape only (the marked shapes
//!   need no context, see "No keyword gate for the marked shapes" below),
//!   per the issue's own "ambiguous unprefixed
//!   values require reliable context" instruction, this detector requires a
//!   case-insensitive `newrelic`/`new_relic`/`new-relic`/`new relic`
//!   substring ([`CONTEXT_KEYWORDS`]) anywhere on the same line -- the same
//!   "same line" scope [`super::twilio`] already uses for its own bare-hex
//!   formats, for the same incremental-consistency reason documented there.
//!   `Medium` confidence, `Provider` specificity, confidence-gated (not in
//!   `ALWAYS_REDACT_TYPES`) -- there is no paired-identifier signal available
//!   here the way [`super::twilio`]'s Account SID/API Key SID give its own
//!   bare formats a `High`-confidence tier, so this format never rises above
//!   `Medium`.
//!
//! A candidate that is a single repeated character
//! ([`text::is_repeated_character_filler`]) is excluded from both formats:
//! both alphabets (`[A-Z0-9]`, `[0-9a-f]`) contain characters (`X`, `0`, ...)
//! a masked placeholder commonly repeats, so, per [`super::twilio`]'s same
//! precedent, neither format has a structural marker of its own that would
//! otherwise keep a masked value like `NRAK-XXXXXXXXXXXXXXXXXXXXXXXXXXX` from
//! matching the bare alphabet.
//!
//! ## Scope
//!
//! Per the issue's explicit scope ("Scope user API keys and documented
//! license/ingest credentials... exclude application IDs and public
//! configuration identifiers"), and its instruction to "explicitly review
//! browser/mobile ingestion-key policy instead of assuming client visibility
//! implies harmlessness":
//!
//! - The Browser Key and Mobile App Token are reviewed and intentionally
//!   excluded, not assumed harmless by default: New Relic's own
//!   documentation classifies both as public and client-visible by design
//!   (the Browser Key ships in every monitored page's rendered HTML; the
//!   Mobile App Token is compiled into the distributed app binary) -- an
//!   intended public-distribution mechanism, not a leak, unlike the secret
//!   License Key and User Key this module targets. Flagging a value New
//!   Relic itself designs for public embedding would treat correct usage as
//!   an incident.
//! - The legacy Insights Insert/Query Keys (`NRII-`/`NRIQ-`), the deprecated
//!   Admin Key (REST API keys reached end-of-life in 2025, per New Relic's
//!   own `whats-new-03-01-rest-api-keys-eol` notice), and the bare
//!   account-scoped "user API id" gitleaks separately detects (a 64-byte
//!   alphanumeric identifier, not a secret) are all out of the issue's
//!   explicit scope sentence above and not implemented here; they remain
//!   documented gaps for a future issue, not silently dropped.
//!
//! ### Current-generation suffix (`FFFFNRAL`)
//!
//! The currently issued License Key is 32 lowercase-hex bytes followed by the
//! literal `FFFFNRAL`; its EU-region form is `eu01xx`, 26 lowercase-hex bytes,
//! then `FFFFNRAL`. This was previously rejected as uncorroborated; it is now
//! adopted (issue #672, on the research recorded in issue #656) because:
//!
//! - New Relic's own documentation (the IBM MQ host-integration guide)
//!   describes the ingest license key as "40 chars, suffix NRAL", and its
//!   eBPF install pages show `licenseKey` examples ending `FFFFNRAL`;
//! - New Relic-authored code (`newrelic-cli`'s `IsValidLicenseKeyFormat`,
//!   which also names the `eu01xx` prefix, and `docs-website`'s
//!   `check-for-keys.sh`) and New Relic staff on its support forum agree on
//!   the suffix and on EU keys always starting `eu01xx`;
//! - trufflehog 3.97.4's `newreliclicensekey` independently matches
//!   `([0-9a-f]{32}|eu01xx[0-9a-f]{26})FFFFNRAL`.
//!
//! The provider's own sources leave the body alphabet and the `FFFF` segment
//! tool-corroborated only. Both marked shapes keep the legacy shape's
//! `Medium` confidence and finding type: promoting confidence is a separate
//! policy decision neither #672 nor #754 makes. The hex body must be a
//! maximal run of exactly the stated length, so a longer hex run before the
//! marker is rejected rather than truncated.
//!
//! ### No keyword gate for the marked shapes (issue #754)
//!
//! #672 first put the marked shapes behind the same-line keyword gate built
//! for the legacy bare 40-hex shape. That gate missed the contexts New Relic's
//! own agents and APIs actually use, which name the credential rather than the
//! provider: `license_key:` in `newrelic.yml` and `newrelic.js`, the Log API's
//! `X-License-Key:` header, and the Helm bundle's `licenseKey:`. The marked
//! shapes are now detected without any context, and the gate stays only on the
//! legacy shape. `FFFFNRAL` is the provider-documented `NRAL` suffix
//! ("40 chars, suffix NRAL", docs.newrelic.com IBM MQ host-integration guide;
//! `FFFFNRAL`-ending `licenseKey` examples on the eBPF install pages). It is an
//! uppercase, 8-byte literal that must follow an exact 32-byte lowercase-hex
//! run (or `eu01xx` plus 26), with an alphanumeric boundary on both sides.
//! Commit hashes, SHA-1/SHA-256 digests and other hex blobs cannot take that
//! shape, and the legacy bare 40-hex shape still needs a keyword. The
//! context-free path emits the `new-relic-license-key-suffix-marker` signal
//! only; a keyword on the line adds `new-relic-keyword-cooccurrence`.
//!
//! Not adopted, and left as documented gaps: the first `NRAL` generation (36
//! hex bytes then `NRAL`, labelled `_OLD` in New Relic's own scanner), and
//! region prefixes other than `eu01xx` (`us01`, `gov01`, `jp`, one-`x`
//! padding), none of which the issue evidence covers.
//!
//! ## Consequences and known gaps
//!
//! - A User Key issued under the legacy, pre-`NRAK-` scheme (the guide's own
//!   "most User API keys have the `NRAK-` prefix" hedge) goes undetected by
//!   this dedicated path; a qualified `name=value` assignment of one still
//!   gets a lower-confidence, lower-specificity contextual finding through
//!   the existing generic-token path regardless of format.
//! - A first-generation `NRAL` License Key (36 hex bytes then `NRAL`) or a
//!   region-aware key with a prefix other than `eu01xx` goes undetected.
//! - A legacy all-hex License Key with no
//!   `newrelic`/`new_relic`/`new-relic`/`new relic` keyword anywhere on its
//!   own line goes undetected -- the same accepted tradeoff
//!   [`super::twilio`]'s own bare hex formats already carry. The marked
//!   (`FFFFNRAL`) shapes need no keyword (#754).
//! - A benign 40-byte lowercase-hex value (a commit SHA, a digest) that
//!   happens to share a line with one of the four keywords would false
//!   positive; this is the same class of risk every other keyword-gated
//!   bare-format detector in this registry already accepts.

use crate::detectors::pattern::{self, RunLength};
use crate::detectors::prefilter::Literals;
use crate::detectors::text;
use crate::error::DetectorFailure;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};

const USER_API_KEY_PREFIX: &str = "NRAK-";
const USER_API_KEY_BODY_LEN: usize = 27;
const LICENSE_KEY_LEN: usize = 40;
/// The current-generation License Key: a 32-byte lowercase-hex body followed
/// by this literal marker, 40 bytes total.
const LICENSE_KEY_MARKER: &str = "FFFFNRAL";
const LICENSE_KEY_MARKED_BODY_LEN: usize = LICENSE_KEY_LEN - LICENSE_KEY_MARKER.len();
/// The EU-region current-generation License Key: this literal, then 26
/// lowercase-hex bytes, then [`LICENSE_KEY_MARKER`] (40 bytes total).
const LICENSE_KEY_EU_PREFIX: &str = "eu01xx";
const LICENSE_KEY_EU_BODY_LEN: usize = LICENSE_KEY_MARKED_BODY_LEN - LICENSE_KEY_EU_PREFIX.len();

/// New Relic's own documented naming for the License Key credential,
/// case-insensitively, in every spelling gitleaks's independent
/// `new-relic-*` keyword lists already converge on (`newrelic`,
/// `new_relic`, `new-relic`) plus the two-word prose spelling New Relic's
/// own product name uses (`new relic`), for a same-line comment such as
/// `# New Relic license key: <hex>`.
const CONTEXT_KEYWORDS: [&str; 4] = ["newrelic", "new_relic", "new-relic", "new relic"];

/// [`CONTEXT_KEYWORDS`] as they appear in a normalized key name.
const NAMED_ASSIGNMENT_KEYWORDS: [&str; 2] = ["newrelic", "new_relic"];

/// `[0-9a-f]`: the License Key's documented "hexadecimal string" alphabet,
/// lowercase only. Matches [`super::twilio`]'s own `is_lower_hex`, which
/// documents the same rationale: an uppercase-hex run is not a coincidental
/// case variant of this format, it is a structurally different value.
fn is_lower_hex(byte: u8) -> bool {
    byte.is_ascii_digit() || byte.is_ascii_lowercase() && byte <= b'f'
}

/// Detects a New Relic User API Key by its documented `NRAK-` prefix and
/// exact 27-byte uppercase-alphanumeric body.
pub(super) struct NewRelicUserApiKeyDetector;

impl Detector for NewRelicUserApiKeyDetector {
    fn id(&self) -> &'static str {
        "new-relic-user-api-key"
    }

    fn detect(
        &self,
        input: &str,
        _context: &DetectorContext,
    ) -> Result<Vec<Candidate>, DetectorFailure> {
        let mut candidates = Vec::new();
        for (start, end) in pattern::scan_prefixed_runs(
            input,
            &[USER_API_KEY_PREFIX],
            RunLength::Exact(USER_API_KEY_BODY_LEN),
            pattern::is_upper_alnum,
            pattern::is_alnum,
        ) {
            if text::is_repeated_character_filler(&input[start + USER_API_KEY_PREFIX.len()..end]) {
                continue;
            }
            let Some(range) = ByteRange::new(start, end) else {
                continue;
            };
            candidates.push(
                Candidate::built_in("new_relic_user_api_key", Confidence::High, range)
                    .with_specificity(Specificity::Provider)
                    .with_signals([
                        "new-relic-documented-prefix",
                        "exact-length-upper-alnum-body",
                    ]),
            );
        }
        Ok(candidates)
    }
}

/// `true` when any of [`CONTEXT_KEYWORDS`] occurs (case-insensitively)
/// anywhere in `line`.
fn line_has_context_keyword(line: &str) -> bool {
    CONTEXT_KEYWORDS
        .iter()
        .any(|needle| text::contains_ci(line, needle))
}

/// Detects a New Relic License Key: a marked current-generation key
/// (`FFFFNRAL` suffix) anywhere, or a bare 40-byte lowercase-hex run on a
/// line that also carries a [`CONTEXT_KEYWORDS`] substring.
pub(super) struct NewRelicLicenseKeyDetector;

impl Detector for NewRelicLicenseKeyDetector {
    fn id(&self) -> &'static str {
        "new-relic-license-key"
    }

    fn detect(
        &self,
        input: &str,
        _context: &DetectorContext,
    ) -> Result<Vec<Candidate>, DetectorFailure> {
        let mut candidates = Vec::new();
        text::for_each_long_run_line(input, |line_start, line_end| {
            let line = &input[line_start..line_end];
            let has_keyword = line_has_context_keyword(line);
            // A context-free line can only hold a marked (current-generation)
            // key, so skip it outright unless the marker literal is present.
            if !has_keyword && !line.contains(LICENSE_KEY_MARKER) {
                return;
            }
            let push = |candidates: &mut Vec<Candidate>, start: usize, end: usize, marked: bool| {
                let Some(range) = ByteRange::new(line_start + start, line_start + end) else {
                    return;
                };
                let signals: &[&str] = match (has_keyword, marked) {
                    (true, true) => &[
                        "new-relic-keyword-cooccurrence",
                        "new-relic-license-key-suffix-marker",
                    ],
                    (true, false) => &["new-relic-keyword-cooccurrence"],
                    (false, _) => &["new-relic-license-key-suffix-marker"],
                };
                // Issue #702: a key that names New Relic's credential is the
                // same evidence generic-token redacts at high confidence.
                let named = has_keyword
                    && text::is_provider_named_assignment(line, start, &NAMED_ASSIGNMENT_KEYWORDS);
                let confidence = if named {
                    Confidence::High
                } else {
                    Confidence::Medium
                };
                candidates.push(
                    Candidate::built_in("new_relic_license_key", confidence, range)
                        .with_specificity(Specificity::Provider)
                        .with_signals(
                            signals
                                .iter()
                                .copied()
                                .chain(named.then_some("new-relic-named-assignment")),
                        ),
                );
            };

            let bytes = line.as_bytes();
            let mut start = 0usize;
            while start < bytes.len() {
                if !is_lower_hex(bytes[start]) {
                    start += 1;
                    continue;
                }
                let run_end = pattern::run_end(bytes, start, is_lower_hex);
                // The legacy all-hex shape carries no marker of its own, so it
                // still needs a same-line keyword; the marked shape does not.
                let key_end = if has_keyword && run_end - start == LICENSE_KEY_LEN {
                    Some(run_end)
                } else if run_end - start == LICENSE_KEY_MARKED_BODY_LEN
                    && line[run_end..].starts_with(LICENSE_KEY_MARKER)
                {
                    Some(run_end + LICENSE_KEY_MARKER.len())
                } else {
                    None
                };
                if let Some(key_end) = key_end
                    && pattern::boundary_ok(bytes, start, key_end, pattern::is_alnum)
                    && !text::is_repeated_character_filler(&line[start..run_end])
                {
                    push(&mut candidates, start, key_end, key_end != run_end);
                }
                start = run_end;
            }

            let mut ends = pattern::RunCursor::new(bytes, is_lower_hex);
            for (prefix_at, _) in line.match_indices(LICENSE_KEY_EU_PREFIX) {
                let body_start = prefix_at + LICENSE_KEY_EU_PREFIX.len();
                let body_end = ends.end(body_start);
                let key_end = body_end + LICENSE_KEY_MARKER.len();
                if body_end - body_start == LICENSE_KEY_EU_BODY_LEN
                    && line[body_end..].starts_with(LICENSE_KEY_MARKER)
                    && pattern::boundary_ok(bytes, prefix_at, key_end, pattern::is_alnum)
                    && !text::is_repeated_character_filler(&line[body_start..body_end])
                {
                    push(&mut candidates, prefix_at, key_end, true);
                }
            }
        });
        super::sort_candidates_by_start(&mut candidates);
        Ok(candidates)
    }
}

/// The literals one of which every New Relic user-key candidate contains, for the
/// shared prefilter (`super::prefilter`, issue #983).
pub(super) const USER_REQUIRED_LITERALS: &[Literals] = &[Literals::Strs(&[USER_API_KEY_PREFIX])];

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

    const USER_API_KEY_BODY: &str = "SYNTHETICREVOKEDNEWRELICUSA";
    const LICENSE_KEY: &str = "0123456789abcdef0123456789abcdef01234567";

    fn user_api_key() -> String {
        format!("{USER_API_KEY_PREFIX}{USER_API_KEY_BODY}")
    }

    fn detect_user_api_key(input: &str) -> Vec<Candidate> {
        NewRelicUserApiKeyDetector
            .detect(input, &DetectorContext::new(input.len()))
            .unwrap()
    }

    fn detect_license_key(input: &str) -> Vec<Candidate> {
        NewRelicLicenseKeyDetector
            .detect(input, &DetectorContext::new(input.len()))
            .unwrap()
    }

    #[test]
    fn fixtures_are_exactly_documented_length() {
        assert_eq!(USER_API_KEY_BODY.len(), USER_API_KEY_BODY_LEN);
        assert_eq!(LICENSE_KEY.len(), LICENSE_KEY_LEN);
    }

    #[test]
    fn detects_the_synthetic_user_api_key() {
        let value = user_api_key();
        let candidates = detect_user_api_key(&value);
        assert_eq!(candidates.len(), 1);
        assert_eq!(candidates[0].type_name(), "new_relic_user_api_key");
        assert_eq!(candidates[0].confidence(), Confidence::High);
        assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(0, value.len()).unwrap()
        );
    }

    #[test]
    fn detects_the_user_api_key_bare_in_env_and_control_contexts() {
        let value = user_api_key();
        for input in [
            value.clone(),
            format!("NEW_RELIC_API_KEY={value}"),
            format!("api_key: {value}"),
            format!("{{\"apiKey\": \"{value}\"}}"),
        ] {
            let candidates = detect_user_api_key(&input);
            assert_eq!(candidates.len(), 1, "{input}");
            let (start, end) = (candidates[0].range().start(), candidates[0].range().end());
            assert_eq!(&input[start..end], value, "{input}");
        }
    }

    #[test]
    fn rejects_a_user_api_key_body_one_byte_short_of_the_required_length() {
        let short = &USER_API_KEY_BODY[..USER_API_KEY_BODY.len() - 1];
        assert!(detect_user_api_key(&format!("{USER_API_KEY_PREFIX}{short}")).is_empty());
    }

    #[test]
    fn rejects_a_user_api_key_body_one_byte_longer_than_the_required_length_rather_than_truncating()
    {
        let long = format!("{USER_API_KEY_BODY}A");
        assert!(detect_user_api_key(&format!("{USER_API_KEY_PREFIX}{long}")).is_empty());
    }

    #[test]
    fn rejects_a_legacy_admin_key_prefix() {
        assert!(detect_user_api_key(&format!("NRAA-{USER_API_KEY_BODY}")).is_empty());
    }

    #[test]
    fn rejects_a_lowercase_user_api_key_body() {
        let lower = USER_API_KEY_BODY.to_ascii_lowercase();
        assert!(detect_user_api_key(&format!("{USER_API_KEY_PREFIX}{lower}")).is_empty());
    }

    #[test]
    fn rejects_a_masked_user_api_key() {
        assert!(
            detect_user_api_key(&format!("{USER_API_KEY_PREFIX}{}", "X".repeat(27))).is_empty()
        );
    }

    #[test]
    fn rejects_a_user_api_key_environment_variable_reference() {
        assert!(
            detect_user_api_key(&format!("{USER_API_KEY_PREFIX}${{NEW_RELIC_API_KEY}}")).is_empty()
        );
    }

    #[test]
    fn rejects_a_user_api_key_embedded_in_a_wider_identifier() {
        let value = user_api_key();
        assert!(detect_user_api_key(&format!("x{value}")).is_empty());
        assert!(detect_user_api_key(&format!("{value}x")).is_empty());
    }

    #[test]
    fn does_not_flag_a_bare_short_application_or_account_id() {
        assert!(detect_user_api_key("accountId: 1234567890").is_empty());
    }

    #[test]
    fn finds_a_user_api_key_match_across_crlf_and_a_unicode_prefix() {
        let value = user_api_key();
        let input = format!("# \u{1F511} caf\u{e9}\r\n{value}\r\n");
        let candidates = detect_user_api_key(&input);
        assert_eq!(candidates.len(), 1);
        let start = input.find(&value).unwrap();
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(start, start + value.len()).unwrap()
        );
    }

    #[test]
    fn finds_deterministic_user_api_key_findings_across_repeated_calls() {
        let input = user_api_key();
        assert_eq!(detect_user_api_key(&input), detect_user_api_key(&input));
    }

    #[test]
    fn stays_bounded_over_a_long_run_of_rejected_user_api_key_prefixes() {
        let input = format!("{}!{}", user_api_key(), "NRAK-!".repeat(10_000));
        let candidates = detect_user_api_key(&input);
        assert_eq!(candidates.len(), 1);
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(0, user_api_key().len()).unwrap()
        );
    }

    #[test]
    fn detects_a_license_key_named_by_a_new_relic_keyword() {
        for input in [
            format!("NEW_RELIC_LICENSE_KEY={LICENSE_KEY}"),
            format!("newrelic.license_key: {LICENSE_KEY}"),
            format!("# New Relic license key: {LICENSE_KEY}"),
            format!("{{\"new-relic-license-key\": \"{LICENSE_KEY}\"}}"),
        ] {
            let candidates = detect_license_key(&input);
            assert_eq!(candidates.len(), 1, "{input}");
            assert_eq!(candidates[0].type_name(), "new_relic_license_key");
            let expected = if input.starts_with('#') {
                Confidence::Medium
            } else {
                Confidence::High
            };
            assert_eq!(candidates[0].confidence(), expected, "{input}");
            assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
            let start = input.rfind(LICENSE_KEY).unwrap();
            assert_eq!(
                candidates[0].range(),
                ByteRange::new(start, start + LICENSE_KEY.len()).unwrap()
            );
        }
    }

    const CURRENT_KEY: &str = "0123456789abcdef0123456789abcdefFFFFNRAL";
    const EU_KEY: &str = "eu01xx0123456789abcdef0123456789FFFFNRAL";

    #[test]
    fn current_generation_fixtures_are_exactly_forty_bytes() {
        assert_eq!(CURRENT_KEY.len(), LICENSE_KEY_LEN);
        assert_eq!(EU_KEY.len(), LICENSE_KEY_LEN);
    }

    #[test]
    fn detects_the_current_generation_license_key_named_by_a_keyword() {
        for key in [CURRENT_KEY, EU_KEY] {
            for input in [
                format!("newrelic {key}"),
                format!("NEW_RELIC_LICENSE_KEY={key}"),
                format!("{{\"new-relic-license-key\": \"{key}\"}}"),
                format!("newrelic '{key}'"),
                format!("# \u{1F511} caf\u{e9}\r\nnewrelic {key}\r\n"),
            ] {
                let candidates = detect_license_key(&input);
                assert_eq!(candidates.len(), 1, "{input}");
                assert_eq!(candidates[0].type_name(), "new_relic_license_key");
                let expected = if input.contains('=') || input.contains(':') {
                    Confidence::High
                } else {
                    Confidence::Medium
                };
                assert_eq!(candidates[0].confidence(), expected, "{input}");
                assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
                let start = input.rfind(key).unwrap();
                assert_eq!(
                    candidates[0].range(),
                    ByteRange::new(start, start + key.len()).unwrap()
                );
            }
        }
    }

    #[test]
    fn detects_a_current_generation_license_key_with_no_keyword_on_the_line() {
        for key in [CURRENT_KEY, EU_KEY] {
            for input in [
                key.to_owned(),
                format!("common: &default_settings\n  license_key: '{key}'\n"),
                format!("exports.config = {{\n  license_key: '{key}',\n}}\n"),
                format!("POST /log/v1 HTTP/1.1\nX-License-Key: {key}\n"),
                format!("global:\n  licenseKey: {key}\n"),
            ] {
                let candidates = detect_license_key(&input);
                assert_eq!(candidates.len(), 1, "{input}");
                assert_eq!(candidates[0].type_name(), "new_relic_license_key");
                assert_eq!(candidates[0].confidence(), Confidence::Medium);
                assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
                let start = input.find(key).unwrap();
                assert_eq!(
                    candidates[0].range(),
                    ByteRange::new(start, start + key.len()).unwrap()
                );
            }
        }
    }

    #[test]
    fn keeps_the_keyword_gate_on_the_legacy_shape_beside_a_context_free_marked_key() {
        let input = format!("license_key: {CURRENT_KEY} commit {LICENSE_KEY}");
        let candidates = detect_license_key(&input);
        assert_eq!(candidates.len(), 1);
        let start = input.find(CURRENT_KEY).unwrap();
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(start, start + CURRENT_KEY.len()).unwrap()
        );
    }

    #[test]
    fn rejects_marked_lookalikes_with_the_wrong_body_length_and_no_keyword() {
        let short = &CURRENT_KEY[1..];
        let long = format!("0{CURRENT_KEY}");
        for input in [
            format!("license_key: {short}"),
            format!("license_key: {long}"),
            "commit 9b2f47c1e08d3a65f4c7b29e1d0a86f3c5e71b24".to_owned(),
            format!("license_key: x{CURRENT_KEY}"),
            format!("license_key: {CURRENT_KEY}x"),
        ] {
            assert!(detect_license_key(&input).is_empty(), "{input}");
        }
    }

    #[test]
    fn rejects_current_generation_keys_with_the_wrong_body_length() {
        let long = format!("0{CURRENT_KEY}");
        let short = &CURRENT_KEY[1..];
        let eu_long = format!("eu01xx0{}", &EU_KEY[6..]);
        let eu_short = format!("eu01xx{}", &EU_KEY[7..]);
        for key in [long, short.to_owned(), eu_long, eu_short] {
            assert!(
                detect_license_key(&format!("newrelic {key}")).is_empty(),
                "{key}"
            );
        }
    }

    #[test]
    fn rejects_a_current_generation_key_with_a_wrong_marker_or_case() {
        for key in [
            CURRENT_KEY.replace("FFFFNRAL", "FFFFNRAK"),
            CURRENT_KEY.replace("FFFFNRAL", "ffffnral"),
            CURRENT_KEY.replace("FFFFNRAL", "NRAL"),
            CURRENT_KEY.replace(
                "0123456789abcdef0123456789abcdef",
                "0123456789ABCDEF0123456789ABCDEF",
            ),
            EU_KEY.replace("eu01xx", "EU01XX"),
            EU_KEY.replace("eu01xx", "us01xx"),
        ] {
            assert!(
                detect_license_key(&format!("newrelic {key}")).is_empty(),
                "{key}"
            );
        }
    }

    #[test]
    fn rejects_a_current_generation_key_embedded_in_a_wider_identifier() {
        for key in [CURRENT_KEY, EU_KEY] {
            assert!(detect_license_key(&format!("newrelic x{key}")).is_empty());
            assert!(detect_license_key(&format!("newrelic {key}x")).is_empty());
            assert!(detect_license_key(&format!("newrelic {key}0")).is_empty());
        }
    }

    #[test]
    fn rejects_a_masked_current_generation_key() {
        let masked = format!("{}FFFFNRAL", "0".repeat(32));
        let masked_eu = format!("eu01xx{}FFFFNRAL", "0".repeat(26));
        assert!(detect_license_key(&format!("newrelic {masked}")).is_empty());
        assert!(detect_license_key(&format!("newrelic {masked_eu}")).is_empty());
        // New Relic's own documentation placeholder is asterisks, not hex.
        let placeholder = format!("{}FFFFNRAL", "*".repeat(32));
        assert!(detect_license_key(&format!("newrelic {placeholder}")).is_empty());
    }

    #[test]
    fn reports_every_license_key_generation_on_one_line_in_order() {
        let input = format!("newrelic {EU_KEY} {LICENSE_KEY} {CURRENT_KEY}");
        let candidates = detect_license_key(&input);
        assert_eq!(candidates.len(), 3);
        let starts: Vec<usize> = candidates.iter().map(|c| c.range().start()).collect();
        assert!(starts.windows(2).all(|pair| pair[0] < pair[1]));
    }

    #[test]
    fn stays_bounded_over_a_long_line_of_rejected_current_generation_candidates() {
        let input = format!(
            "newrelic {}",
            format!("{} ", &CURRENT_KEY[1..]).repeat(10_000)
        );
        assert!(detect_license_key(&input).is_empty());
        let eu = format!("newrelic {}", "eu01xx".repeat(10_000));
        assert!(detect_license_key(&eu).is_empty());
    }

    #[test]
    fn rejects_a_bare_license_key_with_no_context() {
        assert!(detect_license_key(LICENSE_KEY).is_empty());
    }

    #[test]
    fn rejects_a_license_key_whose_context_is_on_a_different_line() {
        let input = format!("# new relic\n{LICENSE_KEY}\n");
        assert!(detect_license_key(&input).is_empty());
    }

    #[test]
    fn rejects_an_uppercase_hex_run() {
        let upper = LICENSE_KEY.to_ascii_uppercase();
        assert!(detect_license_key(&format!("newrelic {upper}")).is_empty());
    }

    #[test]
    fn rejects_a_license_key_one_byte_short_of_the_required_length() {
        let short = &LICENSE_KEY[..LICENSE_KEY.len() - 1];
        assert!(detect_license_key(&format!("newrelic {short}")).is_empty());
    }

    #[test]
    fn rejects_a_license_key_one_byte_longer_than_the_required_length_rather_than_truncating() {
        let long = format!("{LICENSE_KEY}0");
        assert!(detect_license_key(&format!("newrelic {long}")).is_empty());
    }

    #[test]
    fn rejects_a_masked_license_key() {
        assert!(
            detect_license_key(&format!("newrelic {}", "0".repeat(LICENSE_KEY_LEN))).is_empty()
        );
    }

    #[test]
    fn rejects_a_license_key_environment_variable_reference() {
        assert!(
            detect_license_key("newrelic NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY}")
                .is_empty()
        );
    }

    #[test]
    fn finds_a_qualified_license_key_match_across_crlf_and_a_unicode_prefix() {
        let input = format!("# \u{1F511} caf\u{e9}\r\nnewrelic {LICENSE_KEY}\r\n");
        let candidates = detect_license_key(&input);
        assert_eq!(candidates.len(), 1);
        let start = input.rfind(LICENSE_KEY).unwrap();
        assert_eq!(
            candidates[0].range(),
            ByteRange::new(start, start + LICENSE_KEY.len()).unwrap()
        );
    }

    #[test]
    fn finds_deterministic_license_key_findings_across_repeated_calls() {
        let input = format!("newrelic {LICENSE_KEY}");
        assert_eq!(detect_license_key(&input), detect_license_key(&input));
    }

    #[test]
    fn stays_bounded_over_a_long_context_free_line_packed_with_license_key_candidates() {
        let input = format!("{LICENSE_KEY} ").repeat(10_000);
        assert_eq!(detect_license_key(&input).len(), 0);
    }

    #[test]
    fn stays_bounded_over_a_long_context_free_line_of_rejected_marked_candidates() {
        let input = format!("{} ", &CURRENT_KEY[1..]).repeat(10_000);
        assert!(detect_license_key(&input).is_empty());
    }

    #[test]
    fn every_license_key_candidate_on_a_context_bearing_line_is_reported() {
        let input = format!("newrelic {LICENSE_KEY} {LICENSE_KEY}");
        let candidates = detect_license_key(&input);
        assert_eq!(candidates.len(), 2);
        assert!(
            candidates
                .iter()
                .all(|candidate| candidate.confidence() == Confidence::Medium)
        );
    }
}