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
//! Security metadata helpers owned by the engine boundary.
//!
//! The severity policy, the catalogue-title lookup, the rule and finding
//! identifiers, and the rule-enabling policy are shared with the core analyze
//! ranking pass via fallow-security so the CLI, SARIF, JSON, and viz Security
//! surfaces can never diverge.
use std::path::{Path, PathBuf};
use fallow_types::results::{AnalysisResults, SecurityFinding};
use rustc_hash::FxHashMap;
pub use fallow_security::{
derive_security_severity, enable_security_rules, resolve_security_finding_severity,
security_catalogue_title, security_rule_id, security_rules_can_error,
};
/// Set the `finding_id` and the `stable_finding_id` of each security finding
/// in `results`.
///
/// The stable id hashes the normalized text of the exact sink expression. This
/// function reads each anchor file once from disk, and gets the text of each
/// sink span from [`fallow_extract::sink_identity_text`]. A finding whose file
/// cannot be read gets no stable id.
///
/// The candidates in `results.suppressed_security_findings` get no id, but
/// they keep their tiebreak position. This function then clears that list.
pub fn stamp_security_finding_ids(results: &mut AnalysisResults, root: &Path) {
let suppressed = std::mem::take(&mut results.suppressed_security_findings);
stamp_findings(&mut results.security_findings, &suppressed, root);
}
fn stamp_findings(findings: &mut [SecurityFinding], suppressed: &[SecurityFinding], root: &Path) {
let mut sources: FxHashMap<PathBuf, Option<String>> = FxHashMap::default();
fallow_security::stamp_security_finding_ids(findings, suppressed, root, |path, start, end| {
let source = sources
.entry(path.to_path_buf())
.or_insert_with_key(|path| std::fs::read_to_string(path).ok());
fallow_extract::sink_identity_text(path, source.as_deref()?, start, end)
});
}