sbom-tools 0.1.18

Semantic SBOM diff and analysis tool
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Rule engine for applying custom matching rules.
//!
//! This module provides the engine that applies custom matching rules
//! to components during the diff process.

use indexmap::IndexMap;
use regex::Regex;
use std::collections::{HashMap, HashSet};

use crate::model::{CanonicalId, Component};

use super::custom_rules::{AliasPattern, EquivalenceGroup, ExclusionRule, MatchingRulesConfig};

/// Result of applying matching rules to components
#[derive(Debug, Clone, Default)]
pub struct RuleApplicationResult {
    /// Original ID -> Canonical ID mapping (for equivalences)
    pub canonical_map: HashMap<CanonicalId, CanonicalId>,
    /// IDs that should be excluded from diff
    pub excluded: HashSet<CanonicalId>,
    /// Log of which rules were applied
    pub applied_rules: Vec<AppliedRule>,
}

/// Record of a rule being applied to a component
#[derive(Debug, Clone)]
pub struct AppliedRule {
    /// The component that was affected
    pub component_id: CanonicalId,
    /// The component name
    pub component_name: String,
    /// The type of rule applied
    pub rule_type: AppliedRuleType,
    /// Index of the rule in the config
    pub rule_index: usize,
    /// Name of the rule (if any)
    pub rule_name: Option<String>,
}

/// Type of rule that was applied
#[derive(Debug, Clone)]
pub enum AppliedRuleType {
    /// Component was mapped to a canonical ID
    Equivalence { canonical: String },
    /// Component was excluded
    Exclusion { reason: Option<String> },
}

/// Engine for applying custom matching rules
pub struct RuleEngine {
    config: MatchingRulesConfig,
    /// Compiled regex patterns for exclusions
    compiled_exclusion_regexes: Vec<Option<Regex>>,
    /// Compiled glob patterns for exclusions (converted to regex)
    compiled_exclusion_globs: Vec<Option<Regex>>,
    /// Compiled regex patterns for equivalence aliases
    compiled_alias_regexes: Vec<Vec<Option<Regex>>>,
    /// Compiled glob patterns for equivalence aliases (converted to regex)
    compiled_alias_globs: Vec<Vec<Option<Regex>>>,
}

