surjection-core 1.1.0

Core library of Surjection: MCP servers, skills and secret references for AI coding clients
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
// Portions derived from SkillDock (https://github.com/DailenG/skilldock), MIT, Copyright (c) 2026 wanghuan

//! Secret classifier (SPEC 11.7), shared by import and the leak scanner.
//!
//! A literal value is sensitive when
//!
//! - its key (env var, header, `--flag` name) matches
//!   `(?i)(token|secret|password|passwd|api[_-]?key|auth|credential|private[_-]?key|bearer)`,
//!   or is one of SkillDock's `SENSITIVE_ARGUMENT_NAMES`;
//! - it holds a known token format: `ghp_`, `github_pat_`, `gho_`, `sk-`,
//!   `sk-ant-`, `xox[abposr]-`, `AKIA` plus 16, `AIza`, `glpat-`, a PEM
//!   private key block or a JWT;
//! - it holds a URL with userinfo or a token-like query parameter;
//! - or it is longer than 20 characters with more than 3.5 bits of entropy
//!   per character under a sensitive key.
//!
//! References (`${env:..}`, `${secret:..}`) are never sensitive; in a value
//! that mixes them with text only the text is classified.
//!
//! Two strengths: [`classify_import`] flags any literal under a sensitive
//! key, because a person confirms every hit in the import wizard. The leak
//! scanner uses [`classify_stored`], which needs evidence in the value
//! itself (a token format, URL credentials, or high entropy under a
//! sensitive key), so `AUTH_MODE = "oauth"` can still be committed.
//!
//! No regular expression engine: the patterns are matched by hand.

use std::ops::Range;

use crate::secrets::grammar::{holds_reference, literal_parts};

/// Why a value counts as sensitive. Never carries the value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Reason {
    /// The key or `--flag` name marks a credential.
    SensitiveKey,
    /// The value holds a known token format.
    TokenFormat(TokenKind),
    /// A URL holds userinfo (`https://user:pass@host`).
    UrlUserinfo,
    /// A URL query parameter is named like a credential (`?access_token=`).
    UrlTokenParameter,
    /// High entropy (more than 3.5 bits per character over 20 or more
    /// characters) under a sensitive key.
    HighEntropy,
}

impl Reason {
    /// Stable `snake_case` name.
    pub const fn as_str(self) -> &'static str {
        match self {
            Reason::SensitiveKey => "sensitive_key",
            Reason::TokenFormat(_) => "token_format",
            Reason::UrlUserinfo => "url_userinfo",
            Reason::UrlTokenParameter => "url_token_parameter",
            Reason::HighEntropy => "high_entropy",
        }
    }

    /// English description for messages.
    pub fn describe(self) -> String {
        match self {
            Reason::SensitiveKey => "named like a credential".to_owned(),
            Reason::TokenFormat(kind) => format!("looks like {}", kind.describe()),
            Reason::UrlUserinfo => "a URL with a user name or password".to_owned(),
            Reason::UrlTokenParameter => "a URL with a credential query parameter".to_owned(),
            Reason::HighEntropy => "a high-entropy value under a credential name".to_owned(),
        }
    }
}

/// Known token formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TokenKind {
    /// `ghp_...`
    GithubClassic,
    /// `github_pat_...`
    GithubFineGrained,
    /// `gho_...`
    GithubOauth,
    /// `sk-...`
    OpenAi,
    /// `sk-ant-...`
    Anthropic,
    /// `xoxa-`, `xoxb-`, `xoxp-`, `xoxo-`, `xoxs-`, `xoxr-`
    Slack,
    /// `AKIA` followed by 16 upper-case letters or digits.
    AwsAccessKey,
    /// `AIza...`
    GoogleApiKey,
    /// `glpat-...`
    GitlabToken,
    /// `-----BEGIN ... PRIVATE KEY-----`
    PrivateKey,
    /// Three base64url segments, the first two JSON objects (`eyJ`).
    Jwt,
}

