keyhog-core 0.5.44

keyhog-core: shared data model and detector specifications for the KeyHog secret scanner
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
//! Regression: EXACT byte/JSON contract of the GitLab code-quality/security
//! report format keyhog ships (`ReportFormat::GitlabSast`).
//!
//! NOTE ON NAMING: the *only* GitLab reporter in the crate is the GitLab SAST
//! security-report reporter (`crates/core/src/report/gitlab_sast.rs`); there is
//! no separate "Code Quality" reporter. GitLab's SAST security report is the
//! surface CI consumes for secret findings, and it carries exactly the fields a
//! code-quality integration cares about: a stable per-finding fingerprint
//! (`vulnerability.id`), a human `description`, a `severity`, and a
//! `location.file` + `location.start_line`. These tests pin those.
//!
//! Every assertion is a specific value. None is a bare non-empty check.

use keyhog_core::{
    hex_encode, write_report, CredentialHash, MatchLocation, ReportFormat, Severity,
    VerificationResult, VerifiedFinding,
};
use std::borrow::Cow;
use std::collections::HashMap;

const SCHEMA_VERSION: &str = "15.2.4";
const SCHEMA_URL: &str =
    "https://gitlab.com/gitlab-org/security-products/security-report-schemas/-/raw/master/dist/sast-report-format.json";
const SCAN_START: &str = "2026-07-01T00:00:00";
const SCAN_END: &str = "2026-07-01T00:05:00";

/// Build a fully-specified finding. `hash_byte` seeds all 32 credential-hash
/// bytes, so the expected hex is `<hash_byte as 2-hex>` repeated 32 times.
fn finding_with(
    detector_id: &'static str,
    detector_name: &'static str,
    service: &'static str,
    severity: Severity,
    redacted: &'static str,
    file_path: Option<&'static str>,
    line: Option<usize>,
    hash_byte: u8,
) -> VerifiedFinding {
    VerifiedFinding {
        detector_id: detector_id.into(),
        detector_name: detector_name.into(),
        service: service.into(),
        severity,
        credential_redacted: Cow::Borrowed(redacted),
        credential_hash: CredentialHash::from_bytes([hash_byte; 32]),
        companions_redacted: std::collections::HashMap::new(),
        location: MatchLocation {
            source: "filesystem".into(),
            file_path: file_path.map(Into::into),
            line,
            offset: 0,
            commit: None,
            author: None,
            date: None,
        },
        verification: VerificationResult::Unverifiable,
        metadata: HashMap::new(),
        additional_locations: vec![],
        entropy: None,
        confidence: Some(0.9),
    }
}

/// Canonical AWS High finding: hash bytes all `0xAB`, at `config/app.env:7`.
fn aws_high() -> VerifiedFinding {
    finding_with(
        "aws-access-key",
        "AWS Access Key",
        "aws",
        Severity::High,
        "AKIA****",
        Some("config/app.env"),
        Some(7),
        0xAB,
    )
}

fn render(findings: &[VerifiedFinding]) -> serde_json::Value {
    let mut buf = Vec::new();
    write_report(
        &mut buf,
        ReportFormat::GitlabSast {
            scan_started_at: SCAN_START.to_string(),
            scan_finished_at: SCAN_END.to_string(),
        },
        findings,
    )
    .expect("GitLab SAST write_report must succeed");
    serde_json::from_slice(&buf).expect("GitLab SAST output must parse as JSON")
}

// ---------------------------------------------------------------------------
// Empty report
// ---------------------------------------------------------------------------

/// Boundary: an empty findings slice still produces a schema-valid report with
/// an EMPTY (not null, not missing) `vulnerabilities` array and empty
/// `remediations`, plus the version/schema envelope.
#[test]
fn empty_findings_emit_valid_empty_arrays() {
    let json = render(&[]);
    assert_eq!(
        json["vulnerabilities"].as_array().map(Vec::len),
        Some(0),
        "empty scan must emit an empty vulnerabilities array"
    );
    assert_eq!(
        json["remediations"].as_array().map(Vec::len),
        Some(0),
        "empty scan must emit an empty remediations array"
    );
    assert_eq!(
        json["version"].as_str(),
        Some(SCHEMA_VERSION),
        "version envelope must be present even for an empty report"
    );
}