impl RuleEngine {
    /// Create a new rule engine from configuration
    pub fn new(config: MatchingRulesConfig) -> Result<Self, String> {
        // Pre-compile regex patterns for exclusions
        let compiled_exclusion_regexes = config
            .exclusions
            .iter()
            .map(|rule| match rule {
                ExclusionRule::Exact(_) => Ok(None),
                ExclusionRule::Conditional { regex, .. } => regex.as_ref().map_or_else(
                    || Ok(None),
                    |re| {
                        Regex::new(re)
                            .map(Some)
                            .map_err(|e| format!("Invalid exclusion regex '{re}': {e}"))
                    },
                ),
            })
            .collect::<Result<Vec<_>, _>>()?;

        // Pre-compile glob patterns for exclusions
        let compiled_exclusion_globs = config
            .exclusions
            .iter()
            .map(|rule| match rule {
                ExclusionRule::Exact(_) => Ok(None),
                ExclusionRule::Conditional { pattern, .. } => pattern
                    .as_ref()
                    .map_or_else(|| Ok(None), |pat| compile_glob(pat).map(Some)),
            })
            .collect::<Result<Vec<_>, _>>()?;

        // Pre-compile regex patterns for equivalence aliases
        let compiled_alias_regexes = config
            .equivalences
            .iter()
            .map(|eq| {
                eq.aliases
                    .iter()
                    .map(|alias| match alias {
                        AliasPattern::Exact(_) => Ok(None),
                        AliasPattern::Pattern { regex, .. } => regex.as_ref().map_or_else(
                            || Ok(None),
                            |re| {
                                Regex::new(re)
                                    .map(Some)
                                    .map_err(|e| format!("Invalid alias regex '{re}': {e}"))
                            },
                        ),
                    })
                    .collect::<Result<Vec<_>, _>>()
            })
            .collect::<Result<Vec<_>, _>>()?;

        // Pre-compile glob patterns for equivalence aliases
        let compiled_alias_globs = config
            .equivalences
            .iter()
            .map(|eq| {
                eq.aliases
                    .iter()
                    .map(|alias| match alias {
                        AliasPattern::Exact(_) => Ok(None),
                        AliasPattern::Pattern { pattern, .. } => pattern
                            .as_ref()
                            .map_or_else(|| Ok(None), |pat| compile_glob(pat).map(Some)),
                    })
                    .collect::<Result<Vec<_>, _>>()
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Self {
            config,
            compiled_exclusion_regexes,
            compiled_exclusion_globs,
            compiled_alias_regexes,
            compiled_alias_globs,
        })
    }

    /// Apply rules to a set of components
    #[must_use]
    pub fn apply(&self, components: &IndexMap<CanonicalId, Component>) -> RuleApplicationResult {
        let mut result = RuleApplicationResult::default();

        for (id, component) in components {
            // Check exclusions first
            if let Some(applied) = self.check_exclusions(id, component) {
                result.excluded.insert(id.clone());
                result.applied_rules.push(applied);
                continue;
            }

            // Check equivalences
            if let Some((canonical_id, applied)) = self.check_equivalences(id, component) {
                result.canonical_map.insert(id.clone(), canonical_id);
                result.applied_rules.push(applied);
            }
        }

        result
    }

    /// Check if a component should be excluded
    fn check_exclusions(&self, id: &CanonicalId, component: &Component) -> Option<AppliedRule> {
        for (idx, rule) in self.config.exclusions.iter().enumerate() {
            if self.exclusion_matches(rule, idx, component) {
                return Some(AppliedRule {
                    component_id: id.clone(),
                    component_name: component.name.clone(),
                    rule_type: AppliedRuleType::Exclusion {
                        reason: rule.get_reason().map(std::string::ToString::to_string),
                    },
                    rule_index: idx,
                    rule_name: None,
                });
            }
        }
        None
    }

    /// Check if an exclusion rule matches a component
    fn exclusion_matches(
        &self,
        rule: &ExclusionRule,
        rule_idx: usize,
        component: &Component,
    ) -> bool {
        match rule {
            ExclusionRule::Exact(purl) => component
                .identifiers
                .purl
                .as_ref()
                .is_some_and(|p| p == purl),
            ExclusionRule::Conditional {
                pattern,
                regex: _,
                ecosystem,
                name,
                scope: _,
                reason: _,
            } => {
                // Check ecosystem
                if let Some(eco) = ecosystem {
                    let comp_eco = component
                        .ecosystem
                        .as_ref()
                        .map(|e| e.to_string().to_lowercase());
                    if comp_eco.as_deref() != Some(&eco.to_lowercase()) {
                        return false;
                    }
                }

                // Check name
                if let Some(n) = name
                    && !component.name.to_lowercase().contains(&n.to_lowercase())
                {
                    return false;
                }

                // Check pre-compiled glob pattern
                if pattern.is_some() {
                    if let Some(purl) = &component.identifiers.purl {
                        if let Some(Some(re)) = self.compiled_exclusion_globs.get(rule_idx)
                            && !re.is_match(purl)
                        {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }

                // Check compiled regex
                if let Some(Some(re)) = self.compiled_exclusion_regexes.get(rule_idx) {
                    if let Some(purl) = &component.identifiers.purl {
                        if !re.is_match(purl) {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }

                // If we get here and at least one condition was specified, it matched
                ecosystem.is_some()
                    || name.is_some()
                    || pattern.is_some()
                    || self
                        .compiled_exclusion_regexes
                        .get(rule_idx)
                        .is_some_and(std::option::Option::is_some)
            }
        }
    }

    /// Check if a component matches any equivalence group
    fn check_equivalences(
        &self,
        id: &CanonicalId,
        component: &Component,
    ) -> Option<(CanonicalId, AppliedRule)> {
        let purl = component.identifiers.purl.as_ref()?;

        for (eq_idx, eq) in self.config.equivalences.iter().enumerate() {
            // Check if the PURL matches the canonical or any alias
            let matches_canonical = purl == &eq.canonical;
            let matches_alias = self.alias_matches(eq_idx, eq, purl);

            if matches_canonical || matches_alias {
                let canonical_id = CanonicalId::from_purl(&eq.canonical);
                let applied = AppliedRule {
                    component_id: id.clone(),
                    component_name: component.name.clone(),
                    rule_type: AppliedRuleType::Equivalence {
                        canonical: eq.canonical.clone(),
                    },
                    rule_index: eq_idx,
                    rule_name: eq.name.clone(),
                };
                return Some((canonical_id, applied));
            }
        }

        None
    }

    /// Check if a PURL matches any alias in an equivalence group
    fn alias_matches(&self, eq_idx: usize, eq: &EquivalenceGroup, purl: &str) -> bool {
        let alias_regexes = self.compiled_alias_regexes.get(eq_idx);
        let alias_globs = self.compiled_alias_globs.get(eq_idx);

        for (alias_idx, alias) in eq.aliases.iter().enumerate() {
            let matches = match alias {
                AliasPattern::Exact(exact_purl) => purl == exact_purl,
                AliasPattern::Pattern {
                    pattern: _,
                    regex: _,
                    ecosystem,
                    name,
                } => {
                    let mut matched = false;

                    // Check pre-compiled glob pattern
                    if let Some(Some(re)) = alias_globs.and_then(|v| v.get(alias_idx))
                        && re.is_match(purl)
                    {
                        matched = true;
                    }

                    // Check regex
                    if let Some(Some(re)) = alias_regexes.and_then(|v| v.get(alias_idx))
                        && re.is_match(purl)
                    {
                        matched = true;
                    }

                    // Check ecosystem match in PURL
                    if let Some(eco) = ecosystem {
                        let purl_lower = purl.to_lowercase();
                        let eco_lower = eco.to_lowercase();
                        // Check if PURL starts with pkg:<ecosystem>/
                        if purl_lower.starts_with("pkg:")
                            && let Some(rest) = purl_lower.strip_prefix("pkg:")
                            && rest.starts_with(&eco_lower)
                            && rest[eco_lower.len()..].starts_with('/')
                        {
                            matched = true;
                        }
                    }

                    // Check name match in PURL
                    if let Some(n) = name
                        && purl.to_lowercase().contains(&n.to_lowercase())
                    {
                        matched = true;
                    }

                    matched
                }
            };

            if matches {
                return true;
            }
        }

        false
    }

    /// Get the configuration
    #[must_use]
    pub const fn config(&self) -> &MatchingRulesConfig {
        &self.config
    }

    /// Check if a PURL is excluded by any rule
    #[must_use]
    pub fn is_excluded(&self, purl: &str) -> bool {
        for (idx, rule) in self.config.exclusions.iter().enumerate() {
            match rule {
                ExclusionRule::Exact(exact) => {
                    if purl == exact {
                        return true;
                    }
                }
                ExclusionRule::Conditional { pattern, .. } => {
                    // Check pre-compiled glob pattern
                    if pattern.is_some()
                        && let Some(Some(re)) = self.compiled_exclusion_globs.get(idx)
                        && re.is_match(purl)
                    {
                        return true;
                    }
                    // Check pre-compiled regex
                    if let Some(Some(re)) = self.compiled_exclusion_regexes.get(idx)
                        && re.is_match(purl)
                    {
                        return true;
                    }
                }
            }
        }
        false
    }

    /// Get the canonical PURL for a given PURL, if any equivalence applies
    #[must_use]
    pub fn get_canonical(&self, purl: &str) -> Option<String> {
        for (eq_idx, eq) in self.config.equivalences.iter().enumerate() {
            if purl == eq.canonical {
                return Some(eq.canonical.clone());
            }
            if self.alias_matches(eq_idx, eq, purl) {
                return Some(eq.canonical.clone());
            }
        }
        None
    }
}

/// Compile a glob pattern to a regex at construction time.
fn compile_glob(pattern: &str) -> Result<Regex, String> {
    let regex_pattern = pattern
        .replace('.', "\\.")
        .replace('*', ".*")
        .replace('?', ".");

    Regex::new(&format!("^{regex_pattern}$"))
        .map_err(|e| format!("Invalid glob pattern '{pattern}': {e}"))
}

/// Simple glob pattern matching (supports * and ?) - used only in tests
#[cfg(test)]
fn glob_matches(pattern: &str, text: &str) -> bool {
    compile_glob(pattern)
        .map(|re| re.is_match(text))
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_component(name: &str, purl: Option<&str>) -> Component {
        use crate::model::*;
        let mut comp = Component::new(name.to_string(), purl.unwrap_or(name).to_string());
        comp.version = Some("1.0.0".to_string());
        comp.identifiers.purl = purl.map(|s| s.to_string());
        comp.ecosystem = Some(Ecosystem::Npm);
        comp
    }

    #[test]
    fn test_glob_matches() {
        assert!(glob_matches("pkg:npm/*", "pkg:npm/lodash"));
        assert!(glob_matches("pkg:npm/lodash*", "pkg:npm/lodash-es"));
        assert!(!glob_matches("pkg:npm/*", "pkg:maven/test"));
        assert!(glob_matches("*.json", "test.json"));
    }

    #[test]
    fn test_exact_exclusion() {
        let config = MatchingRulesConfig {
            exclusions: vec![ExclusionRule::exact("pkg:npm/jest")],
            ..Default::default()
        };
        let engine = RuleEngine::new(config).unwrap();

        assert!(engine.is_excluded("pkg:npm/jest"));
        assert!(!engine.is_excluded("pkg:npm/lodash"));
    }

    #[test]
    fn test_pattern_exclusion() {
        let config = MatchingRulesConfig {
            exclusions: vec![ExclusionRule::pattern("pkg:npm/test-*")],
            ..Default::default()
        };
        let engine = RuleEngine::new(config).unwrap();

        assert!(engine.is_excluded("pkg:npm/test-utils"));
        assert!(engine.is_excluded("pkg:npm/test-runner"));
        assert!(!engine.is_excluded("pkg:npm/lodash"));
    }

    #[test]
    fn test_equivalence_matching() {
        let config = MatchingRulesConfig {
            equivalences: vec![EquivalenceGroup {
                name: Some("Lodash".to_string()),
                canonical: "pkg:npm/lodash".to_string(),
                aliases: vec![
                    AliasPattern::exact("pkg:npm/lodash-es"),
                    AliasPattern::glob("pkg:npm/lodash.*"),
                ],
                version_sensitive: false,
            }],
            ..Default::default()
        };
        let engine = RuleEngine::new(config).unwrap();

        assert_eq!(
            engine.get_canonical("pkg:npm/lodash"),
            Some("pkg:npm/lodash".to_string())
        );
        assert_eq!(
            engine.get_canonical("pkg:npm/lodash-es"),
            Some("pkg:npm/lodash".to_string())
        );
        assert_eq!(
            engine.get_canonical("pkg:npm/lodash.min"),
            Some("pkg:npm/lodash".to_string())
        );
        assert_eq!(engine.get_canonical("pkg:npm/underscore"), None);
    }

    #[test]
    fn test_apply_rules() {
        let config = MatchingRulesConfig {
            equivalences: vec![EquivalenceGroup {
                name: Some("Lodash".to_string()),
                canonical: "pkg:npm/lodash".to_string(),
                aliases: vec![AliasPattern::exact("pkg:npm/lodash-es")],
                version_sensitive: false,
            }],
            exclusions: vec![ExclusionRule::exact("pkg:npm/jest")],
            ..Default::default()
        };
        let engine = RuleEngine::new(config).unwrap();

        let mut components = IndexMap::new();
        components.insert(
            CanonicalId::from_purl("pkg:npm/lodash-es"),
            create_test_component("lodash-es", Some("pkg:npm/lodash-es")),
        );
        components.insert(
            CanonicalId::from_purl("pkg:npm/jest"),
            create_test_component("jest", Some("pkg:npm/jest")),
        );
        components.insert(
            CanonicalId::from_purl("pkg:npm/react"),
            create_test_component("react", Some("pkg:npm/react")),
        );

        let result = engine.apply(&components);

        // lodash-es should be mapped to canonical lodash
        assert!(
            result
                .canonical_map
                .contains_key(&CanonicalId::from_purl("pkg:npm/lodash-es"))
        );

        // jest should be excluded
        assert!(
            result
                .excluded
                .contains(&CanonicalId::from_purl("pkg:npm/jest"))
        );

        // react should have no rules applied
        assert!(
            !result
                .canonical_map
                .contains_key(&CanonicalId::from_purl("pkg:npm/react"))
        );
        assert!(
            !result
                .excluded
                .contains(&CanonicalId::from_purl("pkg:npm/react"))
        );

        // Check applied rules
        assert_eq!(result.applied_rules.len(), 2);
    }
}