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 {
94                path: path.clone(),
95                line: 1,
96                col: 0,
97                role: TraceHopRole::Sink,
98            }],
99            actions: Vec::<IssueAction>::new(),
100            dead_code: None,
101            reachability: None,
102            candidate: SecurityCandidate {
103                source_kind: None,
104                sink: SecurityCandidateSink {
105                    path,
106                    line: 1,
107                    col: 0,
108                    category: Some("dangerous-html".to_string()),
109                    cwe: Some(79),
110                    callee: None,
111                    url_shape: None,
112                },
113                boundary: SecurityCandidateBoundary::default(),
114                network: None,
115            },
116            taint_flow: None,
117            runtime: None,
118            attack_surface: None,
119        }
120    }
121
122    fn reachability(
123        reachable_from_entry: bool,
124        reachable_from_untrusted_source: bool,
125        crosses_boundary: bool,
126    ) -> SecurityReachability {
127        SecurityReachability {
128            reachable_from_entry,
129            reachable_from_untrusted_source,
130            taint_confidence: None,
131            untrusted_source_hop_count: None,
132            untrusted_source_trace: vec![],
133            blast_radius: 1,
134            crosses_boundary,
135        }
136    }
137
138    #[test]
139    fn derives_low_severity_for_baseline_candidate() {
140        assert_eq!(
141            derive_security_severity(&finding("sink.ts")),
142            SecuritySeverity::Low
143        );
144    }
145
146    #[test]
147    fn derives_medium_severity_for_source_signals() {
148        let mut source_backed = finding("source-backed.ts");
149        source_backed.source_backed = true;
150
151        let mut source_reachable = finding("source-reachable.ts");
152        source_reachable.reachability = Some(reachability(false, true, false));
153
154        assert_eq!(
155            derive_security_severity(&source_backed),
156            SecuritySeverity::Medium
157        );
158        assert_eq!(
159            derive_security_severity(&source_reachable),
160            SecuritySeverity::Medium
161        );
162    }
163
164    #[test]
165    fn derives_high_severity_for_boundary_entry_and_runtime_signals() {
166        let mut client_boundary = finding("client-boundary.ts");
167        client_boundary.candidate.boundary.client_server = true;
168
169        let mut architecture_boundary = finding("architecture-boundary.ts");
170        architecture_boundary.candidate.boundary.architecture_zone = Some(SecurityZoneCrossing {
171            from: "web".to_string(),
172            to: "server".to_string(),
173        });
174
175        let mut crossed_boundary = finding("crossed-boundary.ts");
176        crossed_boundary.reachability = Some(reachability(false, false, true));
177
178        let mut source_backed_entry = finding("source-backed-entry.ts");
179        source_backed_entry.source_backed = true;
180        source_backed_entry.reachability = Some(reachability(true, false, false));
181
182        let mut runtime_hot = finding("runtime-hot.ts");
183        runtime_hot.runtime = Some(SecurityRuntimeContext {
184            state: SecurityRuntimeState::RuntimeHot,
185            function: "handler".to_string(),
186            line: 1,
187            invocations: Some(500),
188            stable_id: Some("fallow:fn:test".to_string()),
189            evidence: Some("runtime hot path".to_string()),
190        });
191
192        for finding in [
193            client_boundary,
194            architecture_boundary,
195            crossed_boundary,
196            source_backed_entry,
197            runtime_hot,
198        ] {
199            assert_eq!(derive_security_severity(&finding), SecuritySeverity::High);
200        }
201    }
202}