impl TokenKind {
    pub const fn as_str(self) -> &'static str {
        match self {
            TokenKind::GithubClassic => "github_classic",
            TokenKind::GithubFineGrained => "github_fine_grained",
            TokenKind::GithubOauth => "github_oauth",
            TokenKind::OpenAi => "openai",
            TokenKind::Anthropic => "anthropic",
            TokenKind::Slack => "slack",
            TokenKind::AwsAccessKey => "aws_access_key",
            TokenKind::GoogleApiKey => "google_api_key",
            TokenKind::GitlabToken => "gitlab_token",
            TokenKind::PrivateKey => "private_key",
            TokenKind::Jwt => "jwt",
        }
    }

    const fn describe(self) -> &'static str {
        match self {
            TokenKind::GithubClassic => "a GitHub personal access token",
            TokenKind::GithubFineGrained => "a GitHub fine-grained token",
            TokenKind::GithubOauth => "a GitHub OAuth token",
            TokenKind::OpenAi => "an OpenAI-style API key",
            TokenKind::Anthropic => "an Anthropic API key",
            TokenKind::Slack => "a Slack token",
            TokenKind::AwsAccessKey => "an AWS access key id",
            TokenKind::GoogleApiKey => "a Google API key",
            TokenKind::GitlabToken => "a GitLab token",
            TokenKind::PrivateKey => "a private key",
            TokenKind::Jwt => "a JSON Web Token",
        }
    }
}

/// One classifier hit inside a text: what and where (byte range of the
/// sensitive part).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hit {
    pub reason: Reason,
    pub range: Range<usize>,
}

/// Words that mark a credential in a key, matched case-insensitively
/// anywhere in the key (`api[_-]?key` and `private[_-]?key` spelled out).
const SENSITIVE_WORDS: [&str; 13] = [
    "token",
    "secret",
    "password",
    "passwd",
    "apikey",
    "api_key",
    "api-key",
    "auth",
    "credential",
    "privatekey",
    "private_key",
    "private-key",
    "bearer",
];

/// SkillDock's `SENSITIVE_ARGUMENT_NAMES` (`mcp_manager.rs`): an argument
/// whose name, lower-cased with `_` as `-`, equals one of these or ends in
/// `-<name>`.
pub const SENSITIVE_ARGUMENT_NAMES: [&str; 8] = [
    "api-key",
    "apikey",
    "authorization",
    "password",
    "secret",
    "token",
    "access-token",
    "auth-token",
];

/// Query parameter names that carry credentials besides the sensitive
/// words.
const TOKEN_PARAMETERS: [&str; 3] = ["key", "sig", "signature"];

/// Shortest value [`Reason::HighEntropy`] applies to.
pub const ENTROPY_MIN_LEN: usize = 20;
/// Bits per character above which a value counts as random.
pub const ENTROPY_THRESHOLD: f64 = 3.5;

/// Whether a key (env var, header, argument or parameter name) marks a
/// credential.
pub fn is_sensitive_key(key: &str) -> bool {
    let lower = key.to_ascii_lowercase();
    SENSITIVE_WORDS.iter().any(|word| lower.contains(word)) || is_sensitive_argument_name(key)
}

/// SkillDock's argument rule: `--api-key`, `--github-token=x`, `AUTH_TOKEN`.
pub fn is_sensitive_argument_name(argument: &str) -> bool {
    let name = argument
        .trim()
        .trim_start_matches('-')
        .split('=')
        .next()
        .unwrap_or_default()
        .to_ascii_lowercase()
        .replace('_', "-");
    SENSITIVE_ARGUMENT_NAMES
        .iter()
        .any(|known| name == *known || name.ends_with(&format!("-{known}")))
}

