-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.rs
More file actions
269 lines (251 loc) · 10.1 KB
/
Copy pathclassify.rs
File metadata and controls
269 lines (251 loc) · 10.1 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: MPL-2.0
//! Three-way CVE classification engine.
//!
//! Combines vulnerability data with reachability evidence to produce
//! one of three classifications:
//!
//! - **Mitigable**: A fix exists (semver-compatible or manual upgrade)
//! - **Unmitigable**: No fix available and dependency is reachable
//! - **Informational**: Dependency is phantom or unreachable
//!
//! Phase 2 will add **Concatenative** classification for CVE×CVE
//! interactions across shared trust boundaries.
use super::{Classification, ReachabilityEvidence, ReachabilityStatus, Vulnerability};
/// Classify a vulnerability given its reachability evidence.
///
/// `is_direct` indicates whether the vulnerable package appears as a key in
/// the project's `Cargo.toml` dependency tables. Phantom + transitive is
/// common (a vulnerable crate pulled in through the dep graph but never
/// imported in this project's source) and warrants a different remediation
/// path than phantom + direct (where the manifest entry is genuinely
/// unused). See #47.
///
/// Returns (classification, rationale, suggested_action).
pub fn classify(
vuln: &Vulnerability,
evidence: &ReachabilityEvidence,
is_direct: bool,
) -> (Classification, String, String) {
match evidence.status {
// ─── Phantom dependency: declared but never imported ───
ReachabilityStatus::Phantom if is_direct => (
Classification::Informational,
format!(
"{} {} is declared in Cargo.toml but never imported in any .rs file. \
The vulnerable code is compiled but unreachable. \
Removing the dependency from Cargo.toml eliminates this CVE entirely.",
vuln.package, vuln.version
),
format!(
"Remove unused dependency `{}` from Cargo.toml",
vuln.package
),
),
// ─── Phantom + transitive: pulled in by an upstream, not declared here ───
ReachabilityStatus::Phantom => (
Classification::Informational,
format!(
"{} {} is a transitive dependency (not declared in this project's Cargo.toml) \
and never imported in any .rs file. The vulnerable code is compiled but \
unreachable from this project. The CVE rides along through the dependency \
graph; remediation is upstream, not local.",
vuln.package, vuln.version
),
format!(
"Transitive — run `cargo update -p {}` to pull a non-vulnerable version if \
one is published, or upgrade the upstream crate that pulls it in. \
Otherwise informational: code unreachable from this project.",
vuln.package
),
),
// ─── Unreachable: imported but no taint flow (Phase 2) ───
ReachabilityStatus::Unreachable => (
Classification::Informational,
format!(
"{} {} is imported but no data flow reaches the vulnerable code path. \
(Note: Phase 2 kanren taint analysis will provide higher confidence.)",
vuln.package, vuln.version
),
"Monitor — no immediate action required".to_string(),
),
// ─── Reachable: imported and potentially exploitable ───
ReachabilityStatus::Reachable => classify_reachable(vuln, evidence),
}
}
/// Classify a reachable vulnerability as mitigable or unmitigable.
fn classify_reachable(
vuln: &Vulnerability,
evidence: &ReachabilityEvidence,
) -> (Classification, String, String) {
let import_summary = if evidence.import_sites.len() <= 3 {
evidence
.import_sites
.iter()
.map(|s| format!("{}:{}", s.file.display(), s.line))
.collect::<Vec<_>>()
.join(", ")
} else {
format!(
"{} and {} more",
evidence
.import_sites
.iter()
.take(2)
.map(|s| format!("{}:{}", s.file.display(), s.line))
.collect::<Vec<_>>()
.join(", "),
evidence.import_sites.len() - 2
)
};
if vuln.fixed_versions.is_empty() {
// No fix available — unmitigable
(
Classification::Unmitigable,
format!(
"{} {} has {} ({}) with NO upstream fix available. \
The dependency is imported at: {}. \
The vulnerable code is reachable in this project.",
vuln.package, vuln.version, vuln.id, vuln.summary, import_summary
),
format!(
"Replace `{}` with an alternative or accept the risk. \
No version upgrade can fix this.",
vuln.package
),
)
} else if vuln.semver_fix_available {
// Semver-compatible fix — easiest mitigation
let fix_version = vuln
.fixed_versions
.first()
.map(String::as_str)
.unwrap_or("unknown");
(
Classification::Mitigable,
format!(
"{} {} has {} ({}). \
A semver-compatible fix is available in version {}. \
Run `cargo update {}` to apply.",
vuln.package, vuln.version, vuln.id, vuln.summary, fix_version, vuln.package
),
format!("Run `cargo update {}`", vuln.package),
)
} else {
// Fix exists but requires major version bump
let fix_versions = vuln.fixed_versions.join(", ");
(
Classification::Mitigable,
format!(
"{} {} has {} ({}). \
Fix available in version(s) {} but requires a breaking upgrade. \
The dependency is imported at: {}.",
vuln.package, vuln.version, vuln.id, vuln.summary, fix_versions, import_summary
),
format!(
"Upgrade `{}` to {} in Cargo.toml (breaking change — review API differences)",
vuln.package, fix_versions
),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bridge::{ImportSite, SeverityLabel, SourceTier};
use std::path::PathBuf;
fn mock_vuln(has_fix: bool, semver_fix: bool) -> Vulnerability {
Vulnerability {
id: "RUSTSEC-2026-0001".to_string(),
cve: Some("CVE-2026-00001".to_string()),
summary: "Test vulnerability".to_string(),
package: "test-crate".to_string(),
version: "1.0.0".to_string(),
severity: Some(7.5),
severity_label: SeverityLabel::High,
fixed_versions: if has_fix {
vec!["1.0.1".to_string()]
} else {
vec![]
},
semver_fix_available: semver_fix,
source_tier: SourceTier::Tier1,
references: vec![],
}
}
fn phantom_evidence() -> ReachabilityEvidence {
ReachabilityEvidence {
is_imported: false,
import_sites: vec![],
status: ReachabilityStatus::Phantom,
}
}
fn reachable_evidence() -> ReachabilityEvidence {
ReachabilityEvidence {
is_imported: true,
import_sites: vec![ImportSite {
file: PathBuf::from("src/main.rs"),
line: 5,
statement: "use test_crate::Thing;".to_string(),
}],
status: ReachabilityStatus::Reachable,
}
}
#[test]
fn test_phantom_direct_recommends_removal() {
let (cls, _, action) = classify(&mock_vuln(false, false), &phantom_evidence(), true);
assert_eq!(cls, Classification::Informational);
assert!(
action.contains("Remove unused dependency"),
"direct phantom should recommend removal, got: {action}"
);
}
#[test]
fn test_phantom_transitive_recommends_cargo_update() {
// Regression for #47: phantom-classified transitive deps were
// incorrectly told to "Remove unused dependency from Cargo.toml".
let (cls, rationale, action) =
classify(&mock_vuln(false, false), &phantom_evidence(), false);
assert_eq!(cls, Classification::Informational);
assert!(
!action.contains("Remove unused dependency"),
"transitive phantom must NOT recommend manifest removal, got: {action}"
);
assert!(
action.contains("Transitive"),
"transitive phantom action should label itself transitive, got: {action}"
);
assert!(
action.contains("cargo update"),
"transitive phantom action should suggest cargo update, got: {action}"
);
assert!(
rationale.contains("transitive dependency"),
"rationale should explain transitive status, got: {rationale}"
);
}
#[test]
fn test_reachable_no_fix_is_unmitigable() {
let (cls, _, _) = classify(&mock_vuln(false, false), &reachable_evidence(), true);
assert_eq!(cls, Classification::Unmitigable);
}
#[test]
fn test_reachable_semver_fix_is_mitigable() {
let (cls, _, action) = classify(&mock_vuln(true, true), &reachable_evidence(), true);
assert_eq!(cls, Classification::Mitigable);
assert!(action.contains("cargo update"));
}
#[test]
fn test_reachable_breaking_fix_is_mitigable() {
let (cls, _, action) = classify(&mock_vuln(true, false), &reachable_evidence(), true);
assert_eq!(cls, Classification::Mitigable);
assert!(action.contains("breaking change"));
}
#[test]
fn test_reachable_classification_unaffected_by_is_direct() {
// is_direct is only used for the Phantom arm — reachable findings
// should classify identically regardless of manifest declaration.
let (cls_a, _, _) = classify(&mock_vuln(true, true), &reachable_evidence(), true);
let (cls_b, _, _) = classify(&mock_vuln(true, true), &reachable_evidence(), false);
assert_eq!(cls_a, cls_b);
}
}