bugcrowd_vrt/lib.rs
1pub mod types;
2pub mod cwe_mapping;
3pub mod cvss_v3;
4pub mod categorization;
5
6pub use types::{VrtNode, VrtNodeType, VrtTaxonomy};
7pub use cwe_mapping::{
8 CweId, CweMapping, CweMappingNode, MappingMetadata, MappingStatistics,
9};
10pub use cvss_v3::{
11 AttackComplexity, AttackVector, CvssV3Mapping, CvssV3MappingMetadata,
12 CvssV3MappingNode, CvssV3Statistics, CvssV3Vector, Impact,
13 PrivilegesRequired, Scope, UserInteraction,
14};
15pub use categorization::{CategorizedFinding, VulnerabilityCategorizer};
16
17use std::fs;
18use std::io;
19use std::path::Path;
20
21/// Loads and deserializes a VRT taxonomy from a JSON file
22///
23/// # Arguments
24/// * `path` - Path to the VRT JSON file
25///
26/// # Errors
27/// Returns an error if the file cannot be read or the JSON is invalid
28///
29/// # Example
30/// ```no_run
31/// use bugcrowd_vrt::load_vrt_from_file;
32///
33/// let taxonomy = load_vrt_from_file("vrt.json").expect("Failed to load VRT");
34/// println!("Loaded {} categories", taxonomy.len());
35/// ```
36pub fn load_vrt_from_file<P: AsRef<Path>>(path: P) -> Result<VrtTaxonomy, Box<dyn std::error::Error>> {
37 let content = fs::read_to_string(path)?;
38 let taxonomy = serde_json::from_str(&content)?;
39 Ok(taxonomy)
40}
41
42/// Deserializes a VRT taxonomy from a JSON string
43///
44/// # Arguments
45/// * `json` - JSON string containing the VRT data
46///
47/// # Errors
48/// Returns an error if the JSON is invalid
49///
50/// # Example
51/// ```
52/// use bugcrowd_vrt::load_vrt_from_str;
53///
54/// let json = r#"[{"id": "test", "name": "Test", "type": "category", "children": []}]"#;
55/// let taxonomy = load_vrt_from_str(json).expect("Failed to parse VRT");
56/// assert_eq!(taxonomy.len(), 1);
57/// ```
58pub fn load_vrt_from_str(json: &str) -> Result<VrtTaxonomy, serde_json::Error> {
59 serde_json::from_str(json)
60}
61
62/// Deserializes a VRT taxonomy from a reader
63///
64/// # Arguments
65/// * `reader` - Any type implementing `io::Read` containing JSON data
66///
67/// # Errors
68/// Returns an error if reading fails or the JSON is invalid
69pub fn load_vrt_from_reader<R: io::Read>(reader: R) -> Result<VrtTaxonomy, serde_json::Error> {
70 serde_json::from_reader(reader)
71}
72
73/// Loads and deserializes a CWE mapping from a JSON file
74///
75/// # Arguments
76/// * `path` - Path to the CWE mapping JSON file
77///
78/// # Errors
79/// Returns an error if the file cannot be read or the JSON is invalid
80///
81/// # Example
82/// ```no_run
83/// use bugcrowd_vrt::load_cwe_mapping_from_file;
84///
85/// let mapping = load_cwe_mapping_from_file("cwe.mappings.json")
86/// .expect("Failed to load CWE mapping");
87/// println!("Loaded {} root nodes", mapping.content.len());
88/// ```
89pub fn load_cwe_mapping_from_file<P: AsRef<Path>>(
90 path: P,
91) -> Result<CweMapping, Box<dyn std::error::Error>> {
92 let content = fs::read_to_string(path)?;
93 let mapping = serde_json::from_str(&content)?;
94 Ok(mapping)
95}
96
97/// Deserializes a CWE mapping from a JSON string
98///
99/// # Arguments
100/// * `json` - JSON string containing the CWE mapping data
101///
102/// # Errors
103/// Returns an error if the JSON is invalid
104///
105/// # Example
106/// ```
107/// use bugcrowd_vrt::load_cwe_mapping_from_str;
108///
109/// let json = r#"{
110/// "metadata": {"default": null},
111/// "content": [
112/// {"id": "xss", "cwe": ["CWE-79"]}
113/// ]
114/// }"#;
115/// let mapping = load_cwe_mapping_from_str(json).expect("Failed to parse");
116/// assert_eq!(mapping.content.len(), 1);
117/// ```
118pub fn load_cwe_mapping_from_str(json: &str) -> Result<CweMapping, serde_json::Error> {
119 serde_json::from_str(json)
120}
121
122/// Deserializes a CWE mapping from a reader
123///
124/// # Arguments
125/// * `reader` - Any type implementing `io::Read` containing JSON data
126///
127/// # Errors
128/// Returns an error if reading fails or the JSON is invalid
129pub fn load_cwe_mapping_from_reader<R: io::Read>(reader: R) -> Result<CweMapping, serde_json::Error> {
130 serde_json::from_reader(reader)
131}
132
133/// Loads and deserializes a CVSS v3 mapping from a JSON file
134///
135/// # Arguments
136/// * `path` - Path to the CVSS v3 mapping JSON file
137///
138/// # Errors
139/// Returns an error if the file cannot be read or the JSON is invalid
140///
141/// # Example
142/// ```no_run
143/// use bugcrowd_vrt::load_cvss_v3_mapping_from_file;
144///
145/// let mapping = load_cvss_v3_mapping_from_file("cvss_v3.json")
146/// .expect("Failed to load CVSS v3 mapping");
147/// println!("Loaded {} root nodes", mapping.content.len());
148/// ```
149pub fn load_cvss_v3_mapping_from_file<P: AsRef<Path>>(
150 path: P,
151) -> Result<CvssV3Mapping, Box<dyn std::error::Error>> {
152 let content = fs::read_to_string(path)?;
153 let mapping = serde_json::from_str(&content)?;
154 Ok(mapping)
155}
156
157/// Deserializes a CVSS v3 mapping from a JSON string
158///
159/// # Arguments
160/// * `json` - JSON string containing the CVSS v3 mapping data
161///
162/// # Errors
163/// Returns an error if the JSON is invalid
164///
165/// # Example
166/// ```
167/// use bugcrowd_vrt::load_cvss_v3_mapping_from_str;
168///
169/// let json = r#"{
170/// "metadata": {"default": "AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:N"},
171/// "content": [
172/// {"id": "xss", "cvss_v3": "AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N"}
173/// ]
174/// }"#;
175/// let mapping = load_cvss_v3_mapping_from_str(json).expect("Failed to parse");
176/// assert_eq!(mapping.content.len(), 1);
177/// ```
178pub fn load_cvss_v3_mapping_from_str(json: &str) -> Result<CvssV3Mapping, serde_json::Error> {
179 serde_json::from_str(json)
180}
181
182/// Deserializes a CVSS v3 mapping from a reader
183///
184/// # Arguments
185/// * `reader` - Any type implementing `io::Read` containing JSON data
186///
187/// # Errors
188/// Returns an error if reading fails or the JSON is invalid
189pub fn load_cvss_v3_mapping_from_reader<R: io::Read>(
190 reader: R,
191) -> Result<CvssV3Mapping, serde_json::Error> {
192 serde_json::from_reader(reader)
193}