keyhog 0.5.73

GPU-accelerated secret scanner for code, Git history, cloud, containers, browser assets, and live credential verification
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
//! Regression: `--threads` is a *performance* knob, never a *result* knob.
//!
//! Concurrency must not change what keyhog finds. Scanning the SAME
//! multi-file corpus with `--threads 1` and `--threads 4` (and every
//! other worker count) must yield the byte-identical, order-independent
//! finding set and the same exit code, otherwise the scan is racy and a
//! CI gate would flap on core count. These tests drive the REAL shipped
//! `keyhog` binary (`env!("CARGO_BIN_EXE_keyhog")`) over a `TempDir`
//! corpus planted OUTSIDE the workspace (so repo `.gitignore`/test-path
//! suppression rules can't interfere), on the explicit `cpu` backend so
//! the assertions are HOST-INDEPENDENT (no accelerator assumed; the CPU
//! path is forced regardless of any GPU/env state).
//!
//! Detector ids asserted here (`github`-family, `aws-bedrock-api-key`,
//! canonical `aws-access-key` id is the same concrete id the shipped
//! `e2e_binary.rs` contract already pins for these literal fixtures.

use std::path::Path;
use std::path::PathBuf;
use std::process::Command;

use tempfile::TempDir;

/// Forced CPU backend keeps every assertion host-independent: the result
/// set cannot silently degrade onto an accelerator path.
const CPU_BACKEND: &str = "cpu";

fn binary() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_keyhog"))
}

/// Plant a fixed 4-file corpus: three files each carry a distinct,
/// well-known detectable credential (split-literal so THIS test file is
/// not itself a planted-secret tripwire when keyhog scans its own repo),
/// one file is clean. At least three findings are expected.
fn build_corpus(dir: &Path) {
    // AWS long-term access key id -> `aws-access-key` (or the
    // simdsieve accelerated path); same literal as e2e_binary.rs.
    std::fs::write(
        dir.join("a_aws.env"),
        concat!("AWS_ACCESS_KEY_ID = \"AKIA", "QYLPMN5HFIQR7XYA\"\n"),
    )
    .expect("write a_aws.env");

    // GitHub PAT -> a github-family detector; valid-checksum literal
    // reused from e2e_binary.rs so the checksum wiring accepts it.
    std::fs::write(
        dir.join("b_github.env"),
        concat!(
            "GH_TOKEN = \"ghp_",
            "aBcD1234EFgh5678ijkl9012MNop343hK7n2\"\n"
        ),
    )
    .expect("write b_github.env");

    // AWS Bedrock long-term API key -> `aws-bedrock-api-key`.
    std::fs::write(
        dir.join("c_bedrock.env"),
        concat!(
            "AWS_BEARER_TOKEN_BEDROCK=\"ABSKQmVkcm9ja0FQSUtleS",
            "y2J0fajDUXD1efoRCtqKODGGBi8UWr7UJsq2tkhFhx8ZEDEd9hnKHivse0YHShMdeCAbPEOXOxyhkg5cqNGHA1grwAyKC3Y8HDD62wLdl37iKN\"\n",
        ),
    )
    .expect("write c_bedrock.env");

    // Clean file: no credential, contributes zero findings.
    std::fs::write(dir.join("d_clean.txt"), "fn main() { println!(\"hi\"); }\n")
        .expect("write d_clean.txt");
}

/// Run `keyhog scan --backend cpu --format json --threads <n> <dir>` and
/// return (stdout, stderr, exit-code). `threads` is passed verbatim so
/// invalid values can be exercised too.
fn run_scan(dir: &Path, threads: &str) -> (String, String, Option<i32>) {
    let output = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .args(["--backend", CPU_BACKEND])
        .args(["--threads", threads])
        .args(["--format", "json"])
        .arg(dir)
        .env_remove("KEYHOG_BACKEND")
        .output()
        .expect("spawn keyhog scan");
    (
        String::from_utf8_lossy(&output.stdout).into_owned(),
        String::from_utf8_lossy(&output.stderr).into_owned(),
        output.status.code(),
    )
}

