vulnera-advisor 0.1.7

Aggregates security advisories from GHSA, NVD, OSV, CISA KEV, and more
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
//! Package URL (PURL) builder and parser.
//!
//! Provides a convenient way to construct and parse Package URLs
//! following the [PURL specification](https://github.com/package-url/purl-spec).
//!
//! # Example
//!
//! ```rust
//! use vulnera_advisor::Purl;
//!
//! // Simple PURL
//! let purl = Purl::new("npm", "lodash")
//!     .with_version("4.17.20")
//!     .to_string();
//! assert_eq!(purl, "pkg:npm/lodash@4.17.20");
//!
//! // Maven with namespace (groupId)
//! let purl = Purl::new("maven", "spring-core")
//!     .with_namespace("org.springframework")
//!     .with_version("5.3.9")
//!     .to_string();
//! assert_eq!(purl, "pkg:maven/org.springframework/spring-core@5.3.9");
//! ```

use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};

use crate::ecosystem::canonicalize_ecosystem;

/// Known valid PURL ecosystem types.
///
/// This list includes all ecosystems supported by OSS Index and other
/// vulnerability databases.
pub const KNOWN_ECOSYSTEMS: &[&str] = &[
    "cargo",     // Rust crates
    "cocoapods", // iOS/macOS CocoaPods
    "composer",  // PHP Composer
    "conan",     // C/C++ Conan
    "conda",     // Conda packages
    "cran",      // R packages
    "deb",       // Debian packages
    "gem",       // Ruby gems
    "generic",   // Generic packages
    "github",    // GitHub repositories
    "golang",    // Go modules
    "hex",       // Erlang/Elixir Hex
    "maven",     // Java Maven
    "npm",       // Node.js npm
    "nuget",     // .NET NuGet
    "pub",       // Dart/Flutter pub
    "pypi",      // Python PyPI
    "rpm",       // RPM packages
    "swift",     // Swift packages
];

/// Ecosystem name mappings from common names to PURL types.
/// Some ecosystems use different names in PURL vs common usage.
const ECOSYSTEM_MAPPINGS: &[(&str, &str)] = &[
    ("crates.io", "cargo"),
    ("PyPI", "pypi"),
    ("RubyGems", "gem"),
    ("Go", "golang"),
    ("Packagist", "composer"),
    ("NuGet", "nuget"),
    ("Hex", "hex"),
    ("Pub", "pub"),
];

/// Error returned when PURL validation fails.
#[derive(Debug, Clone, thiserror::Error)]
pub enum PurlError {
    /// The ecosystem/type is not recognized.
    #[error("Unknown ecosystem '{0}'. Known ecosystems: cargo, npm, pypi, maven, etc.")]
    UnknownEcosystem(String),

    /// The PURL string format is invalid.
    #[error("Invalid PURL format: {0}")]
    InvalidFormat(String),

    /// The package name is empty or invalid.
    #[error("Invalid package name: {0}")]
    InvalidName(String),
}

/// A Package URL builder for creating valid PURL strings.
///
/// PURLs are a standardized way to identify software packages across
/// different ecosystems. This struct provides a builder pattern for
/// constructing valid PURL strings.
///
/// # Format
///
/// ```text
/// pkg:type/namespace/name@version?qualifiers#subpath
/// ```
///
/// - **type** (required): Package ecosystem (npm, maven, pypi, etc.)
/// - **namespace** (optional): Package scope/group (e.g., Maven groupId, npm scope)
/// - **name** (required): Package name
/// - **version** (optional): Specific version
///
/// # Example
///
/// ```rust
/// use vulnera_advisor::Purl;
///
/// // Scoped npm package
/// let purl = Purl::new("npm", "core")
///     .with_namespace("@angular")
///     .with_version("12.0.0")
///     .to_string();
/// assert_eq!(purl, "pkg:npm/%40angular/core@12.0.0");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Purl {
    /// Package type (ecosystem).
    pub purl_type: String,
    /// Optional namespace (e.g., Maven groupId, npm scope).
    pub namespace: Option<String>,
    /// Package name.
    pub name: String,
    /// Optional version.
    pub version: Option<String>,
}