/// Shannon entropy of `value` in bits per character.
pub fn entropy(value: &str) -> f64 {
    let chars: Vec<char> = value.chars().collect();
    if chars.is_empty() {
        return 0.0;
    }
    let mut counts = std::collections::BTreeMap::<char, usize>::new();
    for c in &chars {
        *counts.entry(*c).or_default() += 1;
    }
    let total = chars.len() as f64;
    counts
        .values()
        .map(|&count| {
            let p = count as f64 / total;
            -p * p.log2()
        })
        .sum()
}

/// More than [`ENTROPY_THRESHOLD`] bits per character over at least
/// [`ENTROPY_MIN_LEN`] characters.
pub fn is_high_entropy(value: &str) -> bool {
    value.chars().count() >= ENTROPY_MIN_LEN && entropy(value) > ENTROPY_THRESHOLD
}

/// A high-entropy single word: no whitespace, and not a URL or a path
/// (those look random too, and URLs have their own rules).
fn looks_random(value: &str) -> bool {
    let path_like = value.contains("://")
        || value.contains('\\')
        || value.starts_with(['/', '~', '.'])
        || value.as_bytes().get(1) == Some(&b':');
    !path_like && !value.chars().any(char::is_whitespace) && is_high_entropy(value)
}

/// Classifies one imported literal value (SPEC 11.7): `key` is the env var
/// or header name, or the `--flag` an argument belongs to. Any literal
/// under a sensitive key counts.
pub fn classify_import(key: Option<&str>, value: &str) -> Option<Reason> {
    classify(key, value, Strength::Import)
}

/// Classifies a value already in a store file (SPEC 11.8): a sensitive key
/// alone is not enough; the value must hold a token format or URL
/// credentials, or be high-entropy under a sensitive key.
pub fn classify_stored(key: Option<&str>, value: &str) -> Option<Reason> {
    classify(key, value, Strength::Stored)
}

/// Classifies an argument list, one result per argument: each argument on
/// its own, `--token=VALUE` as `VALUE` under `token`, and `--token VALUE`
/// as the next argument under `token`.
pub fn classify_args_import(args: &[String]) -> Vec<Option<Reason>> {
    classify_args(args, Strength::Import)
}

/// [`classify_args_import`] at the leak scanner's strength.
pub fn classify_args_stored(args: &[String]) -> Vec<Option<Reason>> {
    classify_args(args, Strength::Stored)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Strength {
    Import,
    Stored,
}

fn classify(key: Option<&str>, value: &str, strength: Strength) -> Option<Reason> {
    if value.trim().is_empty() {
        return None;
    }
    for part in literal_parts(value) {
        if let Some(hit) = find_tokens(&part).into_iter().next() {
            return Some(hit.reason);
        }
    }
    if let Some(hit) = find_url_credentials(value).into_iter().next() {
        return Some(hit.reason);
    }
    if holds_reference(value) {
        return None;
    }
    let sensitive = key.is_some_and(is_sensitive_key);
    match strength {
        Strength::Import if sensitive => Some(Reason::SensitiveKey),
        _ if sensitive && looks_random(value) => Some(Reason::HighEntropy),
        _ => None,
    }
}

fn classify_args(args: &[String], strength: Strength) -> Vec<Option<Reason>> {
    let mut results = Vec::with_capacity(args.len());
    let mut pending_key: Option<&str> = None;
    for arg in args {
        let key = pending_key.take();
        let flag = arg.starts_with('-');
        let result = match (flag, arg.split_once('=')) {
            (true, Some((name, value))) => classify(Some(name), value, strength),
            (true, None) => {
                if is_sensitive_key(arg) {
                    pending_key = Some(arg);
                }
                classify(None, arg, strength)
            }
            (false, _) => classify(key, arg, strength),
        };
        results.push(result);
    }
    results
}

// Token formats.

/// `(prefix, kind, shortest tail)`, longer prefixes first.
const PREFIXED: [(&str, TokenKind, usize); 14] = [
    ("github_pat_", TokenKind::GithubFineGrained, 20),
    ("ghp_", TokenKind::GithubClassic, 20),
    ("gho_", TokenKind::GithubOauth, 20),
    ("sk-ant-", TokenKind::Anthropic, 20),
    ("sk-", TokenKind::OpenAi, 20),
    ("glpat-", TokenKind::GitlabToken, 20),
    ("AIza", TokenKind::GoogleApiKey, 30),
    ("xoxa-", TokenKind::Slack, 10),
    ("xoxb-", TokenKind::Slack, 10),
    ("xoxp-", TokenKind::Slack, 10),
    ("xoxo-", TokenKind::Slack, 10),
    ("xoxs-", TokenKind::Slack, 10),
    ("xoxr-", TokenKind::Slack, 10),
    ("AKIA", TokenKind::AwsAccessKey, 16),
];

fn is_token_char(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}

fn is_base64url(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}

/// Every token-format hit in `text`.
pub fn find_tokens(text: &str) -> Vec<Hit> {
    let bytes = text.as_bytes();
    let mut hits = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        let boundary = i == 0 || !bytes[i - 1].is_ascii_alphanumeric();
        let found = if boundary {
            prefixed_at(bytes, i).or_else(|| jwt_at(bytes, i))
        } else {
            None
        };
        match found {
            Some((kind, end)) => {
                hits.push(Hit {
                    reason: Reason::TokenFormat(kind),
                    range: i..end,
                });
                i = end;
            }
            None => i += 1,
        }
    }
    hits.extend(private_keys(text));
    hits
}

