keyhog-core 0.5.84

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
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
//! Deterministic finding verdicts derived from scanner evidence.

use serde::{de::Error as _, ser::Error as _, Deserialize, Deserializer, Serialize, Serializer};

/// Operator-facing evidence tier for one finding.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum EvidenceTier {
    /// Ambiguous evidence that remains visible but does not block default CI.
    Review,
    /// Strong provider and source evidence that blocks default CI.
    Likely,
    /// Intrinsic or live proof that blocks every finding policy.
    Confirmed,
}

impl EvidenceTier {
    /// Return the stable output spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Review => "review",
            Self::Likely => "likely",
            Self::Confirmed => "confirmed",
        }
    }

    /// Whether this tier blocks the selected CI policy.
    pub const fn blocks(self, paranoid: bool) -> bool {
        paranoid || !matches!(self, Self::Review)
    }
}

/// Stable reason code that determines a finding's evidence tier.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum EvidenceReasonCode {
    /// No scanner producer evidence was available.
    Unattributed,
    /// The source syntax was unsupported, malformed, or outside parser bounds.
    UnsupportedContext,
    /// A declared semantic evidence requirement was not proven.
    RequiredEvidenceMissing,
    /// The detector declared only a weak or unanchored context.
    WeakAnchor,
    /// A generic detector pattern produced the candidate.
    GenericDetector,
    /// The generic assignment lane produced the candidate.
    GenericAssignment,
    /// The entropy-only lane produced the candidate.
    EntropyOnly,
    /// The candidate is in a test or example fixture.
    TestFixture,
    /// The candidate is in documentation prose.
    Documentation,
    /// The candidate is inside a regex, scanner rule, or grammar definition.
    RuleDefinition,
    /// The candidate is an identifier, type, or member name.
    Identifier,
    /// The candidate is a command-option declaration rather than its value.
    OptionDeclaration,
    /// The candidate is in generated or vendored material.
    GeneratedMaterial,
    /// Parsed source semantics do not match the detector's declared source roles.
    SourceRoleMismatch,
    /// A provider-specific named pattern matched in a credential-bearing source role.
    VendorPattern,
    /// A detector-owned structural grammar proved the credential shape.
    StructuralGrammar,
    /// Required companion evidence was present.
    RequiredCompanion,
    /// An intrinsic credential checksum validated.
    ChecksumValid,
    /// Live provider verification succeeded.
    LiveVerification,
}

impl EvidenceReasonCode {
    /// Return the tier implied by this reason code.
    pub const fn tier(self) -> EvidenceTier {
        match self {
            Self::VendorPattern => EvidenceTier::Likely,
            Self::StructuralGrammar
            | Self::RequiredCompanion
            | Self::ChecksumValid
            | Self::LiveVerification => EvidenceTier::Confirmed,
            Self::Unattributed
            | Self::UnsupportedContext
            | Self::RequiredEvidenceMissing
            | Self::WeakAnchor
            | Self::GenericDetector
            | Self::GenericAssignment
            | Self::EntropyOnly
            | Self::TestFixture
            | Self::Documentation
            | Self::RuleDefinition
            | Self::Identifier
            | Self::OptionDeclaration
            | Self::GeneratedMaterial
            | Self::SourceRoleMismatch => EvidenceTier::Review,
        }
    }

    /// Return the stable output spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unattributed => "unattributed",
            Self::UnsupportedContext => "unsupported-context",
            Self::RequiredEvidenceMissing => "required-evidence-missing",
            Self::WeakAnchor => "weak-anchor",
            Self::GenericDetector => "generic-detector",
            Self::GenericAssignment => "generic-assignment",
            Self::EntropyOnly => "entropy-only",
            Self::TestFixture => "test-fixture",
            Self::Documentation => "documentation",
            Self::RuleDefinition => "rule-definition",
            Self::Identifier => "identifier",
            Self::OptionDeclaration => "option-declaration",
            Self::GeneratedMaterial => "generated-material",
            Self::SourceRoleMismatch => "source-role-mismatch",
            Self::VendorPattern => "vendor-pattern",
            Self::StructuralGrammar => "structural-grammar",
            Self::RequiredCompanion => "required-companion",
            Self::ChecksumValid => "checksum-valid",
            Self::LiveVerification => "live-verification",
        }
    }
}

