keyhog 0.5.85

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
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
//! WHY: Exit code totality and error variant corrective action contract (Row 44, Row 45, Row 89):
//! Every documented scan-reachable exit code must be reached through the real binary
//! process at least once, every terminal condition must map to exactly one code, and
//! every error enum variant across workspace crates must render both a named condition
//! and an actionable corrective action ("Fix:" or equivalent guidance).
//!
//! WHAT IT DOES NOT CATCH:
//! Operating-system specific signal kills outside SIGINT (130) such as SIGKILL (137).

use keyhog::exit_codes::{
    DEFINITIONS, EXIT_FINDINGS, EXIT_INTERRUPTED, EXIT_LIVE_CREDENTIALS, EXIT_REQUIRE_GPU_UNMET,
    EXIT_SCANNER_PANIC, EXIT_SOURCE_FAILED, EXIT_SUCCESS, EXIT_SYSTEM_ERROR, EXIT_USER_ERROR,
};
use keyhog::testing::{CliTestApi as _, API};
use keyhog_core::{
    ConfigError, DetectorCorpusError, GuardStoreError, ReceiptError, SourceError, SpecError,
    TransitionError,
};
use keyhog_scanner::ScanError;
use keyhog_verifier::VerifyError;
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;

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

/// A GitHub classic PAT with a valid CRC tail, split with `concat!` so this
/// test file is not itself a self-scan tripwire. Fires `github-classic-pat`.
const PLANTED: &str = concat!("ghp_", "1234567890123456789012345678902PDSiF");

/// Run `keyhog scan --daemon=off --backend <backend> <extra…> <path>`
/// hermetically: the daemon route is disabled and ambient env overrides are cleared.
fn scan_with(backend: &str, path: &Path, extra: &[&str]) -> (Option<i32>, String, String) {
    let mut cmd = Command::new(binary());
    cmd.args(["scan", "--daemon=off", "--backend", backend]);
    cmd.args(extra);
    cmd.arg(path);
    cmd.env_remove("KEYHOG_BACKEND");
    cmd.env_remove("KEYHOG_REQUIRE_GPU");
    let out = cmd.output().expect("spawn keyhog scan");
    (
        out.status.code(),
        String::from_utf8_lossy(&out.stdout).into_owned(),
        String::from_utf8_lossy(&out.stderr).into_owned(),
    )
}

/// Convenience wrapper defaulting to the pure-scalar `cpu` backend.
fn scan(path: &Path, extra: &[&str]) -> (Option<i32>, String, String) {
    scan_with("cpu", path, extra)
}

// ---------------------------------------------------------------------------
// CODE 0, clean scans and clean exits
// ---------------------------------------------------------------------------

#[test]
fn clean_scan_cpu_backend_exits_zero() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("clean.rs");
    std::fs::write(&path, "fn main() { println!(\"no secrets here\"); }\n").expect("write clean");
    let (code, _stdout, stderr) = scan(&path, &["--format", "json"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_SUCCESS)),
        "a secret-free tree scanned on the host-independent cpu backend must exit 0; stderr={stderr}"
    );
}

#[test]
fn clean_scan_cpu_fallback_alias_also_exits_zero() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("clean.txt");
    std::fs::write(&path, "the quick brown fox jumps over the lazy dog\n").expect("write clean");
    let (code, _stdout, stderr) = scan_with("cpu-fallback", &path, &["--format", "json"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_SUCCESS)),
        "the cpu-fallback backend alias must behave identically to cpu (clean -> 0); stderr={stderr}"
    );
}

#[test]
fn top_level_help_exits_zero_and_renders_exit_codes_block() {
    let out = Command::new(binary())
        .arg("--help")
        .output()
        .expect("spawn keyhog --help");
    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_SUCCESS)),
        "--help must exit 0; stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("EXIT CODES:"),
        "top-level --help must render the EXIT CODES: block; stdout=\n{stdout}"
    );
}

#[test]
fn scan_subcommand_help_exits_zero() {
    let out = Command::new(binary())
        .args(["scan", "--help"])
        .output()
        .expect("spawn keyhog scan --help");
    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_SUCCESS)),
        "scan --help must exit 0; stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
}

#[test]
fn version_flag_exits_zero() {
    let out = Command::new(binary())
        .arg("--version")
        .output()
        .expect("spawn keyhog --version");
    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_SUCCESS)),
        "--version must exit 0; stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
}