fn prefixed_at(bytes: &[u8], start: usize) -> Option<(TokenKind, usize)> {
    let rest = &bytes[start..];
    for (prefix, kind, min_tail) in PREFIXED {
        if !rest.starts_with(prefix.as_bytes()) {
            continue;
        }
        let tail_start = start + prefix.len();
        if kind == TokenKind::AwsAccessKey {
            let tail = bytes.get(tail_start..tail_start + 16)?;
            let after = bytes.get(tail_start + 16).copied();
            let valid = tail
                .iter()
                .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit())
                && !after.is_some_and(|b| b.is_ascii_alphanumeric());
            return valid.then_some((kind, tail_start + 16));
        }
        let tail = bytes[tail_start..]
            .iter()
            .take_while(|b| is_token_char(**b))
            .count();
        // `sk-ant-` shorter than its minimum must not fall through to `sk-`.
        return (tail >= min_tail).then_some((kind, tail_start + tail));
    }
    None
}

/// `eyJ...` `.` `eyJ...` `.` signature, each segment at least 10
/// base64url characters.
fn jwt_at(bytes: &[u8], start: usize) -> Option<(TokenKind, usize)> {
    let segment = |from: usize| {
        bytes[from..]
            .iter()
            .take_while(|b| is_base64url(**b))
            .count()
    };
    let header = segment(start);
    if !bytes[start..].starts_with(b"eyJ") || header < 10 {
        return None;
    }
    let second = start + header + 1;
    if bytes.get(start + header) != Some(&b'.') || !bytes[second..].starts_with(b"eyJ") {
        return None;
    }
    let payload = segment(second);
    let third = second + payload + 1;
    if payload < 10 || bytes.get(second + payload) != Some(&b'.') {
        return None;
    }
    let signature = segment(third);
    (signature >= 10).then_some((TokenKind::Jwt, third + signature))
}

/// `-----BEGIN <...>PRIVATE KEY-----` through its `-----END` line (or the
/// end of the text).
fn private_keys(text: &str) -> Vec<Hit> {
    let mut hits = Vec::new();
    let mut from = 0;
    while let Some(offset) = text[from..].find("-----BEGIN ") {
        let start = from + offset;
        let header_end = text[start..]
            .find('\n')
            .map_or(text.len(), |end| start + end);
        let header = &text[start..header_end];
        if !(header.contains("PRIVATE KEY") && header[11..].contains("-----")) {
            from = start + 11;
            continue;
        }
        let end = text[header_end..]
            .find("-----END ")
            .map(|offset| header_end + offset)
            .map_or(text.len(), |end_start| {
                text[end_start..]
                    .find('\n')
                    .map_or(text.len(), |nl| end_start + nl)
            });
        hits.push(Hit {
            reason: Reason::TokenFormat(TokenKind::PrivateKey),
            range: start..end,
        });
        from = end.max(start + 11);
    }
    hits
}