/// Parse the JSON finding array from a successful scan's stdout.
fn parse_findings(stdout: &str) -> Vec<serde_json::Value> {
    let value: serde_json::Value = serde_json::from_str(stdout)
        .unwrap_or_else(|error| panic!("stdout is not valid JSON: {error}\n{stdout}"));
    value
        .as_array()
        .unwrap_or_else(|| panic!("findings JSON must be an array, got {value}"))
        .clone()
}

/// Stable, order-independent key for one finding: identity is
/// (detector_id, file_path, line, offset, credential_hash). The same
/// corpus dir is scanned by every run, so `file_path` is identical
/// across runs.
fn finding_key(f: &serde_json::Value) -> String {
    let det = f
        .get("detector_id")
        .and_then(|v| v.as_str())
        .unwrap_or("<no-detector>");
    let loc = f.get("location");
    let file = loc
        .and_then(|l| l.get("file_path"))
        .and_then(|v| v.as_str())
        .unwrap_or("<no-path>");
    let line = loc
        .and_then(|l| l.get("line"))
        .and_then(|v| v.as_u64())
        .unwrap_or(u64::MAX);
    let offset = loc
        .and_then(|l| l.get("offset"))
        .and_then(|v| v.as_u64())
        .unwrap_or(u64::MAX);
    let hash = f
        .get("credential_hash")
        .and_then(|v| v.as_str())
        .unwrap_or("<no-hash>");
    format!("{det}|{file}|{line}|{offset}|{hash}")
}

/// Sorted vector of finding keys (order-independent finding SET).
fn sorted_keys(findings: &[serde_json::Value]) -> Vec<String> {
    let mut keys: Vec<String> = findings.iter().map(finding_key).collect();
    keys.sort();
    keys
}

/// Sorted vector of detector ids WITH duplicates (order-independent
/// detector MULTISET).
fn sorted_detector_ids(findings: &[serde_json::Value]) -> Vec<String> {
    let mut ids: Vec<String> = findings
        .iter()
        .map(|f| {
            f.get("detector_id")
                .and_then(|v| v.as_str())
                .unwrap_or("<no-detector>")
                .to_owned()
        })
        .collect();
    ids.sort();
    ids
}

/// Sorted, de-duplicated credential hashes present in a finding set.
fn sorted_unique_hashes(findings: &[serde_json::Value]) -> Vec<String> {
    let mut hashes: Vec<String> = findings
        .iter()
        .filter_map(|f| f.get("credential_hash").and_then(|v| v.as_str()))
        .map(str::to_owned)
        .collect();
    hashes.sort();
    hashes.dedup();
    hashes
}

/// True if the finding set contains a github-family detection
/// (`detector_id` or `service` mentions github).
fn has_github(findings: &[serde_json::Value]) -> bool {
    findings.iter().any(|f| {
        let det = f.get("detector_id").and_then(|v| v.as_str()).unwrap_or("");
        let svc = f.get("service").and_then(|v| v.as_str()).unwrap_or("");
        det.contains("github") || svc.contains("github")
    })
}

fn has_detector(findings: &[serde_json::Value], wanted: &[&str]) -> bool {
    findings.iter().any(|f| {
        f.get("detector_id")
            .and_then(|v| v.as_str())
            .is_some_and(|d| wanted.contains(&d))
    })
}

// ---------------------------------------------------------------------------
// Core determinism: --threads 1 vs --threads 4 on the same corpus.
// ---------------------------------------------------------------------------

