keyhog_scanner/context/
mod.rs1mod documentation;
9mod false_positive;
10mod inference;
11mod placeholder;
12
13pub(crate) use documentation::documentation_line_flags;
14#[cfg(test)]
15pub(crate) use false_positive::parse_disclaimer_phrases;
16pub(crate) use false_positive::{has_disclaimer_comment_bytes, is_integrity_hash_bytes};
17pub(crate) use false_positive::{is_false_positive_context, is_false_positive_match_context};
18pub use inference::infer_context;
19pub(crate) use inference::infer_context_with_documentation;
20#[cfg(test)]
21pub(crate) use inference::parse_test_path_rules;
22pub(crate) use inference::{is_in_test_function, is_rust_fn_signature, strip_comment_prefix};
23pub(crate) use placeholder::is_known_example_credential;
24#[cfg(feature = "entropy")]
25pub(crate) use placeholder::is_monotonic_sequence_placeholder;
26#[cfg(test)]
27pub(crate) use placeholder::is_sequential_placeholder;
28
29const ASSIGNMENT_CONFIDENCE_MULTIPLIER: f64 = 1.0;
30const STRING_LITERAL_CONFIDENCE_MULTIPLIER: f64 = 0.9;
31const UNKNOWN_CONFIDENCE_MULTIPLIER: f64 = 0.8;
32const DOCUMENTATION_CONFIDENCE_MULTIPLIER: f64 = 0.3;
33const COMMENT_CONFIDENCE_MULTIPLIER: f64 = 0.4;
34const TEST_CODE_CONFIDENCE_MULTIPLIER: f64 = 0.3;
35const ENCRYPTED_CONFIDENCE_MULTIPLIER: f64 = 0.05;
36const SOFT_CONTEXT_HARD_SUPPRESSION_THRESHOLD: f64 = 0.5;
37const ENCRYPTED_CONTEXT_HARD_SUPPRESSION_THRESHOLD: f64 = 0.8;
38
39#[derive(Debug, Clone, Copy, PartialEq)]
41pub enum CodeContext {
42 Assignment,
44 Comment,
46 TestCode,
48 Encrypted,
50 Documentation,
52 StringLiteral,
54 Unknown,
56}
57
58impl CodeContext {
59 pub fn confidence_multiplier(&self) -> f64 {
63 match self {
64 Self::Assignment => ASSIGNMENT_CONFIDENCE_MULTIPLIER,
65 Self::StringLiteral => STRING_LITERAL_CONFIDENCE_MULTIPLIER,
66 Self::Unknown => UNKNOWN_CONFIDENCE_MULTIPLIER,
67 Self::Documentation => DOCUMENTATION_CONFIDENCE_MULTIPLIER,
68 Self::Comment => COMMENT_CONFIDENCE_MULTIPLIER,
69 Self::TestCode => TEST_CODE_CONFIDENCE_MULTIPLIER,
70 Self::Encrypted => ENCRYPTED_CONFIDENCE_MULTIPLIER,
71 }
72 }
73
74 pub fn should_hard_suppress(&self, confidence: f64) -> bool {
78 self.hard_suppression_threshold()
79 .is_some_and(|threshold| confidence < threshold)
80 }
81
82 pub const fn hard_suppression_threshold(&self) -> Option<f64> {
84 match self {
85 Self::Documentation | Self::TestCode | Self::Comment => {
86 Some(SOFT_CONTEXT_HARD_SUPPRESSION_THRESHOLD)
87 }
88 Self::Encrypted => Some(ENCRYPTED_CONTEXT_HARD_SUPPRESSION_THRESHOLD),
89 _ => None,
90 }
91 }
92}