impl Purl {
    /// Create a new PURL with the given ecosystem and package name.
    ///
    /// The ecosystem is automatically mapped to the correct PURL type
    /// (e.g., "crates.io" → "cargo", "PyPI" → "pypi").
    ///
    /// # Arguments
    ///
    /// * `ecosystem` - The package ecosystem (e.g., "npm", "crates.io", "PyPI")
    /// * `name` - The package name
    ///
    /// # Example
    ///
    /// ```rust
    /// use vulnera_advisor::Purl;
    ///
    /// let purl = Purl::new("crates.io", "serde");
    /// assert_eq!(purl.purl_type, "cargo");
    /// ```
    pub fn new(ecosystem: impl Into<String>, name: impl Into<String>) -> Self {
        let eco = ecosystem.into();
        let purl_type = Self::map_ecosystem(&eco);

        Self {
            purl_type,
            namespace: None,
            name: name.into(),
            version: None,
        }
    }

    /// Create a new PURL with validation.
    ///
    /// Returns an error if the ecosystem is not in the known list.
    ///
    /// # Example
    ///
    /// ```rust
    /// use vulnera_advisor::Purl;
    ///
    /// // Valid ecosystem
    /// let purl = Purl::new_validated("npm", "lodash").unwrap();
    ///
    /// // Invalid ecosystem
    /// let result = Purl::new_validated("invalid", "package");
    /// assert!(result.is_err());
    /// ```
    pub fn new_validated(
        ecosystem: impl Into<String>,
        name: impl Into<String>,
    ) -> Result<Self, PurlError> {
        let eco = ecosystem.into();
        let name = name.into();

        if name.is_empty() {
            return Err(PurlError::InvalidName(
                "Package name cannot be empty".into(),
            ));
        }

        let purl_type = Self::map_ecosystem(&eco);

        if !Self::is_known_ecosystem(&purl_type) {
            return Err(PurlError::UnknownEcosystem(eco));
        }

        Ok(Self {
            purl_type,
            namespace: None,
            name,
            version: None,
        })
    }

    /// Check if an ecosystem type is in the known list.
    pub fn is_known_ecosystem(purl_type: &str) -> bool {
        KNOWN_ECOSYSTEMS.contains(&purl_type.to_lowercase().as_str())
    }

    /// Add a namespace (e.g., Maven groupId, npm scope like "@angular").
    pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
        self.namespace = Some(namespace.into());
        self
    }

    /// Add a version.
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Map common ecosystem names to PURL types.
    fn map_ecosystem(ecosystem: &str) -> String {
        if let Some(canonical) = canonicalize_ecosystem(ecosystem) {
            return match canonical {
                "cargo" => "cargo".to_string(),
                "go" => "golang".to_string(),
                "packagist" => "composer".to_string(),
                "rubygems" => "gem".to_string(),
                other => other.to_string(),
            };
        }

        for (from, to) in ECOSYSTEM_MAPPINGS {
            if ecosystem.eq_ignore_ascii_case(from) {
                return to.to_string();
            }
        }
        ecosystem.to_lowercase()
    }

    /// URL-encode special characters in PURL components.
    fn encode_component(s: &str) -> String {
        s.replace('@', "%40")
            .replace('/', "%2F")
            .replace('?', "%3F")
            .replace('#', "%23")
    }

    /// URL-decode PURL components.
    fn decode_component(s: &str) -> String {
        s.replace("%40", "@")
            .replace("%2F", "/")
            .replace("%3F", "?")
            .replace("%23", "#")
    }

    /// Parse a PURL string into a Purl struct.
    ///
    /// # Example
    ///
    /// ```rust
    /// use vulnera_advisor::Purl;
    ///
    /// let purl = Purl::parse("pkg:npm/lodash@4.17.20").unwrap();
    /// assert_eq!(purl.purl_type, "npm");
    /// assert_eq!(purl.name, "lodash");
    /// assert_eq!(purl.version, Some("4.17.20".to_string()));
    /// ```
    pub fn parse(s: &str) -> Result<Self, PurlError> {
        let s = s
            .strip_prefix("pkg:")
            .ok_or_else(|| PurlError::InvalidFormat("PURL must start with 'pkg:'".into()))?;

        // Split type from rest
        let (purl_type, rest) = s
            .split_once('/')
            .ok_or_else(|| PurlError::InvalidFormat("Missing '/' after type".into()))?;

        if purl_type.is_empty() {
            return Err(PurlError::InvalidFormat("Empty PURL type".into()));
        }

        // Remove qualifiers and subpath for now (everything after ? or #)
        let rest = rest.split('?').next().unwrap_or(rest);
        let rest = rest.split('#').next().unwrap_or(rest);

        // Handle version
        let (path, version) = if let Some((p, v)) = rest.split_once('@') {
            (p, Some(v.to_string()))
        } else {
            (rest, None)
        };

        // Handle namespace
        let (namespace, name) = if let Some((ns, n)) = path.rsplit_once('/') {
            (Some(Self::decode_component(ns)), Self::decode_component(n))
        } else {
            (None, Self::decode_component(path))
        };

        if name.is_empty() {
            return Err(PurlError::InvalidName(
                "Package name cannot be empty".into(),
            ));
        }

        Ok(Self {
            purl_type: purl_type.to_string(),
            namespace,
            name,
            version,
        })
    }

    /// Get the ecosystem name (reverse mapping from PURL type).
    ///
    /// Returns the common ecosystem name for known mappings,
    /// or the PURL type itself if no mapping exists.
    pub fn ecosystem(&self) -> String {
        // Reverse lookup for common mappings
        for (eco, purl) in ECOSYSTEM_MAPPINGS {
            if self.purl_type.eq_ignore_ascii_case(purl) {
                return eco.to_string();
            }
        }
        self.purl_type.clone()
    }

    /// Generate a hash suitable for use as a cache key.
    ///
    /// This creates a deterministic hash of the PURL for use in
    /// Redis cache keys.
    pub fn cache_key(&self) -> String {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }

    /// Generate a cache key from a PURL string.
    pub fn cache_key_from_str(purl: &str) -> String {
        let mut hasher = DefaultHasher::new();
        purl.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }
}

