Skip to main content

sbom_tools/diff/
graph.rs

1//! Graph-aware dependency diffing module.
2//!
3//! This module provides functionality to detect structural changes in the
4//! dependency graph between two SBOMs, going beyond simple component-level
5//! comparisons to identify:
6//! - Dependencies being added or removed
7//! - Dependencies being reparented (moved from one parent to another)
8//! - Depth changes (transitive becoming direct or vice versa)
9
10use std::collections::{HashMap, HashSet, VecDeque};
11
12use crate::model::{CanonicalId, NormalizedSbom};
13
14use super::result::{
15    DependencyChangeType, DependencyGraphChange, GraphChangeImpact, GraphChangeSummary,
16};
17
18/// Configuration for graph-aware diffing
19#[derive(Debug, Clone)]
20pub struct GraphDiffConfig {
21    /// Whether to detect reparenting (computationally more expensive)
22    pub detect_reparenting: bool,
23    /// Whether to track depth changes
24    pub detect_depth_changes: bool,
25    /// Maximum depth to analyze (0 = unlimited)
26    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
39/// Internal representation of dependency graph for diffing
40struct DependencyGraph<'a> {
41    /// Reference to the SBOM
42    sbom: &'a NormalizedSbom,
43    /// `parent_id` -> Vec<`child_id`>
44    edges: HashMap<CanonicalId, Vec<CanonicalId>>,
45    /// `child_id` -> Vec<`parent_id`> (reverse index)
46    reverse_edges: HashMap<CanonicalId, Vec<CanonicalId>>,
47    /// `component_id` -> minimum depth from root (1 = direct)
48    /// Uses minimum depth when multiple paths exist (diamond dependencies)
49    depths: HashMap<CanonicalId, u32>,
50    /// `component_id` -> has vulnerabilities
51    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        // Build edge maps from SBOM edges
61        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        // Identify vulnerable components
74        for (id, comp) in &sbom.components {
75            if !comp.vulnerabilities.is_empty() {
76                vulnerable_components.insert(id.clone());
77            }
78        }
79
80        // Calculate depths via BFS from roots (respecting max_depth limit)
81        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    /// Calculate minimum depths from roots via BFS.
95    ///
96    /// Uses minimum depth when multiple paths exist (diamond dependencies),
97    /// which gives the most accurate "direct vs transitive" classification.
98    /// Respects `max_depth` limit (0 = unlimited).
99    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        // BFS to calculate minimum depths
108        // We use a queue that may revisit nodes if a shorter path is found
109        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            // Check if we've found a shorter path to this node
118            if let Some(&existing_depth) = depths.get(&id)
119                && depth >= existing_depth {
120                    continue; // Already have a shorter or equal path
121                }
122
123            // Record this depth (it's either new or shorter than existing)
124            depths.insert(id.clone(), depth);
125
126            // Stop traversing if we've reached max_depth (0 = unlimited)
127            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                    // Only queue if this might be a shorter path
135                    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/// Perform graph-aware diff between two SBOMs
177#[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    // Iterate through matched components to find dependency changes
191    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            // Get children in both graphs
196            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            // Detect added dependencies
200            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            // Detect removed dependencies
216            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    // Detect depth changes
233    if config.detect_depth_changes {
234        detect_depth_changes(&old_graph, &new_graph, component_matches, &mut changes);
235    }
236
237    // Detect reparenting (post-process to find moved dependencies)
238    if config.detect_reparenting {
239        detect_reparenting(&old_graph, &new_graph, component_matches, &mut changes);
240    }
241
242    // Sort changes by impact (critical first)
243    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
257/// Assess the impact of adding a dependency
258fn 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
272/// Detect depth changes between matched components
273fn 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                        // Vulnerable component moved closer to root
290                        GraphChangeImpact::High
291                    } else if nd == 1 && od > 1 {
292                        // Became direct dependency
293                        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
312/// Detect reparented components (moved from one parent to another)
313fn 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            // Only consider reparenting if exactly one parent in both
325            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                // Check if the parents are different (accounting for component matching)
330                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                    // Remove any corresponding Added/Removed entries for this component
340                    // as they will be replaced by the Reparented entry
341                    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}