// ---------------------------------------------------------------------------
// Envelope: version / schema / scan metadata / tool identity
// ---------------------------------------------------------------------------

/// Positive: the top-level `version` and `schema` URL are the pinned constants.
#[test]
fn envelope_version_and_schema_url_exact() {
    let json = render(&[aws_high()]);
    assert_eq!(json["version"].as_str(), Some(SCHEMA_VERSION));
    assert_eq!(json["schema"].as_str(), Some(SCHEMA_URL));
}

/// Positive: the `scan` object echoes the caller-supplied timestamps and pins
/// `type == "sast"` and `status == "success"`.
#[test]
fn scan_object_type_status_and_timestamps() {
    let json = render(&[aws_high()]);
    let scan = &json["scan"];
    assert_eq!(scan["type"].as_str(), Some("sast"));
    assert_eq!(scan["status"].as_str(), Some("success"));
    assert_eq!(
        scan["start_time"].as_str(),
        Some(SCAN_START),
        "scan.start_time must echo the caller's start timestamp"
    );
    assert_eq!(
        scan["end_time"].as_str(),
        Some(SCAN_END),
        "scan.end_time must echo the caller's end timestamp"
    );
}

/// Positive: both `analyzer` and `scanner` tool objects carry the fixed keyhog
/// identity, the compiled crate version, and the Santh vendor name.
#[test]
fn tool_identity_analyzer_and_scanner() {
    let json = render(&[aws_high()]);
    for role in ["analyzer", "scanner"] {
        let tool = &json["scan"][role];
        assert_eq!(tool["id"].as_str(), Some("keyhog"), "{role}.id");
        assert_eq!(tool["name"].as_str(), Some("KeyHog"), "{role}.name");
        assert_eq!(
            tool["vendor"]["name"].as_str(),
            Some("Santh Security"),
            "{role}.vendor.name"
        );
        assert_eq!(
            tool["version"].as_str(),
            Some(env!("CARGO_PKG_VERSION")),
            "{role}.version must be the compiled crate version"
        );
    }
}

// ---------------------------------------------------------------------------
// Vulnerability body: description / severity / location / identifiers
// ---------------------------------------------------------------------------

/// Positive: the human `description` is the exact rotate-and-remove sentence,
/// service-interpolated.
#[test]
fn vulnerability_description_exact() {
    let json = render(&[aws_high()]);
    assert_eq!(
        json["vulnerabilities"][0]["description"].as_str(),
        Some(
            "KeyHog detected a redacted aws credential. \
             Rotate the credential and remove it from source control."
        ),
        "description must be the service-interpolated rotate sentence"
    );
}

/// Positive: `name` and `message` are the exact service/detector-interpolated
/// strings.
#[test]
fn vulnerability_name_and_message_exact() {
    let json = render(&[aws_high()]);
    let vuln = &json["vulnerabilities"][0];
    assert_eq!(
        vuln["name"].as_str(),
        Some("aws credential detected"),
        "name is '{{service}} credential detected'"
    );
    assert_eq!(
        vuln["message"].as_str(),
        Some("AWS Access Key found by aws-access-key at config/app.env:7"),
        "message is '{{detector_name}} found by {{detector_id}} at {{file}}:{{line}}'"
    );
}

