Skip to main content

keyhog_scanner/context/
mod.rs

1//! Structural context analysis: understand WHERE in code a potential secret appears.
2//!
3//! Instead of treating code as flat text, we infer the structural context of
4//! each match (assignment, comment, test code, encrypted block, documentation)
5//! and adjust confidence accordingly. Not an AST parser - just fast,
6//! language-agnostic structural inference.
7
8mod 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/// The structural context of a code location.
40#[derive(Debug, Clone, Copy, PartialEq)]
41pub enum CodeContext {
42    /// Direct assignment: `key = value`, `key: value`, `KEY=value`.
43    Assignment,
44    /// Inside a comment (`//`, `#`, `/*`, `--`, and similar).
45    Comment,
46    /// Inside a test function or test file.
47    TestCode,
48    /// Inside an encrypted/sealed block.
49    Encrypted,
50    /// Inside documentation (docstring, markdown code fence).
51    Documentation,
52    /// Inside a string literal in ordinary code.
53    StringLiteral,
54    /// Unknown or unstructured context.
55    Unknown,
56}
57
58impl CodeContext {
59    /// Legacy baseline multiplier for callers that classify context without a
60    /// detector plan. Production candidate scoring uses the active detector's
61    /// compiled `match_confidence` multipliers instead.
62    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    /// Legacy baseline hard-suppression decision for callers without a detector
75    /// plan. Production finalization uses the active detector's compiled
76    /// context thresholds.
77    pub fn should_hard_suppress(&self, confidence: f64) -> bool {
78        self.hard_suppression_threshold()
79            .is_some_and(|threshold| confidence < threshold)
80    }
81
82    /// Legacy baseline threshold paired with [`CodeContext::should_hard_suppress`].
83    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}