keyhog-scanner 0.1.0

Aho-Corasick + regex compiled scanner for credential detection
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
//! Confidence scoring: combines multiple signals into a 0.0–1.0 score.
//! Higher confidence means more likely to be a real secret.

const SCORE_ZERO: f64 = 0.0;
const CONFIDENCE_MIN: f64 = 0.0;
const CONFIDENCE_MAX: f64 = 1.0;
const LITERAL_PREFIX_WEIGHT: f64 = 0.35;
const CONTEXT_ANCHOR_WEIGHT: f64 = 0.20;
const ENTROPY_WEIGHT: f64 = 0.20;
const HIGH_ENTROPY_PARTIAL_WEIGHT: f64 = 0.12;
const MODERATE_ENTROPY_THRESHOLD: f64 = 3.0;
const MODERATE_ENTROPY_WEIGHT: f64 = 0.05;
const LOW_ENTROPY_THRESHOLD: f64 = 2.0;
const LOW_ENTROPY_MIN_MATCH_LENGTH: usize = 10;
const LOW_ENTROPY_PENALTY: f64 = 0.6;
const KEYWORD_NEARBY_WEIGHT: f64 = 0.10;
const SENSITIVE_FILE_WEIGHT: f64 = 0.10;
const COMPANION_WEIGHT: f64 = 0.05;
const HIGH_ENTROPY_THRESHOLD: f64 = 4.5;
const VERY_HIGH_ENTROPY_THRESHOLD: f64 = 5.5;

/// Confidence signals for a potential match.
///
/// # Examples
///
/// ```rust
/// use keyhog_scanner::confidence::ConfidenceSignals;
///
/// let signals = ConfidenceSignals {
///     has_literal_prefix: true,
///     has_context_anchor: true,
///     entropy: 5.0,
///     keyword_nearby: true,
///     sensitive_file: true,
///     match_length: 32,
///     has_companion: false,
/// };
/// assert!(signals.has_literal_prefix);
/// ```
pub struct ConfidenceSignals {
    /// Pattern has a distinctive literal prefix (e.g., sk-proj-, ghp_)
    pub has_literal_prefix: bool,
    /// Pattern uses a capture group with context anchoring
    pub has_context_anchor: bool,
    /// Shannon entropy of the matched credential
    pub entropy: f64,
    /// A secret-related keyword appears nearby
    pub keyword_nearby: bool,
    /// File extension suggests config/env/secret file
    pub sensitive_file: bool,
    /// Matched credential length
    pub match_length: usize,
    /// Companion credential was found
    pub has_companion: bool,
}