impl fmt::Display for Purl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "pkg:{}/", self.purl_type)?;

        if let Some(ns) = &self.namespace {
            write!(f, "{}/", Self::encode_component(ns))?;
        }

        write!(f, "{}", self.name)?;

        if let Some(v) = &self.version {
            write!(f, "@{}", v)?;
        }

        Ok(())
    }
}

/// Create a PURL from ecosystem, name, and version.
///
/// This is a convenience function for creating PURLs without importing
/// the `Purl` struct directly.
///
/// # Example
///
/// ```rust
/// use vulnera_advisor::purl::purl;
///
/// let p = purl("npm", "lodash", "4.17.20");
/// assert_eq!(p.to_string(), "pkg:npm/lodash@4.17.20");
/// ```
pub fn purl(ecosystem: &str, name: &str, version: &str) -> Purl {
    Purl::new(ecosystem, name).with_version(version)
}

/// Create multiple PURLs from a list of (ecosystem, name, version) tuples.
///
/// # Example
///
/// ```rust
/// use vulnera_advisor::purl::purls_from_packages;
///
/// let purls = purls_from_packages(&[
///     ("npm", "lodash", "4.17.20"),
///     ("cargo", "serde", "1.0.130"),
/// ]);
/// assert_eq!(purls.len(), 2);
/// ```
pub fn purls_from_packages(packages: &[(&str, &str, &str)]) -> Vec<Purl> {
    packages
        .iter()
        .map(|(eco, name, ver)| Purl::new(*eco, *name).with_version(*ver))
        .collect()
}