/// Boundary: the severity->GitLab-label table. Both `ClientSafe` and `Info`
/// collapse to "Info"; everything else is Title-cased 1:1.
#[test]
fn severity_label_mapping_table() {
    let cases = [
        (Severity::Critical, "Critical"),
        (Severity::High, "High"),
        (Severity::Medium, "Medium"),
        (Severity::Low, "Low"),
        (Severity::ClientSafe, "Info"),
        (Severity::Info, "Info"),
    ];
    for (severity, expected) in cases {
        let finding = finding_with(
            "generic-password",
            "Generic Password",
            "generic",
            severity,
            "pw_****",
            Some("config/app.env"),
            Some(7),
            0x11,
        );
        let json = render(&[finding]);
        assert_eq!(
            json["vulnerabilities"][0]["severity"].as_str(),
            Some(expected),
            "severity {severity:?} must render GitLab label {expected:?}"
        );
    }
}

/// Positive: `location.file` and `location.start_line` carry the finding's
/// filesystem path and one-based line verbatim, and `category` is "sast".
#[test]
fn location_file_start_line_and_category() {
    let json = render(&[aws_high()]);
    let vuln = &json["vulnerabilities"][0];
    assert_eq!(vuln["category"].as_str(), Some("sast"));
    assert_eq!(
        vuln["location"]["file"].as_str(),
        Some("config/app.env"),
        "location.file must be the finding path"
    );
    assert_eq!(
        vuln["location"]["start_line"].as_u64(),
        Some(7),
        "location.start_line must be the one-based line"
    );
}

/// Positive: the single `identifiers` entry maps the keyhog rule name/value to
/// the detector name/id under `type == "keyhog_rule"`.
#[test]
fn identifier_maps_detector_name_and_id() {
    let json = render(&[aws_high()]);
    let ident = &json["vulnerabilities"][0]["identifiers"][0];
    assert_eq!(ident["type"].as_str(), Some("keyhog_rule"));
    assert_eq!(
        ident["name"].as_str(),
        Some("AWS Access Key"),
        "identifier.name is the detector display name"
    );
    assert_eq!(
        ident["value"].as_str(),
        Some("aws-access-key"),
        "identifier.value is the detector id"
    );
}

/// Positive: the `details` block surfaces the redacted credential, the service,
/// and the 64-char lower-hex credential hash under stable text-detail labels.
#[test]
fn details_credential_service_and_hash() {
    let json = render(&[aws_high()]);
    let details = &json["vulnerabilities"][0]["details"];
    assert_eq!(
        details["credential"]["value"].as_str(),
        Some("AKIA****"),
        "details.credential.value is the redacted secret"
    );
    assert_eq!(
        details["credential"]["name"].as_str(),
        Some("Redacted credential")
    );
    assert_eq!(details["service"]["value"].as_str(), Some("aws"));
    let expected_hex = hex_encode([0xAB_u8; 32]);
    assert_eq!(expected_hex.len(), 64, "SHA-256 hex is 64 chars");
    assert_eq!(
        details["credential_hash"]["value"].as_str(),
        Some(expected_hex.as_str()),
        "details.credential_hash.value is the lower-hex credential hash"
    );
}

// ---------------------------------------------------------------------------
// Fingerprint stability
// ---------------------------------------------------------------------------

/// Positive: the `id` fingerprint is composed as
/// `keyhog:{detector_id}:{hex_hash}:{file}:{start_line}`.
#[test]
fn fingerprint_id_composition_exact() {
    let json = render(&[aws_high()]);
    let expected = format!(
        "keyhog:aws-access-key:{}:config/app.env:7",
        hex_encode([0xAB_u8; 32])
    );
    assert_eq!(
        json["vulnerabilities"][0]["id"].as_str(),
        Some(expected.as_str()),
        "id must be keyhog:{{detector}}:{{hash}}:{{file}}:{{line}}"
    );
}

/// Positive: the fingerprint is STABLE, two independent renders of the same
/// finding produce byte-identical `id`s.
#[test]
fn fingerprint_stable_across_renders() {
    let a = render(&[aws_high()]);
    let b = render(&[aws_high()]);
    let id_a = a["vulnerabilities"][0]["id"].as_str().unwrap();
    let id_b = b["vulnerabilities"][0]["id"].as_str().unwrap();
    assert_eq!(
        id_a, id_b,
        "identical findings must yield identical fingerprints"
    );
}