/// Compute a confidence score from 0.0 to 1.0.
///
/// The hand-tuned weights below were calibrated against the adversarial test
/// corpus before the scanner blends this heuristic score with the ML model:
/// literal prefixes dominate, context and entropy are secondary, and file/path
/// hints plus companion matches provide smaller incremental adjustments.
///
/// # Examples
///
/// ```rust
/// use keyhog_scanner::confidence::{ConfidenceSignals, compute_confidence};
///
/// let score = compute_confidence(&ConfidenceSignals {
///     has_literal_prefix: true,
///     has_context_anchor: true,
///     entropy: 5.0,
///     keyword_nearby: true,
///     sensitive_file: true,
///     match_length: 32,
///     has_companion: false,
/// });
/// assert!(score > 0.5);
/// ```
pub fn compute_confidence(signals: &ConfidenceSignals) -> f64 {
    let mut score = SCORE_ZERO;
    let mut max_possible = SCORE_ZERO;

    // Literal prefix: strongest signal. If it starts with "sk-proj-", it's almost certainly real.
    // Literal prefix is the strongest signal: sk-proj-, ghp_, AKIA are nearly certain.
    // Weight: 0.35 (largest single factor). Validated by ML classifier agreement.
    max_possible += LITERAL_PREFIX_WEIGHT;
    if signals.has_literal_prefix {
        score += LITERAL_PREFIX_WEIGHT;
    }

    // Context anchor: "API_KEY=..." near the value.
    max_possible += CONTEXT_ANCHOR_WEIGHT;
    if signals.has_context_anchor {
        score += CONTEXT_ANCHOR_WEIGHT;
    }

    // Entropy: high entropy = likely random/secret, low entropy = likely placeholder.
    max_possible += ENTROPY_WEIGHT;
    if signals.entropy >= VERY_HIGH_ENTROPY_THRESHOLD {
        score += ENTROPY_WEIGHT;
    } else if signals.entropy >= HIGH_ENTROPY_THRESHOLD {
        score += HIGH_ENTROPY_PARTIAL_WEIGHT;
    } else if signals.entropy >= MODERATE_ENTROPY_THRESHOLD {
        score += MODERATE_ENTROPY_WEIGHT;
    }
    // Very low entropy is a negative signal: multiply down the score.
    // Applied after normalization so the weighted-average math stays consistent.
    let low_entropy_penalty = if signals.entropy < LOW_ENTROPY_THRESHOLD
        && signals.match_length > LOW_ENTROPY_MIN_MATCH_LENGTH
    {
        LOW_ENTROPY_PENALTY
    } else {
        CONFIDENCE_MAX
    };

    // Keyword proximity.
    max_possible += KEYWORD_NEARBY_WEIGHT;
    if signals.keyword_nearby {
        score += KEYWORD_NEARBY_WEIGHT;
    }

    // Sensitive file type (.env, .secrets, credentials.json, etc.)
    max_possible += SENSITIVE_FILE_WEIGHT;
    if signals.sensitive_file {
        score += SENSITIVE_FILE_WEIGHT;
    }

    // Companion found (e.g., AWS secret key near access key).
    max_possible += COMPANION_WEIGHT;
    if signals.has_companion {
        score += COMPANION_WEIGHT;
    }

    // Normalize to 0.0 - 1.0.
    if max_possible == SCORE_ZERO {
        return SCORE_ZERO;
    }
    let normalized_score: f64 = (score / max_possible) * low_entropy_penalty;
    normalized_score.clamp(CONFIDENCE_MIN, CONFIDENCE_MAX)
}

