use crate::types::*;
use keyhog_core::{Chunk, MatchLocation, RawMatch, Severity};
use std::collections::HashMap;
use std::sync::Arc;
fn source_adjusted_severity(source_type: &str, severity: Severity) -> Severity {
if matches!(
source_type,
"git/history" | "git-history" | "git/unreachable"
) {
severity.downgrade_one()
} else {
severity
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn build_raw_match(
detector_severity: Severity,
metadata: (Arc<str>, Arc<str>, Arc<str>),
chunk: &Chunk,
credential: &str,
companions: HashMap<String, String>,
offset: usize,
line: usize,
ent: f64,
confidence: f64,
scan_state: &mut ScanState,
pattern_client_safe: bool,
) -> RawMatch {
let (detector_id, detector_name, service) = metadata;
let severity = if pattern_client_safe {
keyhog_core::Severity::ClientSafe
} else {
source_adjusted_severity(&chunk.metadata.source_type, detector_severity)
};
RawMatch {
detector_id,
detector_name,
service,
severity,
credential_hash: crate::sha256_hash(credential),
credential: scan_state.intern_credential(credential),
companions,
location: MatchLocation {
source: scan_state.intern_metadata(&chunk.metadata.source_type),
file_path: chunk
.metadata
.path
.as_ref()
.map(|p| scan_state.intern_metadata(p)),
line: Some(line + chunk.metadata.base_line),
offset: offset + chunk.metadata.base_offset,
commit: chunk
.metadata
.commit
.as_ref()
.map(|c| scan_state.intern_metadata(c)),
author: chunk
.metadata
.author
.as_ref()
.map(|a| scan_state.intern_metadata(a)),
date: chunk
.metadata
.date
.as_ref()
.map(|d| scan_state.intern_metadata(d)),
},
entropy: Some(ent),
confidence: Some(confidence),
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn build_synthetic_raw_match(
metadata: (Arc<str>, Arc<str>, Arc<str>),
severity: Severity,
chunk: &Chunk,
credential: &str,
absolute_offset: usize,
absolute_line: Option<usize>,
entropy: Option<f64>,
confidence: f64,
scan_state: &mut ScanState,
) -> RawMatch {
let (detector_id, detector_name, service) = metadata;
let severity = source_adjusted_severity(&chunk.metadata.source_type, severity);
RawMatch {
detector_id,
detector_name,
service,
severity,
credential_hash: crate::sha256_hash(credential),
credential: scan_state.intern_credential(credential),
companions: HashMap::new(),
location: MatchLocation {
source: scan_state.intern_metadata(&chunk.metadata.source_type),
file_path: chunk
.metadata
.path
.as_ref()
.map(|path| scan_state.intern_metadata(path)),
line: absolute_line,
offset: absolute_offset,
commit: chunk
.metadata
.commit
.as_ref()
.map(|commit| scan_state.intern_metadata(commit)),
author: chunk
.metadata
.author
.as_ref()
.map(|author| scan_state.intern_metadata(author)),
date: chunk
.metadata
.date
.as_ref()
.map(|date| scan_state.intern_metadata(date)),
},
entropy,
confidence: Some(confidence),
}
}