/// Negative twin: moving the credential to a different file/line changes the
/// fingerprint (location is part of identity), so it is NOT accidentally
/// location-independent.
#[test]
fn fingerprint_changes_with_location() {
    let base = render(&[aws_high()]);
    let moved = render(&[finding_with(
        "aws-access-key",
        "AWS Access Key",
        "aws",
        Severity::High,
        "AKIA****",
        Some("src/other.txt"),
        Some(42),
        0xAB,
    )]);
    let id_base = base["vulnerabilities"][0]["id"].as_str().unwrap();
    let id_moved = moved["vulnerabilities"][0]["id"].as_str().unwrap();
    assert_ne!(
        id_base, id_moved,
        "distinct location must yield a distinct fingerprint"
    );
    assert_eq!(
        id_moved,
        format!(
            "keyhog:aws-access-key:{}:src/other.txt:42",
            hex_encode([0xAB_u8; 32])
        ),
        "moved fingerprint reflects the new file/line"
    );
}

// ---------------------------------------------------------------------------
// Fail-closed: SAST requires a file path and a line
// ---------------------------------------------------------------------------

/// Adversarial: a finding with NO file path must fail the whole report closed
/// (not silently drop the finding or emit an empty path). The error names the
/// requirement and points the operator at json/sarif.
#[test]
fn missing_file_path_fails_closed() {
    let finding = finding_with(
        "aws-access-key",
        "AWS Access Key",
        "aws",
        Severity::High,
        "AKIA****",
        None,
        Some(7),
        0xAB,
    );
    let mut buf = Vec::new();
    let err = write_report(
        &mut buf,
        ReportFormat::GitlabSast {
            scan_started_at: SCAN_START.to_string(),
            scan_finished_at: SCAN_END.to_string(),
        },
        &[finding],
    )
    .expect_err("missing file path must fail the GitLab SAST report closed");
    let msg = err.to_string();
    assert!(
        msg.contains("requires a non-empty file path"),
        "error must name the file-path requirement, got: {msg}"
    );
}

/// Adversarial twin: an empty-string file path is treated the same as absent
/// fail closed, not emit `location.file == ""`.
#[test]
fn empty_file_path_fails_closed() {
    let finding = finding_with(
        "aws-access-key",
        "AWS Access Key",
        "aws",
        Severity::High,
        "AKIA****",
        Some(""),
        Some(7),
        0xAB,
    );
    let mut buf = Vec::new();
    let err = write_report(
        &mut buf,
        ReportFormat::GitlabSast {
            scan_started_at: SCAN_START.to_string(),
            scan_finished_at: SCAN_END.to_string(),
        },
        &[finding],
    )
    .expect_err("empty file path must fail the GitLab SAST report closed");
    assert!(
        err.to_string().contains("requires a non-empty file path"),
        "empty path must be rejected like an absent one"
    );
}

/// Adversarial: a finding with no line number fails closed with the one-based
/// line requirement in the message.
#[test]
fn missing_line_fails_closed() {
    let finding = finding_with(
        "aws-access-key",
        "AWS Access Key",
        "aws",
        Severity::High,
        "AKIA****",
        Some("config/app.env"),
        None,
        0xAB,
    );
    let mut buf = Vec::new();
    let err = write_report(
        &mut buf,
        ReportFormat::GitlabSast {
            scan_started_at: SCAN_START.to_string(),
            scan_finished_at: SCAN_END.to_string(),
        },
        &[finding],
    )
    .expect_err("missing line must fail the GitLab SAST report closed");
    let msg = err.to_string();
    assert!(
        msg.contains("one-based line number"),
        "error must name the line requirement, got: {msg}"
    );
}