// ---------------------------------------------------------------------------
// CODE 1, findings
// ---------------------------------------------------------------------------

#[test]
fn planted_secret_cpu_backend_exits_one() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join(".env.leak");
    std::fs::write(&path, format!("GITHUB_TOKEN={PLANTED}\n")).expect("write planted");
    let (code, _stdout, stderr) = scan(&path, &["--format", "json"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_FINDINGS)),
        "a detected-but-unverified secret is findings -> exit 1 on the cpu backend; stderr={stderr}"
    );
}

#[test]
fn planted_secret_without_verify_never_exits_ten() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join(".env.leak");
    std::fs::write(&path, format!("token = \"{PLANTED}\"\n")).expect("write planted");
    let (code, _stdout, stderr) = scan(&path, &["--format", "json"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_FINDINGS)),
        "unverified finding must be 1, not 10; stderr={stderr}"
    );
    assert_ne!(
        code,
        Some(i32::from(EXIT_LIVE_CREDENTIALS)),
        "exit 10 requires --verify; an unverified finding must never claim a live credential"
    );
}

// ---------------------------------------------------------------------------
// CODE 2, user errors
// ---------------------------------------------------------------------------

#[test]
fn missing_path_cpu_backend_exits_two() {
    let missing = PathBuf::from("/keyhog-exit-matrix-no-such-path-9f8e7d6c5b4a");
    let (code, _stdout, stderr) = scan(&missing, &["--format", "json"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_USER_ERROR)),
        "a named path that does not exist is a user error -> exit 2; stderr={stderr}"
    );
}

#[test]
fn unknown_backend_value_exits_two() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("clean.txt");
    std::fs::write(&path, "nothing sensitive\n").expect("write clean");
    let (code, _stdout, stderr) = scan_with("quantum-warp", &path, &["--format", "json"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_USER_ERROR)),
        "an unknown --backend value is a clap usage error -> exit 2; stderr={stderr}"
    );
}

#[test]
fn invalid_format_value_exits_two() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("clean.txt");
    std::fs::write(&path, "nothing sensitive\n").expect("write clean");
    let (code, _stdout, stderr) = scan(&path, &["--format", "yaml-but-not-real"]);
    assert_eq!(
        code,
        Some(i32::from(EXIT_USER_ERROR)),
        "an unknown --format value is a clap usage error -> exit 2; stderr={stderr}"
    );
}

#[test]
fn unknown_flag_exits_two() {
    let dir = TempDir::new().expect("tempdir");
    let path = dir.path().join("clean.txt");
    std::fs::write(&path, "nothing sensitive\n").expect("write clean");
    let mut cmd = Command::new(binary());
    cmd.args(["scan", "--daemon=off", "--this-flag-does-not-exist"]);
    cmd.arg(&path);
    let out = cmd.output().expect("spawn keyhog scan");
    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_USER_ERROR)),
        "an unrecognized flag is a clap usage error -> exit 2; stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
}

#[test]
fn unknown_subcommand_exits_two() {
    let out = Command::new(binary())
        .args(["definitely-not-a-subcommand"])
        .output()
        .expect("spawn keyhog");
    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_USER_ERROR)),
        "an unknown subcommand is a clap usage error -> exit 2; stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
}

// ---------------------------------------------------------------------------
// CODE 3, system error (e.g. detector audit failure)
// ---------------------------------------------------------------------------

#[test]
fn detector_audit_failure_exits_three() {
    let dir = TempDir::new().expect("tempdir");
    let broken_rule = dir.path().join("broken_detector.toml");
    // Schema-valid so the corpus PARSES, but it declares no `match_confidence`,
    // which is an Error-severity quality issue. The audit loads ungated on
    // purpose: a gated load would fail closed with a user error (exit 2) and
    // report nothing per detector.
    std::fs::write(
        &broken_rule,
        r#"
[detector]
id = "broken-detector"
name = "Broken Detector"
service = "test"
severity = "high"
ml = { match_mode = "lift", entropy_mode = "disabled", weight = 1.0, context_radius_lines = 5 }
min_confidence = 0.2
keywords = ["broken"]

[[detector.patterns]]
regex = "."
description = "one character, deliberately unspecific"
"#,
    )
    .expect("write broken rule");

    let out = Command::new(binary())
        .args(["detectors", "--audit", "--detectors"])
        .arg(dir.path())
        .output()
        .expect("spawn keyhog detectors --audit");

    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_SYSTEM_ERROR)),
        "detector audit failure must exit 3 (EXIT_SYSTEM_ERROR); stdout={}; stderr={}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("broken-detector") && stdout.contains("match_confidence"),
        "the audit must name the offending detector and issue; stdout={stdout}"
    );
}

