keyhog-scanner 0.5.73

keyhog-scanner: high-performance SIMD-accelerated secret detection engine
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
//! #134 perf regression gate: a `CompiledScanner` compiles every pattern AT MOST
//! ONCE for its lifetime, so scanning many chunks (the "many files" workload)
//! rebuilds zero regexes after warm-up. This locks the #13 fix ("cache the
//! pattern compile"); a regression that reintroduced per-scan `Regex::new` would
//! make the process-wide `LazyRegex` compile-event counter climb across scans.
//!
//! Mechanism: detector patterns are seeded eagerly at `compile()` (their
//! `OnceLock` is pre-filled and never runs the init closure), and lazy
//! regexes (multiline / decode / generic-assignment / shared) compile at most
//! once on first touch. `testing::lazy_regex_compile_events()` counts only those
//! real first-use compilations. The gate primes a chunk (one scan, which may
//! compile that chunk's lazy paths once), snapshots the counter, then re-scans
//! and asserts the counter does not move. Run `--test-threads=1` so the global
//! counter's deltas are not perturbed by another test compiling concurrently.

mod support;

use keyhog_core::Chunk;
use keyhog_scanner::testing::lazy_regex_compile_events;
use keyhog_scanner::CompiledScanner;
use std::sync::LazyLock;
use support::contracts::{make_chunk, scanner};

const CHILD_ENV: &str = "KEYHOG_ZERO_PATTERN_RECOMPILE_CHILD";

fn run_isolated_counter_test() -> bool {
    if std::env::var_os(CHILD_ENV).is_some() {
        return false;
    }
    let test_name = std::thread::current()
        .name()
        .expect("test thread has a name")
        .to_owned();
    let output = std::process::Command::new(
        std::env::current_exe().expect("current scanner test executable is available"),
    )
    .env(CHILD_ENV, "1")
    .arg(&test_name)
    .arg("--exact")
    .arg("--test-threads=1")
    .output()
    .expect("isolated compile-event test process starts");
    assert!(
        output.status.success(),
        "isolated compile-event test `{test_name}` failed"
    );
    true
}

/// One shared scanner, warmed once. `warm()` forces first-touch compilation of
/// the lazy regex caches so that, combined with a per-chunk priming scan, the
/// measured re-scans operate entirely on already-compiled regexes.
fn primed() -> &'static CompiledScanner {
    static S: LazyLock<CompiledScanner> = LazyLock::new(|| {
        let scanner = scanner();
        scanner.warm();
        scanner
    });
    &S
}

fn chunk(text: &str) -> Chunk {
    make_chunk(text, "filesystem", "recompile.txt")
}

/// Scan `text` once to prime its lazy paths, then re-scan it `rounds` times and
/// assert the process-wide compile-event counter never advanced, i.e. the
/// re-scans recompiled nothing.
fn assert_rescan_recompiles_nothing(text: &str, rounds: usize) {
    if run_isolated_counter_test() {
        return;
    }
    let s = primed();
    let c = chunk(text);
    s.clear_fragment_cache();
    s.scan(&c)
        .expect("zero-pattern scanner transition should succeed"); // prime this chunk's lazy regex paths (compile-at-most-once)
    let before = lazy_regex_compile_events();
    for _ in 0..rounds {
        s.clear_fragment_cache();
        s.scan(&c)
            .expect("zero-pattern scanner transition should succeed");
    }
    let after = lazy_regex_compile_events();
    assert_eq!(
        after, before,
        "re-scanning recompiled {} regex(es) for {text:?}, pattern compile is not cached across scans",
        after - before
    );
}

// ── representative content classes: re-scanning recompiles nothing ───────────

#[test]
fn rescan_zero_aws_credentials() {
    assert_rescan_recompiles_nothing(
        "[default]\naws_access_key_id = AKIAZ7QH4XNB2WKLP3RV\naws_secret_access_key = wJalrXUtnFEMI7K8MDENGbPxRfiCYEXKEYAAAA\n",
        5,
    );
}

#[test]
fn rescan_zero_gcp_service_account_json() {
    assert_rescan_recompiles_nothing(
        "{\n  \"type\": \"service_account\",\n  \"project_id\": \"demo-proj-7788\",\n  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvAIB\\n-----END PRIVATE KEY-----\\n\"\n}",
        5,
    );
}

#[test]
fn rescan_zero_gitlab_tokens() {
    assert_rescan_recompiles_nothing(
        "GITLAB_TOKEN=glpat-Ab3Cd6Ef9Gh2Ij5Kl8Mn\nKAS_TOKEN=glagent-Hx7Kp2Qm9Rn4Sb6Tw8Vz1Yc3\n",
        5,
    );
}

#[test]
fn rescan_zero_jwt() {
    assert_rescan_recompiles_nothing(
        "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0In0.abcDEF0123456789xyz",
        5,
    );
}

