Skip to main content

fallow_security/
severity.rs

1//! Shared severity policy for security candidates.
2//!
3//! Both the core analyze ranking pass and the engine dead-code boundary derive
4//! severities from the same typed signals; keeping the policy here guarantees
5//! the two surfaces can never diverge.
6
7use fallow_types::results::{SecurityFinding, SecurityRuntimeState, SecuritySeverity};
8
9use crate::{HARDCODED_SECRET_CATEGORY_ID, HARDCODED_SECRET_CATEGORY_TITLE, catalogue_title};
10
11/// Derive the verification-priority tier from existing security signals. This is
12/// ranking only, not a vulnerability verdict.
13#[must_use]
14pub fn derive_security_severity(finding: &SecurityFinding) -> SecuritySeverity {
15    if finding
16        .runtime
17        .as_ref()
18        .is_some_and(|runtime| runtime.state == SecurityRuntimeState::RuntimeHot)
19        || finding.candidate.boundary.client_server
20        || finding
21            .candidate
22            .boundary
23            .architecture_zone
24            .as_ref()
25            .is_some()
26        || finding
27            .reachability
28            .as_ref()
29            .is_some_and(|reach| reach.crosses_boundary)
30        || finding
31            .reachability
32            .as_ref()
33            .is_some_and(|reach| reach.reachable_from_entry && finding.source_backed)
34    {
35        return SecuritySeverity::High;
36    }
37
38    if finding.source_backed
39        || finding
40            .reachability
41            .as_ref()
42            .is_some_and(|reach| reach.reachable_from_untrusted_source)
43    {
44        return SecuritySeverity::Medium;
45    }
46
47    SecuritySeverity::Low
48}
49
50/// Return the human-readable title for a security catalogue identifier,
51/// covering the standalone hardcoded-secret detector that has no catalogue
52/// matcher row.
53#[must_use]
54pub fn security_catalogue_title(kind: &str) -> Option<&'static str> {
55    if kind == HARDCODED_SECRET_CATEGORY_ID {
56        Some(HARDCODED_SECRET_CATEGORY_TITLE)
57    } else {
58        catalogue_title(kind)
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use std::path::PathBuf;
65
66    use fallow_types::{
67        output::IssueAction,
68        results::{
69            SecurityCandidate, SecurityCandidateBoundary, SecurityCandidateSink, SecurityFinding,
70            SecurityFindingKind, SecurityReachability, SecurityRuntimeContext,
71            SecurityRuntimeState, SecuritySeverity, SecurityZoneCrossing, TraceHop, TraceHopRole,
72        },
73    };
74
75    use super::derive_security_severity;
76
77    fn finding(name: &str) -> SecurityFinding {
78        let path = PathBuf::from("/repo").join(name);
79        SecurityFinding {
80            finding_id: String::new(),
81            stable_finding_id: None,
82            stable_id_input: None,
83            kind: SecurityFindingKind::TaintedSink,
84            category: Some("dangerous-html".to_string()),
85            cwe: Some(79),
86            path: path.clone(),
87            line: 1,
88            col: 0,
89            evidence: "candidate".to_string(),
90            source_backed: false,
91            source_read: None,
92            severity: SecuritySeverity::Low,
93            trace: vec![TraceHop::new(path.clone(), 1, 0, TraceHopRole::Sink)],
94            actions: Vec::<IssueAction>::new(),
95            dead_code: None,
96            reachability: None,
97            candidate: SecurityCandidate {
98                source_kind: None,
99                sink: SecurityCandidateSink {
100                    path,
101                    line: 1,
102                    col: 0,
103                    category: Some("dangerous-html".to_string()),
104                    cwe: Some(79),
105                    callee: None,
106                    url_shape: None,
107                },
108                boundary: SecurityCandidateBoundary::default(),
109                network: None,
110            },
111            taint_flow: None,
112            runtime: None,
113            attack_surface: None,
114        }
115    }
116
117    fn reachability(
118        reachable_from_entry: bool,
119        reachable_from_untrusted_source: bool,
120        crosses_boundary: bool,
121    ) -> SecurityReachability {
122        SecurityReachability {
123            reachable_from_entry,
124            reachable_from_untrusted_source,
125            taint_confidence: None,
126            untrusted_source_hop_count: None,
127            untrusted_source_trace: vec![],
128            blast_radius: 1,
129            crosses_boundary,
130        }
131    }
132
133    #[test]
134    fn derives_low_severity_for_baseline_candidate() {
135        assert_eq!(
136            derive_security_severity(&finding("sink.ts")),
137            SecuritySeverity::Low
138        );
139    }
140
141    #[test]
142    fn derives_medium_severity_for_source_signals() {
143        let mut source_backed = finding("source-backed.ts");
144        source_backed.source_backed = true;
145
146        let mut source_reachable = finding("source-reachable.ts");
147        source_reachable.reachability = Some(reachability(false, true, false));
148
149        assert_eq!(
150            derive_security_severity(&source_backed),
151            SecuritySeverity::Medium
152        );
153        assert_eq!(
154            derive_security_severity(&source_reachable),
155            SecuritySeverity::Medium
156        );
157    }
158
159    #[test]
160    fn derives_high_severity_for_boundary_entry_and_runtime_signals() {
161        let mut client_boundary = finding("client-boundary.ts");
162        client_boundary.candidate.boundary.client_server = true;
163
164        let mut architecture_boundary = finding("architecture-boundary.ts");
165        architecture_boundary.candidate.boundary.architecture_zone = Some(SecurityZoneCrossing {
166            from: "web".to_string(),
167            to: "server".to_string(),
168        });
169
170        let mut crossed_boundary = finding("crossed-boundary.ts");
171        crossed_boundary.reachability = Some(reachability(false, false, true));
172
173        let mut source_backed_entry = finding("source-backed-entry.ts");
174        source_backed_entry.source_backed = true;
175        source_backed_entry.reachability = Some(reachability(true, false, false));
176
177        let mut runtime_hot = finding("runtime-hot.ts");
178        runtime_hot.runtime = Some(SecurityRuntimeContext {
179            state: SecurityRuntimeState::RuntimeHot,
180            function: "handler".to_string(),
181            line: 1,
182            invocations: Some(500),
183            stable_id: Some("fallow:fn:test".to_string()),
184            evidence: Some("runtime hot path".to_string()),
185        });
186
187        for finding in [
188            client_boundary,
189            architecture_boundary,
190            crossed_boundary,
191            source_backed_entry,
192            runtime_hot,
193        ] {
194            assert_eq!(derive_security_severity(&finding), SecuritySeverity::High);
195        }
196    }
197}