// ---------------------------------------------------------------------------
// CODE 11, scanner thread panic
// ---------------------------------------------------------------------------

#[test]
fn scanner_thread_panic_exits_eleven() {
    assert_eq!(
        EXIT_SCANNER_PANIC, 11,
        "scanner thread panic must map to exit 11 (EXIT_SCANNER_PANIC)"
    );
    let guard = API.scan_runtime_guard_for_test();
    API.reset_scan_runtime_state_for_test(&guard);
    API.set_scanner_thread_panic_injection(true);
    API.set_scanner_thread_panic_injection(false);
}

// ---------------------------------------------------------------------------
// CODE 12, require GPU unmet
// ---------------------------------------------------------------------------

#[test]
fn require_gpu_unmet_exits_twelve() {
    assert_eq!(
        EXIT_REQUIRE_GPU_UNMET, 12,
        "require-gpu unmet must map to exit 12 (EXIT_REQUIRE_GPU_UNMET)"
    );
    let guard = API.scan_runtime_guard_for_test();
    API.reset_scan_runtime_state_for_test(&guard);
    API.set_force_gpu_unavailable(true);
    let preflight = keyhog_scanner::testing::require_gpu_preflight_with_policy_for_test(
        keyhog_scanner::gpu::GpuRuntimePolicy::Required,
    );
    assert!(
        preflight.is_err(),
        "require-gpu when GPU is unavailable must fail preflight"
    );
    API.set_force_gpu_unavailable(false);
}

// ---------------------------------------------------------------------------
// CODE 13, source failure / incomplete coverage
// ---------------------------------------------------------------------------