/// Check if a file path suggests a sensitive file.
///
/// # Examples
///
/// ```rust
/// use keyhog_scanner::confidence::is_sensitive_path;
///
/// assert!(is_sensitive_path(".env.production"));
/// assert!(!is_sensitive_path("src/main.rs"));
/// ```
pub fn is_sensitive_path(path: &str) -> bool {
    let path_bytes = path.as_bytes();
    const SENSITIVE_NAMES: &[&[u8]] = &[
        b".env",
        b".env.local",
        b".env.production",
        b".env.staging",
        b"credentials",
        b"secrets",
        b"apikeys",
        b"api_keys",
        b".npmrc",
        b".pypirc",
        b".netrc",
        b".pgpass",
        b"terraform.tfvars",
        b"variables.tf",
        b"docker-compose",
        b"application.yml",
        b"application.properties",
        b"config.json",
        b"config.yaml",
    ];

    for name in SENSITIVE_NAMES {
        if path_bytes
            .windows(name.len())
            .any(|w| w.eq_ignore_ascii_case(name))
        {
            return true;
        }
    }
    const SENSITIVE_EXTENSIONS: &[&[u8]] = &[b".env", b".pem", b".key", b".p12", b".pfx", b".jks"];
    for ext in SENSITIVE_EXTENSIONS {
        if path_bytes.len() >= ext.len()
            && path_bytes[path_bytes.len() - ext.len()..].eq_ignore_ascii_case(ext)
        {
            return true;
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn high_confidence_with_prefix_and_entropy() {
        let signals = ConfidenceSignals {
            has_literal_prefix: true,
            has_context_anchor: false,
            entropy: 5.2,
            keyword_nearby: true,
            sensitive_file: true,
            match_length: 50,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!(score > 0.6, "score was {}", score);
    }

    #[test]
    fn low_confidence_generic_hex() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: 3.5,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 32,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!(score < 0.3, "score was {}", score);
    }

    #[test]
    fn medium_confidence_with_context() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: true,
            entropy: 4.8,
            keyword_nearby: true,
            sensitive_file: false,
            match_length: 40,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!(score > 0.4 && score < 0.8, "score was {}", score);
    }

    #[test]
    fn sensitive_paths() {
        assert!(is_sensitive_path(".env.production"));
        assert!(is_sensitive_path("config/credentials.json"));
        assert!(is_sensitive_path("server.key"));
        assert!(!is_sensitive_path("src/main.rs"));
        assert!(!is_sensitive_path("README.md"));
    }

    #[test]
    fn low_entropy_penalty() {
        let signals = ConfidenceSignals {
            has_literal_prefix: true,
            has_context_anchor: false,
            entropy: 1.5, // Very low — likely "aaaaaaa..." or placeholder
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 32,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        // Should be penalized despite having prefix
        assert!(score < 0.5, "score was {}", score);
    }

    #[test]
    fn confidence_is_zero_without_positive_signals() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: 0.0,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 0,
            has_companion: false,
        };
        assert_eq!(compute_confidence(&signals), 0.0);
    }

    #[test]
    fn confidence_clamps_to_one_for_all_positive_signals() {
        let signals = ConfidenceSignals {
            has_literal_prefix: true,
            has_context_anchor: true,
            entropy: 8.0,
            keyword_nearby: true,
            sensitive_file: true,
            match_length: 128,
            has_companion: true,
        };
        assert_eq!(compute_confidence(&signals), 1.0);
    }

    #[test]
    fn very_high_entropy_gets_full_entropy_weight() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: VERY_HIGH_ENTROPY_THRESHOLD,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 32,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.2).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn high_entropy_gets_partial_entropy_weight() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: HIGH_ENTROPY_THRESHOLD,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 32,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.12).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn moderate_entropy_gets_small_weight() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: 3.0,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 32,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.05).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn entropy_below_moderate_threshold_adds_no_weight() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: 2.99,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 32,
            has_companion: false,
        };
        assert_eq!(compute_confidence(&signals), 0.0);
    }

    #[test]
    fn low_entropy_penalty_requires_length_above_threshold() {
        let signals = ConfidenceSignals {
            has_literal_prefix: true,
            has_context_anchor: false,
            entropy: 1.0,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 10,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.35).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn low_entropy_penalty_applies_only_below_threshold() {
        let signals = ConfidenceSignals {
            has_literal_prefix: true,
            has_context_anchor: false,
            entropy: 2.0,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 64,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.35).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn low_entropy_penalty_scales_nonzero_score() {
        let signals = ConfidenceSignals {
            has_literal_prefix: true,
            has_context_anchor: true,
            entropy: 1.0,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 11,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.33).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn companion_signal_adds_expected_weight() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: false,
            entropy: 0.0,
            keyword_nearby: false,
            sensitive_file: false,
            match_length: 24,
            has_companion: true,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.03).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn context_and_keyword_signals_stack_linearly() {
        let signals = ConfidenceSignals {
            has_literal_prefix: false,
            has_context_anchor: true,
            entropy: 0.0,
            keyword_nearby: true,
            sensitive_file: false,
            match_length: 20,
            has_companion: false,
        };
        let score = compute_confidence(&signals);
        assert!((score - 0.18).abs() < 1e-9, "score was {}", score);
    }

    #[test]
    fn sensitive_path_matches_case_insensitively() {
        assert!(is_sensitive_path("CONFIG/.ENV.PRODUCTION"));
        assert!(is_sensitive_path("Secrets/CREDENTIALS.JSON"));
        assert!(is_sensitive_path("keys/CLIENT.P12"));
    }

    #[test]
    fn sensitive_path_rejects_empty_and_non_sensitive_values() {
        assert!(!is_sensitive_path(""));
        assert!(!is_sensitive_path("notes/environment.txt"));
        assert!(!is_sensitive_path("docs/secretary.txt"));
    }

    #[test]
    fn sensitive_path_detects_embedded_sensitive_names_with_special_characters() {
        assert!(is_sensitive_path("deploy/docker-compose.override.yml"));
        assert!(is_sensitive_path("dir/my api_keys-backup.txt"));
        assert!(is_sensitive_path("nested/application.properties.template"));
    }

    #[test]
    fn sensitive_path_handles_huge_input() {
        let long_prefix = "a/".repeat(4096);
        let long_sensitive = format!("{long_prefix}terraform.tfvars");
        let long_non_sensitive = format!("{long_prefix}plain-text-file.txt");
        assert!(is_sensitive_path(&long_sensitive));
        assert!(!is_sensitive_path(&long_non_sensitive));
    }
}