Skip to main content

faker_rust/default/
vulnerability_identifier.rs

1//! Vulnerability Identifier generator (CVE, etc.)
2
3use crate::config::FakerConfig;
4
5/// Generate a random CVE (Common Vulnerabilities and Exposures) ID
6pub fn cve() -> String {
7    let config = FakerConfig::current();
8    let year = config.rand_range(1999, 2026);
9    let number = config.rand_range(1, 100000);
10    format!("CVE-{}-{}", year, number)
11}
12
13/// Generate a random CWE (Common Weakness Enumeration) ID
14pub fn cwe() -> String {
15    let config = FakerConfig::current();
16    let number = config.rand_range(1, 1000);
17    format!("CWE-{}", number)
18}
19
20/// Generate a random GHSA (GitHub Security Advisory) ID
21pub fn ghsa() -> String {
22    let config = FakerConfig::current();
23    let chars: String = (0..4)
24        .map(|_| config.rand_char(&['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']))
25        .collect();
26    format!("GHSA-{}-{}", chars, config.rand_range(1000, 10000))
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_cve() {
35        let id = cve();
36        assert!(id.starts_with("CVE-"));
37        assert!(id.len() >= 10);
38    }
39
40    #[test]
41    fn test_cwe() {
42        let id = cwe();
43        assert!(id.starts_with("CWE-"));
44    }
45
46    #[test]
47    fn test_ghsa() {
48        let id = ghsa();
49        assert!(id.starts_with("GHSA-"));
50    }
51}