/// Scanner lane that produced a finding candidate.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FindingCandidateChannel {
    /// A detector-owned named pattern.
    Pattern,
    /// The generic credential assignment lane.
    GenericAssignment,
    /// The detector-owned entropy lane.
    Entropy,
    /// A caller-created finding without scanner provenance.
    Unattributed,
}

impl FindingCandidateChannel {
    /// Return the stable output spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Pattern => "pattern",
            Self::GenericAssignment => "generic-assignment",
            Self::Entropy => "entropy",
            Self::Unattributed => "unattributed",
        }
    }
}

/// Secret-safe candidate identity retained in public evidence.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FindingProvenance {
    detector_digest: u64,
    pattern_index: u32,
    candidate_channel: FindingCandidateChannel,
    source_role: crate::SemanticSourceRole,
    context_class: EvidenceReasonCode,
}

impl FindingProvenance {
    /// Current persisted provenance schema.
    pub const SCHEMA_VERSION: u8 = 1;

    const fn has_scanner_context(context_class: EvidenceReasonCode) -> bool {
        !matches!(
            context_class,
            EvidenceReasonCode::Unattributed | EvidenceReasonCode::LiveVerification
        )
    }

    const fn fields_are_consistent(self) -> bool {
        match self.candidate_channel {
            FindingCandidateChannel::Pattern
            | FindingCandidateChannel::GenericAssignment
            | FindingCandidateChannel::Entropy => Self::has_scanner_context(self.context_class),
            FindingCandidateChannel::Unattributed => {
                self.detector_digest == 0
                    && self.pattern_index == 0
                    && matches!(self.source_role, crate::SemanticSourceRole::Unknown)
                    && matches!(self.context_class, EvidenceReasonCode::Unattributed)
            }
        }
    }

    /// Construct provenance for a scanner-owned named pattern.
    pub const fn pattern(
        detector_digest: u64,
        pattern_index: u32,
        source_role: crate::SemanticSourceRole,
        context_class: EvidenceReasonCode,
    ) -> Self {
        Self {
            detector_digest,
            pattern_index,
            candidate_channel: FindingCandidateChannel::Pattern,
            source_role,
            context_class,
        }
    }

    /// Construct provenance for the generic assignment lane.
    pub const fn generic_assignment(
        detector_digest: u64,
        source_role: crate::SemanticSourceRole,
        context_class: EvidenceReasonCode,
    ) -> Self {
        Self::lane(
            detector_digest,
            FindingCandidateChannel::GenericAssignment,
            source_role,
            context_class,
        )
    }

    /// Construct provenance for the entropy lane.
    pub const fn entropy(
        detector_digest: u64,
        source_role: crate::SemanticSourceRole,
        context_class: EvidenceReasonCode,
    ) -> Self {
        Self::lane(
            detector_digest,
            FindingCandidateChannel::Entropy,
            source_role,
            context_class,
        )
    }

    const fn lane(
        detector_digest: u64,
        candidate_channel: FindingCandidateChannel,
        source_role: crate::SemanticSourceRole,
        context_class: EvidenceReasonCode,
    ) -> Self {
        Self {
            detector_digest,
            pattern_index: 0,
            candidate_channel,
            source_role,
            context_class,
        }
    }

    /// Construct provenance for a caller-created finding.
    pub const fn unattributed() -> Self {
        Self {
            detector_digest: 0,
            pattern_index: 0,
            candidate_channel: FindingCandidateChannel::Unattributed,
            source_role: crate::SemanticSourceRole::Unknown,
            context_class: EvidenceReasonCode::Unattributed,
        }
    }