/// Convert a list of PURLs to a vector of string references.
///
/// Useful for passing to OSS Index queries.
pub fn purls_to_strings(purls: &[Purl]) -> Vec<String> {
    purls.iter().map(|p| p.to_string()).collect()
}

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

    #[test]
    fn test_simple_purl() {
        let purl = Purl::new("npm", "lodash").with_version("4.17.20");
        assert_eq!(purl.to_string(), "pkg:npm/lodash@4.17.20");
    }

    #[test]
    fn test_ecosystem_mapping() {
        let purl = Purl::new("crates.io", "serde").with_version("1.0.130");
        assert_eq!(purl.to_string(), "pkg:cargo/serde@1.0.130");

        let purl = Purl::new("PyPI", "requests");
        assert_eq!(purl.to_string(), "pkg:pypi/requests");

        let purl = Purl::new("RubyGems", "rails");
        assert_eq!(purl.to_string(), "pkg:gem/rails");
    }

    #[test]
    fn test_maven_with_namespace() {
        let purl = Purl::new("maven", "spring-core")
            .with_namespace("org.springframework")
            .with_version("5.3.9");
        assert_eq!(
            purl.to_string(),
            "pkg:maven/org.springframework/spring-core@5.3.9"
        );
    }

    #[test]
    fn test_npm_scoped() {
        let purl = Purl::new("npm", "core")
            .with_namespace("@angular")
            .with_version("12.0.0");
        assert_eq!(purl.to_string(), "pkg:npm/%40angular/core@12.0.0");
    }

    #[test]
    fn test_parse_simple() {
        let purl = Purl::parse("pkg:npm/lodash@4.17.20").unwrap();
        assert_eq!(purl.purl_type, "npm");
        assert_eq!(purl.name, "lodash");
        assert_eq!(purl.version, Some("4.17.20".to_string()));
        assert_eq!(purl.namespace, None);
    }

    #[test]
    fn test_parse_with_namespace() {
        let purl = Purl::parse("pkg:maven/org.springframework/spring-core@5.3.9").unwrap();
        assert_eq!(purl.purl_type, "maven");
        assert_eq!(purl.namespace, Some("org.springframework".to_string()));
        assert_eq!(purl.name, "spring-core");
        assert_eq!(purl.version, Some("5.3.9".to_string()));
    }

    #[test]
    fn test_parse_scoped_npm() {
        let purl = Purl::parse("pkg:npm/%40angular/core@12.0.0").unwrap();
        assert_eq!(purl.namespace, Some("@angular".to_string()));
        assert_eq!(purl.name, "core");
    }

    #[test]
    fn test_roundtrip() {
        let original = "pkg:npm/lodash@4.17.20";
        let purl = Purl::parse(original).unwrap();
        assert_eq!(purl.to_string(), original);

        let original = "pkg:maven/org.springframework/spring-core@5.3.9";
        let purl = Purl::parse(original).unwrap();
        assert_eq!(purl.to_string(), original);
    }

    #[test]
    fn test_validation() {
        // Valid ecosystem
        assert!(Purl::new_validated("npm", "lodash").is_ok());
        assert!(Purl::new_validated("crates.io", "serde").is_ok());
        assert!(Purl::new_validated("cargo", "serde").is_ok());

        // Invalid ecosystem
        assert!(Purl::new_validated("invalid_eco", "package").is_err());

        // Empty name
        assert!(Purl::new_validated("npm", "").is_err());
    }

    #[test]
    fn test_ecosystem_reverse_mapping() {
        let purl = Purl::new("cargo", "serde");
        assert_eq!(purl.ecosystem(), "crates.io");

        let purl = Purl::new("pypi", "requests");
        assert_eq!(purl.ecosystem(), "PyPI");
    }

    #[test]
    fn test_cache_key() {
        let purl1 = Purl::new("npm", "lodash").with_version("4.17.20");
        let purl2 = Purl::new("npm", "lodash").with_version("4.17.20");
        let purl3 = Purl::new("npm", "lodash").with_version("4.17.21");

        assert_eq!(purl1.cache_key(), purl2.cache_key());
        assert_ne!(purl1.cache_key(), purl3.cache_key());
    }

    #[test]
    fn test_purls_from_packages() {
        let purls =
            purls_from_packages(&[("npm", "lodash", "4.17.20"), ("cargo", "serde", "1.0.130")]);

        assert_eq!(purls.len(), 2);
        assert_eq!(purls[0].to_string(), "pkg:npm/lodash@4.17.20");
        assert_eq!(purls[1].to_string(), "pkg:cargo/serde@1.0.130");
    }

    #[test]
    fn test_known_ecosystems() {
        assert!(Purl::is_known_ecosystem("npm"));
        assert!(Purl::is_known_ecosystem("cargo"));
        assert!(Purl::is_known_ecosystem("pypi"));
        assert!(Purl::is_known_ecosystem("NPM")); // Case insensitive
        assert!(!Purl::is_known_ecosystem("unknown"));
    }
}