#[test]
fn rescan_zero_pem_private_key() {
    assert_rescan_recompiles_nothing(
        "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJK\n-----END RSA PRIVATE KEY-----\n",
        5,
    );
}

#[test]
fn rescan_zero_url_credentials() {
    assert_rescan_recompiles_nothing(
        "DATABASE_URL=postgres://app:Pg5ecretPass99Xy@db.example.com:5432/mydb\n",
        5,
    );
}

#[test]
fn rescan_zero_base64_wrapped_secret() {
    // Exercises the decode-through path (a base64 blob the scanner may decode).
    assert_rescan_recompiles_nothing(
        "token: QUtJQVo3UUg0WE5CMldLTFAzUlY= secret: d0phbHJYVXRuRkVNSTdLOE1ERU5HYlB4UmZpQ1k=",
        5,
    );
}

#[test]
fn rescan_zero_multiline_concat() {
    // Exercises the multiline string-concatenation preprocessor path.
    assert_rescan_recompiles_nothing(
        "const KEY = \"AKIA\" +\n  \"Z7QH4XNB2W\" +\n  \"KLP3RV\";\nSECRET = 'abc' . 'def' . 'ghijklmnop';",
        5,
    );
}

#[test]
fn rescan_zero_unicode_homoglyph() {
    // Exercises the unicode-normalization / homoglyph fold path.
    assert_rescan_recompiles_nothing(
        "AKIA\u{200b}password\u{0301} = \"S3cr3tValue1234567890\"\n",
        5,
    );
}

#[test]
fn rescan_zero_binary_like_bytes() {
    assert_rescan_recompiles_nothing(
        "\x00\x01\x02ELF\x7f binary\x00 AKIAZ7QH4XNB2WKLP3RV \x00\x1b embedded",
        5,
    );
}

#[test]
fn rescan_zero_empty_chunk() {
    assert_rescan_recompiles_nothing("", 5);
}

#[test]
fn rescan_zero_large_repeated_chunk() {
    let text = "password=Sup3rSecretValue12345 ".repeat(4096);
    assert_rescan_recompiles_nothing(&text, 3);
}

#[test]
fn rescan_zero_high_entropy_random() {
    assert_rescan_recompiles_nothing("api_key = 7Hx9Kp2Qm4Rn8Sb6Tw1Vz3Yc5Ad0Be7Cf2Dg9Eh4Fi6Gj", 5);
}

#[test]
fn rescan_zero_kitchen_sink() {
    let text = concat!(
        "AKIAZ7QH4XNB2WKLP3RV glpat-Ab3Cd6Ef9Gh2Ij5Kl8Mn ",
        "postgres://u:Pg5ecretPass99Xy@h/db GOCSPX-Me6Qq1St5Uv2Wy8Ab4Zd ",
        "-----BEGIN PRIVATE KEY-----\\nMIIE\\n-----END PRIVATE KEY-----\\n ",
        "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig AZURE_CLIENT_SECRET=Xy8Q~kPv3mNz.aB7dEfGhIjKlMnOpQ"
    );
    assert_rescan_recompiles_nothing(text, 5);
}

// ── structural invariants ─────────────────────────────────────────────────────

#[test]
fn warm_is_idempotent_compiles_nothing() {
    if run_isolated_counter_test() {
        return;
    }
    let s = primed(); // already warmed once in the OnceLock initializer
    let before = lazy_regex_compile_events();
    s.warm();
    s.warm();
    let after = lazy_regex_compile_events();
    assert_eq!(
        after,
        before,
        "a redundant warm() recompiled {} regex(es)",
        after - before
    );
}

#[test]
fn fifty_rescans_compile_nothing() {
    if run_isolated_counter_test() {
        return;
    }
    let s = primed();
    let c = chunk(
        "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI7K8MDENGbPxRfiCYEXKEYAAAA password=Hunter2Value99",
    );
    s.clear_fragment_cache();
    s.scan(&c)
        .expect("zero-pattern scanner transition should succeed");
    let before = lazy_regex_compile_events();
    for _ in 0..50 {
        s.clear_fragment_cache();
        s.scan(&c)
            .expect("zero-pattern scanner transition should succeed");
    }
    assert_eq!(
        lazy_regex_compile_events(),
        before,
        "50 re-scans recompiled a regex"
    );
}

#[test]
fn cold_first_scan_then_warm_rescans_compile_nothing() {
    if run_isolated_counter_test() {
        return;
    }
    // The "cold vs warm" measurement: the first scan of a chunk may compile its
    // lazy paths once; every subsequent (warm) scan must recompile nothing.
    let s = primed();
    let c = chunk(
        "token: glsoat-Kc4Np8Qr3St9Uw6Xz2Yb5Bd7E client_secret=Xy8Q~kPv3mNz.aB7dEfGhIjKlMnOp",
    );
    s.clear_fragment_cache();
    s.scan(&c)
        .expect("zero-pattern scanner transition should succeed"); // cold for this chunk's lazy paths
    let warm0 = lazy_regex_compile_events();
    s.clear_fragment_cache();
    s.scan(&c)
        .expect("zero-pattern scanner transition should succeed"); // warm
    let warm1 = lazy_regex_compile_events();
    assert_eq!(
        warm1,
        warm0,
        "the warm re-scan recompiled {} regex(es)",
        warm1 - warm0
    );
}