#[test]
fn threads_1_matches_threads_4_finding_keys() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let (out1, _err1, code1) = run_scan(dir.path(), "1");
    let (out4, _err4, code4) = run_scan(dir.path(), "4");

    assert_eq!(code1, Some(1), "single-thread scan must exit 1 (findings)");
    assert_eq!(code4, Some(1), "four-thread scan must exit 1 (findings)");

    let keys1 = sorted_keys(&parse_findings(&out1));
    let keys4 = sorted_keys(&parse_findings(&out4));

    // The load-bearing determinism contract: concurrency does not change
    // the finding SET. Byte-identical after sorting.
    assert_eq!(
        keys1, keys4,
        "--threads 1 and --threads 4 must produce the identical finding set;\n1={keys1:#?}\n4={keys4:#?}"
    );
}

#[test]
fn threads_1_matches_threads_4_exit_code_is_1() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let (_o1, _e1, code1) = run_scan(dir.path(), "1");
    let (_o4, _e4, code4) = run_scan(dir.path(), "4");

    assert_eq!(code1, Some(1), "1 thread: exit code");
    assert_eq!(code4, Some(1), "4 threads: exit code");
    assert_eq!(code1, code4, "exit code must not depend on thread count");
}

#[test]
fn threads_1_matches_threads_4_finding_count() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let n1 = parse_findings(&run_scan(dir.path(), "1").0).len();
    let n4 = parse_findings(&run_scan(dir.path(), "4").0).len();

    assert_eq!(n1, n4, "finding COUNT must not depend on thread count");
    // Three distinct planted credentials -> at least three findings; a
    // zero/one count would mean the corpus silently stopped detecting.
    assert!(
        n1 >= 3,
        "expected >=3 findings from a 3-credential corpus, got {n1}"
    );
}

#[test]
fn threads_2_matches_baseline_threads_1() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let base = sorted_keys(&parse_findings(&run_scan(dir.path(), "1").0));
    let two = sorted_keys(&parse_findings(&run_scan(dir.path(), "2").0));

    assert_eq!(
        base, two,
        "--threads 2 must match the single-thread baseline set"
    );
}

#[test]
fn threads_8_matches_baseline_threads_1() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let base = sorted_keys(&parse_findings(&run_scan(dir.path(), "1").0));
    let eight = sorted_keys(&parse_findings(&run_scan(dir.path(), "8").0));

    assert_eq!(
        base, eight,
        "--threads 8 (over-subscribed vs corpus size) must match the single-thread baseline set"
    );
}

#[test]
fn credential_hash_set_identical_across_thread_counts() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let base = sorted_unique_hashes(&parse_findings(&run_scan(dir.path(), "1").0));
    // A single-thread scan of a 3-credential corpus yields 3 distinct
    // credential hashes at minimum.
    assert!(
        base.len() >= 3,
        "expected >=3 distinct credential hashes, got {}: {base:?}",
        base.len()
    );

    for threads in ["3", "4", "7"] {
        let other = sorted_unique_hashes(&parse_findings(&run_scan(dir.path(), threads).0));
        assert_eq!(
            base, other,
            "credential-hash set must be identical at --threads {threads}"
        );
    }
}

#[test]
fn detector_id_multiset_identical_across_thread_counts() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let base = sorted_detector_ids(&parse_findings(&run_scan(dir.path(), "1").0));
    let multi = sorted_detector_ids(&parse_findings(&run_scan(dir.path(), "4").0));

    // Multiset (duplicates preserved), catches a thread count that drops
    // or duplicates a detection without changing the unique-hash set.
    assert_eq!(
        base, multi,
        "detector-id multiset must be identical across thread counts"
    );
}

// ---------------------------------------------------------------------------
// The corpus really fires its expected detectors under BOTH thread counts,
// so the equality tests above are not passing vacuously over empty sets.
// ---------------------------------------------------------------------------

#[test]
fn corpus_fires_github_detector_under_both_thread_counts() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let f1 = parse_findings(&run_scan(dir.path(), "1").0);
    let f4 = parse_findings(&run_scan(dir.path(), "4").0);

    assert!(
        has_github(&f1),
        "single-thread scan must fire a github detector; got {f1:#?}"
    );
    assert!(
        has_github(&f4),
        "four-thread scan must fire a github detector; got {f4:#?}"
    );
}