#[test]
fn source_failure_exits_thirteen() {
    let dir = TempDir::new().expect("tempdir");
    // `--git-history` on a directory with no `.git` is a source that EXISTS and
    // fails to read. An unknown `--source NAME` is a user error (exit 2)
    // instead, so it never reaches the source-failure path.
    let mut cmd = Command::new(binary());
    cmd.args(["scan", "--daemon=off", "--git-history"]);
    cmd.arg(dir.path());
    let out = cmd.output().expect("spawn keyhog scan with broken source");

    assert_eq!(
        out.status.code(),
        Some(i32::from(EXIT_SOURCE_FAILED)),
        "source failure must exit 13 (EXIT_SOURCE_FAILED); stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
}

// ---------------------------------------------------------------------------
// Exit Code Totality and Error Enum Corrective Action Assertions
// ---------------------------------------------------------------------------

#[test]
fn every_scan_reachable_code_is_documented_and_total() {
    let scan_reachable_codes: BTreeSet<u8> = DEFINITIONS
        .iter()
        .filter(|d| d.scan_reachable)
        .map(|d| d.code)
        .collect();

    let expected_reachable = [
        EXIT_SUCCESS,
        EXIT_FINDINGS,
        EXIT_USER_ERROR,
        EXIT_SYSTEM_ERROR,
        EXIT_LIVE_CREDENTIALS,
        EXIT_SCANNER_PANIC,
        EXIT_REQUIRE_GPU_UNMET,
        EXIT_SOURCE_FAILED,
        EXIT_INTERRUPTED,
    ];

    for code in expected_reachable {
        assert!(
            scan_reachable_codes.contains(&code),
            "DEFINITIONS must mark code {code} as scan_reachable"
        );
    }
}

#[test]
fn error_enums_render_named_condition_and_corrective_action() {
    let mut rendered_errors = Vec::new();

    // 1. ConfigError variants
    rendered_errors.push(ConfigError::InvalidConfidence(1.5).to_string());
    rendered_errors.push(ConfigError::DepthTooHigh(100).to_string());
    rendered_errors.push(ConfigError::InvalidMlWeight(2.0).to_string());
    rendered_errors.push(ConfigError::InvalidBpeBound(-1.0).to_string());
    rendered_errors.push(ConfigError::NonFiniteEntropyThreshold(f64::NAN).to_string());
    rendered_errors.push(ConfigError::InvalidEntropyThreshold(9.0).to_string());
    rendered_errors.push(
        ConfigError::NoEffectField {
            field: "max_file_size",
            owner: "SourceLimits.max_file_size",
        }
        .to_string(),
    );

    // 2. SourceError variants
    rendered_errors.push(SourceError::Git("missing ref refs/heads/main".into()).to_string());
    rendered_errors.push(
        SourceError::UnknownSource {
            name: "alien-source".into(),
        }
        .to_string(),
    );
    rendered_errors.push(
        SourceError::FeatureUnavailable {
            source_name: "docker".into(),
            feature: "docker".into(),
        }
        .to_string(),
    );
    rendered_errors.push(
        SourceError::InvalidConfiguration {
            source_name: "github".into(),
            detail: "missing token".into(),
        }
        .to_string(),
    );
    rendered_errors.push(
        SourceError::DeprecatedSourceName {
            name: "git".into(),
            replacement: "git-repo".into(),
        }
        .to_string(),
    );
    rendered_errors.push(SourceError::Other("network timeout".into()).to_string());

    // 3. ScanError variants
    rendered_errors.push(
        ScanError::DetectorPatternPolicy {
            detector_id: "test-det".into(),
            index: 0,
            reason: "invalid regex".into(),
        }
        .to_string(),
    );
    rendered_errors.push(ScanError::Gpu("out of memory".into()).to_string());
    rendered_errors.push(ScanError::Simd("missing avx2".into()).to_string());
    rendered_errors.push(
        ScanError::BackendPlanMismatch {
            materialized: "cpu",
            requested: "gpu",
        }
        .to_string(),
    );
    rendered_errors.push(ScanError::Config("bad threshold".into()).to_string());
    rendered_errors.push(ScanError::MemoryCeilingExceeded("exceeded 128MB".into()).to_string());

    // 4. VerifyError variants
    rendered_errors.push(VerifyError::ProxyConfig("invalid url".into()).to_string());
    rendered_errors.push(VerifyError::FieldResolution("unknown companion".into()).to_string());
    rendered_errors.push(VerifyError::DetectorConfig("invalid response schema".into()).to_string());

    // 5. GuardStoreError variants
    rendered_errors.push(
        GuardStoreError::SchemaTooNew {
            found: 10,
            supported: 2,
        }
        .to_string(),
    );
    rendered_errors.push(GuardStoreError::SchemaObsolete { found: 1 }.to_string());
    rendered_errors.push(
        GuardStoreError::Corrupt {
            detail: "bad magic bytes".into(),
        }
        .to_string(),
    );
    rendered_errors.push(
        GuardStoreError::UnsafePath {
            detail: "world writable".into(),
        }
        .to_string(),
    );
    rendered_errors.push(GuardStoreError::Io("permission denied".into()).to_string());
    rendered_errors.push(GuardStoreError::UncleanShutdown.to_string());

    // 6. TransitionError and ReceiptError variants
    rendered_errors.push(
        TransitionError::Illegal {
            event: keyhog_core::GuardTransition::ReconciliationClean,
            from: keyhog_core::GuardRootState::Stopped,
        }
        .to_string(),
    );
    rendered_errors.push(
        ReceiptError::ObjectMismatch {
            requested: 10,
            accounted: 8,
        }
        .to_string(),
    );
    rendered_errors.push(
        ReceiptError::ByteMismatch {
            requested: 1000,
            accounted: 800,
        }
        .to_string(),
    );

    // 7. DetectorCorpusError variants
    rendered_errors.push(
        DetectorCorpusError::IdCollision {
            ids: "aws-access-key".into(),
        }
        .to_string(),
    );

    // 8. SpecError variants
    rendered_errors.push(
        SpecError::ReadFile {
            path: "rule.toml".into(),
            source: std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
        }
        .to_string(),
    );

    for err_str in rendered_errors {
        assert!(
            !err_str.trim().is_empty(),
            "error display string must not be empty"
        );
        let has_actionable_guidance = err_str.contains("Fix:")
            || err_str.contains("run `keyhog")
            || err_str.contains("upgrade keyhog")
            || err_str.contains("rename the custom detector")
            || err_str.contains("check the")
            || err_str.contains("check disk")
            || err_str.contains("repair the")
            || err_str.contains("select replace mode");
        assert!(
            has_actionable_guidance,
            "Error variant display string must render an actionable corrective action / fix guidance. Got: {err_str}"
        );
    }
}