#[test]
fn distinct_primed_files_in_sequence_compile_nothing() {
    if run_isolated_counter_test() {
        return;
    }
    let s = primed();
    let texts = [
        "AKIAZ7QH4XNB2WKLP3RV",
        "glpat-Ab3Cd6Ef9Gh2Ij5Kl8Mn",
        "postgres://u:Pg5ecretPass99Xy@h/db",
        "-----BEGIN PRIVATE KEY-----\\nMIIE\\n-----END PRIVATE KEY-----\\n",
        "AZURE_CLIENT_SECRET=Xy8Q~kPv3mNz.aB7dEfGhIjKlMnOpQ",
    ];
    // Prime each distinct file once.
    for t in texts {
        let c = chunk(t);
        s.clear_fragment_cache();
        s.scan(&c)
            .expect("zero-pattern scanner transition should succeed");
    }
    let before = lazy_regex_compile_events();
    // Now scan the whole sequence again: a real multi-file scan, zero recompiles.
    for t in texts {
        let c = chunk(t);
        s.clear_fragment_cache();
        s.scan(&c)
            .expect("zero-pattern scanner transition should succeed");
    }
    assert_eq!(
        lazy_regex_compile_events(),
        before,
        "scanning a primed file sequence recompiled a regex"
    );
}

#[test]
fn clearing_fragment_cache_does_not_recompile_patterns() {
    if run_isolated_counter_test() {
        return;
    }
    // Clearing the per-chunk fragment memo must not touch the regex OnceLocks.
    let s = primed();
    let c = chunk("password=Sup3rSecretValue12345 AKIAZ7QH4XNB2WKLP3RV");
    s.clear_fragment_cache();
    s.scan(&c)
        .expect("zero-pattern scanner transition should succeed");
    let before = lazy_regex_compile_events();
    for _ in 0..10 {
        s.clear_fragment_cache();
    }
    s.clear_fragment_cache();
    s.scan(&c)
        .expect("zero-pattern scanner transition should succeed");
    assert_eq!(
        lazy_regex_compile_events(),
        before,
        "clearing the fragment cache recompiled a regex"
    );
}

#[test]
fn compile_event_counter_is_monotonic_non_decreasing() {
    if run_isolated_counter_test() {
        return;
    }
    // The observable only ever ticks forward on a real compile; it never resets.
    let a = lazy_regex_compile_events();
    let s = primed();
    s.clear_fragment_cache();
    s.scan(&chunk("just some text without obvious secrets"))
        .expect("zero-pattern scanner transition should succeed");
    let b = lazy_regex_compile_events();
    assert!(b >= a, "compile-event counter went backwards: {a} -> {b}");
}

#[test]
fn second_scanner_from_same_corpus_rescans_compile_nothing() {
    if run_isolated_counter_test() {
        return;
    }
    // The compile-once guarantee is per-scanner: a freshly built, warmed scanner
    // also reaches steady state with zero recompiles on re-scan.
    let fresh = scanner();
    fresh.warm();
    let c = chunk("AKIAZ7QH4XNB2WKLP3RV glpat-Ab3Cd6Ef9Gh2Ij5Kl8Mn password=Hunter2Value99");
    fresh.clear_fragment_cache();
    fresh
        .scan(&c)
        .expect("zero-pattern scanner transition should succeed");
    let before = lazy_regex_compile_events();
    for _ in 0..5 {
        fresh.clear_fragment_cache();
        fresh
            .scan(&c)
            .expect("zero-pattern scanner transition should succeed");
    }
    assert_eq!(
        lazy_regex_compile_events(),
        before,
        "a second scanner recompiled a regex on re-scan"
    );
}

#[test]
fn interleaved_diverse_chunks_after_priming_compile_nothing() {
    if run_isolated_counter_test() {
        return;
    }
    let s = primed();
    let primers = [
        "AKIAZ7QH4XNB2WKLP3RV",
        "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig",
        "QUtJQVo3UUg0WE5CMldLTFAzUlY=",
    ];
    for t in primers {
        s.clear_fragment_cache();
        s.scan(&chunk(t))
            .expect("zero-pattern scanner transition should succeed");
    }
    let before = lazy_regex_compile_events();
    for _ in 0..3 {
        for t in primers {
            s.clear_fragment_cache();
            s.scan(&chunk(t))
                .expect("zero-pattern scanner transition should succeed");
        }
    }
    assert_eq!(
        lazy_regex_compile_events(),
        before,
        "interleaved diverse re-scans recompiled a regex"
    );
}