#[test]
fn corpus_fires_aws_bedrock_detector_under_both_thread_counts() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let f1 = parse_findings(&run_scan(dir.path(), "1").0);
    let f4 = parse_findings(&run_scan(dir.path(), "4").0);

    assert!(
        has_detector(&f1, &["aws-bedrock-api-key"]),
        "single-thread scan must fire aws-bedrock-api-key; got {f1:#?}"
    );
    assert!(
        has_detector(&f4, &["aws-bedrock-api-key"]),
        "four-thread scan must fire aws-bedrock-api-key; got {f4:#?}"
    );
}

#[test]
fn corpus_fires_aws_access_key_detector_under_both_thread_counts() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let f1 = parse_findings(&run_scan(dir.path(), "1").0);
    let f4 = parse_findings(&run_scan(dir.path(), "4").0);

    // AKIA is caught by the named detector or the simdsieve fast path;
    // either is a correct AWS-access-key detection (see e2e_binary.rs).
    assert!(
        has_detector(&f1, &["aws-access-key"]),
        "single-thread scan must fire an AWS access-key detector; got {f1:#?}"
    );
    assert!(
        has_detector(&f4, &["aws-access-key"]),
        "four-thread scan must fire an AWS access-key detector; got {f4:#?}"
    );
}

// ---------------------------------------------------------------------------
// Stability: repeating the SAME thread count is idempotent.
// ---------------------------------------------------------------------------

#[test]
fn repeated_threads_4_runs_are_stable() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let a = sorted_keys(&parse_findings(&run_scan(dir.path(), "4").0));
    let b = sorted_keys(&parse_findings(&run_scan(dir.path(), "4").0));

    assert_eq!(
        a, b,
        "two --threads 4 runs of the same corpus must be identical (no run-to-run nondeterminism)"
    );
}

// ---------------------------------------------------------------------------
// Invalid --threads values are USER errors -> exit 2, with a message that
// is itself the fix (per value_parsers.rs).
// ---------------------------------------------------------------------------

#[test]
fn threads_zero_is_user_error_exit_2() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let (_out, err, code) = run_scan(dir.path(), "0");

    assert_eq!(
        code,
        Some(2),
        "--threads 0 is a usage error -> exit 2, never 1/3; stderr={err}"
    );
    assert!(
        err.contains("--threads must be >= 1"),
        "stderr must state the lower-bound fix; got {err}"
    );
}

#[test]
fn threads_non_integer_is_user_error_exit_2() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    let (_out, err, code) = run_scan(dir.path(), "abc");

    assert_eq!(
        code,
        Some(2),
        "--threads abc is a usage error -> exit 2; stderr={err}"
    );
    assert!(
        err.contains("not a valid integer"),
        "stderr must name the parse failure; got {err}"
    );
}

#[test]
fn threads_negative_is_user_error_exit_2() {
    let dir = TempDir::new().expect("tempdir");
    build_corpus(dir.path());

    // `--threads=-3`: the `=` form binds "-3" as the value (a bare
    // `--threads -3` would let clap reject "-3" as an unexpected flag
    // before the value parser runs). usize parse fails -> unparseable.
    let output = Command::new(binary())
        .arg("scan")
        .arg("--daemon=off")
        .args(["--backend", CPU_BACKEND])
        .arg("--threads=-3")
        .args(["--format", "json"])
        .arg(dir.path())
        .env_remove("KEYHOG_BACKEND")
        .output()
        .expect("spawn keyhog scan");
    let err = String::from_utf8_lossy(&output.stderr).into_owned();
    let code = output.status.code();

    assert_eq!(
        code,
        Some(2),
        "--threads=-3 is a usage error -> exit 2; stderr={err}"
    );
    assert!(
        err.contains("not a valid integer"),
        "stderr must name the parse failure for a negative count; got {err}"
    );
}