    /// Return the active detector-corpus digest.
    pub const fn detector_digest(self) -> Option<u64> {
        if matches!(
            self.candidate_channel,
            FindingCandidateChannel::Unattributed
        ) {
            None
        } else {
            Some(self.detector_digest)
        }
    }

    /// Return the detector-local source pattern ordinal.
    pub const fn pattern_index(self) -> Option<u32> {
        if matches!(self.candidate_channel, FindingCandidateChannel::Pattern) {
            Some(self.pattern_index)
        } else {
            None
        }
    }

    /// Return the candidate producer lane.
    pub const fn candidate_channel(self) -> FindingCandidateChannel {
        self.candidate_channel
    }

    /// Return the parsed source role.
    pub const fn source_role(self) -> crate::SemanticSourceRole {
        self.source_role
    }

    /// Return the pre-verification evidence context.
    pub const fn context_class(self) -> EvidenceReasonCode {
        self.context_class
    }
}

impl Serialize for FindingProvenance {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if !self.fields_are_consistent() {
            return Err(S::Error::custom(
                "finding provenance fields are inconsistent with candidate_channel",
            ));
        }
        #[derive(Serialize)]
        struct WireProvenance<'a> {
            schema_version: u8,
            detector_digest: Option<&'a str>,
            pattern_index: Option<u32>,
            candidate_channel: FindingCandidateChannel,
            source_role: crate::SemanticSourceRole,
            context_class: EvidenceReasonCode,
        }

        const HEX: &[u8; 16] = b"0123456789abcdef";
        let mut digest_hex = [b'0'; 16];
        let detector_digest = if let Some(digest) = self.detector_digest() {
            for (index, digit) in digest_hex.iter_mut().enumerate() {
                let shift = (15 - index) * 4;
                *digit = HEX[((digest >> shift) & 0x0f) as usize];
            }
            Some(std::str::from_utf8(&digest_hex).map_err(S::Error::custom)?)
        } else {
            None
        };
        WireProvenance {
            schema_version: Self::SCHEMA_VERSION,
            detector_digest,
            pattern_index: self.pattern_index(),
            candidate_channel: self.candidate_channel,
            source_role: self.source_role,
            context_class: self.context_class,
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for FindingProvenance {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        fn required_nullable<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
        where
            D: Deserializer<'de>,
            T: Deserialize<'de>,
        {
            Option::<T>::deserialize(deserializer)
        }

        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct WireProvenance {
            schema_version: u8,
            #[serde(deserialize_with = "required_nullable")]
            detector_digest: Option<String>,
            #[serde(deserialize_with = "required_nullable")]
            pattern_index: Option<u32>,
            candidate_channel: FindingCandidateChannel,
            source_role: crate::SemanticSourceRole,
            context_class: EvidenceReasonCode,
        }

        let wire = WireProvenance::deserialize(deserializer)?;
        if wire.schema_version != Self::SCHEMA_VERSION {
            return Err(D::Error::custom(format!(
                "unsupported finding provenance schema {}; expected {}",
                wire.schema_version,
                Self::SCHEMA_VERSION
            )));
        }
        let detector_digest = wire
            .detector_digest
            .map(|digest| {
                if digest.len() != 16
                    || !digest
                        .bytes()
                        .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
                {
                    return Err(D::Error::custom(
                        "finding provenance detector_digest must be 16 lowercase hex digits",
                    ));
                }
                u64::from_str_radix(&digest, 16).map_err(D::Error::custom)
            })
            .transpose()?;
        let has_scanner_context = Self::has_scanner_context(wire.context_class);
        let fields_are_consistent = match wire.candidate_channel {
            FindingCandidateChannel::Pattern => {
                detector_digest.is_some() && wire.pattern_index.is_some() && has_scanner_context
            }
            FindingCandidateChannel::GenericAssignment | FindingCandidateChannel::Entropy => {
                detector_digest.is_some() && wire.pattern_index.is_none() && has_scanner_context
            }
            FindingCandidateChannel::Unattributed => {
                detector_digest.is_none()
                    && wire.pattern_index.is_none()
                    && matches!(wire.source_role, crate::SemanticSourceRole::Unknown)
                    && matches!(wire.context_class, EvidenceReasonCode::Unattributed)
            }
        };
        if !fields_are_consistent {
            return Err(D::Error::custom(
                "finding provenance fields are inconsistent with candidate_channel",
            ));
        }
        Ok(Self {
            detector_digest: detector_digest.unwrap_or(0),
            pattern_index: wire.pattern_index.unwrap_or(0),
            candidate_channel: wire.candidate_channel,
            source_role: wire.source_role,
            context_class: wire.context_class,
        })
    }
}