// URLs.

/// Userinfo and credential query parameters of every `scheme://` URL in
/// `text`. A component that is only references (`${secret:X}`) is fine.
pub fn find_url_credentials(text: &str) -> Vec<Hit> {
    let bytes = text.as_bytes();
    let mut hits = Vec::new();
    let mut from = 0;
    while let Some(offset) = text[from..].find("://") {
        let sep = from + offset;
        let scheme_start = bytes[..sep]
            .iter()
            .rposition(|b| !(b.is_ascii_alphanumeric() || b"+.-".contains(b)))
            .map_or(0, |i| i + 1);
        from = sep + 3;
        if scheme_start == sep || !bytes[scheme_start].is_ascii_alphabetic() {
            continue;
        }
        let end = bytes[from..]
            .iter()
            .position(|b| b.is_ascii_whitespace() || b"\"'<>`".contains(b))
            .map_or(text.len(), |i| from + i);
        url_hits(text, from, end, &mut hits);
        from = end.max(from);
    }
    hits
}

fn url_hits(text: &str, start: usize, end: usize, hits: &mut Vec<Hit>) {
    let url = &text[start..end];
    let authority_len = url.find(['/', '?', '#']).unwrap_or(url.len());
    let authority = &url[..authority_len];
    if let Some(at) = authority.rfind('@') {
        let userinfo = &authority[..at];
        let (user, password) = match colon_outside_references(userinfo) {
            Some(colon) => (&userinfo[..colon], Some(&userinfo[colon + 1..])),
            None => (userinfo, None),
        };
        let flagged = match password {
            Some(password) => is_literal(password) || (password.is_empty() && is_literal(user)),
            None => is_literal(user),
        };
        if flagged {
            hits.push(Hit {
                reason: Reason::UrlUserinfo,
                range: start..start + at,
            });
        }
    }
    let Some(query_start) = url.find('?') else {
        return;
    };
    let query_end = url[query_start..]
        .find('#')
        .map_or(url.len(), |i| query_start + i);
    let mut offset = start + query_start + 1;
    for pair in url[query_start + 1..query_end].split('&') {
        if let Some((name, value)) = pair.split_once('=') {
            let named = is_sensitive_key(name)
                || TOKEN_PARAMETERS
                    .iter()
                    .any(|known| name.eq_ignore_ascii_case(known));
            if named && is_literal(value) {
                let value_start = offset + name.len() + 1;
                hits.push(Hit {
                    reason: Reason::UrlTokenParameter,
                    range: value_start..value_start + value.len(),
                });
            }
        }
        offset += pair.len() + 1;
    }
}

/// The first `:` not inside a `${...}` reference.
fn colon_outside_references(text: &str) -> Option<usize> {
    let bytes = text.as_bytes();
    let mut depth = 0usize;
    for (i, b) in bytes.iter().enumerate() {
        match b {
            b'{' if i > 0 && bytes[i - 1] == b'$' => depth += 1,
            b'}' if depth > 0 => depth -= 1,
            b':' if depth == 0 => return Some(i),
            _ => {}
        }
    }
    None
}

/// Non-empty and not made of references only.
fn is_literal(component: &str) -> bool {
    !component.is_empty() && literal_parts(component).iter().any(|part| !part.is_empty())
}

