1use std::collections::{HashMap, HashSet, VecDeque};
11
12use crate::model::{CanonicalId, NormalizedSbom};
13
14use super::result::{
15 DependencyChangeType, DependencyGraphChange, GraphChangeImpact, GraphChangeSummary,
16};
17
18#[derive(Debug, Clone)]
20pub struct GraphDiffConfig {
21 pub detect_reparenting: bool,
23 pub detect_depth_changes: bool,
25 pub max_depth: u32,
27}
28
29impl Default for GraphDiffConfig {
30 fn default() -> Self {
31 Self {
32 detect_reparenting: true,
33 detect_depth_changes: true,
34 max_depth: 0,
35 }
36 }
37}
38
39struct DependencyGraph<'a> {
41 sbom: &'a NormalizedSbom,
43 edges: HashMap<CanonicalId, Vec<CanonicalId>>,
45 reverse_edges: HashMap<CanonicalId, Vec<CanonicalId>>,
47 depths: HashMap<CanonicalId, u32>,
50 vulnerable_components: HashSet<CanonicalId>,
52}
53
54impl<'a> DependencyGraph<'a> {
55 fn from_sbom(sbom: &'a NormalizedSbom, config: &GraphDiffConfig) -> Self {
56 let mut edges: HashMap<CanonicalId, Vec<CanonicalId>> = HashMap::new();
57 let mut reverse_edges: HashMap<CanonicalId, Vec<CanonicalId>> = HashMap::new();
58 let mut vulnerable_components = HashSet::new();
59
60 for edge in &sbom.edges {
62 edges
63 .entry(edge.from.clone())
64 .or_default()
65 .push(edge.to.clone());
66
67 reverse_edges
68 .entry(edge.to.clone())
69 .or_default()
70 .push(edge.from.clone());
71 }
72
73 for (id, comp) in &sbom.components {
75 if !comp.vulnerabilities.is_empty() {
76 vulnerable_components.insert(id.clone());
77 }
78 }
79
80 let all_components: HashSet<_> = sbom.components.keys().cloned().collect();
82 let depths =
83 Self::calculate_depths(&edges, &reverse_edges, &all_components, config.max_depth);
84
85 Self {
86 sbom,
87 edges,
88 reverse_edges,
89 depths,
90 vulnerable_components,
91 }
92 }
93
94 fn calculate_depths(
100 edges: &HashMap<CanonicalId, Vec<CanonicalId>>,
101 reverse_edges: &HashMap<CanonicalId, Vec<CanonicalId>>,
102 all_components: &HashSet<CanonicalId>,
103 max_depth: u32,
104 ) -> HashMap<CanonicalId, u32> {
105 let mut depths = HashMap::new();
106
107 let mut queue: VecDeque<(CanonicalId, u32)> = all_components
110 .iter()
111 .filter(|id| reverse_edges.get(*id).is_none_or(std::vec::Vec::is_empty))
112 .cloned()
113 .map(|id| (id, 1))
114 .collect();
115
116 while let Some((id, depth)) = queue.pop_front() {
117 if let Some(&existing_depth) = depths.get(&id)
119 && depth >= existing_depth {
120 continue; }
122
123 depths.insert(id.clone(), depth);
125
126 if max_depth > 0 && depth >= max_depth {
128 continue;
129 }
130
131 if let Some(children) = edges.get(&id) {
132 for child_id in children {
133 let child_depth = depth + 1;
134 let dominated = depths.get(child_id).is_some_and(|&d| d <= child_depth);
136 if !dominated {
137 queue.push_back((child_id.clone(), child_depth));
138 }
139 }
140 }
141 }
142
143 depths
144 }
145
146 fn get_parents(&self, component_id: &CanonicalId) -> Vec<CanonicalId> {
147 self.reverse_edges
148 .get(component_id)
149 .cloned()
150 .unwrap_or_default()
151 }
152
153 fn get_children(&self, component_id: &CanonicalId) -> Vec<CanonicalId> {
154 self.edges.get(component_id).cloned().unwrap_or_default()
155 }
156
157 fn get_depth(&self, component_id: &CanonicalId) -> Option<u32> {
158 self.depths.get(component_id).copied()
159 }
160
161 fn is_vulnerable(&self, component_id: &CanonicalId) -> bool {
162 self.vulnerable_components.contains(component_id)
163 }
164
165 fn get_component_name(&self, component_id: &CanonicalId) -> String {
166 self.sbom
167 .components
168 .get(component_id)
169 .map_or_else(
170 || component_id.to_string(),
171 |c| c.version.as_ref().map_or_else(|| c.name.clone(), |v| format!("{}@{}", c.name, v)),
172 )
173 }
174}
175
176#[allow(clippy::implicit_hasher)]
178#[must_use]
179pub fn diff_dependency_graph(
180 old_sbom: &NormalizedSbom,
181 new_sbom: &NormalizedSbom,
182 component_matches: &HashMap<CanonicalId, Option<CanonicalId>>,
183 config: &GraphDiffConfig,
184) -> (Vec<DependencyGraphChange>, GraphChangeSummary) {
185 let old_graph = DependencyGraph::from_sbom(old_sbom, config);
186 let new_graph = DependencyGraph::from_sbom(new_sbom, config);
187
188 let mut changes = Vec::new();
189
190 for (old_id, new_id_option) in component_matches {
192 if let Some(new_id) = new_id_option {
193 let component_name = new_graph.get_component_name(new_id);
194
195 let old_children: HashSet<_> = old_graph.get_children(old_id).into_iter().collect();
197 let new_children: HashSet<_> = new_graph.get_children(new_id).into_iter().collect();
198
199 for child_id in new_children.difference(&old_children) {
201 let dep_name = new_graph.get_component_name(child_id);
202 let impact = assess_impact_added(&new_graph, child_id);
203
204 changes.push(DependencyGraphChange {
205 component_id: new_id.clone(),
206 component_name: component_name.clone(),
207 change: DependencyChangeType::DependencyAdded {
208 dependency_id: child_id.clone(),
209 dependency_name: dep_name,
210 },
211 impact,
212 });
213 }
214
215 for child_id in old_children.difference(&new_children) {
217 let dep_name = old_graph.get_component_name(child_id);
218
219 changes.push(DependencyGraphChange {
220 component_id: new_id.clone(),
221 component_name: component_name.clone(),
222 change: DependencyChangeType::DependencyRemoved {
223 dependency_id: child_id.clone(),
224 dependency_name: dep_name,
225 },
226 impact: GraphChangeImpact::Low,
227 });
228 }
229 }
230 }
231
232 if config.detect_depth_changes {
234 detect_depth_changes(&old_graph, &new_graph, component_matches, &mut changes);
235 }
236
237 if config.detect_reparenting {
239 detect_reparenting(&old_graph, &new_graph, component_matches, &mut changes);
240 }
241
242 changes.sort_by(|a, b| {
244 let impact_order = |i: &GraphChangeImpact| match i {
245 GraphChangeImpact::Critical => 0,
246 GraphChangeImpact::High => 1,
247 GraphChangeImpact::Medium => 2,
248 GraphChangeImpact::Low => 3,
249 };
250 impact_order(&a.impact).cmp(&impact_order(&b.impact))
251 });
252
253 let summary = GraphChangeSummary::from_changes(&changes);
254 (changes, summary)
255}
256
257fn assess_impact_added(graph: &DependencyGraph, component_id: &CanonicalId) -> GraphChangeImpact {
259 if graph.is_vulnerable(component_id) {
260 if graph.get_depth(component_id) == Some(1) {
261 GraphChangeImpact::Critical
262 } else {
263 GraphChangeImpact::High
264 }
265 } else if graph.get_depth(component_id) == Some(1) {
266 GraphChangeImpact::Medium
267 } else {
268 GraphChangeImpact::Low
269 }
270}
271
272fn detect_depth_changes(
274 old_graph: &DependencyGraph,
275 new_graph: &DependencyGraph,
276 matches: &HashMap<CanonicalId, Option<CanonicalId>>,
277 changes: &mut Vec<DependencyGraphChange>,
278) {
279 for (old_id, new_id_opt) in matches {
280 if let Some(new_id) = new_id_opt {
281 let old_depth = old_graph.get_depth(old_id);
282 let new_depth = new_graph.get_depth(new_id);
283
284 if let (Some(od), Some(nd)) = (old_depth, new_depth)
285 && od != nd {
286 let component_name = new_graph.get_component_name(new_id);
287
288 let impact = if nd < od && new_graph.is_vulnerable(new_id) {
289 GraphChangeImpact::High
291 } else if nd == 1 && od > 1 {
292 GraphChangeImpact::Medium
294 } else {
295 GraphChangeImpact::Low
296 };
297
298 changes.push(DependencyGraphChange {
299 component_id: new_id.clone(),
300 component_name,
301 change: DependencyChangeType::DepthChanged {
302 old_depth: od,
303 new_depth: nd,
304 },
305 impact,
306 });
307 }
308 }
309 }
310}
311
312fn detect_reparenting(
314 old_graph: &DependencyGraph,
315 new_graph: &DependencyGraph,
316 matches: &HashMap<CanonicalId, Option<CanonicalId>>,
317 changes: &mut Vec<DependencyGraphChange>,
318) {
319 for (old_id, new_id_opt) in matches {
320 if let Some(new_id) = new_id_opt {
321 let old_parents = old_graph.get_parents(old_id);
322 let new_parents = new_graph.get_parents(new_id);
323
324 if old_parents.len() == 1 && new_parents.len() == 1 {
326 let old_parent = &old_parents[0];
327 let new_parent = &new_parents[0];
328
329 let old_parent_matched = matches.get(old_parent).and_then(|opt| opt.as_ref());
331
332 let is_reparented = !old_parent_matched.is_some_and(|old_parent_in_new| old_parent_in_new == new_parent);
333
334 if is_reparented {
335 let component_name = new_graph.get_component_name(new_id);
336 let old_parent_name = old_graph.get_component_name(old_parent);
337 let new_parent_name = new_graph.get_component_name(new_parent);
338
339 changes.retain(|c| match &c.change {
342 DependencyChangeType::DependencyAdded { dependency_id, .. }
343 | DependencyChangeType::DependencyRemoved { dependency_id, .. } => {
344 dependency_id != new_id
345 }
346 _ => true,
347 });
348
349 changes.push(DependencyGraphChange {
350 component_id: new_id.clone(),
351 component_name,
352 change: DependencyChangeType::Reparented {
353 dependency_id: new_id.clone(),
354 dependency_name: new_graph.get_component_name(new_id),
355 old_parent_id: old_parent.clone(),
356 old_parent_name,
357 new_parent_id: new_parent.clone(),
358 new_parent_name,
359 },
360 impact: GraphChangeImpact::Medium,
361 });
362 }
363 }
364 }
365 }
366}
367
368#[cfg(test)]
369mod tests {
370 use super::*;
371
372 #[test]
373 fn test_graph_diff_config_default() {
374 let config = GraphDiffConfig::default();
375 assert!(config.detect_reparenting);
376 assert!(config.detect_depth_changes);
377 assert_eq!(config.max_depth, 0);
378 }
379
380 #[test]
381 fn test_graph_change_impact_display() {
382 assert_eq!(GraphChangeImpact::Critical.as_str(), "critical");
383 assert_eq!(GraphChangeImpact::High.as_str(), "high");
384 assert_eq!(GraphChangeImpact::Medium.as_str(), "medium");
385 assert_eq!(GraphChangeImpact::Low.as_str(), "low");
386 }
387}