-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.rs
More file actions
320 lines (300 loc) · 12.2 KB
/
Copy pathclassify.rs
File metadata and controls
320 lines (300 loc) · 12.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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.
///
/// The three-way output (Mitigable / Unmitigable / Informational) is
/// unchanged from #47 — both phantom variants classify as `Informational` —
/// but the rationale and suggested fix differ:
///
/// - [`ReachabilityStatus::PhantomDeclared`] — declared in Cargo.toml but
/// no `use` site. Strip the dep via `cargo machete --fix`. Canonical
/// case: file-soup#50.
/// - [`ReachabilityStatus::PhantomTransitive`] — not declared anywhere in
/// the workspace, pulled in by a parent dep. Local strip is impossible;
/// the fix requires bumping the parent (named in `evidence.parent_dep`
/// when identifiable). Track E found ~26 of 29 issues here.
///
/// Returns (classification, rationale, suggested_action).
pub fn classify(
vuln: &Vulnerability,
evidence: &ReachabilityEvidence,
) -> (Classification, String, String) {
match evidence.status {
// ─── Phantom + declared: manifest entry is genuinely unused ───
ReachabilityStatus::PhantomDeclared => (
Classification::Informational,
format!(
"{} {} is declared in Cargo.toml but never imported in any .rs file. \
The vulnerable code is compiled but unreachable. \
Stripping the dependency eliminates this CVE entirely.",
vuln.package, vuln.version
),
format!(
"Strip from Cargo.toml — run `cargo machete --fix` (or remove the \
dependency line manually) for `{}`.",
vuln.package
),
),
// ─── Phantom + transitive: pulled in by an upstream parent ───
ReachabilityStatus::PhantomTransitive => {
let parent_clause = match evidence.parent_dep.as_deref() {
Some(p) => format!("`{p}`"),
None => "an upstream parent dependency".to_string(),
};
(
Classification::Informational,
format!(
"{} {} is a transitive dependency (not declared in this project's \
Cargo.toml) and never imported in any .rs file. Pulled in by \
{parent_clause}. The vulnerable code is compiled but unreachable \
from this project; remediation lives upstream.",
vuln.package, vuln.version
),
format!(
"Pulled in transitively by {parent_clause} — fix requires bumping \
the parent dependency past the affected version of `{}`. No local \
strip is possible.",
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_declared_evidence() -> ReachabilityEvidence {
ReachabilityEvidence {
is_imported: false,
import_sites: vec![],
status: ReachabilityStatus::PhantomDeclared,
parent_dep: None,
}
}
fn phantom_transitive_evidence(parent: Option<&str>) -> ReachabilityEvidence {
ReachabilityEvidence {
is_imported: false,
import_sites: vec![],
status: ReachabilityStatus::PhantomTransitive,
parent_dep: parent.map(str::to_string),
}
}
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,
parent_dep: None,
}
}
#[test]
fn test_phantom_declared_recommends_machete_strip() {
// file-soup#50 shape: crate declared in Cargo.toml, no `use` site —
// strip the manifest entry.
let (cls, rationale, action) = classify(&mock_vuln(false, false), &phantom_declared_evidence());
assert_eq!(cls, Classification::Informational);
assert!(
action.contains("cargo machete --fix") || action.contains("Strip from Cargo.toml"),
"declared phantom should recommend strip, got: {action}"
);
assert!(
rationale.contains("declared in Cargo.toml"),
"rationale should explain declared status, got: {rationale}"
);
}
#[test]
fn test_phantom_transitive_recommends_parent_bump() {
// Track E shape: ~26 of 29 issues were misclassified as phantom-declared
// when they're actually transitive. The fix is to bump the parent, NOT
// to strip the local manifest.
let (cls, rationale, action) = classify(
&mock_vuln(false, false),
&phantom_transitive_evidence(Some("reqwest")),
);
assert_eq!(cls, Classification::Informational);
assert!(
!action.contains("cargo machete --fix"),
"transitive phantom must NOT recommend manifest strip, got: {action}"
);
assert!(
action.contains("Pulled in transitively"),
"transitive phantom action should label itself transitive, got: {action}"
);
assert!(
action.contains("`reqwest`"),
"transitive phantom action should name the parent dep, got: {action}"
);
assert!(
action.contains("bumping the parent"),
"transitive phantom action should suggest parent bump, got: {action}"
);
assert!(
rationale.contains("transitive dependency"),
"rationale should explain transitive status, got: {rationale}"
);
}
#[test]
fn test_phantom_transitive_unknown_parent_falls_back_gracefully() {
// Best-effort parent identification: if Cargo.lock didn't reveal one,
// we still produce useful output.
let (cls, rationale, action) = classify(
&mock_vuln(false, false),
&phantom_transitive_evidence(None),
);
assert_eq!(cls, Classification::Informational);
assert!(
action.contains("an upstream parent dependency"),
"unknown-parent transitive should fall back to generic phrasing, got: {action}"
);
assert!(rationale.contains("transitive dependency"));
}
#[test]
fn test_reachable_no_fix_is_unmitigable() {
let (cls, _, _) = classify(&mock_vuln(false, false), &reachable_evidence());
assert_eq!(cls, Classification::Unmitigable);
}
#[test]
fn test_reachable_semver_fix_is_mitigable() {
let (cls, _, action) = classify(&mock_vuln(true, true), &reachable_evidence());
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());
assert_eq!(cls, Classification::Mitigable);
assert!(action.contains("breaking change"));
}
#[test]
fn test_phantom_variants_both_classify_informational() {
// Three-way classifier output is unchanged from #47.
let (cls_decl, _, _) = classify(&mock_vuln(false, false), &phantom_declared_evidence());
let (cls_trans, _, _) = classify(&mock_vuln(false, false), &phantom_transitive_evidence(None));
assert_eq!(cls_decl, Classification::Informational);
assert_eq!(cls_trans, Classification::Informational);
}
}