/// One internally consistent finding verdict.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EvidenceVerdict {
    reason_code: EvidenceReasonCode,
    provenance: FindingProvenance,
}

impl EvidenceVerdict {
    /// Construct a verdict from its canonical reason code.
    pub const fn from_reason(reason_code: EvidenceReasonCode) -> Self {
        Self {
            reason_code,
            provenance: FindingProvenance::unattributed(),
        }
    }

    /// Compatibility verdict for a caller-created finding with no producer proof.
    pub const fn review_unattributed() -> Self {
        Self::from_reason(EvidenceReasonCode::Unattributed)
    }

    /// Attach exact secret-safe scanner provenance.
    pub const fn with_provenance(mut self, provenance: FindingProvenance) -> Self {
        self.provenance = provenance;
        self
    }

    /// Replace the final reason while retaining candidate provenance.
    pub const fn with_reason(mut self, reason_code: EvidenceReasonCode) -> Self {
        self.reason_code = reason_code;
        self
    }

    /// Return the derived evidence tier.
    pub const fn tier(self) -> EvidenceTier {
        self.reason_code.tier()
    }

    /// Return the canonical reason code.
    pub const fn reason_code(self) -> EvidenceReasonCode {
        self.reason_code
    }

    /// Return the exact secret-safe candidate provenance.
    pub const fn provenance(self) -> FindingProvenance {
        self.provenance
    }

    /// Select the stronger verdict, with stable reason and provenance tiebreaks.
    pub fn stronger(self, other: Self) -> Self {
        let self_tier = self.tier() as u8;
        let other_tier = other.tier() as u8;
        let same_reason = other_tier == self_tier && other.reason_code == self.reason_code;
        let self_attributed = !matches!(
            self.provenance.candidate_channel(),
            FindingCandidateChannel::Unattributed
        );
        let other_attributed = !matches!(
            other.provenance.candidate_channel(),
            FindingCandidateChannel::Unattributed
        );
        if other_tier > self_tier
            || (other_tier == self_tier && other.reason_code as u8 > self.reason_code as u8)
            || (same_reason && other_attributed && !self_attributed)
            || (same_reason
                && other_attributed == self_attributed
                && other.provenance > self.provenance)
        {
            other
        } else {
            self
        }
    }
}

impl Serialize for EvidenceVerdict {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[derive(Serialize)]
        struct WireVerdict {
            tier: EvidenceTier,
            reason_code: EvidenceReasonCode,
            provenance: FindingProvenance,
        }

        WireVerdict {
            tier: self.tier(),
            reason_code: self.reason_code,
            provenance: self.provenance,
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for EvidenceVerdict {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct WireVerdict {
            tier: EvidenceTier,
            reason_code: EvidenceReasonCode,
            provenance: FindingProvenance,
        }

        let wire = WireVerdict::deserialize(deserializer)?;
        let verdict = Self::from_reason(wire.reason_code).with_provenance(wire.provenance);
        if verdict.tier() != wire.tier {
            return Err(D::Error::custom(format!(
                "evidence tier {:?} does not match reason code {:?}",
                wire.tier, wire.reason_code
            )));
        }
        Ok(verdict)
    }
}