/// Every token-format and URL-credential hit in arbitrary text, sorted by
/// position: what the leak scanner looks for in any staged file.
pub fn find_in_text(text: &str) -> Vec<Hit> {
    let mut hits = find_tokens(text);
    hits.extend(find_url_credentials(text));
    hits.sort_by_key(|hit| (hit.range.start, hit.range.end));
    hits.dedup_by(|later, earlier| later.range.start < earlier.range.end);
    hits
}

/// Words a placeholder starts out from: one of them marks the value as a
/// stand-in for the user's own value.
const PLACEHOLDER_MARKERS: [&str; 5] = ["your", "placeholder", "changeme", "replaceme", "replace"];

/// Every other word a placeholder may hold (`YOUR_ACCESS_TOKEN_HERE`).
const PLACEHOLDER_VOCABULARY: [&str; 29] = [
    "my",
    "the",
    "a",
    "an",
    "own",
    "me",
    "here",
    "goes",
    "insert",
    "enter",
    "put",
    "this",
    "token",
    "key",
    "api",
    "apikey",
    "access",
    "secret",
    "password",
    "passwd",
    "pass",
    "auth",
    "bearer",
    "value",
    "id",
    "client",
    "personal",
    "pat",
    "credential",
];

/// Whether a flagged literal is an obvious placeholder a published snippet
/// ships for the user to fill in, not a secret (#45): anything in angle
/// brackets (`<YOUR_ACCESS_TOKEN_HERE>`), a value made only of placeholder
/// words with at least one of `your`, `placeholder`, `changeme` or
/// `replace` (`YOUR_TOKEN`, `your-api-key`, `REPLACE_ME`; not the
/// passphrase `mind-your-step-42`), a run of three or more `x` after a
/// separator or at the start (`xxxx`, `sk-xxxxxxxx`, `xxx...`), or filler
/// only (`...`, `****`).
pub fn is_placeholder(value: &str) -> bool {
    let value = value.trim();
    if value.len() > 2 && value.starts_with('<') && value.ends_with('>') {
        return true;
    }
    if value.chars().count() >= 3 && value.chars().all(|c| matches!(c, '.' | '*' | '…')) {
        return true;
    }
    let words: Vec<String> = value
        .split(|c: char| !c.is_ascii_alphanumeric())
        .filter(|word| !word.is_empty())
        .map(str::to_ascii_lowercase)
        .collect();
    let marked = words
        .iter()
        .any(|word| PLACEHOLDER_MARKERS.contains(&word.as_str()));
    let all_vocabulary = words.iter().all(|word| {
        PLACEHOLDER_MARKERS.contains(&word.as_str())
            || PLACEHOLDER_VOCABULARY.contains(&word.as_str())
    });
    if marked && all_vocabulary {
        return true;
    }
    let core = value.trim_end_matches(['.', '…', '*']);
    let xs = core
        .chars()
        .rev()
        .take_while(|c| matches!(c, 'x' | 'X'))
        .count();
    let before = core[..core.len() - xs].chars().next_back();
    xs >= 3 && before.is_none_or(|c| !c.is_ascii_alphanumeric())
}

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

    #[test]
    fn placeholders_are_recognized() {
        for placeholder in [
            "<YOUR_ACCESS_TOKEN_HERE>",
            "YOUR_ACCESS_TOKEN_HERE",
            "<token>",
            "YOUR_TOKEN",
            "your-api-key",
            "Your token here",
            "REPLACE_ME",
            "changeme",
            "xxxx",
            "xxx...",
            "sk-xxxxxxxx",
            "xxxx-xxxx-xxxx",
            "...",
            "****",
        ] {
            assert!(is_placeholder(placeholder), "{placeholder}");
        }
        for secret in [
            "not-a-real-credential-0001",
            "ghp_0123456789abcdefghijABCDEFGHIJ",
            "boxxx",
            "xx",
            "<>",
            "yourself-4f2a9c",
            "mind-your-step-42",
            "your-4f2a9c-token",
            "token",
            "",
        ] {
            assert!(!is_placeholder(secret), "{secret}");
        }
    }
}